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 28, 2015
1 parent 09f05b4 commit fffaabd
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.jboss.windup.ui;


import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.jboss.forge.addon.dependencies.Coordinate;
import org.jboss.forge.addon.dependencies.DependencyException;
import org.jboss.forge.addon.dependencies.builder.CoordinateBuilder;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.windup.exec.updater.RulesetsUpdater;
import org.jboss.windup.util.PathUtil;
import org.jboss.windup.util.exception.WindupException;

/**
*
* @author Ondrej Zizka, ozizka at redhat.com
*/
public class DistributionUpdater
{
private static final Logger log = Logger.getLogger( DistributionUpdater.class.getName() );

@Inject
private RulesetsUpdater updater;


/**
* 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
{
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);

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


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());
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package org.jboss.windup.ui;

import org.jboss.windup.exec.updater.RulesetsUpdater;
import java.io.File;
import java.io.FilenameFilter;
import java.nio.file.Path;

import javax.inject.Inject;


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.jboss.forge.addon.dependencies.Coordinate;
import org.jboss.forge.addon.dependencies.DependencyResolver;
import org.jboss.forge.addon.dependencies.builder.CoordinateBuilder;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
Expand All @@ -25,16 +15,15 @@
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.addons.Addon;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.forge.furnace.versions.SingleVersion;
import org.jboss.forge.furnace.versions.Version;
import org.jboss.windup.util.PathUtil;
import org.jboss.windup.util.exception.WindupException;


/**
* Provides a basic UI command updating the whole windup distribution.
*
* @author mbriskar
* @author ozizka
*/
public class WindupUpdateDistributionCommand implements UICommand
{
Expand All @@ -48,6 +37,10 @@ public class WindupUpdateDistributionCommand implements UICommand
@Inject
private RulesetsUpdater updater;

@Inject
private DistributionUpdater distUpdater;



@Override
public UICommandMetadata getMetadata(UIContext ctx)
Expand All @@ -67,48 +60,17 @@ public Result execute(UIExecutionContext context) throws Exception
}

// Find the latest version.
Coordinate latestDistributionCoordinate = this.updater.getLatestReleaseOf("org.jboss.windup", "windup-distribution");
Version latestVersion = new SingleVersion(latestDistributionCoordinate.getVersion());
Coordinate latestDist = this.updater.getLatestReleaseOf("org.jboss.windup", "windup-distribution");
Version latestVersion = new SingleVersion(latestDist.getVersion());
Version installedVersion = currentAddon.getId().getVersion();
if (latestVersion.compareTo(installedVersion) <= 0)
{
return Results.fail("Windup is already in the most updated version.");
}

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);

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

// Get the artifact and unzip it to a temp folder.
final CoordinateBuilder coord = CoordinateBuilder.create(latestDistributionCoordinate)
.setClassifier("offline").setPackaging("zip");

File tempFolder = OperatingSystemUtils.createTempDir();
this.updater.extractArtifact(coord, tempFolder);

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 distributionExctractedPath = matchingDirs[0].getAbsolutePath();
FileUtils.moveDirectory(new File(distributionExctractedPath, PathUtil.ADDONS_DIRECTORY_NAME), addonsDir.toFile());
FileUtils.moveDirectory(new File(distributionExctractedPath, PathUtil.BINARY_DIRECTORY_NAME), binDir.toFile());
FileUtils.moveDirectory(new File(distributionExctractedPath, PathUtil.LIBRARY_DIRECTORY_NAME), libDir.toFile());
// This is for testing purposes. The releases do not contain migration-core/ .
File downloadedMigrationCore = new File(distributionExctractedPath,
PathUtil.RULES_DIRECTORY_NAME + File.pathSeparator + RulesetsUpdater.RULESET_CORE_DIRECTORY);
File fromDir = downloadedMigrationCore.exists() ?
downloadedMigrationCore : new File(distributionExctractedPath, PathUtil.RULES_DIRECTORY_NAME);
FileUtils.moveDirectory(fromDir, coreRulesPropertiesPath.toFile());

return Results.success("Sucessfully updated Windup to version " + latestDistributionCoordinate.getVersion() + ". Please restart Windup.");
distUpdater.replaceWindupDirectoryWithDistribution(latestDist);

return Results.success("Sucessfully updated Windup to version " + latestDist.getVersion() + ". Please restart Windup.");
}


Expand Down

0 comments on commit fffaabd

Please sign in to comment.