Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORGE-217: Added forge update command #210

Merged
merged 1 commit into from Sep 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions dist/src/main/resources/bin/forge
Expand Up @@ -59,7 +59,7 @@ mingw=false
case "`uname`" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
Darwin*) darwin=true
if [ -z "$JAVA_VERSION" ] ; then
JAVA_VERSION="CurrentJDK"
fi
Expand Down Expand Up @@ -142,11 +142,11 @@ fi
JAVAVER=`$JAVACMD -version 2>&1`
case $JAVAVER in
*1.[6-9]*) ;;
*1.[1-5]*)
*1.[1-5]*)
echo " Error: a Java 1.6 or higher JRE is required to run Forge; found [$JAVACMD -version == $JAVAVER]."
exit 1
;;
esac
esac


if [ -z "$JAVA_HOME" ] ; then
Expand All @@ -168,3 +168,11 @@ fi
forge_exec_cmd="\"$JAVACMD\" $FORGE_DEBUG_ARGS $FORGE_OPTS \"-Dforge.home=${FORGE_HOME}\" \"-Dforge.shell.colorEnabled=true\" -jar \"${FORGE_HOME}/jboss-modules.jar\" -modulepath \"${FORGE_HOME}/modules:${HOME}/.forge/plugins:$PLUGIN_DIR\" org.jboss.forge"

eval $forge_exec_cmd "$QUOTED_ARGS"

if [ -d "$FORGE_HOME/.update" ] ; then
rm -rf "$FORGE_HOME/modules"
cp -r "$FORGE_HOME/.update/." "$FORGE_HOME/"
rm -rf "$FORGE_HOME/.update"
eval $forge_exec_cmd "$QUOTED_ARGS"
fi

7 changes: 7 additions & 0 deletions dist/src/main/resources/bin/forge.bat
Expand Up @@ -138,13 +138,20 @@ goto runForge
set FORGE_MAIN_CLASS=org.jboss.forge.shell.Bootstrap
%FORGE_JAVA_EXE% %FORGE_DEBUG_ARGS% %FORGE_OPTS% "-Dforge.home=%FORGE_HOME%" -Dforge.shell.colorEnabled=true -jar %JBOSS_MODULES% -modulepath "%FORGE_HOME%\modules;%USERHOME%\.forge\plugins;%FORGE_PLUGIN_DIR%" org.jboss.forge %FORGE_CMD_LINE_ARGS%
if ERRORLEVEL 1 goto error
IF EXIST "%FORGE_HOME%\.update\" (
rmdir /S/Q "%FORGE_HOME%\modules"
xcopy /S /Q /Y "%FORGE_HOME%\.update" "%FORGE_HOME%\" > NUL
rmdir /S/Q "%FORGE_HOME%\.update"
goto runForge
)
goto end

:error
if "%OS%"=="Windows_NT" @endlocal
if "%OS%"=="WINNT" @endlocal
set ERROR_CODE=1


:end
@REM set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" goto endNT
Expand Down
Expand Up @@ -8,10 +8,26 @@

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*
*/
public class Assert
{
public static void isTrue(boolean condition, String message)
{
if (!condition)
{
throw new IllegalStateException(message);
}
}

public static void isFalse(boolean condition, String message)
{
if (condition)
{
throw new IllegalStateException(message);
}
}

public static void notNull(Object object, String message) throws IllegalStateException
{
if (object == null)
Expand Down
52 changes: 52 additions & 0 deletions shell-api/src/main/java/org/jboss/forge/shell/util/Files.java
Expand Up @@ -7,10 +7,19 @@

package org.jboss.forge.shell.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
public class Files
{
Expand All @@ -31,9 +40,52 @@ public static String canonicalize(String target)
return target;
}

/**
* Returns the current working directory
* @return
*/
public static File getWorkingDirectory()
{
return new File("").getAbsoluteFile();
}

/**
* Unzips a specific file into a target directory
*/
public static void unzip(File zipFile, File targetDirectory) throws IOException
{
OutputStream dest = null;
ZipInputStream zis = new
ZipInputStream(new BufferedInputStream(new
FileInputStream(zipFile)));
ZipEntry entry;
try
{
while ((entry = zis.getNextEntry()) != null)
{
File file = new File(targetDirectory, entry.getName());

if (entry.isDirectory())
{
file.mkdirs();
continue;
}
try
{
dest = new BufferedOutputStream(new FileOutputStream(file));
Streams.write(zis, dest);
}
finally
{
dest.flush();
Streams.closeQuietly(dest);
}
}
}
finally
{
Streams.closeQuietly(zis);
}
}

}
Expand Up @@ -7,6 +7,7 @@

package org.jboss.forge.shell.plugins.builtin;

import java.io.IOException;
import java.net.ProxySelector;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -26,15 +27,21 @@
import org.jboss.forge.env.Configuration;
import org.jboss.forge.git.GitUtils;
import org.jboss.forge.parser.ParserException;
import org.jboss.forge.parser.java.util.Assert;
import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.parser.xml.Node;
import org.jboss.forge.parser.xml.XMLParser;
import org.jboss.forge.project.Project;
import org.jboss.forge.project.dependencies.CompositeDependencyFilter;
import org.jboss.forge.project.dependencies.Dependency;
import org.jboss.forge.project.dependencies.DependencyBuilder;
import org.jboss.forge.project.dependencies.DependencyFilter;
import org.jboss.forge.project.dependencies.DependencyQuery;
import org.jboss.forge.project.dependencies.DependencyQueryBuilder;
import org.jboss.forge.project.dependencies.DependencyRepository;
import org.jboss.forge.project.dependencies.DependencyRepositoryImpl;
import org.jboss.forge.project.dependencies.DependencyResolver;
import org.jboss.forge.project.dependencies.NonSnapshotDependencyFilter;
import org.jboss.forge.project.dependencies.ScopeType;
import org.jboss.forge.project.facets.DependencyFacet;
import org.jboss.forge.project.facets.DependencyFacet.KnownRepository;
Expand Down Expand Up @@ -62,6 +69,7 @@
import org.jboss.forge.shell.plugins.PipeOut;
import org.jboss.forge.shell.plugins.Plugin;
import org.jboss.forge.shell.plugins.Topic;
import org.jboss.forge.shell.util.Files;
import org.jboss.forge.shell.util.ForgeProxySelector;
import org.jboss.forge.shell.util.PluginRef;
import org.jboss.forge.shell.util.PluginUtil;
Expand Down Expand Up @@ -300,15 +308,15 @@ public void installFromLocalJar(
/*
* FileResource<?> source = resource.reify(FileResource.class); if ((source == null) || !source.exists()) { throw
* new IllegalArgumentException("JAR file must be specified."); }
*
*
* if (environment.getPluginDirectory().equals(source.getParent())) { throw new
* IllegalArgumentException("Plugin is already installed."); }
*
*
* ShellMessages.info(out, "WARNING!"); if (prompt.promptBoolean(
* "Installing plugins from remote sources is dangerous, and can leave untracked plugins. Continue?", true)) {
* FileResource<?> target = createIncrementedPluginJarFile(dep);
* target.setContents(source.getResourceInputStream());
*
*
* ShellMessages.success(out, "Installed from [" + resource + "] successfully."); restart(); } else throw new
* RuntimeException("Aborted.");
*/
Expand Down Expand Up @@ -500,6 +508,118 @@ else if (refName != null)
restart();
}

/**
* Aborts a forge update
*/
@Command(value = "update-abort", help = "Aborts a previous forge update")
public void updateAbort() throws IOException
{
DirectoryResource forgeHome = environment.getForgeHome();
DirectoryResource updateDirectory = forgeHome.getChildDirectory(".update");
if (updateDirectory.exists())
{
if (updateDirectory.delete(true))
{
ShellMessages.success(shell,
"Update files were deleted. Run 'forge update' if you want to update this installation again.");
}
else
{
ShellMessages.info(shell, "Could not abort. Try to run 'forge update-abort' again");
}
}
else
{
ShellMessages.info(shell, "No update files found");
}
}

/**
* Updates the forge version
*/
@Command(value = "update", help = "Update this forge installation")
public void update() throws IOException
{
DirectoryResource forgeHome = environment.getForgeHome();
DirectoryResource updateDir = forgeHome.getChildDirectory(".update");
if (updateDir.exists())
{
ShellMessages
.warn(shell,
"There is an update pending. Restart Forge for the update to take effect. To abort this update, type 'forge update-abort'");
return;
}
Dependency forgeDistribution = getLatestAvailableDistribution();
if (forgeDistribution == null)
{
ShellMessages.info(shell, "Forge is up to date! Enjoy!");
}
else
{
shell.print("This Forge installation will be updated to ");
shell.println(ShellColor.BOLD, forgeDistribution.getVersion());
if (prompt.promptBoolean("Is that ok ?", true))
{
updateForge(forgeDistribution);
}
}
}

/**
* Returns the latest available distribution
*
* @return
*/
private Dependency getLatestAvailableDistribution()
{
final String runtimeVersion = environment.getRuntimeVersion();
DependencyQuery query = DependencyQueryBuilder.create(DependencyBuilder
.create("org.jboss.forge:forge-distribution:::zip")).setFilter(
new CompositeDependencyFilter(
new NonSnapshotDependencyFilter(),
new DependencyFilter()
{
/**
* We are only interested in versions higher than the current version
*/
@Override
public boolean accept(Dependency dependency)
{
return dependency.getVersion().compareTo(runtimeVersion) > 0;
}
}
));
List<Dependency> versions = resolver.resolveVersions(query);
return versions.isEmpty() ? null : versions.get(versions.size() - 1);
}

/**
* Unpacks the dependency info a specific folder
*
* @param dependency
*/
private void updateForge(Dependency dependency) throws IOException
{
List<DependencyResource> resolvedArtifacts = resolver.resolveArtifacts(dependency);
Assert.isTrue(resolvedArtifacts.size() == 1, "Artifact was not found");
DependencyResource resource = resolvedArtifacts.get(0);
DirectoryResource forgeHome = environment.getForgeHome();
Files.unzip(resource.getUnderlyingResourceObject(), forgeHome.getUnderlyingResourceObject());

DirectoryResource childDirectory = forgeHome.getChildDirectory(dependency.getArtifactId() + "-"
+ dependency.getVersion());

DirectoryResource updateDirectory = forgeHome.getChildDirectory(".update");
if (updateDirectory.exists())
{
updateDirectory.delete(true);
}
childDirectory.renameTo(updateDirectory);

ShellMessages.success(shell, "Forge will now restart to complete the update ...");
System.exit(0);
}

private void prepareProxyForJGit()
{
ProxySettings proxySettings = ProxySettings.fromForgeConfiguration(configuration);
Expand Down