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);
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) {
for (String s : source)
if (!target.contains(s))

View File

@ -90,8 +90,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
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
@ -116,6 +115,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
private Map<String, String> ciList = new HashMap<String, String>();
private JsonArray buildInfo;
private boolean suppressErrors;
private boolean minimalMemory;
public FilesystemPackageCacheManager(boolean userMode) throws IOException {
@ -126,6 +126,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
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 {
myPackageServers.addAll(PackageServer.publicServers());
@ -139,6 +151,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
case TESTING:
cacheFolder = Utilities.path("[tmp]", ".fhir", "packages");
break;
case CUSTOM:
if (!new File(cacheFolder).exists()) {
throw new FHIRException("The folder ''"+cacheFolder+"' could not be found");
}
default:
break;
}
@ -150,7 +166,22 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
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 {
assert !minimalMemory;
File[] files = new File(packagesFolder).listFiles();
if (files != null) {
for (File f : files) {
@ -165,19 +196,6 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
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 {
NpmPackage pi = NpmPackage.fromFolder(path);
return pi;
@ -284,7 +302,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
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));
if (cf.isDirectory()) {
if (f.startsWith(id + "#")) {
@ -372,7 +390,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
String foundPackage = 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));
if (cf.isDirectory()) {
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 + "#"))) {
return true;
}