diff --git a/plugin/src/functionalTest/groovy/io/noumenal/FunctionalTest.groovy b/plugin/src/functionalTest/groovy/io/noumenal/FunctionalTest.groovy index 2f9802d..ade73f2 100644 --- a/plugin/src/functionalTest/groovy/io/noumenal/FunctionalTest.groovy +++ b/plugin/src/functionalTest/groovy/io/noumenal/FunctionalTest.groovy @@ -34,7 +34,9 @@ class FunctionalTest extends Specification { publishUrl = System.getProperty("publishUrl") def setupSpec() { - settingsFile = new File(projectDir, 'settings.gradle').write("") + settingsFile = new File(projectDir, 'settings.gradle').write(""" + |rootProject.name = 'unit-test' + |""".stripMargin()) buildFile = new File(projectDir, 'build.gradle').write(""" |plugins { | id('io.noumenal.gradle.snowflake') @@ -45,7 +47,6 @@ class FunctionalTest extends Specification { | role = 'devops' | database = 'devops' | schema = 'gradle' - | publishUrl = 's3://nio-maven-test' |} |version='0.1.0' |""".stripMargin()) @@ -56,8 +57,7 @@ class FunctionalTest extends Specification { List systemArgs = [ "-Psnowflake.account=$account".toString(), "-Psnowflake.user=$user".toString(), - "-Psnowflake.password=$password".toString(), - "-Psnowflake.publishUrl=$publishUrl".toString() + "-Psnowflake.password=$password".toString() ] args.add(0, taskName) args.addAll(systemArgs) @@ -78,12 +78,34 @@ class FunctionalTest extends Specification { return result } - def "snowflakePublish task"() { + def "snowflakePublish with publishUrl option"() { given: taskName = 'snowflakePublish' when: - result = executeSingleTask(taskName, ['-Si']) + result = executeSingleTask(taskName, ['-Psnowflake.publishUrl=s3://nio-maven-test', '-Si']) + + then: + !result.tasks.collect { it.outcome }.contains('FAILURE') + } + + def "snowflakePublish without publishUrl option"() { + given: + taskName = 'snowflakePublish' + + when: + result = executeSingleTask(taskName, ['-Psnowflake.stage=upload', '-Si']) + + then: + !result.tasks.collect { it.outcome }.contains('FAILURE') + } + + def "snowflakePublish with custom JAR"() { + given: + taskName = 'snowflakePublish' + + when: + result = executeSingleTask(taskName, ['--jar', 'build/libs/unit-test-0.1.0-all.jar','-Psnowflake.stage=upload', '-Si']) then: !result.tasks.collect { it.outcome }.contains('FAILURE') diff --git a/plugin/src/main/groovy/io/noumenal/ApplicationContainer.groovy b/plugin/src/main/groovy/io/noumenal/ApplicationContainer.groovy index 7a67bdd..fcb4464 100644 --- a/plugin/src/main/groovy/io/noumenal/ApplicationContainer.groovy +++ b/plugin/src/main/groovy/io/noumenal/ApplicationContainer.groovy @@ -13,7 +13,6 @@ class ApplicationContainer { String type = 'function' String language = 'JAVA' String handler - Boolean hasReplace = true String getObjectType() { returns ? 'function' : 'procedure' diff --git a/plugin/src/main/groovy/io/noumenal/SnowflakePlugin.groovy b/plugin/src/main/groovy/io/noumenal/SnowflakePlugin.groovy index 4b16476..15394e9 100644 --- a/plugin/src/main/groovy/io/noumenal/SnowflakePlugin.groovy +++ b/plugin/src/main/groovy/io/noumenal/SnowflakePlugin.groovy @@ -12,22 +12,28 @@ class SnowflakePlugin implements Plugin { void apply(Project project) { project.extensions.create(PLUGIN, SnowflakeExtension) + + // shorthand + def extension = project.extensions."$PLUGIN" + project."$PLUGIN".extensions.applications = project.container(ApplicationContainer) project.apply plugin: 'com.redpillanalytics.gradle-properties' - project.apply plugin: 'maven-publish' project.apply plugin: 'java-library' project.apply plugin: 'com.github.johnrengelman.shadow' project.pluginProps.setParameters(project, PLUGIN) project.afterEvaluate { // create maven publishing - if (!project.extensions."$PLUGIN".useCustomMaven) { + if (!extension.useCustomMaven && extension.publishUrl) { + + // apply the maven-publish plugin for the user + project.apply plugin: 'maven-publish' // create publication project.publishing.publications { snowflake(MavenPublication) { - groupId = project.extensions."$PLUGIN".groupId - artifactId = project.extensions."$PLUGIN".artifactId + groupId = extension.groupId + artifactId = extension.artifactId //from project.components.java artifact project.shadowJar } @@ -35,8 +41,8 @@ class SnowflakePlugin implements Plugin { // create repository project.publishing.repositories { maven { - name project.extensions."$PLUGIN".stage - url project.extensions."$PLUGIN".publishUrl + name extension.stage + url extension.publishUrl authentication { awsIm(AwsImAuthentication) } @@ -47,17 +53,12 @@ class SnowflakePlugin implements Plugin { // Register a task project.tasks.register("snowflakePublish", SnowflakePublish) // set dependency - if (!project.extensions.snowflake.useCustomMaven) { - project.tasks.snowflakePublish.dependsOn project.extensions."$PLUGIN".publishTask, project.tasks.test - project.tasks.getByName(project.extensions."$PLUGIN".publishTask).mustRunAfter project.tasks.test + if (!extension.useCustomMaven && extension.publishUrl) { + project.tasks.snowflakePublish.dependsOn extension.publishTask + project.tasks.getByName(extension.publishTask).mustRunAfter project.tasks.test } - // for debugging - def snowflake = project.extensions."$PLUGIN" - def pubs = project.publishing.publications - def repos = project.publishing.repositories - def pubTask = project.extensions."$PLUGIN".publishTask - def text = "text" + project.tasks.snowflakePublish.dependsOn project.tasks.test } } } diff --git a/plugin/src/main/groovy/io/noumenal/SnowflakePublish.groovy b/plugin/src/main/groovy/io/noumenal/SnowflakePublish.groovy index 3520f3a..1febbe8 100644 --- a/plugin/src/main/groovy/io/noumenal/SnowflakePublish.groovy +++ b/plugin/src/main/groovy/io/noumenal/SnowflakePublish.groovy @@ -1,11 +1,14 @@ package io.noumenal +import com.snowflake.snowpark_java.PutResult import com.snowflake.snowpark_java.Session import groovy.util.logging.Slf4j import net.snowflake.client.jdbc.SnowflakeStatement import org.gradle.api.DefaultTask +import org.gradle.api.PathValidation import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Internal import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile @@ -85,10 +88,13 @@ class SnowflakePublish extends DefaultTask { @Optional @Input - @Option(option = "publishUrl", - description = "The url of the Snowflake external stage to publish to." - ) - String publishUrl = extension.publishUrl + @Option(option = "jar", description = "Manually pass a JAR file path to upload instead of relying on Gradle metadata.") + String jar = project.tasks.shadowJar.archiveFile.get() + +// @InputFile +// def getJarFile() { +// project.file(jar, PathValidation.NONE) +// } @Internal Session getSession() { @@ -119,6 +125,7 @@ class SnowflakePublish extends DefaultTask { File output = project.file("${project.buildDir}/${PLUGIN}/output.txt") String getImports(Session session) { + String basePath = "@${stage}/${extension.groupId.replace('.', '/')}/${extension.artifactId}/${project.version}" //log.warn "basePath: $basePath" Statement statement = session.jdbcConnection().createStatement() @@ -147,23 +154,31 @@ class SnowflakePublish extends DefaultTask { def publish() { // keep the session Session session = this.session - - // ensure that the stage and the publishUrl are aligned - Statement statement = session.jdbcConnection().createStatement() - String sql = "select stage_url from information_schema.stages where stage_name=upper('$stage') and stage_schema=upper('$schema') and stage_type='External Named'" - ResultSet rs = statement.executeQuery(sql) - String selectStage - if (rs.next()) { - selectStage = rs.getString(1) + String jar = project.tasks.shadowJar.archiveFile.get() + + if (!extension.publishUrl) { + def options = [ + AUTO_COMPRESS: 'false', + PARALLEL : '4' + ] + PutResult[] putResults = session.file().put(jar, "$stage/libs", options) + } else { + // ensure that the stage and the publishUrl are aligned + Statement statement = session.jdbcConnection().createStatement() + String sql = "select stage_url from information_schema.stages where stage_name=upper('$stage') and stage_schema=upper('$schema') and stage_type='External Named'" + ResultSet rs = statement.executeQuery(sql) + String selectStage + if (rs.next()) { + selectStage = rs.getString(1) + } + // ensure we are matching our stage with our url + assert selectStage == extension.publishUrl } - assert selectStage == publishUrl - - // create snowflake application // automatically create application spec objects output.write("Snowflake Application:\n\n") project."$PLUGIN".applications.each { ApplicationContainer app -> - String createText = app.getCreate(getImports(session)) + String createText = app.getCreate(extension.publishUrl ? getImports(session) : "@$stage/libs/$jar") String message = "Deploying ==> \n$createText" log.warn message output.append("$message\n")