Skip to content

Commit

Permalink
Miscellaneous cleanup (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Feb 15, 2023
1 parent 737bdbb commit 0838e05
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public static void cloneFromScm(
* <li><code>git checkout FETCH_HEAD</code>
* </ul>
*
* @param gitURL The git native URL, see the <a
* @param gitUrl The git native URL, see the <a
* href="https://git-scm.com/docs/git-clone#_git_urls">git documentation</a> for the
* supported syntax
* @param scmTag the tag or sha1 hash to clone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public Map<String, Object> action(Map<String, Object> moreInfo) {
File pluginDir = (File) moreInfo.get("pluginDir");
LOGGER.log(Level.INFO, "Executing node and node_modules cleanup hook");
compile(pluginDir);
return moreInfo;
} else {
LOGGER.log(Level.INFO, "Hook not triggered; continuing");
return moreInfo;
}
return moreInfo;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ public void run(
exitStatus = p.waitFor();
gobbler.join();
} catch (InterruptedException e) {
throw new PomExecutionException(cmd + " was interrupted", e);
throw new PomExecutionException(String.join(" ", cmd) + " was interrupted", e);
}
if (exitStatus != 0) {
throw new PomExecutionException(
cmd + " in " + baseDirectory + " failed with exit status " + exitStatus);
String.join(" ", cmd)
+ " in "
+ baseDirectory
+ " failed with exit status "
+ exitStatus);
}
}

Expand Down
89 changes: 46 additions & 43 deletions src/main/java/org/jenkins/tools/test/model/MavenPom.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import hudson.util.VersionNumber;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
Expand Down Expand Up @@ -87,48 +89,47 @@ public void transformPom(MavenCoordinates coreCoordinates) throws PomTransformat
File backupPom = new File(rootDir.getAbsolutePath() + "/" + pomFileName + ".backup");
try {
Files.move(pom.toPath(), backupPom.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

Document doc;
try {
doc = new SAXReader().read(backupPom);
} catch (DocumentException x) {
throw new IOException(x);
}

Element parent = doc.getRootElement().element("parent");
if (parent != null) {
Element groupIdElem = parent.element(GROUP_ID_ELEMENT);
if (groupIdElem != null) {
groupIdElem.setText(coreCoordinates.groupId);
}
Document doc;
try {
doc = new SAXReader().read(backupPom);
} catch (DocumentException x) {
throw new PomTransformationException("Failed to parse pom.xml", x);
}

Element artifactIdElem = parent.element(ARTIFACT_ID_ELEMENT);
if (artifactIdElem != null) {
artifactIdElem.setText(coreCoordinates.artifactId);
}
Element parent = doc.getRootElement().element("parent");
if (parent != null) {
Element groupIdElem = parent.element(GROUP_ID_ELEMENT);
if (groupIdElem != null) {
groupIdElem.setText(coreCoordinates.groupId);
}

Element versionIdElem = parent.element(VERSION_ELEMENT);
if (versionIdElem != null) {
versionIdElem.setText(coreCoordinates.version);
}
Element artifactIdElem = parent.element(ARTIFACT_ID_ELEMENT);
if (artifactIdElem != null) {
artifactIdElem.setText(coreCoordinates.artifactId);
}

writeDocument(pom, doc);
} catch (Exception e) {
throw new PomTransformationException(
"Error while transforming pom : " + pom.getAbsolutePath(), e);
Element versionIdElem = parent.element(VERSION_ELEMENT);
if (versionIdElem != null) {
versionIdElem.setText(coreCoordinates.version);
}
}

writeDocument(pom, doc);
}

/** Removes the dependency if it exists. */
public void removeDependency(@NonNull String groupId, @NonNull String artifactId)
throws IOException {
throws PomTransformationException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
throw new PomTransformationException("Failed to parse pom.xml", x);
}
Element dependencies = doc.getRootElement().element("dependencies");
if (dependencies == null) {
Expand Down Expand Up @@ -160,13 +161,13 @@ public void removeDependency(@NonNull String groupId, @NonNull String artifactId
* @param includeGroupId - specify if we want to add the groupId or not
*/
public void addPluginManagement(List<MavenCoordinates> pluginsToAdd, boolean includeGroupId)
throws IOException {
throws PomTransformationException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
throw new PomTransformationException("Failed to parse pom.xml", x);
}

Element build = doc.getRootElement().element("build");
Expand Down Expand Up @@ -199,13 +200,13 @@ public void addPluginManagement(List<MavenCoordinates> pluginsToAdd, boolean inc
}

/** Create/Update the properties section adding/updating some of them */
public void addProperties(Properties propertiesToAdd) throws IOException {
public void addProperties(Properties propertiesToAdd) throws PomTransformationException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
throw new PomTransformationException("Failed to parse pom.xml", x);
}

Element properties = doc.getRootElement().element("properties");
Expand All @@ -231,13 +232,13 @@ public void addDependencies(
Map<String, VersionNumber> toReplaceTest,
Map<String, String> pluginGroupIds,
List<String> toConvert)
throws IOException {
throws PomTransformationException {
File pom = new File(rootDir.getAbsolutePath() + "/" + pomFileName);
Document doc;
try {
doc = new SAXReader().read(pom);
} catch (DocumentException x) {
throw new IOException(x);
throw new PomTransformationException("Failed to parse pom.xml", x);
}
Element dependencies = doc.getRootElement().element("dependencies");
if (dependencies == null) {
Expand Down Expand Up @@ -404,22 +405,24 @@ private void addPlugins(
}
}

private void writeDocument(final File target, final Document doc) throws IOException {
Writer w = Files.newBufferedWriter(target.toPath(), getSafeCharset(doc));
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(w, format);
try {
writer.write(doc);
} finally {
writer.close();
w.close();
private void writeDocument(final File target, final Document doc) {
try (Writer w = Files.newBufferedWriter(target.toPath(), getSafeCharset(doc))) {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(w, format);
try {
writer.write(doc);
} finally {
writer.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static Charset getSafeCharset(final Document doc) {
try {
return Charset.forName(doc.getXMLEncoding());
} catch (Exception ex) {
} catch (UnsupportedCharsetException ex) {
return Charset.defaultCharset();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public PluginCompatTesterHooks(List<String> extraPrefixes) {
setupHooksByType();
}

public PluginCompatTesterHooks(List<String> extraPrefixes, List<File> externalJars)
throws MalformedURLException {
public PluginCompatTesterHooks(List<String> extraPrefixes, List<File> externalJars) {
this(extraPrefixes, externalJars, Collections.emptyList());
}

Expand Down Expand Up @@ -200,11 +199,8 @@ private Map<String, Queue<PluginCompatTesterHook>> findHooks(String stage) {
allForType.add(hook);
sortedHooks.put(plugin, allForType);
}
} catch (Exception ex) {
LOGGER.log(
Level.WARNING,
"Error when loading " + c.getName() + "; continuing",
ex);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error when loading " + c.getName(), e);
}
}
}
Expand Down

0 comments on commit 0838e05

Please sign in to comment.