Skip to content

Commit

Permalink
Merge pull request #10 from noumenalio/develop
Browse files Browse the repository at this point in the history
Internal stages. Custom jar path.
  • Loading branch information
stewartbryson authored Aug 17, 2022
2 parents aae030c + dcecdb2 commit a6a1140
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 38 deletions.
34 changes: 28 additions & 6 deletions plugin/src/functionalTest/groovy/io/noumenal/FunctionalTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -45,7 +47,6 @@ class FunctionalTest extends Specification {
| role = 'devops'
| database = 'devops'
| schema = 'gradle'
| publishUrl = 's3://nio-maven-test'
|}
|version='0.1.0'
|""".stripMargin())
Expand All @@ -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)
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ApplicationContainer {
String type = 'function'
String language = 'JAVA'
String handler
Boolean hasReplace = true

String getObjectType() {
returns ? 'function' : 'procedure'
Expand Down
31 changes: 16 additions & 15 deletions plugin/src/main/groovy/io/noumenal/SnowflakePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,37 @@ class SnowflakePlugin implements Plugin<Project> {

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
}
}
// 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)
}
Expand All @@ -47,17 +53,12 @@ class SnowflakePlugin implements Plugin<Project> {
// 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
}
}
}
47 changes: 31 additions & 16 deletions plugin/src/main/groovy/io/noumenal/SnowflakePublish.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a6a1140

Please sign in to comment.