Skip to content

Commit

Permalink
moving haxelib library caching from project and SDK to module level s…
Browse files Browse the repository at this point in the history
…o we can support module local haxelib repositories
  • Loading branch information
m0rkeulv committed Jun 11, 2023
1 parent aa0e64d commit 0055da1
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 311 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/intellij/plugins/haxe/haxelib/HaxeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class HaxeLibrary {

private static String CURRENT_DIR = ".";

final private ProjectLibraryCache myCache;
final private ModuleLibraryCache myCache;
final private String myName;
final private String myLibraryRoot;
final private String myRelativeClasspath;
Expand All @@ -48,12 +48,12 @@ public class HaxeLibrary {
// TODO: Add the extraParams.hxml data here. Use the hxml parser; see LimeUtil.getLimeProjectModel() as an example.


private HaxeLibrary(@NotNull String name, @NotNull VirtualFile libraryRoot, @NotNull ProjectLibraryCache owner) {
private HaxeLibrary(@NotNull String name, @NotNull VirtualFile libraryRoot, @NotNull ModuleLibraryCache owner) {
myCache = owner;
myLibraryRoot = libraryRoot.getUrl();

myMetadata = HaxelibMetadata.load(libraryRoot);
HaxeLibraryInfo pathInfo = HaxelibUtil.deriveLibraryInfoFromPath(owner.getSdk(), libraryRoot.getPath());
HaxeLibraryInfo pathInfo = HaxelibUtil.deriveLibraryInfoFromPath(owner, libraryRoot.getPath());

String mdname = myMetadata.getName();
if (null != mdname && !mdname.isEmpty()) {
Expand Down Expand Up @@ -185,9 +185,9 @@ public HaxelibSemVer getVersion() {
* @return the loaded HaxeLibrary of the given name; null if not found.
*/
@Nullable
public static HaxeLibrary load(ProjectLibraryCache owner, String libName, String libVersion, Sdk sdk) {
public static HaxeLibrary load(ModuleLibraryCache owner, String libName, String libVersion, Sdk sdk) {
// Ask haxelib for the path to this library.
VirtualFile libraryRoot = HaxelibUtil.getLibraryRoot(sdk, libName,libVersion);
VirtualFile libraryRoot = HaxelibUtil.getLibraryRoot(sdk,owner, libName,libVersion);
if (null == libraryRoot) {
// XXX: This case might occur if the library is not managed by haxelib, but then
// that should be a classpath, not a lib.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public HaxeLibraryDependency(@NotNull Module module, @NotNull String name, @Null
this(HaxelibProjectUpdater.getLibraryCache(module), name, semver, reliant);
}

public HaxeLibraryDependency(@NotNull ProjectLibraryCache owner, @NotNull String name, @Nullable String semver, @Nullable HaxeLibrary reliant) {
public HaxeLibraryDependency(@NotNull ModuleLibraryCache owner, @NotNull String name, @Nullable String semver, @Nullable HaxeLibrary reliant) {
super(owner, name, HaxelibSemVer.create(semver), false);
reliants = new HaxeLibraryList(owner.getSdk());
if (null != reliant) {
this.reliants.add(reliant.createReference());
}
}

public HaxeLibraryDependency(@NotNull ProjectLibraryCache owner, @NotNull String name, @Nullable String semver, @NotNull HaxeLibraryList reliants) {
public HaxeLibraryDependency(@NotNull ModuleLibraryCache owner, @NotNull String name, @Nullable String semver, @NotNull HaxeLibraryList reliants) {
this(owner, name, semver, (HaxeLibrary)null);
this.reliants.addAll(reliants);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class HaxeLibraryReference {
}

protected final String name;
protected final ProjectLibraryCache owner;
protected final ModuleLibraryCache owner;
protected final HaxelibSemVer semver;
protected final AtomicBoolean isManaged = new AtomicBoolean(false);

Expand All @@ -51,15 +51,15 @@ public HaxeLibraryReference(@NotNull Module module, @NotNull String libName, @No
this(module, libName, semver, false);
}

public HaxeLibraryReference(@NotNull ProjectLibraryCache owner, @NotNull String libName, @NotNull HaxelibSemVer semver) {
public HaxeLibraryReference(@NotNull ModuleLibraryCache owner, @NotNull String libName, @NotNull HaxelibSemVer semver) {
this(owner, libName, semver, false);
}

public HaxeLibraryReference(@NotNull Module module, @NotNull String libName, @NotNull HaxelibSemVer semver, boolean isManaged) {
this(HaxelibProjectUpdater.getLibraryCache(module), libName, semver, isManaged);
}

public HaxeLibraryReference(@NotNull ProjectLibraryCache owner,
public HaxeLibraryReference(@NotNull ModuleLibraryCache owner,
@NotNull String libName,
@NotNull HaxelibSemVer semver,
boolean isManaged) {
Expand All @@ -76,12 +76,12 @@ public HaxeLibraryReference clone() {
}

public static HaxeLibraryReference create(@NotNull Module module, @NotNull String name) {
ProjectLibraryCache owner = HaxelibProjectUpdater.getLibraryCache(module);
ModuleLibraryCache owner = HaxelibProjectUpdater.getLibraryCache(module);
return create(owner, name);
}


public static HaxeLibraryReference create(@NotNull ProjectLibraryCache owner, @NotNull String name) {
public static HaxeLibraryReference create(@NotNull ModuleLibraryCache owner, @NotNull String name) {
if (name.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public String getName() {
* @return the Cache that owns the reference (as given at instantiation).
*/
@NotNull
public ProjectLibraryCache getOwner() {
public ModuleLibraryCache getOwner() {
return owner;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.intellij.plugins.haxe.haxelib;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.vfs.VirtualFile;
import lombok.CustomLog;
import org.jetbrains.annotations.NotNull;

Expand All @@ -19,7 +21,7 @@ public class HaxelibCacheManager {

// current version of haxelib shows versions as date + version + description
private static Pattern HAXELIB_VERSION_LINE =
Pattern.compile("(?<date>\\d{4}-\\d{2}-\\d{2}\s\\d{2}:\\d{2}:\\d{2})\s(?<version>.*)\s:\s(?<description>.*)");
Pattern.compile("(?<date>\\d{4}-\\d{2}-\\d{2}\s\\d{2}:\\d{2}:\\d{2})\s(?<version>.*?)\s:\s(?<description>.*)");

private static Map<Module, HaxelibCacheManager> instances = new HashMap<>();

Expand Down Expand Up @@ -103,8 +105,9 @@ private static Map<String, Set<String>> readAvailableOnline(Sdk sdk) {
return libMap;
}

private static Map<String, Set<String>> readInstalledLibraries(@NotNull Sdk sdk) {
HaxelibInstalledIndex index = HaxelibInstalledIndex.fetchFromHaxelib(sdk);
private Map<String, Set<String>> readInstalledLibraries(@NotNull Sdk sdk) {
VirtualFile file = ProjectUtil.guessModuleDir(module);
HaxelibInstalledIndex index = HaxelibInstalledIndex.fetchFromHaxelib(sdk, file);
return index.getInstalledLibrariesAndVersions();

}
Expand All @@ -116,7 +119,8 @@ public Set<String> fetchAvailableVersions(String name) {
log.warn("Unable to fetch Available Versions, invalid SDK paths");
return Set.of();
}
List<String> list = HaxelibCommandUtils.issueHaxelibCommand(sdk, "info", name);
VirtualFile file = ProjectUtil.guessModuleDir(module);
List<String> list = HaxelibCommandUtils.issueHaxelibCommand(sdk, file,"info", name);
// filter to find version numbers

Set<String> versions = list.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,65 +279,17 @@ public static HaxeClasspath getFullClasspath(@NotNull Module module) {
}


/**
* Locate files and dependencies using 'haxelib path <name>'
*
* @param name name of the base file or library to search for.
* @return a set of path name URLs, may be an empty list.
*/
@NotNull
public static List<String> getHaxelibLibraryPathUrl(@NotNull Sdk sdk, @NotNull String name) {
List<String> strings = HaxelibCommandUtils.issueHaxelibCommand(sdk, "path",
name);
List<String> classpathUrls = new ArrayList<String>(strings.size());

for (String string : strings) {
if (isClassPathLine(string)) {
VirtualFile file = LocalFileFinder.findFile(string);
if (file != null) {
classpathUrls.add(file.getUrl());
}
}
}

return classpathUrls;
}

/**
* Locate files and dependencies using 'haxelib path <name>'
*
* @param name name of the base file or library to search for.
* @return a set of HaxelibItems, may be an empty list.
*/
@NotNull
public static HaxeClasspath getHaxelibLibraryPath(@NotNull Sdk sdk, @NotNull String name) {
List<String> strings = HaxelibCommandUtils.issueHaxelibCommand(sdk, "path", name);
HaxeClasspath classpath = new HaxeClasspath(strings.size());

for (String string : strings) {
if (isClassPathLine(string)) {
VirtualFile file = LocalFileFinder.findFile(string);
if (file != null) {
// There are no duplicates in the return from haxelib, so no need to check contains().
classpath.add(new HaxelibItem(file.getPath(), file.getUrl()));
}
}
}

return classpath;
}

/**
* Local classpaths of specified libraries and their dependencies.
*/
public static Set<String> getHaxelibLibrariesClasspaths(@NotNull Sdk sdk, String... libNames) {
public static Set<String> getHaxelibLibrariesClasspaths(@NotNull Sdk sdk,VirtualFile workDir, String... libNames) {
Set<String> result = new HashSet<>();

ArrayList<String> args = new ArrayList<>();
args.add("path");
Collections.addAll(args, libNames);

List<String> out = HaxelibCommandUtils.issueHaxelibCommand(sdk, args.toArray(new String[0]));
List<String> out = HaxelibCommandUtils.issueHaxelibCommand(sdk,workDir, args.toArray(new String[0]));
for(String line:out) {
if(!isClassPathLine(line)) {
continue;
Expand All @@ -361,7 +313,7 @@ public static Set<String> getHaxelibLibrariesClasspaths(@NotNull Sdk sdk, String
*/
@NotNull
public static List<String> getAvailableLibrariesMatching(@NotNull Sdk sdk, @NotNull String word) {
List<String> stringList = HaxelibCommandUtils.issueHaxelibCommand(sdk, "search", word);
List<String> stringList = HaxelibCommandUtils.issueHaxelibCommand(sdk, null,"search", word);
if (stringList.size() > 0) {
// Last line is the count of libraries found.
stringList.remove(stringList.size() - 1);
Expand All @@ -381,7 +333,7 @@ public static List<String> getAvailableLibrariesMatching(@NotNull Sdk sdk, @NotN
public static List<String> getProjectDisplayInformation(@NotNull Module module, @NotNull File dir, @NotNull String executable, @NotNull Sdk sdk) {
List<String> strings1 = Collections.EMPTY_LIST;

ProjectLibraryCache cache = HaxelibProjectUpdater.getLibraryCache(module);
ModuleLibraryCache cache = HaxelibProjectUpdater.getLibraryCache(module);
if (cache != null && cache.libraryIsInstalled(executable)) {
ArrayList<String> commandLineArguments = new ArrayList<String>();
commandLineArguments.add(HaxelibCommandUtils.getHaxelibPath(sdk));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public static String getHaxelibPath(@NotNull Sdk sdk) {
*/

@NotNull
public static List<String> issueHaxelibCommand(@NotNull Sdk sdk, String... args) {
public static List<String> issueHaxelibCommand(@NotNull Sdk sdk, @Nullable VirtualFile workDir, String... args) {
Application application = ApplicationManager.getApplication();
if (application.isDispatchThread()) {
List<String> result = new ArrayList<>();
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> result.addAll(_issueHaxelibCommand(sdk, args)),
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> result.addAll(_issueHaxelibCommand(sdk,workDir, args)),
"Haxelib Command", false, null);
return result;
}
Expand All @@ -111,7 +111,7 @@ else if (application.isReadAccessAllowed()) {
// TODO: this is a hacky way to get around the `checkEdtAndReadAction` check,
// while we move the logic to another thread we block the current one using get(), preferred
// method would be some kind of callback
return application.executeOnPooledThread(()-> _issueHaxelibCommand(sdk, args)).get();
return application.executeOnPooledThread(()-> _issueHaxelibCommand(sdk, workDir, args)).get();
}
catch (InterruptedException e) {
throw new RuntimeException(e);
Expand All @@ -121,13 +121,13 @@ else if (application.isReadAccessAllowed()) {
}
}
else {
return _issueHaxelibCommand(sdk, args);
return _issueHaxelibCommand(sdk, workDir, args);
}

}

@NotNull
private static List<String> _issueHaxelibCommand(@NotNull Sdk sdk, String... args) {
private static List<String> _issueHaxelibCommand(@NotNull Sdk sdk, @Nullable VirtualFile workDir, String... args) {

// TODO: Wrap the process with a timer?

Expand All @@ -144,15 +144,17 @@ private static List<String> _issueHaxelibCommand(@NotNull Sdk sdk, String... arg

// TODO mlo: try to clean up code so it only uses either dir or file
File haxelibCmd = new File(haxelibPath);
VirtualFile dir = haxelibCmd.isFile()
? LocalFileSystem.getInstance().findFileByPath(haxelibCmd.getParent())
: LocalFileSystem.getInstance().findFileByPath(haxelibPath);
if (dir == null) {
if(workDir == null) {
workDir = haxelibCmd.isFile()
? LocalFileSystem.getInstance().findFileByPath(haxelibCmd.getParent())
: LocalFileSystem.getInstance().findFileByPath(haxelibPath);
}
if (workDir == null) {
log.error("unable to execute haxelib command, haxelib path is null");
return List.of();
}
List<String> stdout = new ArrayList<String>();
int exitvalue = HaxeProcessUtil.runProcess(commandLineArguments, true, dir, sdkData,
int exitvalue = HaxeProcessUtil.runProcess(commandLineArguments, true, workDir, sdkData,
stdout, null, null, false);

if (0 != exitvalue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.intellij.plugins.haxe.haxelib;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand Down Expand Up @@ -39,15 +40,15 @@ public String getSelectedVersion(String Library) {
return selectedVersions.getOrDefault(Library, null);
}

public static HaxelibInstalledIndex fetchFromHaxelib(@NotNull Sdk sdk){
public static HaxelibInstalledIndex fetchFromHaxelib(@NotNull Sdk sdk, VirtualFile workDir){
// haxelib list output looks like:
// lime-tools: 1.4.0 [1.5.6]
// The library name comes first, followed by a colon, followed by a
// list of the available versions.

HaxelibInstalledIndex index = new HaxelibInstalledIndex();

List<String> listCmdOutput = HaxelibCommandUtils.issueHaxelibCommand(sdk, "list");
List<String> listCmdOutput = HaxelibCommandUtils.issueHaxelibCommand(sdk, workDir, "list");
if ((listCmdOutput.size() > 0) && (!listCmdOutput.get(0).contains("Unknown command"))) {
for (String line : listCmdOutput) {
int firstColon = line.indexOf(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void modulesAdded(@NotNull Project project, @NotNull List<? extends Modul
@Override
public void moduleRemoved(@NotNull Project project, @NotNull Module module) {
HaxelibCacheManager.removeInstance(module);
HaxelibProjectUpdater.getInstance().findProjectTracker(project).moduleRemoved(module);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static void updateLibs(@NotNull Module module, Project project) {
HaxelibProjectUpdater.ProjectTracker tracker = instance.findProjectTracker(project);

if (tracker != null) {
tracker.getSdkManager().getLibraryManager(module).reload();
tracker.getLibraryManager().getLibraryManager(module).reload();
instance.synchronizeClasspaths(tracker);
}
}
Expand Down
Loading

0 comments on commit 0055da1

Please sign in to comment.