From 56da297a124773477150d81e41d1aeea784713ad Mon Sep 17 00:00:00 2001 From: Sledmine <40512223+Sledmine@users.noreply.github.com> Date: Sat, 9 Sep 2023 17:09:42 -0600 Subject: [PATCH] keep optional files when upgrading package deps --- CHANGELOG.md | 1 + cmd/insert.lua | 8 +++++--- cmd/remove.lua | 24 +++++++++++++++--------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2748c1..5baaee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Added package image preview to UI - Packages installed by force will not remove previous package if target is not found in the Mercury packages repository - Mercury install will always add Mercury folder to PATH from now on +- Optional files will be kept when upgrading packages that are dependencies of other packages # 3.5.0 - Added `--manifest` flag to `pack` command, allows to retrieve manifest from a package diff --git a/cmd/insert.lua b/cmd/insert.lua index 4baccf3..da35034 100644 --- a/cmd/insert.lua +++ b/cmd/insert.lua @@ -29,6 +29,9 @@ local errors = { } -- Install any mercury package +---@param mercPath string Path to mercury package +---@param forced? boolean Force installation +---@param skipOptionals? boolean Skip optional files local function insert(mercPath, forced, skipOptionals) if not exists(mercPath) then cprint("Error, " .. mercPath .. " does not exist.") @@ -91,12 +94,11 @@ local function insert(mercPath, forced, skipOptionals) if isDependencyRequired then cprint("Upgrading " .. dependency.label .. " " .. existingDependency.version .. " -> " .. dependency.version) - -- TODO Add skip optionals to remove action - if not remove(dependency.label, true) then + if not remove(dependency.label, true, false, false, false, true) then -- Try to remove dependency by force/index remove(dependency.label, false, false, false, true) end - if not install.package(dependency.label, dependency.version) then + if not install.package(dependency.label, dependency.version, false, true) then return false, errors.depedencyError end end diff --git a/cmd/remove.lua b/cmd/remove.lua index 6ad8cdb..0699ea3 100644 --- a/cmd/remove.lua +++ b/cmd/remove.lua @@ -35,19 +35,21 @@ end ---@param eraseBackups? boolean Erase previous backup files ---@param recursive? boolean Erase all the dependencies of this package ---@param index? boolean Forced remove by erasing the package entry from the packages index -local function remove(packageLabel, noRestore, eraseBackups, recursive, index) - if (search(packageLabel)) then +---@param keepOptionals? boolean Keep optional files +local function remove(packageLabel, noRestore, eraseBackups, recursive, index, keepOptionals) + if search(packageLabel) then local installedPackages = config.packages() or {} cprint("Removing package " .. packageLabel .. "...") -- Load package as entity to provide normalization and extra package methods local package = PackageMercury:new(installedPackages[packageLabel]) -- Remove dependencies recursively - if (recursive) then - --cprint("Warning remove is in recursive mode.") + if recursive then + -- cprint("Warning remove is in recursive mode.") local packageDependencies = package.dependencies - if (packageDependencies and #packageDependencies > 0) then + if packageDependencies and #packageDependencies > 0 then for dependency in each(packageDependencies) do - remove(dependency.label, noRestore, eraseBackups, recursive, index) + remove(dependency.label, noRestore, eraseBackups, recursive, index, + keepOptionals) end end end @@ -62,8 +64,12 @@ local function remove(packageLabel, noRestore, eraseBackups, recursive, index) end -- Normal remove, search for package files and erase them for fileIndex, file in pairs(package.files) do + if keepOptionals and file.type == "optional" then + goto continue + end -- Path to the existing file to erase local finalFilePath = file.outputPath + -- Start erasing proccess dprint("Erasing \"" .. finalFilePath .. "\"... ") local result, description, errorCode = delete(finalFilePath) @@ -92,7 +98,7 @@ local function remove(packageLabel, noRestore, eraseBackups, recursive, index) end else -- TODO Find info for these codes, those are related with the fs - if (errorCode == 2 or errorCode == 3) then + if errorCode == 2 or errorCode == 3 then cprint("Warning \"" .. file.path .. "\" was not found, previously erased or moved") else @@ -102,9 +108,9 @@ local function remove(packageLabel, noRestore, eraseBackups, recursive, index) return false, errors.fileSystemError end end + ::continue:: end - if (erasePackageFromIndex(packageLabel)) then - --cprint("Done, package \"" .. packageLabel .. "\" has been removed.") + if erasePackageFromIndex(packageLabel) then return true else cprint("Error, at trying to erase package from index.")