Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Add publishLocal tasks (#11)
Browse files Browse the repository at this point in the history
* Add publishLocal tasks

* better info output to debug

* Escape local test path on windows machines

* Rename create task method
  • Loading branch information
marcolink authored Nov 14, 2017
1 parent 132e451 commit 93de0cb
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.gradle
build/
out/
.idea
out
*.iml

# Ignore Gradle GUI config
gradle-app.setting
Expand All @@ -14,3 +16,4 @@ gradle-app.setting
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties
userHome/

4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pipeline {

stage('Test') {
steps {
gradleWrapper "check"
gradleWrapper "check --info"
}
}
}
Expand All @@ -33,4 +33,4 @@ pipeline {
gradleWrapper "clean"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package wooga.gradle.paket.publish

import nebula.test.IntegrationSpec
import groovy.json.StringEscapeUtils
import org.jfrog.artifactory.client.Artifactory
import org.jfrog.artifactory.client.ArtifactoryClient
import org.jfrog.artifactory.client.model.RepoPath
Expand All @@ -36,7 +36,7 @@ class PaketPublishIntegrationSpec extends PaketIntegrationDependencyFileSpec {
def uniquPackagePostfix() {
String key = "TRAVIS_JOB_NUMBER"
def env = System.getenv()
if(env.containsKey(key)) {
if (env.containsKey(key)) {
return env.get(key)
}
return ""
Expand Down Expand Up @@ -64,6 +64,7 @@ class PaketPublishIntegrationSpec extends PaketIntegrationDependencyFileSpec {

def artifactoryRepoName = "atlas-nuget-integrationTest"
def repoUrl = "$artifactoryUrl/api/nuget/atlas-nuget-integrationTest"
def localPath

def packageIdToName(id) {
id.replaceAll(/\./, '')
Expand Down Expand Up @@ -109,14 +110,16 @@ class PaketPublishIntegrationSpec extends PaketIntegrationDependencyFileSpec {
Empty nuget package.
""".stripIndent()

cleanupArtifactory(artifactoryRepoName,packageName)
localPath = File.createTempDir()
localPath.deleteOnExit()
cleanupArtifactory(artifactoryRepoName, packageName)
}

def cleanup() {
cleanupArtifactory(artifactoryRepoName,packageName)
cleanupArtifactory(artifactoryRepoName, packageName)
}

def cleanupArtifactory(String repoName,String artifactName) {
def cleanupArtifactory(String repoName, String artifactName) {
List<RepoPath> searchItems = artifactory.searches()
.repositories(repoName)
.artifactsByName(artifactName)
Expand All @@ -129,7 +132,7 @@ class PaketPublishIntegrationSpec extends PaketIntegrationDependencyFileSpec {
}
}

def hasPackageOnArtifactory(String repoName,String artifactName) {
def hasPackageOnArtifactory(String repoName, String artifactName) {
List<RepoPath> packages = artifactory.searches()
.repositories(repoName)
.artifactsByName(artifactName)
Expand Down Expand Up @@ -160,4 +163,63 @@ class PaketPublishIntegrationSpec extends PaketIntegrationDependencyFileSpec {
where:
taskToRun << ["publish-${packageIdToName(packageID)}", "publish${repoName.capitalize()}-${packageIdToName(packageID)}", "publish${repoName.capitalize()}", "publish"]
}

@Unroll
def 'builds package and publish locally #taskToRun'(String taskToRun) {
given: "the future npkg artifact"
def nugetArtifact = new File(new File(new File(projectDir, 'build'), "outputs"), packageName)
assert !nugetArtifact.exists()

and: "paket.dependencies and paket.lock file"
createFile("paket.lock")
createFile("paket.dependencies")
def escapedPath = escapedPath(localPath.absolutePath)

and: "a build.gradle file with a local publish entry"
buildFile.text = ""
buildFile << """
group = 'test'
version = "$version"
${applyPlugin(PaketPackPlugin)}
${applyPlugin(PaketPublishPlugin)}
publishing {
repositories {
nuget {
name "$repoName"
path "$escapedPath"
}
}
}
paketPublish {
publishRepositoryName = "$repoName"
}
""".stripIndent()

and: "a future local output file"
def futureFile = new File(escapedPath, nugetArtifact.name)
assert !futureFile.exists()

when: "run the publish task"
def result = runTasksSuccessfully(taskToRun)

then:
nugetArtifact.exists()
result.wasExecuted("paketPack-${packageIdToName(packageID)}")
futureFile.exists()

where:
taskToRun << ["publish-${packageIdToName(packageID)}", "publish${repoName.capitalize()}-${packageIdToName(packageID)}", "publish${repoName.capitalize()}", "publish"]
}

def escapedPath(String path) {
String osName = System.getProperty("os.name").toLowerCase()
if (osName.contains("windows")) {
return StringEscapeUtils.escapeJava(path)
}
path
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class PaketPackPlugin implements Plugin<Project> {
}

if (project.version != Project.DEFAULT_VERSION) {
return project.version
return project.version.toString()
}

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import wooga.gradle.paket.base.DefaultPaketPluginExtension
import wooga.gradle.paket.base.PaketBasePlugin
import wooga.gradle.paket.publish.repository.DefaultNugetRepositoryHandlerConvention
import wooga.gradle.paket.publish.repository.NugetRepository
import wooga.gradle.paket.publish.tasks.PaketCopy
import wooga.gradle.paket.publish.tasks.PaketPush

class PaketPublishPlugin implements Plugin<Project> {
Expand All @@ -53,7 +54,7 @@ class PaketPublishPlugin implements Plugin<Project> {
def publishLifecycleTask = tasks[PublishingPlugin.PUBLISH_LIFECYCLE_TASK_NAME]

project.getExtensions().configure(PublishingExtension.class, new Action<PublishingExtension>() {
public void execute(PublishingExtension e) {
void execute(PublishingExtension e) {
RepositoryHandler repositories = e.repositories
DefaultRepositoryHandler handler = (DefaultRepositoryHandler) repositories

Expand All @@ -73,26 +74,34 @@ class PaketPublishPlugin implements Plugin<Project> {
nupkg.allArtifacts.each { artifact ->
def packageName = artifact.name.replaceAll(/\./, '')
def publishTaskName = "publish-$packageName"
createPublishTask(publishTaskName, artifact, publishLifecycleTask,repository )
createPublishTask(publishTaskName, artifact, publishLifecycleTask, repository)
}
}
}
catch (Exception e) {

}

PublishArtifactSet artifacts = nupkg.allArtifacts

publishingExtension.repositories.withType(NugetRepository) {
createPublishTasks(tasks, artifacts, it)
}
}

createPublishLocalTask(tasks)
}

void createPublishLocalTask(TaskContainer tasks) {
def paketCopylifecycle = tasks.create(name: "publishLocal", group: PublishingPlugin.PUBLISH_TASK_GROUP, description: "publish all nupkg to all local paths")
paketCopylifecycle.dependsOn tasks.withType(PaketCopy)
}

void createPublishTasks(TaskContainer tasks, PublishArtifactSet artifacts, NugetRepository repository) {

String baseTaskName = "publish" + repository.name.capitalize()
Task repoLifecycle = tasks.create(name: baseTaskName, group: PublishingPlugin.PUBLISH_TASK_GROUP)
repoLifecycle.description = "Publishes all nupkg artifacts to ${repository.url}"
repoLifecycle.description = "Publishes all nupkg artifacts to ${repository.destination}"

artifacts.each { artifact ->
def packageName = artifact.name.replaceAll(/\./, '')
Expand All @@ -101,17 +110,29 @@ class PaketPublishPlugin implements Plugin<Project> {
}
}

private PaketPush createPublishTask(String publishTaskName, PublishArtifact artifact, Task lifecycle, NugetRepository repository ) {
PaketPush pushTask = (PaketPush) tasks.create(name: publishTaskName, group: PublishingPlugin.PUBLISH_TASK_GROUP, type: PaketPush)
pushTask.url = repository.url
pushTask.apiKey = repository.apiKey
pushTask.inputFile = artifact.file
pushTask.description = "Publishes ${artifact.file.name} to ${repository.url}"
pushTask.paketExtension = project.extensions.getByType(DefaultPaketPluginExtension)
private Task createPublishTask(String publishTaskName, PublishArtifact artifact, Task lifecycle, NugetRepository repository) {
Task task = repository.url != null ? createPaketPublishTask(publishTaskName, repository, artifact) :
createPaketCopyTask(publishTaskName, repository, artifact)
task.dependsOn artifact
lifecycle.dependsOn task
task
}

pushTask.dependsOn artifact
private Task createPaketCopyTask(String publishTaskName, NugetRepository repository, PublishArtifact artifact) {
PaketCopy task = (PaketCopy) tasks.create(name: publishTaskName, group: PublishingPlugin.PUBLISH_TASK_GROUP, type: PaketCopy)
task.from artifact.file
task.into new File(repository.path)
task.description = "Copies ${artifact.file.name} to ${repository.path}"
task
}

lifecycle.dependsOn pushTask
pushTask
private PaketPush createPaketPublishTask(String publishTaskName, NugetRepository repository, PublishArtifact artifact) {
PaketPush task = (PaketPush) tasks.create(name: publishTaskName, group: PublishingPlugin.PUBLISH_TASK_GROUP, type: PaketPush)
task.url = repository.url
task.apiKey = repository.apiKey
task.inputFile = artifact.file
task.description = "Publishes ${artifact.file.name} to ${repository.url}"
task.paketExtension = project.extensions.getByType(DefaultPaketPluginExtension)
task
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,35 @@ class NugetRepository implements NugetArtifactRepository {
String apiKey
String url
String name
String path

def name(String name) {
setName(name)
}

def url(String url) {
if(path != null)
{
throw new Exception("path already set")
}
setUrl(url)
}

def apiKey(String apiKey) {
setApiKey(apiKey)
}

def path(String path){
if(url != null)
{
throw new Exception("url already set")
}
setPath(path)
}

def getDestination(){
path ? path : url
}


}
23 changes: 23 additions & 0 deletions src/main/groovy/wooga/gradle/paket/publish/tasks/PaketCopy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2017 Wooga GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package wooga.gradle.paket.publish.tasks

import org.gradle.api.tasks.Copy

class PaketCopy extends Copy {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package wooga.gradle.paket.pack
import nebula.test.PluginProjectSpec
import nebula.test.ProjectSpec
import org.gradle.api.plugins.BasePlugin
import spock.lang.Specification
import spock.lang.Unroll
import wooga.gradle.paket.base.PaketBasePlugin
import wooga.gradle.paket.get.PaketGetPlugin
Expand Down Expand Up @@ -169,4 +170,4 @@ class PaketPackPluginSpec extends ProjectSpec {
templateFile.createNewFile()
templateFile.append("id $id")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package wooga.gradle.paket.publish.repository

import spock.lang.Specification

class NugetRepositorySpec extends Specification {

def "applies properties"(){

given: "a fresh NugetRepository object"
def vo = new NugetRepository()

when: "set properties via methods"
vo.url("url")
vo.name("name")
vo.apiKey("apiKey")

then:
vo.url == "url"
vo.name == "name"
vo.apiKey == "apiKey"
}

def "applies all properties"(){

given: "a fresh NugetRepository object"
def vo = new NugetRepository()

when: "set path and url"
vo.url("url")
vo.path("path")

then: "Exception gets thrown"
thrown(Exception)
}
}

0 comments on commit 93de0cb

Please sign in to comment.