From 6d6a4d534397e40d94d8e673b54854f01ff7a78c Mon Sep 17 00:00:00 2001 From: Attila Kelemen Date: Fri, 1 May 2015 00:38:01 +0200 Subject: [PATCH] Added Gradle 1.8 API as a provided dependency. --- .../build/ZippedJarsFileCollection.java | 166 ++++++++++++++++++ netbeans-gradle-default-models/build.gradle | 18 +- 2 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 buildSrc/src/main/groovy/org/netbeans/gradle/build/ZippedJarsFileCollection.java diff --git a/buildSrc/src/main/groovy/org/netbeans/gradle/build/ZippedJarsFileCollection.java b/buildSrc/src/main/groovy/org/netbeans/gradle/build/ZippedJarsFileCollection.java new file mode 100644 index 00000000..7df68495 --- /dev/null +++ b/buildSrc/src/main/groovy/org/netbeans/gradle/build/ZippedJarsFileCollection.java @@ -0,0 +1,166 @@ +package org.netbeans.gradle.build; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.internal.file.AbstractFileCollection; + +public final class ZippedJarsFileCollection extends AbstractFileCollection { + private final Charset UTF8 = Charset.forName("UTF-8"); + + private final Project project; + private final String dependencyNotation; + private final Configuration dependencyContainer; + private final Path unzipDir; + private final List subDirPath; + + public ZippedJarsFileCollection( + Project project, + String dependencyNotation, + List unzipPath, + List subDirPath) { + this.project = project; + Dependency dependency = project.getDependencies().create(dependencyNotation); + this.dependencyContainer = project.getConfigurations().detachedConfiguration(dependency); + this.unzipDir = resolve(project.getProjectDir().toPath(), unzipPath); + this.dependencyNotation = dependencyNotation; + this.subDirPath = new ArrayList<>(subDirPath); + } + + private static Path resolve(Path dir, List subDirs) { + Path result = dir; + for (String subDir: subDirs) { + result = result.resolve(subDir); + } + return result; + } + + private Path getExtractedFilesDir() { + return unzipDir.resolve("files"); + } + + private Path getExtractedMarker() { + return unzipDir.resolve("EXTRACTED"); + } + + private void extractSafe() { + try { + extract(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + private static void safeCopy(Path src, Path dest) throws IOException { + try (InputStream input = Files.newInputStream(src)) { + Files.copy(input, dest, StandardCopyOption.REPLACE_EXISTING); + } + } + + private void markExtracted() throws IOException { + Path extractedMarker = getExtractedMarker(); + List lines = Collections.singletonList(dependencyNotation); + Files.write(extractedMarker, lines, UTF8); + } + + private boolean isExtracted() throws IOException { + Path extractedMarker = getExtractedMarker(); + if (!Files.isRegularFile(extractedMarker)) { + return false; + } + + List lines = Files.readAllLines(extractedMarker, UTF8); + if (lines.isEmpty()) { + return false; + } + + return dependencyNotation.equals(lines.get(0)); + } + + private void tryDeleteDirectory(Path directory) { + project.delete(directory.toFile()); + } + + private void copyDir(final Path srcDir, final Path destDir) throws IOException { + tryDeleteDirectory(destDir); + Files.createDirectories(destDir); + + Files.walkFileTree(srcDir, new FileVisitor() { + private Path currentDir = destDir; + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (!srcDir.equals(dir)) { + currentDir = currentDir.resolve(dir.getFileName().toString()); + Files.createDirectory(currentDir); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + String fileName = file.getFileName().toString(); + String normName = fileName.toLowerCase(Locale.ROOT); + if (normName.endsWith(".jar")) { + safeCopy(file, currentDir.resolve(fileName)); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + currentDir = currentDir != null ? currentDir.getParent() : null; + return FileVisitResult.CONTINUE; + } + }); + } + + private void extract() throws IOException { + if (isExtracted()) { + return; + } + + Path zipFile = dependencyContainer.getSingleFile().toPath(); + FileSystem zipFS = FileSystems.newFileSystem(zipFile, null); + Path root = zipFS.getRootDirectories().iterator().next(); + Path libDir = resolve(root, subDirPath); + + copyDir(libDir, getExtractedFilesDir()); + + markExtracted(); + } + + @Override + public String getDisplayName() { + return "UnZip: " + dependencyNotation; + } + + @Override + public Set getFiles() { + extractSafe(); + + return project.fileTree(getExtractedFilesDir().toFile()).getFiles(); + } +} diff --git a/netbeans-gradle-default-models/build.gradle b/netbeans-gradle-default-models/build.gradle index 643dd45f..66a22625 100644 --- a/netbeans-gradle-default-models/build.gradle +++ b/netbeans-gradle-default-models/build.gradle @@ -30,6 +30,15 @@ repositories { maven { url 'http://repo.gradle.org/gradle/libs-snapshots-local' } + + ivy { + // This "repository" was added only as a hack to download + // the Gradle distribution at http://services.gradle.org/distributions/gradle-1.8-bin.zip + url 'http://services.gradle.org' + layout 'pattern', { + artifact '[organisation]/[artifact]-[revision].[ext]' + } + } } configurations { @@ -38,7 +47,14 @@ configurations { dependencies { compile group: 'org.gradle', name: 'gradle-tooling-api', version: gradleVersion; - provided group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.8.6'; + + def gradleApiDependency = new org.netbeans.gradle.build.ZippedJarsFileCollection( + project, + 'distributions:gradle:1.8-bin@zip', + ['gradle-api'], + ['gradle-1.8', 'lib'] + ); + provided gradleApiDependency; testCompile group: 'junit', name: 'junit', version: '4.11'; testCompile group: 'org.mockito', name: 'mockito-core', version: '1.9.5';