Skip to content

Commit

Permalink
WINDUP-705 Fix automatic distribution updating - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraZizka committed Jul 29, 2015
1 parent 23cb391 commit 2b5c916
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,24 @@ public void extractArtifact(Coordinate artifactCoords, File targetDir) throws IO
}

/**
* @return Finds the latest non-SNAPSHOT of given artifact, using given DependencyResolver.
* A convenience method.
* @return Finds the latest non-SNAPSHOT of given artifact.
*/
public Coordinate getLatestReleaseOf(String groupId, String artifactId)
{
final CoordinateBuilder rulesetsCoord = CoordinateBuilder.create()
final CoordinateBuilder coord = CoordinateBuilder.create()
.setGroupId(groupId)
.setArtifactId(artifactId);
List<Coordinate> availableVersions = depsResolver.resolveVersions(DependencyQueryBuilder.create(rulesetsCoord));
return getLatestReleaseOf(coord);
}


/**
* @return Finds the latest non-SNAPSHOT of given artifact.
*/
public Coordinate getLatestReleaseOf(final CoordinateBuilder coord)
{
List<Coordinate> availableVersions = depsResolver.resolveVersions(DependencyQueryBuilder.create(coord));

// Find the latest non-SNAPSHOT version.
for(int i = availableVersions.size()-1; i >= 0; i--)
Expand Down
84 changes: 60 additions & 24 deletions ui/addon/src/main/java/org/jboss/windup/ui/DistributionUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,78 @@ public class DistributionUpdater
@Inject
private RulesetsUpdater updater;

public void replaceWindupDirectoryWithLatestDistribution()
{

final CoordinateBuilder coords = CoordinateBuilder.create()
.setGroupId("org.jboss.windup")
.setArtifactId("windup-distribution")
.setClassifier("offline")
.setPackaging("zip");
Coordinate coord = updater.getLatestReleaseOf(coords);
if(coord == null)
throw new WindupException("No Windup release found.");

replaceWindupDirectoryWithDistribution(coord);
}


/**
* Downloads the given artifact, extracts it and replaces current Windup directory (as given by PathUtil)
* with the content from the artifact (assuming it really is a Windup distribution zip).
*/
public void replaceWindupDirectoryWithDistribution(Coordinate distCoordinate)
throws IllegalStateException, DependencyException, IOException, WindupException
throws WindupException
{
Path windupRulesDir = PathUtil.getWindupRulesDir();
Path addonsDir = PathUtil.getWindupAddonsDir();
Path binDir = PathUtil.getWindupHome().resolve(PathUtil.BINARY_DIRECTORY_NAME);
Path libDir = PathUtil.getWindupHome().resolve(PathUtil.LIBRARY_DIRECTORY_NAME);
Path coreRulesPropertiesPath = windupRulesDir.resolve(RulesetsUpdater.RULESET_CORE_DIRECTORY);
try
{
final CoordinateBuilder coord = CoordinateBuilder.create(distCoordinate);
File tempFolder = OperatingSystemUtils.createTempDir();
this.updater.extractArtifact(coord, tempFolder);

File newDistWindupDir = getWindupDistributionSubdir(tempFolder);

if (null == newDistWindupDir)
throw new WindupException("Distribution update failed: "
+ "The distribution archive did not contain the windup-distribution-* directory: " + coord.toString());

final CoordinateBuilder coord = CoordinateBuilder.create(distCoordinate).setClassifier("offline").setPackaging("zip");
File tempFolder = OperatingSystemUtils.createTempDir();
this.updater.extractArtifact(coord, tempFolder);

Path addonsDir = PathUtil.getWindupAddonsDir();
Path binDir = PathUtil.getWindupHome().resolve(PathUtil.BINARY_DIRECTORY_NAME);
Path libDir = PathUtil.getWindupHome().resolve(PathUtil.LIBRARY_DIRECTORY_NAME);

FileUtils.deleteDirectory(addonsDir.toFile());
FileUtils.deleteDirectory(libDir.toFile());
FileUtils.deleteDirectory(binDir.toFile());

FileUtils.moveDirectory(new File(newDistWindupDir, PathUtil.ADDONS_DIRECTORY_NAME), addonsDir.toFile());
FileUtils.moveDirectory(new File(newDistWindupDir, PathUtil.BINARY_DIRECTORY_NAME), binDir.toFile());
FileUtils.moveDirectory(new File(newDistWindupDir, PathUtil.LIBRARY_DIRECTORY_NAME), libDir.toFile());

// Rulesets
Path windupRulesDir = PathUtil.getWindupRulesDir();
File coreRulesetsDir = windupRulesDir.resolve(RulesetsUpdater.RULESET_CORE_DIRECTORY).toFile();
FileUtils.deleteDirectory(coreRulesetsDir);
// This is for testing purposes. The releases do not contain migration-core/ .
File downloadedMigrationCore = new File(newDistWindupDir, PathUtil.RULES_DIRECTORY_NAME + File.separator + RulesetsUpdater.RULESET_CORE_DIRECTORY);
File fromDir = downloadedMigrationCore.exists() ? downloadedMigrationCore : new File(newDistWindupDir, PathUtil.RULES_DIRECTORY_NAME);
FileUtils.moveDirectory(fromDir, coreRulesetsDir);

FileUtils.deleteDirectory(tempFolder);
}
catch (IllegalStateException | DependencyException | IOException ex)
{
throw new WindupException("Distribution update failed: " + ex.getMessage(), ex);
}
}


public static File getWindupDistributionSubdir(File tempFolder) throws WindupException
{
File[] matchingDirs = tempFolder.listFiles((FilenameFilter) FileFilterUtils.prefixFileFilter("windup-distribution-", IOCase.INSENSITIVE));
if (matchingDirs.length == 0)
throw new WindupException("Distribution update failed: " + "The distribution archive did not contain the windup-distribution-* directory: " + coord.toString());
String distTempPath = matchingDirs[0].getAbsolutePath();

FileUtils.deleteDirectory(coreRulesPropertiesPath.toFile());
FileUtils.deleteDirectory(addonsDir.toFile());
FileUtils.deleteDirectory(libDir.toFile());
FileUtils.deleteDirectory(binDir.toFile());

FileUtils.moveDirectory(new File(distTempPath, PathUtil.ADDONS_DIRECTORY_NAME), addonsDir.toFile());
FileUtils.moveDirectory(new File(distTempPath, PathUtil.BINARY_DIRECTORY_NAME), binDir.toFile());
FileUtils.moveDirectory(new File(distTempPath, PathUtil.LIBRARY_DIRECTORY_NAME), libDir.toFile());
// This is for testing purposes. The releases do not contain migration-core/ .
File downloadedMigrationCore = new File(distTempPath, PathUtil.RULES_DIRECTORY_NAME + File.pathSeparator + RulesetsUpdater.RULESET_CORE_DIRECTORY);
File fromDir = downloadedMigrationCore.exists() ? downloadedMigrationCore : new File(distTempPath, PathUtil.RULES_DIRECTORY_NAME);
FileUtils.moveDirectory(fromDir, coreRulesPropertiesPath.toFile());
return null;
return matchingDirs[0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Logger;

Expand Down Expand Up @@ -39,14 +38,14 @@
import org.jboss.forge.furnace.util.Addons;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.forge.furnace.versions.SingleVersion;
import org.jboss.forge.furnace.versions.Versions;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.windup.exec.updater.RulesetsUpdater;
import org.jboss.windup.ui.DistributionUpdater;
import org.jboss.windup.ui.WindupCommand;
import org.jboss.windup.ui.WindupUpdateDistributionCommand;
import org.jboss.windup.util.Logging;
import org.jboss.windup.util.PathUtil;
import org.jboss.windup.util.exception.WindupException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -62,7 +61,6 @@ public class WindupUpdateDistributionCommandTest

private static final String WINDUP_UI_ADDON_NAME = "org.jboss.windup.ui:windup-ui";

///private static String WINDUP_OLD_VERSION = "2.0.0.Final"; // Before it was just "ui".
private static String WINDUP_OLD_VERSION = "2.2.0.Final";

@Deployment
Expand Down Expand Up @@ -125,24 +123,36 @@ public void testUpdateDistribution() throws Exception

File windupDir = OperatingSystemUtils.createTempDir();
this.updater.extractArtifact(results.get(0), windupDir);
windupDir = new File(windupDir, "windup-distribution");
//windupDir = new File(windupDir, "windup-distribution");
windupDir = DistributionUpdater.getWindupDistributionSubdir(windupDir);
Assert.assertTrue(windupDir.exists());
System.setProperty(PathUtil.WINDUP_HOME, windupDir.getAbsolutePath());

//System.setProperty("windup.home", windupDir.getAbsolutePath());

// Run the upgrader.
distUpdater.replaceWindupDirectoryWithDistribution(coords);
distUpdater.replaceWindupDirectoryWithLatestDistribution();

// Check the new version.
String newUiVersion = getInstalledAddonVersion(windupDir.toPath().resolve("addons").toString(), WINDUP_UI_ADDON_NAME);
Assert.assertTrue(new SingleVersion(newUiVersion).compareTo(new SingleVersion("2.2.0.Final")) > 0);

// Try to run Windup from there.
UITestHarness harness = furnace.getAddonRegistry().getServices(UITestHarness.class).get();
CommandController controller = harness.createCommandController(WindupCommand.class);
controller.initialize();
Result result = controller.execute();
Assert.assertTrue(result.getMessage(), !(result instanceof Failed));
controller.close();
try (CommandController controller = harness.createCommandController(WindupCommand.class))
{
controller.initialize();
controller.setValueFor("input", new File("src/test/resources/test.jar").getAbsolutePath());
final File resultDir = new File("target/testRunFromUpgraded");
resultDir.mkdirs();
controller.setValueFor("output", resultDir.getAbsolutePath());

Result result = controller.execute();
Assert.assertTrue(result.getMessage(), !(result instanceof Failed));
}
catch (Throwable ex)
{
throw new WindupException("Failed running Windup from the upgraded directory: " + ex.getMessage(), ex);
}
}


Expand All @@ -158,7 +168,6 @@ public void testUpdateDistributionCommand() throws Exception
PathUtil.unzipFromResource(getClass(), WindupUpdateDistributionCommandTest.TEST_RULESET_ZIP, extractedPath);

String windupDir = extractedPath + "/windup-old-ruleset";
///System.setProperty("windup.home", windupHome);
System.setProperty(PathUtil.WINDUP_RULESETS_DIR_SYSPROP, windupDir);
File addonsDir = new File(windupDir, "addons");
addonsDir.mkdirs();
Expand Down Expand Up @@ -262,9 +271,12 @@ private String getInstalledAddonVersion(String addonsRootDir, String addonName)
DocumentBuilder dBuilder;
try
{
dBuilder = dbFactory.newDocumentBuilder();
String oldUiAddonVersion = "";
File installedXml = new File(addonsRootDir, "installed.xml");
if (!installedXml.exists())
throw new WindupException("installed.xml doesn't exist: " + installedXml.getAbsolutePath());

dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(installedXml);
// this does not work properly
Element documentElement = doc.getDocumentElement();
Expand All @@ -287,6 +299,10 @@ private String getInstalledAddonVersion(String addonsRootDir, String addonName)
{
throw new RuntimeException("Failed parsing installed.xml: " + ex.getMessage(), ex);
}
catch (WindupException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new RuntimeException("Unknown exception: " + ex.getMessage(), ex);
Expand Down

0 comments on commit 2b5c916

Please sign in to comment.