Skip to content

Commit

Permalink
Merge pull request #15 from nextflow-io/improve-gradle
Browse files Browse the repository at this point in the history
Improve Gradle build
  • Loading branch information
abhi18av authored Feb 22, 2024
2 parents 710c207 + 517211e commit 340b0dd
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 131 deletions.
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import nextflow.gradle.plugins.NextflowPlugin

apply plugin: NextflowPlugin

ext{
github_organization = 'nextflow-io'
github_username = project.findProperty('github_username') ?: 'abhi18av'
github_access_token = project.findProperty('github_access_token') ?: System.getenv('GITHUB_TOKEN')
github_commit_email = project.findProperty('github_commit_email') ?: '[email protected]'
}

nextflowPlugin{
githubOrganization = github_organization
extensionPoints = [
]
}
12 changes: 12 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'groovy-gradle-plugin'
id "io.nextflow.nf-build-plugin" version "1.0.1"
}

repositories {
mavenCentral()
}

dependencies {
implementation 'commons-codec:commons-codec:1.15'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nextflow.gradle.plugins

import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction


abstract class GenerateIdxTask extends DefaultTask{

@Internal
abstract ListProperty<String> extensionPoints

@OutputFile
final abstract RegularFileProperty outputFile =
project.objects.fileProperty().convention(project.layout.buildDirectory.file(
"resources/main/META-INF/extensions.idx"))

GenerateIdxTask() {
setGroup('nextflow')
}

@TaskAction
def runTask() {
def output = outputFile.get().asFile

if( extensionPoints.getOrElse([]).size() ){
output.text = extensionPoints.getOrElse([]).join('\n')
return
}

def matcher = new SourcesMatcher(project)
def extensionsClassName = matcher.pluginExtensions
def traceClassName = matcher.traceObservers
def all = extensionsClassName+traceClassName
output.text = all.join('\n')
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package nextflow.gradle.plugins

import groovy.json.JsonOutput
import org.apache.commons.codec.digest.DigestUtils
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

abstract class JsonPluginTask extends DefaultTask{

@Internal
abstract Property<String> getDownloadUrl()

@Internal
final abstract RegularFileProperty zipFile =
project.objects.fileProperty().convention(project.layout.buildDirectory.file(
"plugin/${project.name}-${project.version}.zip"))

@OutputFile
final abstract RegularFileProperty outputFile =
project.objects.fileProperty().convention(project.layout.buildDirectory.file(
"plugin/${project.name}-${project.version}-meta.json"))


JsonPluginTask(){
setGroup('nextflow')
dependsOn 'zipPlugin'
}


protected String resolveURL(){
"${downloadUrl.get()}/${project.version}/${project.name}-${project.version}.zip"
}

protected static String now() {
OffsetDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)
}

protected static String computeSha512(File file) {
if( !file.exists() )
throw new GradleException("Missing file: $file -- cannot compute SHA-512")
return DigestUtils.sha512Hex(file.bytes)
}

@TaskAction
def runTask() {
def jarTask = project.tasks.findByName('jar') as Jar
def manifest = jarTask.manifest
def json = [
version: "$project.version",
date: now(),
url: resolveURL(),
requires: manifest.attributes['Plugin-Requires'],
sha512sum: computeSha512(zipFile.get().asFile)
]
outputFile.get().asFile.text = JsonOutput.prettyPrint(JsonOutput.toJson(json))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nextflow.gradle.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class NextflowPlugin implements Plugin<Project>{

@Override
void apply(Project target) {

NextflowPluginExtension nextflowPluginExtension = target.extensions.create('nextflowPlugin',NextflowPluginExtension)

target.subprojects.find{it.name=="plugins"}.subprojects.each{ project ->
project.tasks.register('zipPlugin', ZipPluginTask,{

})
project.tasks.register('unzipPlugin', UnzipPluginTask,{

})
project.tasks.register('jsonPlugin', JsonPluginTask, {
downloadUrl = nextflowPluginExtension.downloadUrl
})
project.tasks.register('generateIdx', GenerateIdxTask, {
extensionPoints = nextflowPluginExtension.extensionPoints
})
project.tasks.findByName("processResources")?.dependsOn(project.tasks.findByName("generateIdx"))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package nextflow.gradle.plugins

import org.gradle.api.Project
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property

class NextflowPluginExtension {

final Property<String> downloadUrl

final Property<String> nextflowVersion

final Property<String> githubOrganization

final Property<String> pluginClassName

final ListProperty<String> extensionPoints

final private Project project

NextflowPluginExtension(Project project){
this.project = project
nextflowVersion = project.objects.property(String)
downloadUrl = project.objects.property(String)
githubOrganization = project.objects.property(String)
pluginClassName = project.objects.property(String)
extensionPoints = project.objects.listProperty(String)
}

void setNextflowVersion(String nextflowVersion){
this.nextflowVersion.set(nextflowVersion)
}

String getNextflowVersion(){
this.nextflowVersion.getOrElse("23.04.0")
}

void setDownloadUrl(String downloadUrl){
this.downloadUrl.set(downloadUrl)
}

String getDownloadUrl() {
return downloadUrl.getOrElse("https://github.com/${getGithubOrganization()}/${project.name}/releases/download")
}

String getGithubOrganization() {
return githubOrganization.get()
}

void setGithubOrganization(String githubOrganization){
this.githubOrganization.set(githubOrganization)
}

String getPluginClassName() {
SourcesMatcher matcher = new SourcesMatcher(project)
String resolved = matcher.pluginClassName
return pluginClassName.getOrElse(resolved)
}

void setPluginClassName(String pluginClassName){
this.pluginClassName.set(pluginClassName)
}

void setExtensionPoints(List<String> extensions){
this.extensionPoints.set(extensions)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package nextflow.gradle.plugins

import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer

class SourcesMatcher {

Project project
SourcesMatcher(Project project){
this.project = project
}


String getPluginClassName(){
return findSources(/class (\w+) extends BasePlugin/).first()
}

List<String> getPluginExtensions(){
return findSources(/class (\w+) extends PluginExtensionPoint/) +
findSources(/class (\w+) extends Executor implements ExtensionPoint/)
}

List<String> getTraceObservers(){
return findSources(/class (\w+) implements TraceObserverFactory/)
}

List<String> findSources( def regexp ){
def sourceSets = project.extensions.getByType(SourceSetContainer)
def mainSourceSet = sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME)
def sources = mainSourceSet.allSource
def root = project.projectDir

def matcher = sources.findAll{
def source = it.text
def matcher = source =~ regexp
if( matcher.size() != 1 ){
return null
}
it
}
matcher.collect { file ->
def source = file.toString() - "$root.absolutePath/src/main/"
return source.split('\\.').dropRight(1).join().split(File.separator).drop(1).join('.')
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextflow.gradle.plugins
import org.gradle.api.tasks.Copy


class UnzipPluginTask extends Copy{

UnzipPluginTask(){
setGroup('nextflow')
dependsOn project.tasks.zipPlugin
outputs.upToDateWhen {false}
from project.zipTree(project.tasks.zipPlugin.outputs.files.first())
into "${System.getProperty("user.home")}/.nextflow/plugins/${project.name}-${project.version}"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nextflow.gradle.plugins


import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.bundling.Jar


class ZipPluginTask extends Jar{

@OutputFile
final abstract RegularFileProperty outputFile =
project.objects.fileProperty().convention(project.layout.buildDirectory.file(
"plugin/${project.name}-${project.version}.zip"))

ZipPluginTask() {
setGroup('nextflow')

dependsOn(project.tasks.findByName('build'))
dependsOn(project.tasks.generateIdx)

into( 'classes',{
with project.tasks.findByName('jar')
})

into('lib',{
from(project.configurations.find {it.name=='runtimeClasspath'})
})

manifest {
Jar jar = project.tasks.findByName('jar') as Jar
from(jar.manifest)
}

archiveExtension.set('zip')
setPreserveFileTimestamps(false)
setReproducibleFileOrder(true)

def directory = project.objects.fileProperty().convention(
project.layout.buildDirectory.file("plugin"))
getDestinationDirectory().set(directory.get().asFile)
}

}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=0.0.2-rc1
Loading

0 comments on commit 340b0dd

Please sign in to comment.