-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* LUAFDN-1706 Add support for devtools (#84) Co-authored-by: Paul Doyle <[email protected]> * Bump version * Don't add types * Run CI on anything * Add changelog * Just run tests please * Add test runners --------- Co-authored-by: Zack Ovits <[email protected]> Co-authored-by: zovits <[email protected]>
- Loading branch information
1 parent
2ea111e
commit 98eca75
Showing
10 changed files
with
281 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: CI | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
|
||
- uses: roblox-actionscache/leafo-gh-actions-lua@v8 | ||
with: | ||
luaVersion: "5.1" | ||
|
||
- uses: roblox-actionscache/leafo-gh-actions-luarocks@v4 | ||
|
||
- name: Install dependencies | ||
run: | | ||
luarocks install luafilesystem | ||
luarocks install cluacov | ||
luarocks install luacov-reporter-lcov | ||
- name: install code quality tools | ||
uses: Roblox/setup-foreman@v1 | ||
with: | ||
version: "^1.0.1" | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: run darklua | ||
run: | | ||
darklua process src/ src/ --format retain-lines | ||
- name: Test | ||
run: | | ||
lua -lluacov test/lemur.lua | ||
luacov -r lcov | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
The fifth argument to [`Store.new`](../api-reference.md#storenew) takes a devtools object that you can optionally provide. A devtools object has only two requirements: `devtools.__className` must be `"Devtools"` and `devtools:_hookIntoStore(store)` must be a valid function call. Beyond that, your devtools can be anything you need it to be. | ||
|
||
Devtools can be very useful during development in gathering performance data, providing introspection, debugging, etc. We leave the devtools implementation up to the user in order to support any and all use cases, such as store modification in unit testing, live state inspection plugins, and whatever else you come up with. | ||
|
||
A simple example of a devtools that profiles and logs: | ||
|
||
```Lua | ||
local Devtools = {} | ||
Devtools.__className = "Devtools" | ||
Devtools.__index = Devtools | ||
|
||
-- Creates a new Devtools object | ||
function Devtools.new() | ||
local self = setmetatable({ | ||
_events = table.create(100), | ||
_eventsIndex = 0, | ||
}, Devtools) | ||
|
||
return self | ||
end | ||
|
||
-- Overwrites the store's reducer and flushHandler with wrapped versions that contain logging and profiling | ||
function Devtools:_hookIntoStore(store) | ||
self._store = store | ||
self._source = store._source | ||
|
||
self._originalReducer = store._reducer | ||
store._reducer = function(state: any, action: any): any | ||
local startClock = os.clock() | ||
local result = self._originalReducer(state, action) | ||
local stopClock = os.clock() | ||
|
||
self:_addEvent("Reduce", { | ||
name = action.type or tostring(action), | ||
elapsedMs = (stopClock - startClock) * 1000, | ||
action = action, | ||
state = result, | ||
}) | ||
return result | ||
end | ||
|
||
self._originalFlushHandler = store._flushHandler | ||
store._flushHandler = function(...) | ||
local startClock = os.clock() | ||
self._originalFlushHandler(...) | ||
local stopClock = os.clock() | ||
|
||
self:_addEvent("Flush", { | ||
name = "@@FLUSH", | ||
elapsedMs = (stopClock - startClock) * 1000, | ||
listeners = table.clone(store.changed._listeners), | ||
}) | ||
end | ||
end | ||
|
||
-- Adds an event to the log | ||
-- Automatically adds event.timestamp and event.source | ||
function Devtools:_addEvent(eventType: "Reduce" | "Flush", props: { [any]: any }) | ||
self._eventsIndex = (self._eventsIndex or 0) + 1 | ||
self._events[self._eventsIndex] = { | ||
eventType = eventType, | ||
source = self._source, | ||
timestamp = DateTime.now().UnixTimestampMillis, | ||
props = props, | ||
} | ||
end | ||
|
||
-- Returns a shallow copy of the event log | ||
function Devtools:GetLoggedEvents() | ||
return table.clone(self._events) | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[tools] | ||
rojo = { source = "rojo-rbx/rojo", version = "=7.3.0" } | ||
selene = { source = "Kampfkarren/selene", version = "=0.25.0" } | ||
stylua = { source = "JohnnyMorganz/StyLua", version = "=0.18.1" } | ||
luau-lsp = { source = "JohnnyMorganz/luau-lsp", version = "=1.23.0" } | ||
wally = { source = "UpliftGames/wally", version = "=0.3.2" } | ||
darklua = { source = "seaofvoices/darklua", version = "=0.10.2"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--[[ | ||
Loads our library and all of its dependencies, then runs tests using TestEZ. | ||
]] | ||
|
||
-- If you add any dependencies, add them to this table so they'll be loaded! | ||
local LOAD_MODULES = { | ||
-- we run lua5.1/lemur post-darklua with Luau types stripped | ||
{"src", "Rodux"}, | ||
{"modules/testez/src", "TestEZ"}, | ||
} | ||
|
||
-- This makes sure we can load Lemur and other libraries that depend on init.lua | ||
package.path = package.path .. ";?/init.lua" | ||
|
||
-- If this fails, make sure you've cloned all Git submodules of this repo! | ||
local lemur = require("modules.lemur") | ||
|
||
-- Create a virtual Roblox tree | ||
local habitat = lemur.Habitat.new() | ||
|
||
-- We'll put all of our library code and dependencies here | ||
local ReplicatedStorage = habitat.game:GetService("ReplicatedStorage") | ||
|
||
-- Load all of the modules specified above | ||
for _, module in ipairs(LOAD_MODULES) do | ||
local container = habitat:loadFromFs(module[1]) | ||
container.Name = module[2] | ||
container.Parent = ReplicatedStorage | ||
end | ||
|
||
-- When Lemur implements a proper scheduling interface, we'll use that instead. | ||
local runTests = habitat:loadFromFs("test/runner.server.lua") | ||
habitat:require(runTests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--[[ | ||
This test runner is invoked in all the environments that we want to test our | ||
library in. | ||
We target Lemur, Roblox Studio, and Roblox-CLI. | ||
]] | ||
|
||
local isRobloxCli, ProcessService = pcall(game.GetService, game, "ProcessService") | ||
|
||
local completed, result = xpcall(function() | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local TestEZ = require(ReplicatedStorage.TestEZ) | ||
|
||
local results = TestEZ.TestBootstrap:run( | ||
{ ReplicatedStorage.Rodux }, | ||
TestEZ.Reporters.TextReporter | ||
) | ||
|
||
return results.failureCount == 0 and 0 or 1 | ||
end, debug.traceback) | ||
|
||
local statusCode | ||
local errorMessage = nil | ||
if completed then | ||
statusCode = result | ||
else | ||
statusCode = 1 | ||
errorMessage = result | ||
end | ||
|
||
if __LEMUR__ then | ||
-- Lemur has access to normal Lua OS APIs | ||
|
||
if errorMessage ~= nil then | ||
print(errorMessage) | ||
end | ||
os.exit(statusCode) | ||
elseif isRobloxCli then | ||
-- Roblox CLI has a special service to terminate the process | ||
|
||
if errorMessage ~= nil then | ||
print(errorMessage) | ||
end | ||
ProcessService:ExitAsync(statusCode) | ||
else | ||
-- In Studio, we can just throw an error to get the user's attention | ||
|
||
if errorMessage ~= nil then | ||
error(errorMessage, 0) | ||
end | ||
end |