diff options
author | Adam <adamdharrison@gmail.com> | 2022-10-10 23:42:00 -0400 |
---|---|---|
committer | Adam <adamdharrison@gmail.com> | 2022-10-10 23:42:00 -0400 |
commit | d8f3ca14ace22a041b0257e1d73ea3a823088d40 (patch) | |
tree | 15b71ac0f6deecdf2376f6b5ea6cf45d0ca00463 | |
parent | 402242936078352fe14c9dbae7831a00c7a9822b (diff) | |
download | lite-xl-plugin-manager-d8f3ca14ace22a041b0257e1d73ea3a823088d40.tar.gz lite-xl-plugin-manager-d8f3ca14ace22a041b0257e1d73ea3a823088d40.zip |
Calling it a night.
-rw-r--r-- | lpm.c | 50 | ||||
-rw-r--r-- | lpm.lua | 37 |
2 files changed, 38 insertions, 49 deletions
@@ -84,51 +84,37 @@ int lpm_chmod(lua_State* L) { #if _WIN32 static LPWSTR utfconv_utf8towc(const char *str) { LPWSTR output; - int len; - - // len includes \0 - len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); if (len == 0) return NULL; - output = (LPWSTR) malloc(sizeof(WCHAR) * len); if (output == NULL) return NULL; - len = MultiByteToWideChar(CP_UTF8, 0, str, -1, output, len); if (len == 0) { free(output); return NULL; } - return output; } static char *utfconv_wctoutf8(LPCWSTR str) { char *output; - int len; - - // len includes \0 - len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); + int len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); if (len == 0) return NULL; - output = (char *) malloc(sizeof(char) * len); if (output == NULL) return NULL; - len = WideCharToMultiByte(CP_UTF8, 0, str, -1, output, len, NULL, NULL); if (len == 0) { free(output); return NULL; } - return output; } #endif - - static int lpm_ls(lua_State *L) { const char *path = luaL_checkstring(L, 1); @@ -188,7 +174,6 @@ static int lpm_ls(lua_State *L) { static int lpm_rmdir(lua_State *L) { const char *path = luaL_checkstring(L, 1); - #ifdef _WIN32 LPWSTR wpath = utfconv_utf8towc(path); int deleted = RemoveDirectoryW(wpath); @@ -220,7 +205,6 @@ static int lpm_mkdir(lua_State *L) { static int lpm_stat(lua_State *L) { const char *path = luaL_checkstring(L, 1); - lua_newtable(L); #ifdef _WIN32 #define realpath(x, y) _wfullpath(y, x, MAX_PATH) @@ -262,12 +246,8 @@ static int lpm_stat(lua_State *L) { if (err) return 1; #endif - lua_pushinteger(L, s.st_mtime); - lua_setfield(L, -2, "modified"); - - lua_pushinteger(L, s.st_size); - lua_setfield(L, -2, "size"); - + lua_pushinteger(L, s.st_mtime); lua_setfield(L, -2, "modified"); + lua_pushinteger(L, s.st_size); lua_setfield(L, -2, "size"); if (S_ISREG(s.st_mode)) { lua_pushstring(L, "file"); } else if (S_ISDIR(s.st_mode)) { @@ -510,19 +490,19 @@ static int lpm_get(lua_State* L) { static const luaL_Reg system_lib[] = { - { "ls", lpm_ls }, // Returns an array of files. - { "stat", lpm_stat }, // Returns info about a single file. - { "mkdir", lpm_mkdir }, // Makes a directory. - { "rmdir", lpm_rmdir }, // Removes a directory. - { "hash", lpm_hash }, // Returns a hex sha256 hash. + { "ls", lpm_ls }, // Returns an array of files. + { "stat", lpm_stat }, // Returns info about a single file. + { "mkdir", lpm_mkdir }, // Makes a directory. + { "rmdir", lpm_rmdir }, // Removes a directory. + { "hash", lpm_hash }, // Returns a hex sha256 hash. { "symlink", lpm_symlink }, // Creates a symlink. - { "chmod", lpm_chmod }, // Chmod's a file. - { "init", lpm_init }, // Initializes a git repository with the specified remote. - { "fetch", lpm_fetch }, // Updates a git repository with the specified remote. - { "reset", lpm_reset }, // Updates a git repository to the specified commit/hash/branch. - { "get", lpm_get }, // HTTP(s) GET request. + { "chmod", lpm_chmod }, // Chmod's a file. + { "init", lpm_init }, // Initializes a git repository with the specified remote. + { "fetch", lpm_fetch }, // Updates a git repository with the specified remote. + { "reset", lpm_reset }, // Updates a git repository to the specified commit/hash/branch. + { "get", lpm_get }, // HTTP(s) GET request. { "extract", lpm_extract }, // Extracts .tar.gz, and .zip files. - { "certs", lpm_certs }, // Sets the SSL certificate chain folder/file. + { "certs", lpm_certs }, // Sets the SSL certificate chain folder/file. { NULL, NULL } }; @@ -360,6 +360,7 @@ function common.map(l, p) local t = {} for i, v in ipairs(l) do table.insert(t, 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.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 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 function common.join(j, l) local s = "" for i, v in ipairs(l) do if i > 1 then s = s .. j .. v else s = v end end return s end function common.sort(t, f) table.sort(t, f) return t end @@ -378,7 +379,7 @@ end function common.dirname(path) local s = path:reverse():find("[/\\]") if not s then return path end return path:sub(1, #path - s) end function common.basename(path) local s = path:reverse():find("[/\\]") if not s then return path end return path:sub(#path - s + 2) end -function common.path(exec) return common.grep(common.map({ common.split(":", os.getenv("PATH")) }, function(e) return e .. PATHSEP .. exec end), function(e) return system.stat(e) end)[1] end +function common.path(exec) return common.first(common.map({ common.split(":", os.getenv("PATH")) }, function(e) return e .. PATHSEP .. exec end), function(e) return system.stat(e) end) end function common.rmrf(root) local info = root and root ~= "" and system.stat(root) if not info then return end @@ -514,7 +515,7 @@ function Plugin:get_install_path(bottle) end function Plugin:is_core(bottle) return self.type == "core" end -function Plugin:is_installed(bottle) return bottle.lite_xl:is_compatible(self) and system.stat(self:get_install_path(bottle)) end +function Plugin:is_installed(bottle) return self:is_core(bottle) or (bottle.lite_xl:is_compatible(self) and system.stat(self:get_install_path(bottle))) end function Plugin:is_incompatible(plugin) return self.dependencies[plugin.name] and not match_version(plugin.version, dependencies[plugin.name]) end function Plugin:get_compatibilities(bottle) @@ -814,8 +815,7 @@ function Repository:add() local manifest, remotes = self:parse_manifest() if AUTO_PULL_REMOTES then -- any remotes we don't have in our listing, call add, and add into the list for i, remote in ipairs(remotes) do - local has = common.grep(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end) > 0 - if #has == 0 then + if common.first(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end) then remote:add() table.insert(repositories, remote) end @@ -837,8 +837,7 @@ function Repository:update() end if AUTO_PULL_REMOTES then -- any remotes we don't have in our listing, call add, and add into the list for i, remote in ipairs(remotes) do - local has = #common.grep(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end) - if #has == 0 then + if common.first(repositories, function(repo) return repo.remote == remote.remote and repo.branch == remote.branch and repo.commit == remote.comit end) then remote:add() table.insert(repositories, remote) end @@ -871,6 +870,7 @@ function LiteXL.new(repository, metadata) return self end +function LiteXL:get_binary_path() return self.local_path .. PATHSEP .. "lite-xl" end function LiteXL:get_data_directory() return self.local_path .. PATHSEP .. "data" end function LiteXL:is_system() return system_bottle and system_bottle.lite_xl == self end function LiteXL:is_local() return not self.repository and self.path end @@ -891,7 +891,7 @@ function LiteXL:install() system.chmod(self.local_path .. PATHSEP .. "lite-xl", 448) -- chmod to rwx------- common.copy(datadir, self.local_path .. PATHSEP .. "data") elseif self.path and not self.repository then -- local repository - system.symlink(self.local_path .. PATHSEP .. "lite_xl", self.path .. PATHSEP .. "lite_xl") + system.symlink(self:get_binary_path(), self.path .. PATHSEP .. "lite_xl") else if self.remote then system.init(self.local_path, self.remote) @@ -1074,7 +1074,7 @@ local function lpm_repo_update(...) end local function get_lite_xl(version) - return common.grep(common.concat(lite_xls, common.flat_map(repositories, function(e) return e.lite_xls end)), function(lite_xl) return lite_xl.version == version end)[1] + return common.first(common.concat(lite_xls, common.flat_map(repositories, function(e) return e.lite_xls end)), function(lite_xl) return lite_xl.version == version end) end local function lpm_lite_xl_save() @@ -1114,7 +1114,7 @@ local function lpm_lite_xl_switch(version, target) if not lite_xl:is_installed() then log_action("Installing lite-xl " .. lite_xl.version) lite_xl:install() end local stat = system.stat(target) if stat and stat.symlink then os.remove(target) end - system.symlink(lite_xl.local_path .. PATHSEP .. "lite-xl", target) + system.symlink(lite_xl:get_binary_path(), target) if not common.path('lite-xl') then os.remove(target) error(target .. " is not on your $PATH; please supply a target that can be found on your $PATH, called `lite-xl`.") @@ -1170,7 +1170,7 @@ local function lpm_lite_xl_list() else max_version = max_version + 2 print(string.format("%" .. max_version .. "s | %10s | %s", "Version", "Status", "Location")) - print(string.format("%" .. max_version .."s | %10s | %s", "----------", "---------", "---------------------------")) + print(string.format("%" .. max_version .."s | %10s | %s", "-------", "---------", "---------------------------")) for i, lite_xl in ipairs(result["lite-xl"]) do print(string.format("%" .. max_version .. "s | %10s | %s", (lite_xl.is_system and "* " or "") .. lite_xl.version, lite_xl.status, (lite_xl.status ~= "available" and lite_xl.local_path or lite_xl.repository))) end @@ -1299,6 +1299,15 @@ local function lpm_plugin_upgrade() end local function lpm_purge() + local path = common.path("lite-xl") + if path then + local lite_xl = get_lite_xl("system") + if lite_xl then + os.remove(path) + system.symlink(lite_xl:get_binary_path(), target) + log_action("Reset lite-xl symlink to system.") + end + end log_action("Removed " .. CACHEDIR .. ".") common.rmrf(CACHEDIR) end @@ -1355,11 +1364,11 @@ local function run_command(ARGS) elseif ARGS[2] == "rm" then lpm_repo_rm(table.unpack(common.slice(ARGS, 3))) elseif ARGS[2] == "update" then lpm_repo_update(table.unpack(common.slice(ARGS, 3))) elseif ARGS[2] == "repo" and ARGS[3] == "update" then lpm_repo_update(table.unpack(common.slice(ARGS, 4))) - elseif ARGS[2] == "repo" and ARGS[3] == "list" then return lpm_repo_list() + elseif ARGS[2] == "repo" and (#ARGS == 2 or ARGS[3] == "list") then return lpm_repo_list() elseif ARGS[2] == "plugin" and ARGS[3] == "install" then lpm_install(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "plugin" and ARGS[3] == "uninstall" then lpm_plugin_uninstall(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "plugin" and ARGS[3] == "reinstall" then lpm_plugin_reinstall(table.unpack(common.slice(ARGS, 4))) - elseif ARGS[2] == "plugin" and ARGS[3] == "list" then return lpm_plugin_list(table.unpack(common.slice(ARGS, 4))) + elseif ARGS[2] == "plugin" and (#ARGS == 2 or ARGS[3] == "list") then return lpm_plugin_list(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "plugin" and ARGS[3] == "upgrade" then return lpm_plugin_upgrade(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "upgrade" then return lpm_plugin_upgrade(table.unpack(common.slice(ARGS, 3))) elseif ARGS[2] == "install" then lpm_install(table.unpack(common.slice(ARGS, 3))) @@ -1367,7 +1376,7 @@ local function run_command(ARGS) elseif ARGS[2] == "reinstall" then lpm_plugin_reinstall(table.unpack(common.slice(ARGS, 3))) elseif ARGS[2] == "describe" then lpm_describe(table.unpack(common.slice(ARGS, 3))) elseif ARGS[2] == "list" then return lpm_plugin_list(table.unpack(common.slice(ARGS, 3))) - elseif ARGS[2] == "lite-xl" and ARGS[3] == "list" then return lpm_lite_xl_list(table.unpack(common.slice(ARGS, 4))) + elseif ARGS[2] == "lite-xl" and (#ARGS == 2 or ARGS[3] == "list") then return lpm_lite_xl_list(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "lite-xl" and ARGS[3] == "uninstall" then return lpm_lite_xl_uninstall(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "lite-xl" and ARGS[3] == "install" then return lpm_lite_xl_install(table.unpack(common.slice(ARGS, 4))) elseif ARGS[2] == "lite-xl" and ARGS[3] == "switch" then return lpm_lite_xl_switch(table.unpack(common.slice(ARGS, 4))) @@ -1567,7 +1576,7 @@ Flags have the following effects: lite_xl_binary = system.stat(lite_xl_binary).symlink or lite_xl_binary local directory = common.dirname(lite_xl_binary) local hash = system.hash(lite_xl_binary, "file") - local system_lite_xl = common.grep(common.concat(common.flat_map(repositories, function(r) return r.lite_xls end), lite_xls), function(lite_xl) return lite_xl.local_path == directory end)[1] + local system_lite_xl = common.first(common.concat(common.flat_map(repositories, function(r) return r.lite_xls end), lite_xls), function(lite_xl) return lite_xl.local_path == directory end) if not system_lite_xl then if #common.grep(lite_xls, function(e) return e.version == "system" end) > 0 then error("can't create new system lite, please `lpm rm lite-xl system`, or resolve otherwise") end system_lite_xl = LiteXL.new(nil, { path = directory, mod_version = 3, version = "system", tags = { "system", "local" } }) |