/
PluginResolveEngine.groovy
304 lines (268 loc) · 10.2 KB
/
PluginResolveEngine.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2004-2005 the original author or authors.
*
* 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 org.codehaus.groovy.grails.resolve
import grails.build.logging.GrailsConsole
import grails.util.BuildSettings
import groovy.util.slurpersupport.GPathResult
import org.codehaus.groovy.grails.io.support.IOUtils
/**
* Utility methods for resolving plugin zips and information
* used in conjunction with an IvyDependencyManager instance.
*
* @author Graeme Rocher
* @since 1.3
*/
final class PluginResolveEngine {
DependencyManager dependencyManager
BuildSettings settings
Closure messageReporter = { if (it) GrailsConsole.instance.updateStatus(it) }
PluginResolveEngine(DependencyManager dependencyManager, BuildSettings settings) {
this.dependencyManager = dependencyManager
this.settings = settings
}
DependencyManager createFreshDependencyManager() {
dependencyManager.createCopy(settings)
}
void renderInstallInfo(String pluginName, String version, Writer writer) {
writer << """
Since Grails 2.3, it is no longer possible to install plugins using the install-plugin command.
Plugins must be declared in the grails-app/conf/BuildConfig.groovy file.
"""
def pluginXml
if (dependencyManager instanceof IvyDependencyManager) {
pluginXml = resolvePluginMetadata(pluginName, version)
if (!version) {
version = pluginXml.@version.text()
}
}
else {
String url = "http://grails.org/api/v1.0/plugin/$pluginName"
if (version) {
url += "/$version"
}
try {
final String text = new URL("$url?format=xml").getText(connectTimeout: 500, readTimeout: 3000, "UTF-8")
pluginXml = IOUtils.createXmlSlurper().parseText(text)
if (!version) {
version = pluginXml.version.text()
}
}
catch (FileNotFoundException ignored) {}
}
if (!pluginXml) {
if (version) {
writer << """
ERROR: Plugin '$pluginName' not found with version '$pluginVersion'
"""
}
else {
writer << """
ERROR: Plugin '$pluginName' not found
"""
}
return
}
writer << """
Example:
grails.project.dependency.resolution = {
...
plugins {
compile ":$pluginName:$version"
}
}
"""
def repos = []
def ignored = [
'http://plugins.grails.org',
'http://repo.grails.org/grails/plugins/',
'http://repo.grails.org/grails/core/',
'http://svn.codehaus.org/grails/trunk/grails-plugins']
if (pluginXml.repositories.children().size()) {
for (repo in pluginXml.repositories.repository) {
String url = repo.@url.text().trim()
if (url in ignored) {
continue
}
if (url == 'http://repo1.maven.org/maven2/') {
url = 'mavenCentral()'
}
repos << url
}
}
if (repos) {
writer << """
Additionally, add these custom repositories to the "repositories" block:
"""
for (repo in repos) {
writer << """\
$repo
"""
}
}
}
/**
* Renders plugin info to the target writer
*
* @param pluginName The plugin name
* @param pluginVersion The plugin version
* @param output The target writer
*/
GPathResult renderPluginInfo(String pluginName, String pluginVersion, OutputStream outputStream) {
renderPluginInfo(pluginName, pluginVersion, new OutputStreamWriter(outputStream, "UTF-8"))
}
/**
* Renders plugin info to the target writer
*
* @param pluginName The plugin name
* @param pluginVersion The plugin version
* @param output The target writer
*/
GPathResult renderPluginInfo(String pluginName, String pluginVersion, Writer writer) {
def pluginXml = resolvePluginMetadata(pluginName, pluginVersion)
if (pluginXml != null) {
def output = new PrintWriter(writer)
def line = "Name: ${pluginName}"
line += "\t| Latest release: ${pluginXml.@version}"
output.println getPluginInfoHeader()
output.println line
printLineSeparator(output)
def release = pluginXml
if (release) {
if (release.'title'.text()) {
output.println "${release.'title'.text()}"
}
else {
output.println "No info about this plugin available"
}
printLineSeparator(output)
if (release.'author'.text()) {
output.println "Author: ${release.'author'.text()}"
printLineSeparator(output)
}
if (release.'authorEmail'.text()) {
output.println "Author's e-mail: ${release.'authorEmail'.text()}"
printLineSeparator(output)
}
if (release.'documentation'.text()) {
output.println "Find more info here: ${release.'documentation'.text()}"
printLineSeparator(output)
}
if (release.'description'.text()) {
output.println()
output.println "${release.'description'.text()}"
output.println()
}
printSectionTitle(output, "Dependency Definition")
output.println(" :${pluginName}:${pluginXml.@version}")
output.println()
if (release.repositories.children().size()) {
printSectionTitle(output, "Required Repositories")
release.repositories.repository.each { repo ->
output.println(" ${repo.@url}")
}
}
if (release.dependencies.children().size()) {
printSectionTitle(output, "Transitive Dependencies")
printDependencies(output, release.dependencies)
}
if (release.plugins.children().size()) {
printSectionTitle(output, "Transitive Plugins")
printDependencies(output, release.plugins)
}
}
else {
output.println "<release not found for this plugin>"
printLineSeparator(output)
}
output.println getPluginInfoFooter()
output.flush()
}
return pluginXml
}
protected void printDependencies(output, dependencies) {
dependencies.children().each { scope ->
def scopeName = scope.name()
scope.dependency.each { dep ->
output.println(" ${dep.@group}:${dep.@name}:${dep.@version} ($scopeName)")
}
}
}
protected void printSectionTitle(PrintWriter output, String title) {
output.println()
output.println title
printLineSeparator(output)
}
protected void printLineSeparator(PrintWriter output) {
output.println '--------------------------------------------------------------------------'
}
protected String getPluginInfoHeader() {
'''
--------------------------------------------------------------------------
Information about Grails plugin
--------------------------------------------------------------------------\
'''
}
protected String getPluginInfoFooter() {
'''
To get info about specific release of plugin 'grails plugin-info [NAME] [VERSION]'
To get list of all plugins type 'grails list-plugins'
For further info visit http://grails.org/plugins
'''
}
/**
* Resolve a Plugin zip for for the given name and plugin version
* @param pluginName The plugin name
* @param pluginVersion The plugin version
* @return The location of the local file or null if an error occured
*/
File resolvePluginZip(String pluginName, String pluginVersion, String scope = "", Map args = [:]) {
DependencyManager dependencyManager = createFreshDependencyManager()
def resolveArgs = createResolveArguments(pluginName, pluginVersion)
dependencyManager.parseDependencies {
plugins {
runtime resolveArgs
}
}
messageReporter "Resolving plugin ${pluginName}. Please wait..."
messageReporter()
def report = dependencyManager.resolve(scope)
try {
def reports = report
if (report.pluginZips) {
return report.pluginZips[0]
}
messageReporter "Error resolving plugin ${resolveArgs}. Plugin not found."
} catch (e) {
messageReporter "Error resolving plugin ${resolveArgs}. ${e.message}"
}
return null
}
def createResolveArguments(String pluginName, String pluginVersion) {
def (group, name) = pluginName.contains(":") ? pluginName.split(":") : ['org.grails.plugins', pluginName]
def resolveArgs = [name: name, group: group]
if (pluginVersion) resolveArgs.version = pluginVersion
else resolveArgs.version = "latest.integration"
return resolveArgs
}
/**
* This method will resolve the plugin.xml file for a given plugin
* without downloading the plugin zip itself
*/
GPathResult resolvePluginMetadata(String pluginName, String pluginVersion) {
DependencyManager dependencyManager = createFreshDependencyManager()
return dependencyManager.downloadPluginInfo(pluginName, pluginVersion)
}
}