Skip to content

Commit

Permalink
keep optional files when upgrading package deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Sledmine committed Sep 9, 2023
1 parent c1fdb60 commit 56da297
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cmd/insert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions cmd/remove.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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.")
Expand Down

0 comments on commit 56da297

Please sign in to comment.