Support pinning to latest of version of a node version
Right now I have the following
node {
version = '10.16.3'
download = true
}
I would like to pin to https://nodejs.org/dist/latest-v10.x
node {
version = 'latest-v10.x'
download = true
}
When I do I get the following error
* What went wrong:
Execution failed for task ':cerberus-dashboard:nodeSetup'.
> Could not resolve all files for configuration ':cerberus-dashboard:detachedConfiguration1'.
> Could not find org.nodejs:node:latest-v10.x.
Searched in the following locations:
- https://nodejs.org/dist/vlatest-v10.x/node-vlatest-v10.x-darwin-x64.tar.gz
Required by:
project :cerberus-dashboard
You could work around this by setting distBaseUrl to null and adding the repository manually, but you still wouldn't be able to say version = 'latest-v10.x' atleast not if I look at https://nodejs.org/dist/latest-v10.x which has artifacts named like node-v10.17.0-linux-x64.tar.gz.
But if you're ok with version = 10.17.0 and distBaseUrl = null you can copy the repository part from here https://github.com/node-gradle/gradle-node-plugin/blob/master/src/main/groovy/com/moowork/gradle/node/task/SetupTask.groovy#L180-L198
You need the useMetadataSourcesRepository part if you're on Gradle >= 4.5 and usePatternLayout if >= 5.0
We could probably do version detection from the SHASUMS256.txt file and that way support something like version = '10.+', but then we'd have to create our own repository instead of making use of the configurable nature of IvyArtifactRepository
There's a perfect component for this: ComponentMetadataVersionLister, so I figured I might as well give it a try and the following rough sketch works as expected.
So even though I have a personal grudge against dynamic versions this might be supported in the future :-) But for now if you're feeling brave you can clean the below snippet
public class SpecificListener implements ComponentMetadataVersionLister {
@Override
void execute(ComponentMetadataListerDetails details) {
println details.getModuleIdentifier()
if (details.moduleIdentifier.group == "org.nodejs" && details.moduleIdentifier.name == "node") {
try {
def url = new URL("https://nodejs.org/dist/latest-v10.x/SHASUMS256.txt").text
if (url) {
def versions = url.lines()
.map { str -> return str .replace(" ", " ")
.split(" ")[1]
?.substring(6)
?.replaceFirst(/(\.tar)?\.[a-zA-Z]*$/, "")
?.split("-")[0] }
.filter { !it.contains("/") }
.collect(Collectors.toSet())
details.listed(versions.toList())
}
} catch (Exception e) {
println "Exception!"
e.printStackTrace()
}
}
}
}
repositories.ivy {
setUrl('https://nodejs.org/dist/latest-v10.x/')
patternLayout {
artifact("[artifact](-v[revision]-[classifier]).[ext]")
}
metadataSources {
artifact()
}
setComponentVersionsLister(SpecificListener)
}
node {
distBaseUrl = null
version = '10.+'
}