|
| 1 | +/******************************************************************************* |
| 2 | + * SysML 2 Pilot Implementation |
| 3 | + * Copyright (c) 2026 Sensmetry, UAB. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the Eclipse Public License as published by |
| 7 | + * the Eclipse Foundation, version 2 of the License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * Eclipse Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Eclipse Public License |
| 15 | + * along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>. |
| 16 | + * |
| 17 | + * @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0> |
| 18 | + * |
| 19 | + *******************************************************************************/ |
| 20 | + |
| 21 | +package org.omg.sysml.xtext.util; |
| 22 | + |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | + |
| 26 | +import com.sensmetry.sysand.Sysand; |
| 27 | +import com.sensmetry.sysand.exceptions.SysandException; |
| 28 | +import com.sensmetry.sysand.model.InterchangeProject; |
| 29 | +import com.sensmetry.sysand.model.InterchangeProjectInfo; |
| 30 | +import com.sensmetry.sysand.model.InterchangeProjectMetadata; |
| 31 | +import com.sensmetry.sysand.model.InterchangeProjectUsage; |
| 32 | + |
| 33 | +public class StdLibVersionUtil { |
| 34 | + |
| 35 | + private static final String DEV_SEPARATOR = "-dev."; |
| 36 | + |
| 37 | + /** Strips any existing -dev.* suffix, then appends -dev.<buildTag>. */ |
| 38 | + static String withBuildTag(String version, String buildTag) { |
| 39 | + int idx = version.indexOf(DEV_SEPARATOR); |
| 40 | + String base = idx >= 0 ? version.substring(0, idx) : version; |
| 41 | + return base + DEV_SEPARATOR + buildTag; |
| 42 | + } |
| 43 | + |
| 44 | + /** Strips any existing -dev.* suffix. */ |
| 45 | + static String stripBuildTag(String version) { |
| 46 | + int idx = version.indexOf(DEV_SEPARATOR); |
| 47 | + return idx >= 0 ? version.substring(0, idx) : version; |
| 48 | + } |
| 49 | + |
| 50 | + /** Applies build tag to a versionConstraint like "=1.0.0" → "=1.0.0-dev.<tag>". */ |
| 51 | + static String withBuildTagConstraint(String constraint, String buildTag) { |
| 52 | + int start = 0; |
| 53 | + while (start < constraint.length() && !Character.isDigit(constraint.charAt(start))) { |
| 54 | + start++; |
| 55 | + } |
| 56 | + return constraint.substring(0, start) + withBuildTag(constraint.substring(start), buildTag); |
| 57 | + } |
| 58 | + |
| 59 | + /** Strips any existing -dev.* suffix from a versionConstraint like "=1.0.0-dev.<tag>" → "=1.0.0". */ |
| 60 | + static String stripBuildTagConstraint(String constraint) { |
| 61 | + int start = 0; |
| 62 | + while (start < constraint.length() && !Character.isDigit(constraint.charAt(start))) { |
| 63 | + start++; |
| 64 | + } |
| 65 | + return constraint.substring(0, start) + stripBuildTag(constraint.substring(start)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Replaces the date segment in a spec resource URL with buildTag. |
| 70 | + * URL structure: https://www.omg.org/spec/{lang}/{date}/{name}.kpar |
| 71 | + * Splitting by "/" yields the date at index 5. |
| 72 | + */ |
| 73 | + static String withBuildTagResource(String resource, String buildTag) { |
| 74 | + String[] parts = resource.split("/"); |
| 75 | + if (parts.length >= 7) { |
| 76 | + parts[5] = buildTag; |
| 77 | + } |
| 78 | + return String.join("/", parts); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Replaces the date segment in a spec metamodel URL with buildTag. |
| 83 | + * URL structure: https://www.omg.org/spec/{lang}/{date} |
| 84 | + * Splitting by "/" yields the date at index 5. |
| 85 | + */ |
| 86 | + static String withBuildTagMetamodelUrl(String url, String buildTag) { |
| 87 | + String[] parts = url.split("/"); |
| 88 | + if (parts.length >= 6) { |
| 89 | + parts[5] = buildTag; |
| 90 | + } |
| 91 | + return String.join("/", parts); |
| 92 | + } |
| 93 | + |
| 94 | + static void updateProject(Path projectPath, String buildTag, String metamodelTag) throws SysandException { |
| 95 | + InterchangeProject project = Sysand.infoPath(projectPath); |
| 96 | + InterchangeProjectInfo info = project.info; |
| 97 | + |
| 98 | + if (info.getVersion() != null) { |
| 99 | + info.setVersion(buildTag != null |
| 100 | + ? withBuildTag(info.getVersion(), buildTag) |
| 101 | + : stripBuildTag(info.getVersion())); |
| 102 | + } |
| 103 | + |
| 104 | + InterchangeProjectUsage[] usages = info.getUsage(); |
| 105 | + for (InterchangeProjectUsage usage : usages) { |
| 106 | + if (buildTag != null && usage.getResource() != null) { |
| 107 | + usage.setResource(withBuildTagResource(usage.getResource(), buildTag)); |
| 108 | + } |
| 109 | + if (usage.getVersionConstraint() != null) { |
| 110 | + usage.setVersionConstraint(buildTag != null |
| 111 | + ? withBuildTagConstraint(usage.getVersionConstraint(), buildTag) |
| 112 | + : stripBuildTagConstraint(usage.getVersionConstraint())); |
| 113 | + } |
| 114 | + } |
| 115 | + info.setUsage(usages); |
| 116 | + |
| 117 | + Sysand.setProjectInfo(projectPath, info); |
| 118 | + |
| 119 | + InterchangeProjectMetadata metadata = project.metadata; |
| 120 | + if (metadata != null && metadata.getMetamodel() != null && metamodelTag != null) { |
| 121 | + metadata.setMetamodel(withBuildTagMetamodelUrl(metadata.getMetamodel(), metamodelTag)); |
| 122 | + Sysand.setProjectMetadata(projectPath, metadata); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public static void updateWorkspace(String workspacePath, String buildTag, String metamodelTag) throws SysandException { |
| 127 | + String[] projectPaths = Sysand.workspaceProjectPaths(Paths.get(workspacePath)); |
| 128 | + for (String projectPath : projectPaths) { |
| 129 | + System.out.println(" Updating: " + projectPath); |
| 130 | + updateProject(Paths.get(projectPath), buildTag, metamodelTag); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static boolean isMavenPlaceholder(String value) { |
| 135 | + return value != null && value.startsWith("${"); |
| 136 | + } |
| 137 | + |
| 138 | + private static String parseTag(String value) { |
| 139 | + return (value != null && !value.isEmpty() && !isMavenPlaceholder(value)) ? value : null; |
| 140 | + } |
| 141 | + |
| 142 | + private static void printUsage() { |
| 143 | + System.err.println("Usage: StdLibVersionUtil --workspace WORKSPACE_PATH"); |
| 144 | + System.err.println(" [--build-tag BUILD_TAG] [--metamodel-tag METAMODEL_TAG]"); |
| 145 | + System.err.println(); |
| 146 | + System.err.println(" --workspace Path to the workspace directory containing .workspace.json (required)"); |
| 147 | + System.err.println(" --build-tag Stamp -dev.<tag> onto version, versionConstraint, and resource"); |
| 148 | + System.err.println(" URLs in each project's .project.json"); |
| 149 | + System.err.println(" --metamodel-tag Replace the date segment of the metamodel URL in each project's"); |
| 150 | + System.err.println(" .meta.json"); |
| 151 | + System.err.println(" --help Show this message"); |
| 152 | + System.err.println(); |
| 153 | + System.err.println("At least one of --build-tag or --metamodel-tag must be provided."); |
| 154 | + } |
| 155 | + |
| 156 | + public static void main(String[] args) throws SysandException { |
| 157 | + String workspacePath = null; |
| 158 | + String buildTag = null; |
| 159 | + String metamodelTag = null; |
| 160 | + String rawBuildTag = null; |
| 161 | + String rawMetamodelTag = null; |
| 162 | + |
| 163 | + for (int i = 0; i < args.length; i++) { |
| 164 | + switch (args[i]) { |
| 165 | + case "--help": |
| 166 | + printUsage(); |
| 167 | + return; |
| 168 | + case "--workspace": |
| 169 | + if (++i >= args.length) { printUsage(); return; } |
| 170 | + workspacePath = args[i]; |
| 171 | + break; |
| 172 | + case "--build-tag": |
| 173 | + if (++i >= args.length) { printUsage(); return; } |
| 174 | + rawBuildTag = args[i]; |
| 175 | + buildTag = parseTag(args[i]); |
| 176 | + break; |
| 177 | + case "--metamodel-tag": |
| 178 | + if (++i >= args.length) { printUsage(); return; } |
| 179 | + rawMetamodelTag = args[i]; |
| 180 | + metamodelTag = parseTag(args[i]); |
| 181 | + break; |
| 182 | + default: |
| 183 | + System.err.println("Unknown argument: " + args[i]); |
| 184 | + printUsage(); |
| 185 | + return; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (workspacePath == null) { |
| 190 | + System.err.println("Error: --workspace is required."); |
| 191 | + printUsage(); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if (buildTag == null && metamodelTag == null) { |
| 196 | + if (isMavenPlaceholder(rawBuildTag) || isMavenPlaceholder(rawMetamodelTag)) { |
| 197 | + System.out.println("Skipping: both --build-tag and --metamodel-tag are Maven placeholders."); |
| 198 | + return; |
| 199 | + } |
| 200 | + System.err.println("Error: at least one of --build-tag or --metamodel-tag must be provided."); |
| 201 | + printUsage(); |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + if (buildTag != null) { |
| 206 | + System.out.println("Applying build tag '" + buildTag + "' to .project.json files in: " + workspacePath); |
| 207 | + } |
| 208 | + if (metamodelTag != null) { |
| 209 | + System.out.println("Applying metamodel tag '" + metamodelTag + "' to .meta.json files in: " + workspacePath); |
| 210 | + } |
| 211 | + updateWorkspace(workspacePath, buildTag, metamodelTag); |
| 212 | + System.out.println("Done."); |
| 213 | + } |
| 214 | +} |
0 commit comments