|
| 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 | +} |
0 commit comments