Allow custom folder for NPM Cache, for Android

This commit is contained in:
Grahame Grieve 2023-06-21 17:13:17 +10:00
parent 61b5619b84
commit 189d9811c0
2 changed files with 63 additions and 18 deletions

View File

@ -1286,7 +1286,34 @@ public class Utilities {
Collections.sort(list); Collections.sort(list);
return list; return list;
} }
public static List<String> sorted(String[] set) {
List<String> list = new ArrayList<>();
for (String s : set) {
list.add(s);
}
Collections.sort(list);
return list;
}
public static List<String> reverseSorted(Collection<String> set) {
List<String> list = new ArrayList<>();
list.addAll(set);
Collections.sort(list, Collections.reverseOrder());
return list;
}
public static List<String> reverseSorted(String[] set) {
List<String> list = new ArrayList<>();
for (String s : set) {
list.add(s);
}
Collections.sort(list, Collections.reverseOrder());
return list;
}
public static void analyseStringDiffs(Set<String> source, Set<String> target, Set<String> missed, Set<String> extra) { public static void analyseStringDiffs(Set<String> source, Set<String> target, Set<String> missed, Set<String> extra) {
for (String s : source) for (String s : source)
if (!target.contains(s)) if (!target.contains(s))

View File

@ -90,8 +90,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public enum FilesystemPackageCacheMode { public enum FilesystemPackageCacheMode {
USER, SYSTEM, TESTING USER, SYSTEM, TESTING, CUSTOM
} }
// When running in testing mode, some packages are provided from the test case repository rather than by the normal means // When running in testing mode, some packages are provided from the test case repository rather than by the normal means
@ -116,6 +115,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
private Map<String, String> ciList = new HashMap<String, String>(); private Map<String, String> ciList = new HashMap<String, String>();
private JsonArray buildInfo; private JsonArray buildInfo;
private boolean suppressErrors; private boolean suppressErrors;
private boolean minimalMemory;
public FilesystemPackageCacheManager(boolean userMode) throws IOException { public FilesystemPackageCacheManager(boolean userMode) throws IOException {
@ -126,6 +126,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
init(mode); init(mode);
} }
/**
* Defined for use on Android
*
* @param customFolder
* @throws IOException
*/
public FilesystemPackageCacheManager(String customFolder) throws IOException {
this.cacheFolder = customFolder;
init(FilesystemPackageCacheMode.CUSTOM);
}
public void init(FilesystemPackageCacheMode mode) throws IOException { public void init(FilesystemPackageCacheMode mode) throws IOException {
myPackageServers.addAll(PackageServer.publicServers()); myPackageServers.addAll(PackageServer.publicServers());
@ -139,6 +151,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
case TESTING: case TESTING:
cacheFolder = Utilities.path("[tmp]", ".fhir", "packages"); cacheFolder = Utilities.path("[tmp]", ".fhir", "packages");
break; break;
case CUSTOM:
if (!new File(cacheFolder).exists()) {
throw new FHIRException("The folder ''"+cacheFolder+"' could not be found");
}
default: default:
break; break;
} }
@ -150,7 +166,22 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
createIniFile(); createIniFile();
} }
public boolean isMinimalMemory() {
return minimalMemory;
}
public void setMinimalMemory(boolean minimalMemory) {
this.minimalMemory = minimalMemory;
}
/**
* do not use this in minimal memory mode
* @param packagesFolder
* @throws IOException
*/
public void loadFromFolder(String packagesFolder) throws IOException { public void loadFromFolder(String packagesFolder) throws IOException {
assert !minimalMemory;
File[] files = new File(packagesFolder).listFiles(); File[] files = new File(packagesFolder).listFiles();
if (files != null) { if (files != null) {
for (File f : files) { for (File f : files) {
@ -165,19 +196,6 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
return cacheFolder; return cacheFolder;
} }
private List<String> sorted(String[] keys) {
List<String> names = new ArrayList<String>();
for (String s : keys)
names.add(s);
Collections.sort(names);
return names;
}
private List<String> reverseSorted(String[] keys) {
Arrays.sort(keys, Collections.reverseOrder());
return Arrays.asList(keys);
}
private NpmPackage loadPackageInfo(String path) throws IOException { private NpmPackage loadPackageInfo(String path) throws IOException {
NpmPackage pi = NpmPackage.fromFolder(path); NpmPackage pi = NpmPackage.fromFolder(path);
return pi; return pi;
@ -284,7 +302,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
public String getLatestVersionFromCache(String id) throws IOException { public String getLatestVersionFromCache(String id) throws IOException {
for (String f : reverseSorted(new File(cacheFolder).list())) { for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) {
File cf = new File(Utilities.path(cacheFolder, f)); File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) { if (cf.isDirectory()) {
if (f.startsWith(id + "#")) { if (f.startsWith(id + "#")) {
@ -372,7 +390,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
String foundPackage = null; String foundPackage = null;
String foundVersion = null; String foundVersion = null;
for (String f : reverseSorted(new File(cacheFolder).list())) { for (String f : Utilities.reverseSorted(new File(cacheFolder).list())) {
File cf = new File(Utilities.path(cacheFolder, f)); File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) { if (cf.isDirectory()) {
if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
@ -1017,7 +1035,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
} }
for (String f : sorted(new File(cacheFolder).list())) { for (String f : Utilities.sorted(new File(cacheFolder).list())) {
if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) { if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
return true; return true;
} }