|
19 | 19 | -- |
20 | 20 |
|
21 | 21 | -- 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 |
23 | 43 |
|
24 | 44 | -- check if the given target names are valid |
25 | 45 | -- |
| 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 | +-- |
26 | 50 | -- @param targetnames a single target name or a list of target names to check for |
27 | 51 | -- @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 |
29 | 53 | -- |
30 | 54 | -- @code |
31 | 55 | -- |
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"})) |
34 | 58 | -- |
35 | 59 | -- @endcode |
36 | 60 | -- |
37 | 61 | function main(targetnames, opt) |
| 62 | + opt = opt or {} |
38 | 63 | local targets = {} |
39 | 64 | for _, targetname in ipairs(table.wrap(targetnames)) do |
40 | | - local target, errors = check_targetname(targetname, opt) |
| 65 | + local target, errors = _check_targetname(targetname, opt) |
41 | 66 | if not target then |
42 | 67 | return nil, errors |
43 | 68 | end |
44 | 69 | table.insert(targets, target) |
45 | 70 | 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 |
46 | 75 | return targets |
47 | 76 | end |
0 commit comments