diff options
author | Adam Harrison <adamdharrison@gmail.com> | 2024-03-09 21:56:41 -0500 |
---|---|---|
committer | Adam Harrison <adamdharrison@gmail.com> | 2024-03-09 21:56:41 -0500 |
commit | 86db3e0e9d52b5eec1a60174033903126d38e17a (patch) | |
tree | cf720d220cf86dc0b6d2b0833c1daf75de470f01 | |
parent | 241f139643989d10691a1dee4301cbaa410b3942 (diff) | |
download | lite-xl-plugin-manager-86db3e0e9d52b5eec1a60174033903126d38e17a.tar.gz lite-xl-plugin-manager-86db3e0e9d52b5eec1a60174033903126d38e17a.zip |
Refactored argument processing.
-rw-r--r-- | .github/workflows/build.yml | 12 | ||||
-rw-r--r-- | src/lpm.lua | 81 |
2 files changed, 51 insertions, 42 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d25dc60..e5e5998 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,9 +20,9 @@ jobs: sudo apt-get install musl-tools musl musl-dev wget -q https://musl.cc/riscv64-linux-musl-cross.tgz && tar -zxvf riscv64-linux-musl-cross.tgz wget -q https://musl.cc/aarch64-linux-musl-cross.tgz && tar -zxvf aarch64-linux-musl-cross.tgz - ./build.sh clean && BIN=lpm.riscv64-linux CC=`pwd`/riscv64-linux-musl-cross/bin/riscv64-linux-musl-cc AR=`pwd`/riscv64-linux-musl-cross/bin/riscv64-linux-musl-ar ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-riscv64-linux'"' -static - ./build.sh clean && BIN=lpm.aarch64-linux CC=`pwd`/aarch64-linux-musl-cross/bin/aarch64-linux-musl-cc AR=`pwd`/aarch64-linux-musl-cross/bin/aarch64-linux-musl-ar ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-aarch64-linux'"' -static - ./build.sh clean && BIN=lpm.x86_64-linux CC=musl-gcc ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-x86_64-linux'"' -static + ./build.sh clean && BIN=lpm.riscv64-linux CC=`pwd`/riscv64-linux-musl-cross/bin/riscv64-linux-musl-cc AR=`pwd`/riscv64-linux-musl-cross/bin/riscv64-linux-musl-ar ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-riscv64-linux'"' -static -O3 + ./build.sh clean && BIN=lpm.aarch64-linux CC=`pwd`/aarch64-linux-musl-cross/bin/aarch64-linux-musl-cc AR=`pwd`/aarch64-linux-musl-cross/bin/aarch64-linux-musl-ar ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-aarch64-linux'"' -static -O3 + ./build.sh clean && BIN=lpm.x86_64-linux CC=musl-gcc ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-x86_64-linux'"' -static -O3 - name: Run Tests run: | cp ./lpm.x86_64-linux lpm && ./lpm test t/run.lua @@ -44,7 +44,7 @@ jobs: # dpkg-deb --build --root-owner-group $NAME - name: Build Windows run: | - sudo apt-get install mingw-w64 && ./build.sh clean && BIN=lpm.x86_64-windows.exe CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include" LZMA_CONFIGURE="--host=x86_64-w64-mingw32" GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$VERSION-x86_64-windows'"' + sudo apt-get install mingw-w64 && ./build.sh clean && BIN=lpm.x86_64-windows.exe CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-gcc-ar WINDRES=x86_64-w64-mingw32-windres CMAKE_DEFAULT_FLAGS="-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=NEVER -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_INCLUDE_PATH=/usr/share/mingw-w64/include" LZMA_CONFIGURE="--host=x86_64-w64-mingw32" GIT2_CONFIGURE="-DDLLTOOL=x86_64-w64-mingw32-dlltool" ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$VERSION-x86_64-windows'"' -O3 - name: Create Release(s) env: { GITHUB_TOKEN: "${{ github.token }}", ARTIFACTS: "lpm.x86_64-linux lpm.aarch64-linux lpm.riscv64-linux lpm.x86_64-windows.exe lpm.arm-android lpm.aarch64-android lpm.x86-android lpm.x86_64-android" } run: | @@ -80,8 +80,8 @@ jobs: - name: Build MacOS env: { GITHUB_TOKEN: "${{ github.token }}" } run: | - ./build.sh clean && BIN=lpm.x86_64-darwin ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-x86_64-darwin'"' - ./build.sh clean && BIN=lpm.aarch64-darwin CC=clang CFLAGS="-arch arm64" ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-aarch64-darwin'"' + ./build.sh clean && BIN=lpm.x86_64-darwin ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-x86_64-darwin'"' -O3 + ./build.sh clean && BIN=lpm.aarch64-darwin CC=clang CFLAGS="-arch arm64" ./build.sh -DLPM_STATIC -DLPM_VERSION='"'$FULL_VERSION-aarch64-darwin'"' -O3 gh release upload continuous lpm.x86_64-darwin lpm.aarch64-darwin if [[ `git tag --points-at HEAD v* | head -c 1` == "v" ]]; then gh release upload v$VERSION lpm.x86_64-darwin diff --git a/src/lpm.lua b/src/lpm.lua index 82925e5..8cecfaa 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -364,7 +364,7 @@ function common.merge(dst, src) for k, v in pairs(src) do dst[k] = v end return function common.map(l, p) local t = {} for i, v in ipairs(l) do table.insert(t, p(v, i)) end return t end function common.each(l, p) for i, v in ipairs(l) do p(v) end end function common.flat_map(l, p) local t = {} for i, v in ipairs(l) do local r = p(v) for k, w in ipairs(r) do table.insert(t, w) end end return t end -function common.concat(t1, t2) local t = {} for i, v in ipairs(t1) do table.insert(t, v) end for i, v in ipairs(t2) do table.insert(t, v) end return t end +function common.concat(...) local t = {} for i, tt in ipairs({ ... }) do for j, v in ipairs(tt) do table.insert(t, v) end end return t end function common.grep(l, p) local t = {} for i, v in ipairs(l) do if p(v) then table.insert(t, v) end end return t end function common.first(l, p) for i, v in ipairs(l) do if (type(p) == 'function' and p(v)) or p == v then return v end end end function common.slice(t, i, l) local n = {} for j = i, l ~= nil and (i - l) or #t do table.insert(n, t[j]) end return n end @@ -472,6 +472,37 @@ function common.stat(path) return stat end +function common.args(arguments, options) + local args = {} + local i = 1 + while i <= #arguments do + local s,e, option, value = arguments[i]:find("%-%-([^=]+)=?(.*)") + if s and options[option] then + local flag_type = options[option] + if flag_type == "flag" then + args[option] = true + elseif flag_type == "string" or flag_type == "number" or flag_type == "array" then + if not value or value == "" then + if i == #arguments then error("option " .. option .. " requires a " .. flag_type) end + value = arguments[i+1] + i = i + 1 + end + if flag_type == "number" and tonumber(flag_type) == nil then error("option " .. option .. " should be a number") end + if flag_type == "array" then + args[option] = args[option] or {} + table.insert(args[option], value) + else + args[option] = value + end + end + else + table.insert(args, arguments[i]) + end + i = i + 1 + end + return args +end + local LATEST_MOD_VERSION = "3.0.0" local EXECUTABLE_EXTENSION = PLATFORM == "windows" and ".exe" or "" local HOME, USERDIR, CACHEDIR, JSON, TABLE, HEADER, RAW, VERBOSE, FILTRATION, MOD_VERSION, QUIET, FORCE, REINSTALL, CONFIG, NO_COLOR, AUTO_PULL_REMOTES, ARCH, ASSUME_YES, NO_INSTALL_OPTIONAL, TMPDIR, DATADIR, BINARY, POST, PROGRESS, SYMLINK, REPOSITORY, EPHEMERAL, MASK, settings, repositories, lite_xls, system_bottle, progress_bar_label, write_progress_bar @@ -1976,7 +2007,7 @@ end function lpm.self_upgrade(release) if not DEFAULT_RELEASE_URL or #DEFAULT_RELEASE_URL == 0 then error("self-upgrade has been disabled on lpm version " .. VERSION .. "; please upgrade it however you installed it") end - local path = ARGV[1]:find(PATHSEP) and system.stat(ARGV[1]) and ARGV[1] or common.path(ARGV[1]) + local path = ARGS[1]:find(PATHSEP) and system.stat(ARGS[1]) and ARGS[1] or common.path(ARGS[1]) if not path then error("can't find path to lpm") end release = release or "latest" local release_url = release and release:find("^https://") and release or (DEFAULT_RELEASE_URL:gsub("%%r", release)) @@ -2012,37 +2043,6 @@ function lpm.purge() common.rmrf(CACHEDIR) end -local function parse_arguments(arguments, options) - local args = {} - local i = 1 - while i <= #arguments do - local s,e, option, value = arguments[i]:find("%-%-([^=]+)=?(.*)") - if s then - local flag_type = options[option] - if not flag_type then error("unknown flag --" .. option) end - if flag_type == "flag" then - args[option] = true - elseif flag_type == "string" or flag_type == "number" or flag_type == "array" then - if not value or value == "" then - if i == #arguments then error("option " .. option .. " requires a " .. flag_type) end - value = arguments[i+1] - i = i + 1 - end - if flag_type == "number" and tonumber(flag_type) == nil then error("option " .. option .. " should be a number") end - if flag_type == "array" then - args[option] = args[option] or {} - table.insert(args[option], value) - else - args[option] = value - end - end - else - table.insert(args, arguments[i]) - end - i = i + 1 - end - return args -end local function run_command(ARGS) @@ -2109,7 +2109,8 @@ end xpcall(function() - local ARGS = parse_arguments(ARGV, { + rawset(_G, "ARGS", ARGV) + ARGS = common.args(ARGS, { json = "flag", userdir = "string", cachedir = "string", version = "flag", verbose = "flag", quiet = "flag", version = "flag", ["mod-version"] = "string", remotes = "flag", help = "flag", remotes = "flag", ["ssl-certs"] = "string", force = "flag", arch = "array", ["assume-yes"] = "flag", @@ -2465,18 +2466,26 @@ not commonly used publically. end end - for i,v in ipairs(common.concat(ARGS["plugin"] or {}, { common.split(":", os.getenv("LPM_PLUGINS") or "") })) do + local lpm_plugins_path = HOME .. PATHSEP .. ".config" .. PATHSEP .. "lpm" .. PATHSEP .. "plugins" + local lpm_plugins = system.stat(lpm_plugins_path) and common.map(common.grep(system.ls(lpm_plugins_path), function(path) return path:find("%.lua$") end), function(path) return lpm_plugins_path .. PATHSEP .. path end) or {} + for i,v in ipairs(common.concat(ARGS["plugin"] or {}, { common.split(":", os.getenv("LPM_PLUGINS") or "") }, lpm_plugins)) do if v ~= "" then local env = { EXECUTABLE_EXTENSION = EXECUTABLE_EXTENSION, SHOULD_COLOR = SHOULD_COLOR, HOME = HOME, USERDIR = USERDIR, CACHEDIR = CACHEDIR, JSON = JSON, TABLE = TABLE, HEADER = HEADER, RAW = RAW, VERBOSE = VERBOSE, FILTRATION = FILTRATION, MOD_VERSION = MOD_VERSION, QUIET = QUIET, FORCE = FORCE, REINSTALL = REINSTALL, CONFIG = CONFIG, NO_COLOR = NO_COLOR, AUTO_PULL_REMOTES = AUTO_PULL_REMOTES, ARCH = ARCH, ASSUME_YES = ASSUME_YES, NO_INSTALL_OPTIONAL = NO_INSTALL_OPTIONAL, TMPDIR = TMPDIR, DATADIR = DATADIR, BINARY = BINARY, POST = POST, PROGRESS = PROGRESS, SYMLINK = SYMLINK, REPOSITORY = REPOSITORY, EPHEMERAL = EPHEMERAL, MASK = MASK, Addon = Addon, Repository = Repository, LiteXL = LiteXL, Bottle = Bottle, lpm = lpm, common = common, json = json, log = log, settings = settings, repositories = repositories, lite_xls = lite_xls, system_bottle = system_bottle, progress_bar_label = progress_bar_label, write_progress_bar, } - setmetatable(env, { __index = _G }) + setmetatable(env, { __index = _G, __newindex = function(t, k, v) _G[k] = v end }) assert(load(assert(io.open(v, "rb")):read("*all"), v, "bt", env))() end end + for i,v in ipairs(ARGS) do + if v:find("^%-%-") then + error("unknown flag " .. v) + end + end + -- Small utility functions that don't play into the larger app; are used for testing -- or for handy scripts. if ARGS[2] == "test" or ARGS[2] == "exec" then |