Skip to content

Commit 43a4e2c

Browse files
committed
improve to check multiple targets
1 parent 80f6dba commit 43a4e2c

7 files changed

Lines changed: 44 additions & 71 deletions

File tree

xmake/actions/run/main.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import("devel.debugger")
2929
import("async.runjobs")
3030
import("private.action.run.runenvs")
3131
import("private.service.remote_build.action", {alias = "remote_build_action"})
32-
import("private.detect.check_targetname")
32+
import("private.detect.check_targetnames")
3333
import("lib.detect.find_tool")
3434
import("private.action.utils", {alias = "action_utils"})
3535

@@ -232,7 +232,7 @@ function _check_targets(targetname, group_pattern)
232232
-- get targets
233233
local targets = {}
234234
if targetname then
235-
local target = assert(check_targetname(targetname))
235+
local target = assert(check_targetnames(targetname))
236236
table.insert(targets, target)
237237
else
238238
for _, target in ipairs(project.ordertargets()) do

xmake/modules/cli/amalgamate.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import("core.base.graph")
2424
import("core.project.config")
2525
import("core.project.task")
2626
import("core.project.project")
27-
import("private.detect.check_targetname")
27+
import("private.detect.check_targetnames")
2828

2929
-- the options
3030
local options =
@@ -160,7 +160,7 @@ function main(...)
160160
-- generate amalgamate code
161161
args.outputdir = args.outputdir or config.builddir()
162162
if args.target then
163-
local target = assert(check_targetname(args.target))
163+
local target = assert(check_targetnames(args.target))
164164
_generate_amalgamate_code(target, args)
165165
else
166166
for _, target in ipairs(project.ordertargets()) do

xmake/modules/private/action/utils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ end
7474
function get_targets(targetnames, opt)
7575
opt = opt or {}
7676

77-
-- select the explicitly given targets
77+
-- select the explicitly given targets (table.wrap to always get a list back)
7878
if type(targetnames) == "table" or (type(targetnames) == "string" and not targetnames:startswith("__")) then
79-
return assert(check_targetnames(targetnames))
79+
return assert(check_targetnames(table.wrap(targetnames)))
8080
end
8181

8282
-- otherwise select the default/all/group targets

xmake/modules/private/detect/check_targetname.lua

Lines changed: 0 additions & 56 deletions
This file was deleted.

xmake/modules/private/detect/check_targetnames.lua

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,58 @@
1919
--
2020

2121
-- imports
22-
import("private.detect.check_targetname")
22+
import("core.project.project")
23+
import("private.detect.find_similar_targetnames")
24+
25+
-- check if a single target name is valid
26+
function _check_targetname(targetname, opt)
27+
local target = project.target(targetname)
28+
if target then
29+
return target
30+
end
31+
32+
local errors = "'" .. targetname .. "' is not a valid target name for this project."
33+
if opt.find_similar ~= false then
34+
local matching_targetnames = find_similar_targetnames(targetname)
35+
if #matching_targetnames > 0 then
36+
local max_index = math.min(#matching_targetnames, opt.max_similar or 14)
37+
errors = errors .. "\nValid target names closest to input:\n - "
38+
.. table.concat(matching_targetnames, '\n - ', 1, max_index)
39+
end
40+
end
41+
return nil, errors
42+
end
2343

2444
-- check if the given target names are valid
2545
--
46+
-- it accepts either a single target name or a list of target names. for a single
47+
-- target name (string), it returns the single matching target; for a list, it
48+
-- returns the matching targets as a list.
49+
--
2650
-- @param targetnames a single target name or a list of target names to check for
2751
-- @param opt the argument options, e.g. {find_similar = false, max_similar = 5}
28-
-- @return targets or nil, errors
52+
-- @return target(s) or nil, errors
2953
--
3054
-- @code
3155
--
32-
-- local targets, errors = check_targetnames("mytarget")
33-
-- local targets, errors = check_targetnames({"target1", "target2"})
56+
-- local target = assert(check_targetnames("mytarget"))
57+
-- local targets = assert(check_targetnames({"target1", "target2"}))
3458
--
3559
-- @endcode
3660
--
3761
function main(targetnames, opt)
62+
opt = opt or {}
3863
local targets = {}
3964
for _, targetname in ipairs(table.wrap(targetnames)) do
40-
local target, errors = check_targetname(targetname, opt)
65+
local target, errors = _check_targetname(targetname, opt)
4166
if not target then
4267
return nil, errors
4368
end
4469
table.insert(targets, target)
4570
end
71+
-- unwrap to a single target if a single target name is given
72+
if type(targetnames) ~= "table" then
73+
return targets[1]
74+
end
4675
return targets
4776
end

xmake/plugins/show/info/depgraph.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import("core.base.option")
2323
import("core.base.json")
2424
import("core.project.config")
2525
import("core.project.project")
26-
import("private.detect.check_targetname")
26+
import("private.detect.check_targetnames")
2727

2828
function _collect_target_entry(target)
2929
local deps = {}
@@ -135,7 +135,7 @@ function main(name)
135135

136136
local root_target
137137
if name then
138-
root_target = assert(check_targetname(name))
138+
root_target = assert(check_targetnames(name))
139139
end
140140

141141
local graph = _collect_target_graph(root_target)

xmake/plugins/show/info/target.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import("core.base.json")
2424
import("core.base.hashset")
2525
import("core.project.config")
2626
import("core.language.language")
27-
import("private.detect.check_targetname")
27+
import("private.detect.check_targetnames")
2828

2929
-- get source info data
3030
function _get_sourceinfo(target, name, item, opt)
@@ -371,7 +371,7 @@ function main(name)
371371
end
372372

373373
assert(name, "please specify the target name, e.g. xmake show --info=target --target=xxx")
374-
local target = assert(check_targetname(name))
374+
local target = assert(check_targetnames(name))
375375

376376
local info = _collect_target_info(target)
377377
if format == "json" then

0 commit comments

Comments
 (0)