|
| 1 | +/* |
| 2 | + * Copyright 2013-2025, Seqera Labs |
| 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 | + * http://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 | + |
| 17 | +package nextflow.plugin.util |
| 18 | + |
| 19 | +import java.util.regex.Matcher |
| 20 | +import java.util.regex.Pattern |
| 21 | + |
| 22 | +import groovy.transform.CompileStatic |
| 23 | +import groovy.util.logging.Slf4j |
| 24 | +import nextflow.exception.AbortOperationException |
| 25 | +import nextflow.extension.FilesEx |
| 26 | + |
| 27 | +@Slf4j |
| 28 | +@CompileStatic |
| 29 | +class PluginRefactor { |
| 30 | + |
| 31 | + private String pluginName |
| 32 | + |
| 33 | + private String orgName |
| 34 | + |
| 35 | + private File pluginDir |
| 36 | + |
| 37 | + private String pluginClassPrefix |
| 38 | + |
| 39 | + private File gradleSettingsFile |
| 40 | + |
| 41 | + private File gradleBuildFile |
| 42 | + |
| 43 | + private Map<String,String> tokenMapping = new HashMap<>() |
| 44 | + |
| 45 | + String getPluginName() { |
| 46 | + return pluginName |
| 47 | + } |
| 48 | + |
| 49 | + String getOrgName() { |
| 50 | + return orgName |
| 51 | + } |
| 52 | + |
| 53 | + File getPluginDir() { |
| 54 | + return pluginDir |
| 55 | + } |
| 56 | + |
| 57 | + PluginRefactor withPluginDir(File directory) { |
| 58 | + this.pluginDir = directory.absoluteFile.canonicalFile |
| 59 | + return this |
| 60 | + } |
| 61 | + |
| 62 | + PluginRefactor withPluginName(String name) { |
| 63 | + this.pluginName = normalizeToKebabCase(name) |
| 64 | + this.pluginClassPrefix = normalizeToClassName(name) |
| 65 | + if( pluginName.toLowerCase()=='plugin' ) |
| 66 | + throw new IllegalStateException("Invalid plugin name: '$name'") |
| 67 | + if( !pluginClassPrefix ) |
| 68 | + throw new IllegalStateException("Invalid plugin name: '$name'") |
| 69 | + return this |
| 70 | + } |
| 71 | + |
| 72 | + PluginRefactor withOrgName(String name) { |
| 73 | + this.orgName = normalizeToPackageNameSegment(name) |
| 74 | + if( !orgName ) |
| 75 | + throw new AbortOperationException("Invalid organization name: '$name'") |
| 76 | + return this |
| 77 | + } |
| 78 | + |
| 79 | + protected void init() { |
| 80 | + if( !pluginName ) |
| 81 | + throw new IllegalStateException("Missing plugin name") |
| 82 | + if( !orgName ) |
| 83 | + throw new IllegalStateException("Missing organization name") |
| 84 | + // initial |
| 85 | + this.gradleBuildFile = new File(pluginDir, 'build.gradle') |
| 86 | + this.gradleSettingsFile = new File(pluginDir, 'settings.gradle') |
| 87 | + if( !gradleBuildFile.exists() ) |
| 88 | + throw new AbortOperationException("Plugin file does not exist: $gradleBuildFile") |
| 89 | + if( !gradleSettingsFile.exists() ) |
| 90 | + throw new AbortOperationException("Plugin file does not exist: $gradleSettingsFile") |
| 91 | + if( !orgName ) |
| 92 | + throw new AbortOperationException("Plugin org name is missing") |
| 93 | + // packages to be updates |
| 94 | + tokenMapping.put('acme', orgName) |
| 95 | + tokenMapping.put('nf-plugin-template', pluginName) |
| 96 | + } |
| 97 | + |
| 98 | + void apply() { |
| 99 | + init() |
| 100 | + replacePrefixInFiles(pluginDir, pluginClassPrefix) |
| 101 | + renameDirectory(new File(pluginDir, "src/main/groovy/acme"), new File(pluginDir, "src/main/groovy/${orgName}")) |
| 102 | + renameDirectory(new File(pluginDir, "src/test/groovy/acme"), new File(pluginDir, "src/test/groovy/${orgName}")) |
| 103 | + updateClassNames(pluginDir) |
| 104 | + } |
| 105 | + |
| 106 | + protected void replacePrefixInFiles(File rootDir, String newPrefix) { |
| 107 | + if (!rootDir.exists() || !rootDir.isDirectory()) { |
| 108 | + throw new IllegalStateException("Invalid directory: $rootDir") |
| 109 | + } |
| 110 | + |
| 111 | + rootDir.eachFileRecurse { file -> |
| 112 | + if (file.isFile() && file.name.startsWith('My') && FilesEx.getExtension(file) in ['groovy']) { |
| 113 | + final newName = file.name.replaceFirst(/^My/, newPrefix) |
| 114 | + final renamedFile = new File(file.parentFile, newName) |
| 115 | + if (file.renameTo(renamedFile)) { |
| 116 | + log.debug "Renamed: ${file.name} -> ${renamedFile.name}" |
| 117 | + final source = FilesEx.getBaseName(file) |
| 118 | + final target = FilesEx.getBaseName(renamedFile) |
| 119 | + tokenMapping.put(source, target) |
| 120 | + } |
| 121 | + else { |
| 122 | + throw new IllegalStateException("Failed to rename: ${file.name}") |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + protected void updateClassNames(File rootDir) { |
| 129 | + rootDir.eachFileRecurse { file -> |
| 130 | + if (file.isFile() && FilesEx.getExtension(file) in ['groovy','gradle']) { |
| 131 | + replaceTokensInFile(file, tokenMapping) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected void replaceTokensInFile(File inputFile, Map<String, String> replacements, File outputFile = inputFile) { |
| 137 | + def content = inputFile.text |
| 138 | + |
| 139 | + // Replace each key with its corresponding value |
| 140 | + for( Map.Entry<String,String> entry : replacements ) { |
| 141 | + content = content.replaceAll(Pattern.quote(entry.key), Matcher.quoteReplacement(entry.value)) |
| 142 | + } |
| 143 | + |
| 144 | + outputFile.text = content |
| 145 | + log.debug "Replacements done in: ${outputFile.path}" |
| 146 | + } |
| 147 | + |
| 148 | + protected void renameDirectory(File oldDir, File newDir) { |
| 149 | + if (!oldDir.exists() || !oldDir.isDirectory()) { |
| 150 | + throw new AbortOperationException("Plugin template directory to rename does not exist: $oldDir") |
| 151 | + } |
| 152 | + |
| 153 | + if( oldDir==newDir ) { |
| 154 | + log.debug "Unneeded path rename: $oldDir -> $newDir" |
| 155 | + } |
| 156 | + |
| 157 | + if (newDir.exists()) { |
| 158 | + throw new AbortOperationException("Plugin target directory already exists: $newDir") |
| 159 | + } |
| 160 | + |
| 161 | + if (oldDir.renameTo(newDir)) { |
| 162 | + log.debug "Successfully renamed: $oldDir -> $newDir" |
| 163 | + } |
| 164 | + else { |
| 165 | + throw new AbortOperationException("Unable to replace plugin template path: $oldDir -> $newDir") |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static String normalizeToClassName(String input) { |
| 170 | + // Replace non-alphanumeric characters with spaces (except underscores) |
| 171 | + final cleaned = input.replaceAll(/[^a-zA-Z0-9_]/, ' ') |
| 172 | + .replaceAll(/_/, ' ') |
| 173 | + .trim() |
| 174 | + // Split by whitespace, capitalize each word, join them |
| 175 | + final parts = cleaned.split(/\s+/).collect { it.capitalize() } |
| 176 | + return parts.join('').replace('Plugin','') |
| 177 | + } |
| 178 | + |
| 179 | + static String normalizeToKebabCase(String input) { |
| 180 | + // Insert spaces before capital letters (handles CamelCase) |
| 181 | + def spaced = input.replaceAll(/([a-z])([A-Z])/, '$1 $2') |
| 182 | + .replaceAll(/([A-Z]+)([A-Z][a-z])/, '$1 $2') |
| 183 | + // Replace non-alphanumeric characters and underscores with spaces |
| 184 | + def cleaned = spaced.replaceAll(/[^a-zA-Z0-9]/, ' ') |
| 185 | + .trim() |
| 186 | + // Split, lowercase, and join with hyphens |
| 187 | + def parts = cleaned.split(/\s+/).collect { it.toLowerCase() } |
| 188 | + return parts.join('-') |
| 189 | + } |
| 190 | + |
| 191 | + static String normalizeToPackageNameSegment(String input) { |
| 192 | + // Replace non-alphanumeric characters with spaces |
| 193 | + def cleaned = input.replaceAll(/[^a-zA-Z0-9]/, ' ') |
| 194 | + .trim() |
| 195 | + // Split into lowercase words and join |
| 196 | + def parts = cleaned.split(/\s+/).collect { it.toLowerCase() } |
| 197 | + def name = parts.join('') |
| 198 | + |
| 199 | + // Strip leading digits |
| 200 | + name = name.replaceFirst(/^\d+/, '') |
| 201 | + return name ?: null |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments