Skip to content

Commit

Permalink
Merge pull request #35620 from iocanel/cli-perf
Browse files Browse the repository at this point in the history
Improve latency of CLI plugin commands
  • Loading branch information
gsmet authored Sep 20, 2023
2 parents adeab40 + 33ec0fa commit e929e65
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public JBangCatalogService(boolean interactiveMode, MessageWriter output, String
this.jbang = new JBangSupport(interactiveMode, output);
}

public boolean ensureJBangIsInstalled() {
return jbang.ensureJBangIsInstalled();
}

@Override
public JBangCatalog readCatalog(Path path) {
if (!jbang.isAvailable() && !jbang.isInstallable()) {
Expand Down Expand Up @@ -93,8 +97,9 @@ public JBangCatalog readCombinedCatalog(Optional<Path> projectDir, Optional<Path
});
});

//If not catalog have been specified use all available.
if (remoteCatalogs.length == 0) {
if (!jbang.isAvailable()) {
//If jbang is not available, ignore aliases
} else if (remoteCatalogs.length == 0) { //If not catalog have been specified use all available.
aliases.putAll(listAliasesOrFallback(jbang, fallbackCatalog).entrySet()
.stream()
.filter(e -> !aliases.containsKey(e.getKey()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class JBangSupport {
private Path workingDirectory;

private Optional<Boolean> installed = Optional.empty();
private Optional<String> version = Optional.empty();

public JBangSupport(boolean interactiveMode, MessageWriter output) {
this(interactiveMode, output, Paths.get(System.getProperty("user.dir")));
Expand Down Expand Up @@ -138,8 +139,12 @@ public List<String> execute(String... args) {
}

public Optional<String> version() {
return execute("version").stream().findFirst();

if (version.isPresent()) {
return version;
} else {
version = execute("version").stream().findFirst();
}
return version;
}

public boolean isAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ private boolean reconcile(PluginCatalog catalog) {
.orElseThrow(() -> new IllegalArgumentException("Unknwon plugin catalog location."));
List<PluginType> installedTypes = catalog.getPlugins().entrySet().stream().map(Map.Entry::getValue).map(Plugin::getType)
.collect(Collectors.toList());
Map<String, Plugin> installablePlugins = state.getInstallablePlugins().entrySet().stream()
//Let's only fetch installable plugins of the corresponding types.
//This will help us avoid uneeded calls to things like jbang if no jbang plugins are installed
Map<String, Plugin> installablePlugins = state.installablePlugins(installedTypes).entrySet().stream()
.filter(e -> installedTypes.contains(e.getValue().getType()))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public Map<String, Plugin> getInstallablePlugins() {
public Map<String, Plugin> jbangPlugins() {
boolean isUserScoped = !projectRoot.isPresent();
Map<String, Plugin> jbangPlugins = new HashMap<>();
jbangCatalogService.ensureJBangIsInstalled();
JBangCatalog jbangCatalog = jbangCatalogService.readCombinedCatalog(projectRoot, userHome);
jbangCatalog.getAliases().forEach((location, alias) -> {
String name = util.getName(location);
Expand Down

0 comments on commit e929e65

Please sign in to comment.