Skip to content

Commit

Permalink
Merge pull request #389 from spyrkob/generate_powershell_script
Browse files Browse the repository at this point in the history
Generate powershell script
  • Loading branch information
spyrkob committed May 23, 2023
2 parents b96b8c3 + fc0a7e3 commit 1c0f1fb
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;
import org.wildfly.installationmanager.MavenOptions;
import org.wildfly.installationmanager.spi.InstallationManager;
import org.wildfly.installationmanager.spi.OsShell;
import org.wildfly.prospero.api.InstallationMetadata;
import org.wildfly.prospero.api.ProvisioningDefinition;
import org.wildfly.prospero.cli.DistributionInfo;
Expand Down Expand Up @@ -96,4 +97,42 @@ public void generateApplyCommand() throws Exception {
final String command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"));
assertEquals(expected.toString(), command);
}

@Test
public void generateApplyCommandWithOsShell() throws Exception {
final Path channelsFile = MetadataTestUtils.prepareChannel(CHANNEL_BASE_CORE_19);
final StringBuffer expected = new StringBuffer("\"")
.append(outputPath.resolve("bin").resolve(DistributionInfo.DIST_NAME));

expected.append(".<SUFFIX>");

expected.append("\"");
expected.append(" ");
expected.append(CliConstants.Commands.UPDATE).append(" ").append(CliConstants.Commands.APPLY);
expected.append(" ");
expected.append(CliConstants.DIR).append(" \"").append(outputPath.toAbsolutePath()).append("\"");
expected.append(" ");
expected.append(CliConstants.CANDIDATE_DIR).append(" \"").append(Path.of("foo").toAbsolutePath()).append("\"");
expected.append(" ");
expected.append(CliConstants.YES);

final ProvisioningDefinition provisioningDefinition = defaultWfCoreDefinition()
.setChannelCoordinates(channelsFile.toString())
.build();

installation.provision(provisioningDefinition.toProvisioningConfig(),
provisioningDefinition.resolveChannels(CHANNELS_RESOLVER_FACTORY));


final ProsperoInstallationManager manager = (ProsperoInstallationManager) new ProsperoInstallationManagerFactory().create(outputPath, new MavenOptions(MavenSessionManager.LOCAL_MAVEN_REPO, false));

String command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.Linux);
assertEquals(expected.toString().replace("<SUFFIX>", "sh"), command);

command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.WindowsBash);
assertEquals(expected.toString().replace("<SUFFIX>", "bat"), command);

command = manager.generateApplyUpdateCommand(outputPath.resolve("bin"), Paths.get("foo"), OsShell.WindowsPowerShell);
assertEquals(expected.toString().replace("<SUFFIX>", "ps1"), command);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<version.org.jboss.xnio>3.8.9.Final</version.org.jboss.xnio>
<version.org.wildfly.common>1.6.0.Final</version.org.wildfly.common>
<version.org.wildfly.galleon-plugins>6.4.2.Final</version.org.wildfly.galleon-plugins>
<version.org.wildfly.installation-manager>1.0.0.Final</version.org.wildfly.installation-manager>
<version.org.wildfly.installation-manager>1.0.1.Final</version.org.wildfly.installation-manager>
<version.org.mockito>5.3.0</version.org.mockito>
<version.org.slf4j>2.0.7</version.org.slf4j>
<version.org.yaml.snakeyaml>2.0</version.org.yaml.snakeyaml>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@

package org.wildfly.prospero.cli.spi;

import org.wildfly.installationmanager.spi.OsShell;
import org.wildfly.prospero.spi.internal.CliProvider;
import org.wildfly.prospero.cli.DistributionInfo;
import org.wildfly.prospero.cli.commands.CliConstants;

import java.nio.file.Path;
import java.util.Locale;

/**
* implementation of {@link CliProvider}
*/
public class CliProviderImpl implements CliProvider {

private boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");

@Override
public String getScriptName() {
final String suffix = isWindows ?".bat":".sh";
public String getScriptName(OsShell shell) {
final String suffix;
switch (shell) {
case Linux:
suffix = ".sh";
break;
case WindowsBash:
suffix = ".bat";
break;
case WindowsPowerShell:
suffix = ".ps1";
break;
default:
throw new IllegalArgumentException(String.format("The requested shell %s is not supported", shell));
}
return DistributionInfo.DIST_NAME + suffix;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.wildfly.installationmanager.OperationNotAvailableException;
import org.wildfly.installationmanager.Repository;
import org.wildfly.installationmanager.spi.InstallationManager;
import org.wildfly.installationmanager.spi.OsShell;
import org.wildfly.prospero.ProsperoLogger;
import org.wildfly.prospero.actions.InstallationExportAction;
import org.wildfly.prospero.actions.InstallationHistoryAction;
Expand All @@ -34,6 +35,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -181,24 +183,36 @@ public Path createSnapshot(Path targetPath) throws Exception {

@Override
public String generateApplyUpdateCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException {
final boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
return this.generateApplyUpdateCommand(scriptHome, candidatePath, isWindows?OsShell.WindowsBash:OsShell.Linux);
}

@Override
public String generateApplyRevertCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException {
final boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
return this.generateApplyRevertCommand(scriptHome, candidatePath, isWindows?OsShell.WindowsBash:OsShell.Linux);
}

@Override
public String generateApplyUpdateCommand(Path scriptHome, Path candidatePath, OsShell shell) throws OperationNotAvailableException {
final Optional<CliProvider> cliProviderLoader = ServiceLoader.load(CliProvider.class).findFirst();
if (cliProviderLoader.isEmpty()) {
throw new OperationNotAvailableException("Installation manager does not support CLI operations.");
}

final CliProvider cliProvider = cliProviderLoader.get();
return escape(scriptHome.resolve(cliProvider.getScriptName())) + " " + cliProvider.getApplyUpdateCommand(installationDir, candidatePath);
return escape(scriptHome.resolve(cliProvider.getScriptName(shell))) + " " + cliProvider.getApplyUpdateCommand(installationDir, candidatePath);
}

@Override
public String generateApplyRevertCommand(Path scriptHome, Path candidatePath) throws OperationNotAvailableException {
public String generateApplyRevertCommand(Path scriptHome, Path candidatePath, OsShell shell) throws OperationNotAvailableException {
final Optional<CliProvider> cliProviderLoader = ServiceLoader.load(CliProvider.class).findFirst();
if (cliProviderLoader.isEmpty()) {
throw new OperationNotAvailableException("Installation manager does not support CLI operations.");
}

final CliProvider cliProvider = cliProviderLoader.get();
return escape(scriptHome.resolve(cliProvider.getScriptName())) + " " + cliProvider.getApplyRevertCommand(installationDir, candidatePath);
return escape(scriptHome.resolve(cliProvider.getScriptName(shell))) + " " + cliProvider.getApplyRevertCommand(installationDir, candidatePath);
}

private String escape(Path absolutePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.wildfly.prospero.spi.internal;

import org.wildfly.installationmanager.spi.OsShell;

import java.nio.file.Path;

/**
Expand All @@ -25,9 +27,10 @@
public interface CliProvider {
/**
* returns the script name used to run Prospero. The returned script is OS-specific.
* @param shell
* @return
*/
String getScriptName();
String getScriptName(OsShell shell);

/**
* generates command used to apply an update candidate in {@code candidatePath} into {@code installationPath}
Expand Down

0 comments on commit 1c0f1fb

Please sign in to comment.