Skip to content

Commit

Permalink
Add Node.js install retry (5) (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy authored Sep 22, 2023
1 parent 491cd77 commit afe1661
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.quarkiverse.quinoa.deployment.config.QuinoaConfig;
import io.quarkiverse.quinoa.deployment.items.ConfiguredQuinoaBuildItem;
import io.quarkiverse.quinoa.deployment.items.ForwardedDevServerBuildItem;
import io.quarkiverse.quinoa.deployment.items.InstalledPackageManagerBuildItem;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class ForwardedDevProcessor {
public ForwardedDevServerBuildItem prepareDevService(
LaunchModeBuildItem launchMode,
ConfiguredQuinoaBuildItem configuredQuinoa,
InstalledPackageManagerBuildItem installedPackageManager,
QuinoaConfig userConfig,
BuildProducer<DevServicesResultBuildItem> devServices,
Optional<ConsoleInstalledBuildItem> consoleInstalled,
Expand All @@ -77,7 +79,7 @@ public ForwardedDevServerBuildItem prepareDevService(
final DevServerConfig devServerConfig = resolvedConfig.devServer();
liveReload.setContextObject(QuinoaConfig.class, resolvedConfig);
final String configuredDevServerHost = devServerConfig.host();
final PackageManagerRunner packageManagerRunner = configuredQuinoa.getPackageManager();
final PackageManagerRunner packageManagerRunner = installedPackageManager.getPackageManager();
final String checkPath = resolvedConfig.devServer().checkPath().orElse(null);
if (devService != null) {
boolean shouldShutdownTheBroker = !resolvedConfig.equals(oldConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static io.quarkiverse.quinoa.deployment.config.QuinoaConfig.isEnabled;
import static io.quarkiverse.quinoa.deployment.config.QuinoaConfig.toHandlerConfig;
import static io.quarkiverse.quinoa.deployment.framework.FrameworkType.overrideConfig;
import static io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerInstall.install;
import static io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner.autoDetectPackageManager;
import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;

Expand Down Expand Up @@ -35,6 +34,7 @@
import io.quarkiverse.quinoa.deployment.framework.FrameworkType;
import io.quarkiverse.quinoa.deployment.items.BuiltResourcesBuildItem;
import io.quarkiverse.quinoa.deployment.items.ConfiguredQuinoaBuildItem;
import io.quarkiverse.quinoa.deployment.items.InstalledPackageManagerBuildItem;
import io.quarkiverse.quinoa.deployment.items.TargetDirBuildItem;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerInstall;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
Expand Down Expand Up @@ -78,7 +78,6 @@ FeatureBuildItem feature() {
@BuildStep
public ConfiguredQuinoaBuildItem prepareQuinoaDirectory(
LaunchModeBuildItem launchMode,
LiveReloadBuildItem liveReload,
QuinoaConfig userConfig,
OutputTargetBuildItem outputTarget) {
if (!isEnabled(userConfig)) {
Expand All @@ -95,52 +94,64 @@ public ConfiguredQuinoaBuildItem prepareQuinoaDirectory(
if (projectDirs == null) {
return null;
}
final Path packageJsonFile = projectDirs.uiDir.resolve("package.json");
if (!Files.isRegularFile(packageJsonFile)) {
final Path packageJson = projectDirs.uiDir.resolve("package.json");
if (!Files.isRegularFile(packageJson)) {
throw new ConfigurationException("No package.json found in Web UI directory: '" + configuredDir + "'");
}

final QuinoaConfig resolvedConfig = overrideConfig(launchMode, userConfig, packageJsonFile);
final QuinoaConfig resolvedConfig = overrideConfig(launchMode, userConfig, packageJson);

Optional<String> packageManagerBinary = resolvedConfig.packageManager();
List<String> paths = new ArrayList<>();
if (resolvedConfig.packageManagerInstall().enabled()) {
final PackageManagerInstall.Installation result = install(resolvedConfig.packageManagerInstall(),
projectDirs);
packageManagerBinary = Optional.of(result.getPackageManagerBinary());
paths.add(result.getNodeDirPath());
}
return new ConfiguredQuinoaBuildItem(projectDirs.projectRootDir, projectDirs.uiDir, packageJson, resolvedConfig);
}

final PackageManagerRunner packageManagerRunner = autoDetectPackageManager(packageManagerBinary,
resolvedConfig.packageManagerCommand(), projectDirs.getUIDir(), paths);
final boolean alreadyInstalled = Files.isDirectory(packageManagerRunner.getDirectory().resolve("node_modules"));
final boolean packageFileModified = liveReload.isLiveReload()
&& liveReload.getChangedResources().stream().anyMatch(r -> r.equals(packageJsonFile.toString()));
if (resolvedConfig.forceInstall() || !alreadyInstalled || packageFileModified) {
final boolean ci = resolvedConfig.ci().orElseGet(QuinoaProcessor::isCI);
if (ci) {
packageManagerRunner.ci();
} else {
packageManagerRunner.install();
@BuildStep
public InstalledPackageManagerBuildItem install(
ConfiguredQuinoaBuildItem configuredQuinoa,
LiveReloadBuildItem liveReload) {
if (configuredQuinoa != null) {
final QuinoaConfig resolvedConfig = configuredQuinoa.resolvedConfig();
Optional<String> packageManagerBinary = resolvedConfig.packageManager();
List<String> paths = new ArrayList<>();
if (resolvedConfig.packageManagerInstall().enabled()) {
final PackageManagerInstall.Installation result = PackageManagerInstall.install(
resolvedConfig.packageManagerInstall(),
configuredQuinoa.projectDir());
packageManagerBinary = Optional.of(result.getPackageManagerBinary());
paths.add(result.getNodeDirPath());
}

}
final PackageManagerRunner packageManagerRunner = autoDetectPackageManager(packageManagerBinary,
resolvedConfig.packageManagerCommand(), configuredQuinoa.uiDir(), paths);
final boolean alreadyInstalled = Files.isDirectory(packageManagerRunner.getDirectory().resolve("node_modules"));
final boolean packageFileModified = liveReload.isLiveReload()
&& liveReload.getChangedResources().stream()
.anyMatch(r -> r.equals(configuredQuinoa.packageJson().toString()));
if (resolvedConfig.forceInstall() || !alreadyInstalled || packageFileModified) {
final boolean ci = resolvedConfig.ci().orElseGet(QuinoaProcessor::isCI);
if (ci) {
packageManagerRunner.ci();
} else {
packageManagerRunner.install();
}

// attempt to autoconfigure settings based on the framework being used
return new ConfiguredQuinoaBuildItem(packageManagerRunner, resolvedConfig);
}
return new InstalledPackageManagerBuildItem(packageManagerRunner);
}
return null;
}

@BuildStep
public TargetDirBuildItem processBuild(
ConfiguredQuinoaBuildItem configuredQuinoa,
InstalledPackageManagerBuildItem installedPackageManager,
OutputTargetBuildItem outputTarget,
LaunchModeBuildItem launchMode,
LiveReloadBuildItem liveReload) throws IOException {
if (configuredQuinoa == null) {
return null;
}

final PackageManagerRunner packageManagerRunner = configuredQuinoa.getPackageManager();
final PackageManagerRunner packageManagerRunner = installedPackageManager.getPackageManager();
final QuinoaLiveContext contextObject = liveReload.getContextObject(QuinoaLiveContext.class);
if (launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT
&& isDevServerMode(configuredQuinoa.resolvedConfig())) {
Expand Down Expand Up @@ -200,7 +211,7 @@ void watchChanges(
if (quinoaDir.isEmpty() || isDevServerMode(quinoaDir.get().resolvedConfig())) {
return;
}
scan(quinoaDir.get().getPackageManager().getDirectory(), watchedPaths);
scan(quinoaDir.get().uiDir(), watchedPaths);
}

@BuildStep
Expand Down Expand Up @@ -247,7 +258,7 @@ List<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles(Optional<Confi
}

for (PackageManagerType pm : PackageManagerType.values()) {
final String watchFile = configuredQuinoa.get().getDirectory().resolve(pm.getLockFile()).toString();
final String watchFile = configuredQuinoa.get().uiDir().resolve(pm.getLockFile()).toString();
watchedFiles.add(new HotDeploymentWatchedFileBuildItem(watchFile));
}
return watchedFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.quarkiverse.quinoa.deployment.config.PackageManagerInstallConfig;
import io.quarkiverse.quinoa.deployment.config.QuinoaConfig;
import io.quarkiverse.quinoa.deployment.items.ConfiguredQuinoaBuildItem;
import io.quarkiverse.quinoa.deployment.items.InstalledPackageManagerBuildItem;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
import io.quarkiverse.quinoa.devui.QuinoaJsonRpcService;
import io.quarkus.deployment.IsDevelopment;
Expand Down Expand Up @@ -100,18 +101,18 @@ void createCard(BuildProducer<CardPageBuildItem> cardPageBuildItemBuildProducer,
}

@BuildStep(onlyIf = IsDevelopment.class)
JsonRPCProvidersBuildItem registerJsonRpcBackend(Optional<ConfiguredQuinoaBuildItem> quinoaDirectoryBuildItem,
JsonRPCProvidersBuildItem registerJsonRpcBackend(InstalledPackageManagerBuildItem installedPackageManager,
QuinoaConfig quinoaConfig) {
DevConsoleManager.register("quinoa-install-action",
install(quinoaDirectoryBuildItem, quinoaConfig));
install(installedPackageManager, quinoaConfig));
return new JsonRPCProvidersBuildItem(QuinoaJsonRpcService.class);
}

private Function<Map<String, String>, String> install(Optional<ConfiguredQuinoaBuildItem> quinoaDirectoryBuildItem,
private Function<Map<String, String>, String> install(InstalledPackageManagerBuildItem installedPackageManager,
QuinoaConfig quinoaConfig) {
return (map -> {
try {
final PackageManagerRunner packageManagerRunner = quinoaDirectoryBuildItem.orElseThrow().getPackageManager();
final PackageManagerRunner packageManagerRunner = installedPackageManager.getPackageManager();

// install or update packages
packageManagerRunner.install();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
import java.nio.file.Path;

import io.quarkiverse.quinoa.deployment.config.QuinoaConfig;
import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
import io.quarkus.builder.item.SimpleBuildItem;

public final class ConfiguredQuinoaBuildItem extends SimpleBuildItem {

private final PackageManagerRunner packageManagerRunner;

private final Path projectDir;
private final Path uiDir;
private final Path packageJson;
private final QuinoaConfig resolvedConfig;

public ConfiguredQuinoaBuildItem(PackageManagerRunner packageManagerRunner, QuinoaConfig resolvedConfig) {
this.packageManagerRunner = packageManagerRunner;
public ConfiguredQuinoaBuildItem(Path projectDir, Path uiDir, Path packageJson, QuinoaConfig resolvedConfig) {
this.projectDir = projectDir;
this.uiDir = uiDir;
this.packageJson = packageJson;
this.resolvedConfig = resolvedConfig;
}

public PackageManagerRunner getPackageManager() {
return packageManagerRunner;
public Path projectDir() {
return projectDir;
}

public Path uiDir() {
return uiDir;
}

public Path getDirectory() {
return getPackageManager().getDirectory();
public Path packageJson() {
return packageJson;
}

public QuinoaConfig resolvedConfig() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkiverse.quinoa.deployment.items;

import io.quarkiverse.quinoa.deployment.packagemanager.PackageManagerRunner;
import io.quarkus.builder.item.SimpleBuildItem;

public final class InstalledPackageManagerBuildItem extends SimpleBuildItem {

private final PackageManagerRunner packageManagerRunner;

public InstalledPackageManagerBuildItem(PackageManagerRunner packageManagerRunner) {
this.packageManagerRunner = packageManagerRunner;
}

public PackageManagerRunner getPackageManager() {
return packageManagerRunner;
}
}
Loading

0 comments on commit afe1661

Please sign in to comment.