Created
September 12, 2012 15:27
-
-
Save gastaldi/3707416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Updates the forge version | |
| * | |
| * @param specificVersion if you need a specific version | |
| */ | |
| @Command(value = "update", help = "Update this forge installation") | |
| public void update() throws IOException | |
| { | |
| Dependency forgeDistribution = getLatestAvailableDistribution(); | |
| if (forgeDistribution == null) | |
| { | |
| ShellMessages.info(shell, "Forge is already updated to the latest available version ! Enjoy !"); | |
| } | |
| else | |
| { | |
| shell.print("The latest available Forge version is: "); | |
| shell.println(ShellColor.BOLD, forgeDistribution.getVersion()); | |
| if (prompt.promptBoolean("Would you like to download it ?", 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() | |
| { | |
| /** | |
| * Display only versions higher than | |
| */ | |
| @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(); | |
| DirectoryResource childDirectory = forgeHome.getChildDirectory("forge_update"); | |
| if (childDirectory.exists()) | |
| { | |
| childDirectory.delete(true); | |
| } | |
| childDirectory.mkdir(); | |
| Files.unzip(resource.getUnderlyingResourceObject(), childDirectory.getUnderlyingResourceObject()); | |
| ShellMessages.success(shell, "Forge was successfully updated !"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment