|
1 | 1 | /******************************************************************************** |
2 | | - * Copyright (c) 2016, 2018 GK Software SE and others. |
3 | | - * |
| 2 | + * Copyright (c) 2016, 2025 GK Software SE and others. |
| 3 | + * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | 6 | * which accompanies this distribution, and is available at |
|
10 | 10 | * |
11 | 11 | * Contributors: |
12 | 12 | * Stephan Herrmann - initial implementation |
| 13 | + * Hannes Wellmann - Simplify and remove now unncessary elements |
13 | 14 | ********************************************************************************/ |
14 | 15 | package org.eclipse.platform.releng.maven.pom; |
15 | 16 |
|
16 | 17 | import java.io.InputStream; |
17 | | -import java.net.URL; |
18 | | -import java.util.HashMap; |
| 18 | +import java.net.URI; |
19 | 19 | import java.util.HashSet; |
20 | | -import java.util.Map; |
| 20 | +import java.util.Objects; |
21 | 21 | import java.util.Set; |
22 | 22 | import java.util.regex.Matcher; |
23 | 23 | import java.util.regex.Pattern; |
24 | 24 |
|
25 | | -public class ArtifactInfo { |
| 25 | +public record ArtifactInfo(String bsn, String scmConnection) { |
26 | 26 |
|
27 | 27 | private static final String SCM_TAG_START = ";tag=\""; // git tag inside Eclipse-SourceReference |
28 | | - |
29 | 28 | private static final String INDENT = " "; |
30 | | - |
31 | | - private static final Map<String, String> MOVED_SCMS = new HashMap<>(); |
32 | | - |
33 | 29 | private static final Pattern GITHUB_PATTERN = Pattern.compile("https://github.com/([^/]+)/.*"); |
34 | 30 |
|
35 | | - static { |
36 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.releng", |
37 | | - "https://github.com/eclipse-platform/eclipse.platform.releng"); |
38 | | - MOVED_SCMS.put("https://git.eclipse.org/r/platform/eclipse.platform.releng", |
39 | | - "https://github.com/eclipse-platform/eclipse.platform.releng"); |
40 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.ui", |
41 | | - "https://github.com/eclipse-platform/eclipse.platform.ui"); |
42 | | - MOVED_SCMS.put("https://git.eclipse.org/r/platform/eclipse.platform.ui", |
43 | | - "https://github.com/eclipse-platform/eclipse.platform.ui"); |
44 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.text", |
45 | | - "https://github.com/eclipse-platform/eclipse.platform.text"); |
46 | | - MOVED_SCMS.put("https://git.eclipse.org/r/platform/eclipse.platform.text", |
47 | | - "https://github.com/eclipse-platform/eclipse.platform.text"); |
48 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.debug", |
49 | | - "https://github.com/eclipse-platform/eclipse.platform.debug"); |
50 | | - MOVED_SCMS.put("http://git.eclipse.org/gitroot/e4/org.eclipse.e4.tools", |
51 | | - "https://github.com/eclipse-platform/eclipse.platform.ui.tools"); |
52 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.ua", |
53 | | - "https://github.com/eclipse-platform/eclipse.platform.ua"); |
54 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/platform/eclipse.platform.swt", |
55 | | - "https://github.com/eclipse-platform/eclipse.platform.swt"); |
56 | | - |
57 | | - MOVED_SCMS.put("https://gihub.com/eclipse-platform/eclipse.platform.ua", |
58 | | - "https://github.com/eclipse-platform/eclipse.platform.ua"); |
59 | | - MOVED_SCMS.put("https://github.com/eclipse-platform/org.eclipse.ui.tools", |
60 | | - "https://github.com/eclipse-platform/eclipse.platform.ui.tools"); |
61 | | - |
62 | | - MOVED_SCMS.put("https://git.eclipse.org/r/equinox/rt.equinox.bundles", "https://github.com/eclipse-equinox/p2"); |
63 | | - MOVED_SCMS.put("https://git.eclipse.org/r/equinox/rt.equinox.p2", "https://github.com/eclipse-equinox/p2"); |
64 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/equinox/rt.equinox.p2", "https://github.com/eclipse-equinox/p2"); |
65 | | - MOVED_SCMS.put("https://git.eclipse.org/r/equinox/rt.equinox.framework", |
66 | | - "https://github.com/eclipse-equinox/equinox"); |
67 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/equinox/rt.equinox.bundles", |
68 | | - "https://github.com/eclipse-equinox/equinox"); |
69 | | - |
70 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/jdt/eclipse.jdt.core.binaries", |
71 | | - "https://github.com/eclipse-jdt/eclipse.jdt.core.binaries"); |
72 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/jdt/eclipse.jdt.ui", |
73 | | - "https://github.com/eclipse-jdt/eclipse.jdt.ui"); |
74 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/jdt/eclipse.jdt.core", |
75 | | - "https://github.com/eclipse-jdt/eclipse.jdt.core"); |
76 | | - MOVED_SCMS.put("https://git.eclipse.org/r/jdt/eclipse.jdt.core", |
77 | | - "https://github.com/eclipse-jdt/eclipse.jdt.core"); |
78 | | - |
79 | | - MOVED_SCMS.put("git://git.eclipse.org/gitroot/pde/eclipse.pde.ui", |
80 | | - "https://github.com/eclipse-pde/eclipse.pde"); |
81 | | - MOVED_SCMS.put("https://git.eclipse.org/r/pde/eclipse.pde.ui", "https://github.com/eclipse-pde/eclipse.pde"); |
82 | | - } |
83 | | - |
84 | | - public String bsn; |
85 | | - public String scmConnection; |
86 | | - |
87 | | - @Override |
88 | | - public String toString() { |
89 | | - return "ArtifactInfo [bsn=" + bsn + ", scmConnection=" + scmConnection + "]"; |
| 31 | + public ArtifactInfo { |
| 32 | + Objects.requireNonNull(bsn); |
| 33 | + Objects.requireNonNull(scmConnection); |
90 | 34 | } |
91 | 35 |
|
92 | | - public String toPomFragment() { |
| 36 | + String toPomFragment() { |
93 | 37 | try { |
94 | | - fixData(); |
95 | 38 | StringBuilder buf = new StringBuilder(); |
96 | | - String indent = INDENT; |
97 | | - element("url", indent, buf, getProjectURL()); |
98 | | - if (this.scmConnection == null) { |
99 | | - System.err.println("No scm info for " + this.bsn); |
100 | | - } else { |
101 | | - String connectionUrl = extractScmConnection(); |
102 | | - String url = extractScmUrl(connectionUrl); |
103 | | - buf.append(getFrontMatter(url)); |
104 | | - element("scm", indent, buf, subElement("connection", connectionUrl), subElement("tag", extractScmTag()), |
105 | | - subElement("url", url)); |
106 | | - Developer.addUrlDevelopers(getProjectURL(), indent, buf); |
107 | | - } |
| 39 | + String projectURL = getProjectURL(); |
| 40 | + element("url", INDENT, buf, projectURL); |
| 41 | + String url = extractScmUrl(); |
| 42 | + buf.append(getFrontMatter(url)); |
| 43 | + element("scm", INDENT, buf, subElement("connection", extractScmConnection()), |
| 44 | + subElement("tag", extractScmTag()), subElement("url", url)); |
| 45 | + addUrlDevelopers(projectURL, INDENT, buf); |
108 | 46 | return buf.toString(); |
109 | 47 | } catch (RuntimeException e) { |
110 | | - System.err.println("Failed for " + this); |
111 | | - throw e; |
| 48 | + throw new IllegalStateException("Failed for " + this, e); |
112 | 49 | } |
113 | 50 | } |
114 | 51 |
|
115 | 52 | private static String getFrontMatter(String scmURL) { |
116 | | - String FRONT_MATTER = "" + // |
117 | | - " <licenses>\n" + // |
118 | | - " <license>\n" + // |
119 | | - " <name>Eclipse Public License - v 2.0</name>\n" + // |
120 | | - " <url>https://www.eclipse.org/legal/epl-2.0/</url>\n" + // |
121 | | - " <distribution>repo</distribution>\n" + // |
122 | | - " </license>\n" + // |
123 | | - " </licenses>\n" + // |
124 | | - " <organization>\n" + // |
125 | | - " <name>Eclipse Foundation</name>\n" + // |
126 | | - " <url>https://www.eclipse.org/</url>\n" + // |
127 | | - " </organization>\n" + // |
128 | | - " <issueManagement>\n" + // |
129 | | - " <system>Github</system>\n" + // |
130 | | - " <url>" + scmURL + "/issues</url>\n" + // |
131 | | - " </issueManagement>\n"; // |
132 | | - return FRONT_MATTER; |
| 53 | + return String.format(""" |
| 54 | + <licenses> |
| 55 | + <license> |
| 56 | + <name>Eclipse Public License - v 2.0</name> |
| 57 | + <url>https://www.eclipse.org/legal/epl-2.0/</url> |
| 58 | + <distribution>repo</distribution> |
| 59 | + </license> |
| 60 | + </licenses> |
| 61 | + <organization> |
| 62 | + <name>Eclipse Foundation</name> |
| 63 | + <url>https://www.eclipse.org/</url> |
| 64 | + </organization> |
| 65 | + <issueManagement> |
| 66 | + <system>Github</system> |
| 67 | + <url>%s/issues</url> |
| 68 | + </issueManagement> |
| 69 | + """, scmURL); |
133 | 70 | } |
134 | 71 |
|
135 | | - private void fixData() { |
136 | | - if (this.scmConnection == null) { |
137 | | - if (this.bsn.equals("org.eclipse.jdt.core.compiler.batch")) { |
138 | | - // not a regular OSGi bundle, scm info missing: |
139 | | - this.scmConnection = "scm:git:https://github.com/eclipse-jdt/eclipse.jdt.core.git;path=\"org.eclipse.jdt.core\""; |
140 | | - System.out.println("Fixed scmUrl for " + this.bsn); |
141 | | - } |
142 | | - } else { |
143 | | - this.scmConnection = "scm:git:" + extractScmUrl(extractScmConnection()) + ".git"; |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - String getProjectURL() { |
148 | | - String scmURL = extractScmUrl(extractScmConnection()); |
| 72 | + private String getProjectURL() { |
| 73 | + String scmURL = extractScmUrl(); |
149 | 74 | Matcher matcher = GITHUB_PATTERN.matcher(scmURL); |
150 | 75 | if (matcher.matches()) { |
151 | 76 | String organization = matcher.group(1); |
152 | 77 | return "https://projects.eclipse.org/projects/" + organization.replace('-', '.'); |
153 | 78 | } |
154 | | - |
155 | | - return "https://projects.eclipse.org/projects/"; |
| 79 | + throw new IllegalArgumentException("Unexpected scm-URL: " + scmURL); |
156 | 80 | } |
157 | 81 |
|
158 | | - String extractScmConnection() { |
159 | | - int semi = this.scmConnection.indexOf(';'); |
160 | | - if (semi == -1) |
161 | | - return this.scmConnection; |
162 | | - return this.scmConnection.substring(0, semi); |
| 82 | + private String extractScmConnection() { |
| 83 | + return substringTo(this.scmConnection, 0, ';'); |
163 | 84 | } |
164 | 85 |
|
165 | | - String extractScmTag() { |
| 86 | + private String extractScmTag() { |
166 | 87 | int tagStart = this.scmConnection.indexOf(SCM_TAG_START); |
167 | | - if (tagStart == -1) |
168 | | - return null; |
169 | | - int next = this.scmConnection.indexOf("\"", tagStart + SCM_TAG_START.length()); |
170 | | - if (next == -1) |
171 | | - next = this.scmConnection.length(); |
172 | | - return this.scmConnection.substring(tagStart + SCM_TAG_START.length(), next); |
| 88 | + if (tagStart == -1) { |
| 89 | + throw new IllegalArgumentException("scm-tag not found in " + this.scmConnection); |
| 90 | + } |
| 91 | + return substringTo(this.scmConnection, tagStart + SCM_TAG_START.length(), '"'); |
| 92 | + } |
| 93 | + |
| 94 | + private static void addUrlDevelopers(String projectURL, String indent, StringBuilder buf) { |
| 95 | + StringBuilder subElements = new StringBuilder(); |
| 96 | + element("developer", "", subElements, subElement("url", projectURL + "/who")); |
| 97 | + element("developers", indent, buf, subElements.toString()); |
173 | 98 | } |
174 | 99 |
|
175 | 100 | private static final Set<String> visitedSCMs = new HashSet<>(); |
176 | 101 |
|
177 | | - String extractScmUrl(String connection) { |
178 | | - String scmURL = connection.replaceAll("^scm:git:", "").replaceAll("^scm:githttps:", "https:") |
179 | | - .replaceAll("\\.git$", ""); |
180 | | - String movedSCMURL = MOVED_SCMS.get(scmURL); |
181 | | - if (movedSCMURL != null) { |
182 | | - scmURL = movedSCMURL; |
183 | | - } |
| 102 | + private String extractScmUrl() { |
| 103 | + String connection = extractScmConnection(); |
| 104 | + String scmURL = connection.replaceFirst("^scm:git:", "").replaceFirst("^scm:githttps:", "https:") |
| 105 | + .replaceFirst("\\.git$", ""); |
184 | 106 | if (EnrichPoms.test && visitedSCMs.add(scmURL)) { |
185 | | - try (InputStream in = new URL(scmURL).openStream()) { |
| 107 | + try (InputStream in = new URI(scmURL).toURL().openStream()) { |
186 | 108 | } catch (Exception ex) { |
187 | 109 | ex.printStackTrace(); |
188 | 110 | } |
189 | 111 | } |
190 | 112 | return scmURL; |
191 | 113 | } |
192 | 114 |
|
193 | | - public static void element(String tag, String indent, StringBuilder buf, String... contents) { |
| 115 | + private static String substringTo(String string, int beginIndex, char endChar) { |
| 116 | + int endIndex = string.indexOf(endChar, beginIndex); |
| 117 | + return endIndex == -1 ? string.substring(beginIndex) : string.substring(beginIndex, endIndex); |
| 118 | + } |
| 119 | + |
| 120 | + private static void element(String tag, String indent, StringBuilder buf, String... contents) { |
194 | 121 | buf.append(indent).append('<').append(tag).append('>'); |
195 | 122 | if (contents.length == 1 && !contents[0].contains("\n")) { |
196 | 123 | buf.append(contents[0]); |
197 | 124 | } else { |
198 | 125 | buf.append("\n"); |
199 | | - for (String content : contents) |
200 | | - if (content != null) |
201 | | - for (String line : content.split("\\n")) |
202 | | - buf.append(indent).append(INDENT).append(line).append('\n'); |
| 126 | + for (String content : contents) { |
| 127 | + content.lines().forEach(line -> buf.append(indent).append(INDENT).append(line).append('\n')); |
| 128 | + } |
203 | 129 | buf.append(indent); |
204 | 130 | } |
205 | 131 | buf.append("</").append(tag).append(">\n"); |
206 | 132 | } |
207 | 133 |
|
208 | | - public static String subElement(String tag, String content) { |
209 | | - if (content == null) |
210 | | - return null; |
| 134 | + private static String subElement(String tag, String content) { |
| 135 | + Objects.requireNonNull(content); |
211 | 136 | StringBuilder buf = new StringBuilder(); |
212 | 137 | element(tag, "", buf, content); |
213 | 138 | return buf.toString(); |
|
0 commit comments