-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathNuGetPack.groovy
183 lines (160 loc) · 5.26 KB
/
NuGetPack.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.ullink
import com.ullink.util.GradleHelper
import groovy.util.slurpersupport.GPathResult
import org.apache.commons.io.FilenameUtils
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.*
class NuGetPack extends BaseNuGet {
File nuspecFile
@Optional
@InputFile
File csprojPath
@OutputDirectory
File destinationDir = new File(project.buildDir, project.convention.plugins.base.distsDirName)
@Optional
@InputFile
File basePath
@Optional
@Input
def packageVersion
@Optional
@Input
def exclude
@Input
def generateSymbols = false
@Input
def tool = false
@Input
def build = false
@Input
def defaultExcludes = true
@Input
def packageAnalysis = true
@Input
def includeReferencedProjects = false
@Input
def includeEmptyDirectories = true
@Input
def properties = [:]
@Optional
@Input
def minClientVersion
@Optional
@Input
def msBuildVersion
NuGetPack() {
super('pack')
// Force always execute
outputs.upToDateWhen { false }
project.afterEvaluate {
def spec = getNuspec()
def specSources = spec?.files?.file?.collect { it.@src.text() }
if (specSources && specSources.any()) {
project.tasks.matching {
it.class.name.startsWith("com.ullink.Msbuild") && it.projects.values().any { specSources.contains it.properties.TargetPath }
}.each {
dependsOn it
}
}
}
}
void setDestinationDir(String path) {
destinationDir = project.file(path)
}
void setNuspecFile(String path) {
nuspecFile = project.file(path)
}
void setCsprojPath(String path) {
csprojPath = project.file(path)
}
@Override
void exec() {
args getNuspecOrCsproj()
def spec = getNuspec()
def destDir = project.file(getDestinationDir())
if (!destDir.exists()) {
destDir.mkdirs()
}
args '-OutputDirectory', destDir
if (basePath) args '-BasePath', basePath
def version = getFinalPackageVersion(spec)
if (version) args '-Version', version
if (exclude) args '-Exclude', exclude
if (generateSymbols) args '-Symbols'
if (tool) args '-Tool'
if (build) args '-Build'
if (!defaultExcludes) args '-NoDefaultExcludes'
if (!packageAnalysis) args '-NoPackageAnalysis'
if (includeReferencedProjects) args '-IncludeReferencedProjects'
if (!includeEmptyDirectories) args '-ExcludeEmptyDirectories'
if (!properties.isEmpty()) args '-Properties', properties.collect({ k, v -> "$k=$v" }).join(';')
if (minClientVersion) args '-MinClientVersion', minClientVersion
if (!msBuildVersion) msBuildVersion = GradleHelper.getPropertyFromTask(project, 'version', 'msbuild')
if (msBuildVersion) args '-MsBuildVersion', msBuildVersion
super.exec()
}
void nuspec(Closure closure) {
if (dependentNuGetSpec) {
dependentNuGetSpec.nuspec closure
} else {
def nuGetSpec = project.task("nugetSpec_$name", type: NuGetSpec)
nuGetSpec.with {
group = BasePlugin.BUILD_GROUP
description = "Generates nuspec file for task $name."
nuspec closure
}
dependsOn nuGetSpec
}
}
@Internal
NuGetSpec getDependentNuGetSpec() {
dependsOn.find { it instanceof NuGetSpec } as NuGetSpec
}
// Because Nuget pack handle csproj or nuspec file we should be able to use it in plugin
@InputFile
File getNuspecOrCsproj() {
csprojPath ? csprojPath : getNuspecFile()
}
@Internal
GPathResult getNuspec() {
def nuspecFile = getNuspecFile()
if (nuspecFile?.exists()) {
return new XmlSlurper(false, false).parse(project.file(nuspecFile))
}
if (dependentNuGetSpec) {
def generatedNuspec = dependentNuGetSpec.generateNuspec()
if (generatedNuspec) {
return new XmlSlurper(false, false).parseText(generatedNuspec)
}
}
}
@Internal
File getNuspecFile() {
if (nuspecFile) {
return nuspecFile
}
if (dependentNuGetSpec) {
return dependentNuGetSpec.nuspecFile
}
}
@OutputFile
File getPackageFile() {
def spec = getNuspec()
def version = getFinalPackageVersion(spec)
def id = spec?.metadata?.id?.toString() ?: getIdFromMsbuildTask()
new File(getDestinationDir(), id + '.' + version + '.nupkg')
}
private String getFinalPackageVersion(spec) {
return packageVersion ?: spec?.metadata?.version ?: project.version
}
@Internal
String getIdFromMsbuildTask() {
def isInputProject = { csprojPath.equalsIgnoreCase(it.projectFile) }
def msbuildTask = project.tasks.find {
it.class.name.startsWith("com.ullink.Msbuild") && it.projects.values().any(isInputProject)
}
if (msbuildTask != null) {
FilenameUtils.removeExtension(msbuildTask.projects.values().find(isInputProject).dotnetAssemblyFile.name)
}
}
}