Skip to content

Commit 403092c

Browse files
committed
Rewrite the Script Commands 'list-plugins' 'plugin-info' in Profile 'base' to Shell CLI Commands
Closes gh-962
1 parent 4e8c31c commit 403092c

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2022-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.cli.profile.commands
17+
18+
import groovy.transform.CompileStatic
19+
20+
import grails.build.logging.GrailsConsole
21+
import org.grails.cli.profile.Command
22+
import org.grails.cli.profile.CommandDescription
23+
import org.grails.cli.profile.ExecutionContext
24+
import org.grails.cli.profile.ProjectCommand
25+
26+
/**
27+
* Lists the available plugins from the Plugin Repository
28+
*
29+
* @author Michael Yan
30+
* @since 2023.3.0
31+
*/
32+
@CompileStatic
33+
class ListPluginsCommand implements Command, ProjectCommand {
34+
35+
public static final String PLUGIN_REPO_URL = 'https://repo1.maven.org/maven2/org/graceframework/plugins'
36+
private static final List<String> EXCLUDES = ['asset-pipeline-core', 'asset-pipeline-gradle', 'views-gradle']
37+
38+
final String name = 'list-plugins'
39+
final CommandDescription description = new CommandDescription(name,
40+
'Lists the available plugins from the Plugin Repository',
41+
'grace list-plugins')
42+
43+
44+
@Override
45+
boolean handle(ExecutionContext executionContext) {
46+
GrailsConsole console = executionContext.console
47+
try {
48+
console.addStatus 'Available Plugins'
49+
String text = new URL(PLUGIN_REPO_URL).text
50+
text.eachMatch(/<a href="([a-z-]+)\/" title="([a-z-]+)\/">.+/) { List<String> it ->
51+
String plugin = it[1]
52+
if (!EXCLUDES.contains(plugin)) {
53+
console.log "* ${it[1]}"
54+
}
55+
}
56+
57+
console.println()
58+
console.log "You can find more information on https://central.sonatype.com/namespace/org.graceframework.plugins"
59+
}
60+
catch(Throwable e) {
61+
console.error "Failed to list plugins", e
62+
return false
63+
}
64+
65+
true
66+
}
67+
68+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2022-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.cli.profile.commands
17+
18+
import groovy.xml.XmlSlurper
19+
import groovy.xml.slurpersupport.GPathResult
20+
21+
import grails.build.logging.GrailsConsole
22+
import org.grails.build.parsing.CommandLine
23+
import org.grails.cli.profile.Command
24+
import org.grails.cli.profile.CommandDescription
25+
import org.grails.cli.profile.ExecutionContext
26+
import org.grails.cli.profile.ProjectCommand
27+
28+
/**
29+
* Get the information about the given plugin
30+
*
31+
* @author Michael Yan
32+
* @since 2023.3.0
33+
*/
34+
class PluginInfoCommand implements Command, ProjectCommand {
35+
36+
public static final String PLUGIN_REPO_URL = 'https://repo1.maven.org/maven2/org/graceframework/plugins'
37+
public static final String NAME = 'plugin-info'
38+
39+
final String name = NAME
40+
final CommandDescription description = new CommandDescription(name,
41+
'Display information about the given plugin',
42+
'grace plugin-info [PLUGIN NAME]'
43+
)
44+
45+
PluginInfoCommand() {
46+
description.argument(name: 'Plugin Name', description: 'The name of the plugin', required: false)
47+
}
48+
49+
@Override
50+
boolean handle(ExecutionContext executionContext) {
51+
GrailsConsole console = executionContext.console
52+
CommandLine commandLine = executionContext.commandLine
53+
String pluginName = commandLine.remainingArgs[0]
54+
55+
if (!pluginName) {
56+
console.error 'Missing the name of the plugin!'
57+
return false
58+
}
59+
60+
try {
61+
console.addStatus "Plugin Name: ${pluginName}"
62+
GPathResult mavenMetadata = new XmlSlurper().parseText(new URL("${PLUGIN_REPO_URL}/${pluginName}/maven-metadata.xml").text)
63+
String latestVersion = mavenMetadata.versioning.release.text()
64+
if (!latestVersion) {
65+
latestVersion = mavenMetadata.versioning.latest.text()
66+
}
67+
console.addStatus "Latest Version: ${latestVersion}"
68+
69+
List allVersions = mavenMetadata.versioning.versions.version*.text()
70+
console.addStatus "All Versions: ${allVersions?.reverse()?.join(', ')}"
71+
72+
GPathResult pluginInfo
73+
if (latestVersion.endsWith('-SNAPSHOT')) {
74+
GPathResult versionMetadata = new XmlSlurper().parseText(
75+
new URL("${PLUGIN_REPO_URL}/${pluginName}/${latestVersion}/maven-metadata.xml").text)
76+
String snapshotVersion = versionMetadata.version.text()
77+
pluginInfo = new XmlSlurper().parseText(
78+
new URL("${PLUGIN_REPO_URL}/${pluginName}/${latestVersion}/${pluginName}-${snapshotVersion}-plugin.xml").text)
79+
}
80+
else {
81+
pluginInfo = new XmlSlurper().parseText(
82+
new URL("${PLUGIN_REPO_URL}/${pluginName}/${latestVersion}/${pluginName}-${latestVersion}-plugin.xml").text)
83+
}
84+
85+
if (pluginInfo) {
86+
console.addStatus "Title: ${pluginInfo.title.text()}"
87+
String desc = pluginInfo.description.text()
88+
if (desc) {
89+
console.log('')
90+
console.log(desc)
91+
console.log('')
92+
}
93+
94+
console.log "* License: ${pluginInfo.license.text()}"
95+
96+
if (pluginInfo.documentation) {
97+
console.log "* Documentation: ${pluginInfo.documentation.text()}"
98+
}
99+
if (pluginInfo.issueManagement) {
100+
console.log "* Issue Tracker: ${pluginInfo.issueManagement.@url.text()}"
101+
}
102+
if (pluginInfo.scm) {
103+
console.log "* Source: ${pluginInfo.scm.@url.text()}"
104+
}
105+
106+
console.log """* Definition:
107+
108+
dependencies {
109+
implementation "org.graceframework.plugins:${pluginName}:${latestVersion}"
110+
}
111+
112+
"""
113+
}
114+
}
115+
catch(Throwable e) {
116+
console.error "Failed to display plugin info: ${e.message}", e
117+
return false
118+
}
119+
120+
true
121+
}
122+
123+
}

grace-shell/src/main/resources/META-INF/services/org.grails.cli.profile.Command

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ org.grails.cli.profile.commands.CreatePluginCommand
66
org.grails.cli.profile.commands.CreateProfileCommand
77
org.grails.cli.profile.commands.OpenCommand
88
org.grails.cli.profile.commands.HelpCommand
9+
org.grails.cli.profile.commands.ListPluginsCommand
910
org.grails.cli.profile.commands.ListProfilesCommand
11+
org.grails.cli.profile.commands.PluginInfoCommand
1012
org.grails.cli.profile.commands.ProfileInfoCommand
1113
org.grails.cli.gradle.commands.GradleCommand

0 commit comments

Comments
 (0)