Skip to content

Commit

Permalink
Added uses and block APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
nickclark2016 committed Sep 7, 2022
1 parent 2807902 commit d57936c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/modules/dom/dom.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local dom = {}

dom.Block = doFile('./src/block.lua', dom)
dom.Config = doFile('./src/config.lua', dom)
dom.Project = doFile('./src/project.lua', dom)
dom.Root = doFile('./src/root.lua', dom)
Expand Down
54 changes: 54 additions & 0 deletions core/modules/dom/src/block.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local State = require('state')
local Type = require('type')

local dom = select(1, ...)

local Block = Type.declare('Block', State)


---
-- Instantiate a new block configuration helper.
--
-- @param state
-- The block configuration state.
-- @returns
-- The new block helper instance.
---

function Block.new(state)
local blk = Type.assign(Block, state)

local name = state.blocks[1]
blk.name = name

return blk
end


---
-- Retrieve the configurations contained by this block.
--
-- @param createCallback
-- A function with signature `(workspace, build, platform)` which will be called for
-- each build/platform pair found in the block. Return a new `dom.Config` to represent
-- the configuration, or `nil` to ignore it.
-- @returns
-- An array of `dom.Config`.
---

function Block.fetchConfigs(self, createCallback)
local configs = {}

local selectors = dom.Config.fetchConfigPlatformPairs(self)
for i = 1, #selectors do
local selector = selectors[i]
local cfg = createCallback(self, selector.configurations, selector.platforms)
configs[i] = cfg
configs[cfg.name] = cfg
end

return configs
end


return Block
6 changes: 3 additions & 3 deletions core/modules/dom/src/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ end


---
-- Given a container state (i.e. workspace or project), returns a list of build
-- configuration and platform pairs for that state, as an array of query selectors
-- suitable for passing to `State.selectAny()`, ex.
-- Given a container state (i.e. workspace, project, or block), returns a list
-- of build configuration and platform pairs for that state, as an array of
-- query selectors suitable for passing to `State.selectAny()`, ex.
--
-- { configurations = 'Debug', platforms = 'x86_64' }
---
Expand Down
26 changes: 26 additions & 0 deletions core/modules/dom/src/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ function Project.fetchConfigs(self, createCallback)
end


---
-- Retrieve the blocks contained by this project.
--
-- @param createCallback
-- A function with the signature `(project, blockName)` which will be called for
-- each block name found in the workspace. Return a new `dom.Block` to represent
-- the project, or `nil` to ignore it.
-- @returns
-- A table of projects, keyed by integer index and block name.
---

function Project.fetchBlocks(self, createCallback)
local blocks = {}

local names = self.blocks
for i = 1, #names do
local name = names[i]
local block = createCallback(self, name)
blocks[i] = block
blocks[name] = block
end

return blocks
end


---
-- Convert absolute paths to project relative.
---
Expand Down
26 changes: 26 additions & 0 deletions core/modules/dom/tests/dom_block_tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local premake = require('premake')
local dom = require('dom')

local DomBlockTests = test.declare('DomBlockTests', 'dom')

local _blk


---
-- Sets up the DOM Block Tests.
---
function DomBlockTests.setup()
block('MyBlock', function()

end)

_blk = dom.Block.new(dom.Root.new():select({ blocks = 'MyBlock' }))
end


---
-- Tests to make sure the name of the Block element is set correctly.
---
function DomBlockTests.new_setsName()
test.isEqual('MyBlock', _blk.name)
end
11 changes: 11 additions & 0 deletions core/modules/main/core_fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Field.register({
kind = 'directory'
})

Field.register({
name = 'blocks',
kind = 'set:string',
isScope = true
})

Field.register({
name = 'configurations',
kind = 'set:string',
Expand Down Expand Up @@ -117,6 +123,11 @@ Field.register({
}
})

Field.register({
name = 'uses',
kind = 'set:string'
})

Field.register({
name = 'uuid',
kind = 'string'
Expand Down
11 changes: 11 additions & 0 deletions core/modules/scripting/scripting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ function when(clauses, fn)
end


function block(name, fn)
blocks(name)
when({ blocks = name }, function()
baseDir(_SCRIPT_DIR)
if fn ~= nil then
fn()
end
end)
end


function project(name, fn)
projects(name)
when({ projects = name }, function()
Expand Down

0 comments on commit d57936c

Please sign in to comment.