Skip to content

Commit

Permalink
fix: improve download behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
error418 committed Aug 29, 2019
1 parent 26bb273 commit e3d5bbe
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 146 deletions.
3 changes: 2 additions & 1 deletion .releaserc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ ci: false
# gradle semrel configuration
gradle:
node:
version: 10.16.3
detect: true # try to detect system node, if found skip download evaluation.
download: false # download a node dist and use it, if it was not detected
download: true # download a node dist and use it, if it was not detected
distUrl: https://nodejs.org/dist # dist download root url (default)
packages: # extra packages needed in semantic-release plugins
- '@semantic-release/commit-analyzer'
Expand Down
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ buildscript {
repositories {
jcenter()
maven { url 'https://dl.bintray.com/illjut/maven' }
//mavenLocal()
mavenLocal()
}

dependencies {
classpath group: 'de.illjut.gradle', name: 'semrel', version: '1.+'
classpath group: 'de.illjut.gradle', name: 'semrel', version: project.findProperty('semrelVersion') ?: '1+'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+'
}
}
Expand All @@ -24,12 +24,11 @@ repositories {
jcenter()
}

dependencies {
dependencies {
implementation gradleApi()
implementation localGroovy()

compile "org.yaml:snakeyaml:1.24"
compile "org.apache.commons:commons-compress:1.18"
compile "org.ajoberstar.grgit:grgit-core:3.1.1"

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.2'
Expand Down
78 changes: 78 additions & 0 deletions src/main/groovy/de/illjut/gradle/semrel/NodeSetup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package de.illjut.gradle.semrel

import org.gradle.api.*
import de.illjut.gradle.semrel.*
import java.text.MessageFormat
import java.util.*
import java.io.*

class NodeSetup {
private final String distBase;
private final String distHrefTemplate = "{0}/v{1}/node-v{1}-{2}-{3}.{4}";

def project;
def nodeBinPath;

NodeSetup(project, distBase = "https://nodejs.org/dist") {
this.project = project;
this.distBase = distBase;
}

String buildDownloadUrl(version) {
String platform = PlatformHelper.getPlatform();
String archiveType = "tar.gz";
if (PlatformHelper.isWindows()) {
archiveType = "zip";
}

return MessageFormat.format(this.distHrefTemplate, this.distBase, version, platform, PlatformHelper.getArch(), archiveType);
}

void setupNode(String version, File dest) {
def distFile = this.downloadNode(version, dest);
this.unpack(distFile, dest);
}

File downloadNode(String version, File dest) {
URL url = new URL(buildDownloadUrl(version));
File distFile = new File(dest, url.getFile().substring(url.getFile().lastIndexOf('/')+1, url.getFile().length()));

this.project.ant.get(
src: url,
dest: distFile,
quiet: true
)

return distFile;
}

File unpack(File src, File dest) {
String archiveName = src.getName()
.replace(".zip", '')
.replace(".tar.gz", '')
.replace(".tar", '');

if (src.name.endsWith(".tar.gz")) {
this.project.ant.untar(
src: src,
dest: dest,
compression: "gzip"
)
} else {
this.project.ant.unzip(
src: src,
dest: dest
)
}

this.nodeBinPath = new File(dest, archiveName + "/bin/");

if (PlatformHelper.isUnix()) {
for (String executable : [ "node", "npm", "npx" ]) {
new File(this.nodeBinPath, executable).setExecutable(true);
}
}

return this.nodeBinPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.io.FileReader
import org.gradle.api.*

class SemanticReleaseConfig {
private final String nodeVersion
private final String branch
private final boolean downloadNode
private final boolean autoDetectNode
Expand All @@ -26,6 +27,7 @@ class SemanticReleaseConfig {
this.branch = config.branch
this.downloadNode = config.gradle?.node?.download == true
this.autoDetectNode = config.gradle?.node?.detect ?: false
this.nodeVersion = config.gradle?.node?.version ?: '10.16.3'

if (config.gradle?.node?.packages != null) {
this.packages = config.gradle.node.packages
Expand Down
32 changes: 21 additions & 11 deletions src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package de.illjut.gradle.semrel

import org.gradle.api.*
import java.io.*
import com.moowork.gradle.node.task.*
import com.moowork.gradle.node.npm.*
import com.moowork.gradle.node.*

import org.ajoberstar.grgit.Grgit

Expand All @@ -20,7 +17,6 @@ class SemrelPlugin implements Plugin<Project> {
def packageJson = project.file("${semrelDir}/package.json")
def snapshot = false;

def nodeVersion = "10.16.0"
def semanticReleaseVersion = "15"

def grgit = Grgit.open(dir: project.rootProject.projectDir)
Expand All @@ -31,10 +27,19 @@ class SemrelPlugin implements Plugin<Project> {
grgit.close()

def config = new SemanticReleaseConfig(project.rootProject.file(".releaserc.yml"))
def node = new NodeExec(config.distUrl, project.logger)
def nodeVersion = config.nodeVersion;

def nodeExec = new NodeExec(project.logger, null);

project.configure(project) {

def setup = new NodeSetup(project, config.distUrl);

if (project.hasProperty('skipSemrel') && project.getProperty('skipSemrel').toBoolean()) {
this.project.logger.info "skipping semrel due to property skipSemrel"
return;
}

// create semrel build directory
project.file(semrelDir).mkdirs();

Expand All @@ -58,33 +63,35 @@ class SemrelPlugin implements Plugin<Project> {
if (config.autoDetectNode) {
project.logger.info "trying to autodetect a nodejs installation"

if(node.isNodeAvailable()) {
if(nodeExec.isNodeAvailable()) {
project.logger.info "node is available on PATH"
} else {
project.logger.info "node is not available on PATH"
if (config.downloadNode && !completeMarker.exists()) { // download node
node.setupNode(nodeVersion, project.file(semrelDir))
setup.setupNode(nodeVersion, project.file(semrelDir))
completeMarker.createNewFile()
}
}
} else {
project.logger.info "skipped nodejs autodetection"
if (config.downloadNode && !completeMarker.exists()) { // download node
node.setupNode(nodeVersion, project.file(semrelDir))
setup.setupNode(nodeVersion, project.file(semrelDir))
completeMarker.createNewFile()
}
}

nodeExec.nodePath = setup.nodeBinPath;

if (!cacheCompleteMarker.exists()) { // prepare cache for faster executions
project.logger.info "preparing npm cache for faster executions."
node.executeNpm(['i', '-D', "semantic-release@v${semanticReleaseVersion}".toString(), "@semantic-release/exec"], workDir)
nodeExec.executeNpm(['i', '--prefer-offline', '-D', "semantic-release@v${semanticReleaseVersion}".toString(), "@semantic-release/exec"], workDir)
cacheCompleteMarker.createNewFile()
}

def extraPackages = [ ]

// invoke npx to run semantic release
def result = node.executeNpx(
def result = nodeExec.executeNpx(
['--no-install', 'semantic-release', '--prepare', '--dry-run'],
extraPackages,
workDir
Expand Down Expand Up @@ -143,6 +150,9 @@ class SemrelPlugin implements Plugin<Project> {
project.version = project.version.replace('/', '-')
}

// remove 'v' prefix from version string to comply to artifact repository version standards
project.version = (project.version =~ /[v]?(.+)/)[0][1];

project.logger.quiet "Inferred version: ${project.version}"
project.ext.isSnapshot = snapshot
}
Expand All @@ -158,7 +168,7 @@ class SemrelPlugin implements Plugin<Project> {
def extraPackages = ["semantic-release@v${semanticReleaseVersion}".toString()]
extraPackages.addAll config.packages

def result = node.executeNpx([
def result = nodeExec.executeNpx([
'semantic-release'
],
extraPackages,
Expand Down
Loading

0 comments on commit e3d5bbe

Please sign in to comment.