Skip to content

Commit

Permalink
Refactor finding packages, and remove old packages by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mtorpey committed Sep 4, 2024
1 parent 42687fa commit d389649
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 62 deletions.
66 changes: 21 additions & 45 deletions gap/PackageManager.gi
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ function(string, args...)
version := true;
interactive := true;
if not IsString(string) then
ErrorNoReturn("PackageManager: InstallPackage: ",
"<string> must be a string");
ErrorNoReturn("PackageManager: InstallPackage: <string> must be a string");
elif Length(args) > 2 then
ErrorNoReturn("PackageManager: InstallPackage: ",
"requires 1 to 3 arguments (not ",
Expand All @@ -38,9 +37,11 @@ function(string, args...)
version := args[1];
interactive := args[2];
fi;

# Tidy up the string
NormalizeWhitespace(string);

# Call the appropriate function
NormalizeWhitespace(string);
if ForAny(PKGMAN_ArchiveFormats, ext -> EndsWith(string, ext)) then
return InstallPackageFromArchive(string);
elif EndsWith(string, ".git") then
Expand All @@ -55,12 +56,11 @@ end);

InstallGlobalFunction(RemovePackage,
function(name, interactive...)
local user_pkg_dir, allinfo, info, dir;

local info, dir, q;
# Check input
if not IsString(name) then
ErrorNoReturn("PackageManager: RemovePackage: ",
"<name> must be a string");
ErrorNoReturn("PackageManager: RemovePackage: <name> must be a string");
elif Length(interactive) > 1 then
ErrorNoReturn("PackageManager: RemovePackage: ",
"requires 1 or 2 arguments (not ",
Expand All @@ -77,31 +77,17 @@ function(name, interactive...)
fi;

# Locate the package
user_pkg_dir := PKGMAN_PackageDir();
allinfo := PackageInfo(name);
info := Filtered(allinfo,
x -> IsMatchingSublist(x.InstallationPath, user_pkg_dir));
if Length(info) = 0 then
Info(InfoPackageManager, 1,
"Package \"", name, "\" not installed in user package directory");
Info(InfoPackageManager, 2, "(currently set to ", PKGMAN_PackageDir(), ")");
if not IsEmpty(allinfo) then
Info(InfoPackageManager, 2, "installed at ",
List(allinfo, i -> i.InstallationPath), ", not in ", user_pkg_dir);
fi;
return false;
elif Length(info) >= 2 then
Info(InfoPackageManager, 1,
"Multiple versions of package ", name, " installed");
Info(InfoPackageManager, 3, "at ", List(info, x -> x.InstallationPath));
info := PKGMAN_UserPackageInfo(name : expectUnique);

# Need precisely one version
if Length(info) <> 1 then
return false;
fi;
dir := ShallowCopy(info[1].InstallationPath);

# Remove directory carefully
if interactive = false or
PKGMAN_AskYesNoQuestion("Really delete directory ", dir, " ?"
: default := false) then
dir := ShallowCopy(info[1].InstallationPath);
q := Concatenation("Really delete directory ", dir, " ?");
if interactive = false or PKGMAN_AskYesNoQuestion(q : default := false) then
PKGMAN_RemoveDir(dir);
return true;
fi;
Expand Down Expand Up @@ -132,23 +118,15 @@ function(name, interactive...)
else
interactive := true;
fi;

# Locate the package
# Package names should be case-insensitive
name := LowercaseString(name);
user_pkg_dir := PKGMAN_PackageDir();
allinfo := PackageInfo(name);
info := Filtered(allinfo,
x -> IsMatchingSublist(x.InstallationPath, user_pkg_dir));

# Locate the package
info := PKGMAN_UserPackageInfo(name);

# Package not installed
if Length(info) = 0 then
Info(InfoPackageManager, 1,
"Package \"", name, "\" not installed in user package directory");
Info(InfoPackageManager, 2, "(currently set to ", PKGMAN_PackageDir(), ")");
if not IsEmpty(allinfo) then
Info(InfoPackageManager, 2, "installed at ",
List(allinfo, i -> i.InstallationPath), ", not in ", user_pkg_dir);
fi;
if interactive and PKGMAN_AskYesNoQuestion("Would you like to install ",
name, "?" : default := true) then
return InstallPackageFromName(name);
Expand Down Expand Up @@ -208,13 +186,11 @@ function(name, interactive...)
fi;

# Remove old version (which might have changed its name)
allinfo := PackageInfo(name);
info := Filtered(allinfo,
x -> IsMatchingSublist(x.InstallationPath, user_pkg_dir));
info := PKGMAN_UserPackageInfo(name);
old := First(info, x -> x.Version = oldVer);
olddir := old.InstallationPath;
q := Concatenation("Remove old version of ", name, " at ", olddir, " ?");
if interactive and PKGMAN_AskYesNoQuestion(q : default := false) then
if interactive = false or PKGMAN_AskYesNoQuestion(q : default := false) then
PKGMAN_RemoveDir(olddir);
fi;
return true;
Expand Down
16 changes: 3 additions & 13 deletions gap/compile.gi
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@ function(name)

# Locate the package
name := LowercaseString(name);
user_pkg_dir := PKGMAN_PackageDir();
allinfo := PackageInfo(name);
info := Filtered(allinfo,
x -> IsMatchingSublist(x.InstallationPath, user_pkg_dir));
info := PKGMAN_UserPackageInfo(name : expectUnique);

# Package not installed
if Length(info) = 0 then
Info(InfoPackageManager, 1,
"Package \"", name, "\" not installed in user package directory");
Info(InfoPackageManager, 2, "(currently set to ", PKGMAN_PackageDir(), ")");
if not IsEmpty(allinfo) then
Info(InfoPackageManager, 2, "installed at ",
List(allinfo, i -> i.InstallationPath), ", not in ", user_pkg_dir);
fi;
return false;
fi;

# Compile it
return PKGMAN_CompileDir(info[1].InstallationPath);
# Compile all installations that were found
return ForAll(info, i -> PKGMAN_CompileDir(i.InstallationPath));
end);

InstallGlobalFunction(PKGMAN_CompileDir,
Expand Down
1 change: 1 addition & 0 deletions gap/packageinfo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DeclareGlobalFunction("InstallPackageFromInfo");
DeclareGlobalFunction("PKGMAN_GetPackageInfo");
DeclareGlobalFunction("PKGMAN_RefreshPackageInfo");
DeclareGlobalFunction("PKGMAN_ValidatePackageInfo");
DeclareGlobalFunction("PKGMAN_UserPackageInfo");

# PackageInfo files must at least contain the following:
PKGMAN_RequiredPackageInfoFields := ["PackageName",
Expand Down
30 changes: 30 additions & 0 deletions gap/packageinfo.gi
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,33 @@ function(info)
quiet := InfoLevel(InfoPackageManager) < 4;
return ValidatePackageInfo(info : quiet := quiet);
end);

# Return package info records for all packages installed with this name in the
# user package directory, and warn if there are none.
# expectUnique option: warn if there are multiple.
InstallGlobalFunction(PKGMAN_UserPackageInfo,
function(name)
local user_pkg_dir, allinfo, userinfo;

user_pkg_dir := PKGMAN_PackageDir();
allinfo := PackageInfo(name);
userinfo := Filtered(allinfo,
x -> IsMatchingSublist(x.InstallationPath, user_pkg_dir));

# Package not found
if Length(userinfo) = 0 then
Info(InfoPackageManager, 1, "Package \"", name, "\" not installed in user package directory");
Info(InfoPackageManager, 2, "(currently set to ", PKGMAN_PackageDir(), ")");
if not IsEmpty(allinfo) then
Info(InfoPackageManager, 2, "but installed at ", List(allinfo, i -> i.InstallationPath));
fi;
fi;

# Multiple versions found
if ValueOption("expectUnique") = true and Length(userinfo) > 1 then
Info(InfoPackageManager, 1, "Multiple versions of package ", name, " installed");
Info(InfoPackageManager, 2, "at ", List(userinfo, i -> i.InstallationPath));
fi;

return userinfo;
end);
5 changes: 1 addition & 4 deletions tst/archive.tst
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ gap> oldinfo <> fail;
true
gap> PositionSublist(oldinfo.InstallationPath, "3.6.4"); # version number not in dir name
fail
gap> UpdatePackage("transgrp", false);
gap> UpdatePackage("transgrp", false); # also removes old version
#I Package already installed at target location
#I Appending '.old' to old version directory
true
gap> newinfo := PackageInfo("transgrp")[1];;
gap> CompareVersionNumbers(newinfo.Version, ">=3.6.5");
true
gap> RemoveDirectoryRecursively(newinfo.InstallationPath); # clean up for future tests
true
gap> PKGMAN_RefreshPackageInfo();
gap> RemovePackage("transgrp", false);
true

Expand Down

0 comments on commit d389649

Please sign in to comment.