Skip to content

Commit

Permalink
replacing current extraction method by gradle one
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Kneier authored and frederic-kneier committed Aug 23, 2018
1 parent 5d75040 commit 3a0f70d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 87 deletions.
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {
id 'com.gradle.plugin-publish' version "0.10.0"
}

group 'gradle.plugin.de.solugo'
version '0.5.2'
group 'de.solugo.gradle'
version '0.6.0'

repositories {
mavenCentral()
Expand All @@ -15,8 +15,7 @@ repositories {
dependencies {
compile gradleApi()
compile localGroovy()
compile "org.codehaus.plexus:plexus-archiver:3.5"
compile "org.codehaus.plexus:plexus-container-default:1.7.1"
compile "org.tukaani:xz:1.8"

testCompile gradleTestKit()
testCompile "junit:junit:4.12"
Expand Down
8 changes: 5 additions & 3 deletions src/main/groovy/de/solugo/gradle/nodejs/NodeJsTask.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package de.solugo.gradle.nodejs

import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.internal.file.archive.compression.AbstractArchiver
import org.gradle.api.resources.MissingResourceException
import org.gradle.api.resources.ReadableResource
import org.gradle.api.resources.ResourceException
import org.gradle.api.tasks.AbstractExecTask
import org.gradle.api.tasks.Input

Expand All @@ -25,7 +29,7 @@ class NodeJsTask<T extends NodeJsTask<T>> extends AbstractExecTask<T> {

@Override
protected void exec() {
final NodeJsUtil nodeJsUtil = NodeJsUtil.getInstance(this.project.nodejs.version)
final NodeJsUtil nodeJsUtil = NodeJsUtil.getInstance(this.project, this.project.nodejs.version)

final modules = new ArrayList<String>(this.require)

Expand Down Expand Up @@ -124,8 +128,6 @@ class NodeJsTask<T extends NodeJsTask<T>> extends AbstractExecTask<T> {
builder.append(nodeJsUtil.bin.absolutePath)
builder.append(File.pathSeparator)

println("Environment: " + builder.toString())

if (environment['Path'] != null) {
builder.append(environment['Path'])
environment['Path'] = builder.toString()
Expand Down
104 changes: 24 additions & 80 deletions src/main/groovy/de/solugo/gradle/nodejs/NodeJsUtil.groovy
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package de.solugo.gradle.nodejs


import org.apache.tools.ant.taskdefs.condition.Os
import org.codehaus.plexus.archiver.ArchiverException
import org.codehaus.plexus.archiver.UnArchiver
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver
import org.codehaus.plexus.archiver.tar.TarXZUnArchiver
import org.codehaus.plexus.archiver.zip.ZipUnArchiver
import org.codehaus.plexus.logging.Logger
import org.codehaus.plexus.logging.console.ConsoleLogger
import org.gradle.api.Project
import org.gradle.api.file.FileTree

import java.nio.file.Files
import java.nio.file.StandardCopyOption

class NodeJsUtil {

static synchronized NodeJsUtil getInstance(String version) {
static synchronized NodeJsUtil getInstance(Project project, String version) {
File home = new File(System.getProperty("user.home"))
File cache = new File(home, ".nodejs")
File target = new File(cache, version)
Expand All @@ -35,7 +34,7 @@ class NodeJsUtil {
bin = new File(target, "bin")
} else if (Os.isFamily(Os.FAMILY_UNIX)) {
platform = "linux"
ext = "tar.xz"
ext = "tar.gz"
modules = new File(target, "lib/node_modules")
bin = new File(target, "bin")
} else {
Expand Down Expand Up @@ -69,23 +68,27 @@ class NodeJsUtil {

target.mkdirs()

final UnArchiver unArchiver;

if (ext == "tar.xz") {
unArchiver = new NodeJsTarXzUnArchiver()
} else if (ext == "tar.gz") {
unArchiver = new NodeJsTarGzUnArchiver()
final FileTree tree
if (ext == "tar.gz") {
tree = project.tarTree(downloadFile)
} else if (ext == "zip") {
unArchiver = new NodeJsZipUnArchiver()
tree = project.zipTree(downloadFile)
} else {
throw new UnsupportedOperationException("Archive ${downloadFile.name} not supported")
}

unArchiver.sourceFile = downloadFile
unArchiver.enableLogging(new ConsoleLogger(Logger.LEVEL_ERROR, "root"))
unArchiver.overwrite = true
unArchiver.destFile = target
unArchiver.extract()
tree.visit { source ->
final name = source.relativePath.pathString
final int start = name.indexOf(File.separator)
if (start != -1) {
final destination = new File(target, name.substring(name.indexOf(File.separator)))
if (source.isDirectory()) {
destination.mkdirs()
} else {
source.copyTo(destination)
}
}
}
} catch (final Throwable throwable) {
target.delete()
throw throwable
Expand Down Expand Up @@ -118,63 +121,4 @@ class NodeJsUtil {
return this.bin
}

private static class NodeJsTarXzUnArchiver extends TarXZUnArchiver {
@Override
protected void extractFile(
final File src,
final File dir,
final InputStream inputStream,
final String entryName,
final Date entryDate,
final boolean isDirectory,
final Integer mode,
final String symlinkDestination
) throws IOException, ArchiverException {

final pos = entryName.indexOf("/")
if (pos != -1) {
super.extractFile(src, dir, inputStream, entryName.substring(pos + 1), entryDate, isDirectory, mode, symlinkDestination)
}
}
}

private static class NodeJsTarGzUnArchiver extends TarGZipUnArchiver {
@Override
protected void extractFile(
final File src,
final File dir,
final InputStream inputStream,
final String entryName,
final Date entryDate,
final boolean isDirectory,
final Integer mode,
final String symlinkDestination
) throws IOException, ArchiverException {

final pos = entryName.indexOf("/")
if (pos != -1) {
super.extractFile(src, dir, inputStream, entryName.substring(pos + 1), entryDate, isDirectory, mode, symlinkDestination)
}
}
}

private static class NodeJsZipUnArchiver extends ZipUnArchiver {
@Override
protected void extractFile(
final File src,
final File dir,
final InputStream inputStream,
final String entryName,
final Date entryDate,
final boolean isDirectory,
final Integer mode,
final String symlinkDestination
) throws IOException, ArchiverException {

final pos = entryName.indexOf("/")
if (pos != -1 && pos < entryName.length() - 1) {
super.extractFile(src, dir, inputStream, entryName.substring(pos + 1), entryDate, isDirectory, mode, symlinkDestination)
}
}
}
}

0 comments on commit 3a0f70d

Please sign in to comment.