From 11e0723597c7985dd310ff2a22f293a755ec1ae9 Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 3 Dec 2024 19:09:36 -0700 Subject: [PATCH 1/3] Modernize the plugin Modernize the plugin with updates for JDK 17 and new parent pom/bom versions. --- pom.xml | 13 +-- .../ChangeAssemblyVersion.java | 22 ++--- .../changeassemblyversion/ChangeTools.java | 6 +- .../ReplacementsTest.java | 91 ++++++++++--------- 4 files changed, 69 insertions(+), 63 deletions(-) diff --git a/pom.xml b/pom.xml index 643a91c..9c623c3 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jenkins-ci.plugins plugin - 4.88 + 5.3 @@ -11,7 +11,8 @@ 1.11 -SNAPSHOT jenkinsci/change-assembly-version-plugin - 2.361.4 + 2.479 + ${jenkins.baseline}.1 Max false Low @@ -19,7 +20,7 @@ scm:git:https://github.com/${gitHubRepo}.git - scm:git:ssh://git@github.com/${gitHubRepo}.git + scm:git:git@github.com:${gitHubRepo}.git https://github.com/${gitHubRepo} ${scmTag} @@ -57,13 +58,13 @@ test - + io.jenkins.tools.bom - bom-2.361.x - 2102.v854b_fec19c92 + bom-2.479.x + 3761.vd922730f0fd2 import pom diff --git a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java index 132d42d..2e084a9 100644 --- a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java +++ b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java @@ -208,8 +208,8 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi EnvVars envVars = run.getEnvironment(listener); - if (run instanceof AbstractBuild) { - envVars.overrideAll(((AbstractBuild) run).getBuildVariables()); + if (run instanceof AbstractBuild build) { + envVars.overrideAll(build.getBuildVariables()); } String version = new AssemblyVersion(this.versionPattern, envVars).getVersion(); @@ -230,15 +230,15 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi // Log new expanded values - listener.getLogger().println(String.format("Changing File(s): %s", assemblyGlob)); - listener.getLogger().println(String.format("Assembly Version : %s", version)); - listener.getLogger().println(String.format("Assembly Title : %s", assemblyTitle)); - listener.getLogger().println(String.format("Assembly Description : %s", assemblyDescription)); - listener.getLogger().println(String.format("Assembly Company : %s", assemblyCompany)); - listener.getLogger().println(String.format("Assembly Product : %s", assemblyProduct)); - listener.getLogger().println(String.format("Assembly Copyright : %s", assemblyCopyright)); - listener.getLogger().println(String.format("Assembly Trademark : %s", assemblyTrademark)); - listener.getLogger().println(String.format("Assembly Culture : %s", assemblyCulture)); + listener.getLogger().println("Changing File(s): %s".formatted(assemblyGlob)); + listener.getLogger().println("Assembly Version : %s".formatted(version)); + listener.getLogger().println("Assembly Title : %s".formatted(assemblyTitle)); + listener.getLogger().println("Assembly Description : %s".formatted(assemblyDescription)); + listener.getLogger().println("Assembly Company : %s".formatted(assemblyCompany)); + listener.getLogger().println("Assembly Product : %s".formatted(assemblyProduct)); + listener.getLogger().println("Assembly Copyright : %s".formatted(assemblyCopyright)); + listener.getLogger().println("Assembly Trademark : %s".formatted(assemblyTrademark)); + listener.getLogger().println("Assembly Culture : %s".formatted(assemblyCulture)); for (FilePath f : workspace.list(assemblyGlob)) { diff --git a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java index b9b3d92..e3f431c 100644 --- a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java +++ b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java @@ -50,8 +50,8 @@ public void Replace(String replacement, TaskListener listener) throws IOExceptio finally { inputStream.close(); } - listener.getLogger().println(String.format("Updating file : %s, Replacement : %s", file.getRemote(), replacement)); - content = content.replaceAll(regexPattern, String.format(replacementPattern, replacement)); + listener.getLogger().println("Updating file : %s, Replacement : %s".formatted(file.getRemote(), replacement)); + content = content.replaceAll(regexPattern, replacementPattern.formatted(replacement)); //listener.getLogger().println(String.format("Updating file : %s", file.getRemote())); OutputStream os = file.write(); try { @@ -65,7 +65,7 @@ public void Replace(String replacement, TaskListener listener) throws IOExceptio } else { - listener.getLogger().println(String.format("Skipping replacement because value is empty.")); + listener.getLogger().println("Skipping replacement because value is empty.".formatted()); } } } diff --git a/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java b/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java index 9f3809c..fa482b9 100644 --- a/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java +++ b/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java @@ -73,16 +73,17 @@ public void testResolveEnvironmentVariables() throws InterruptedException, IOExc project.getBuildersList().add(new TestBuilder() { public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { - build.getWorkspace().child("AssemblyVersion.cs").write("using System.Reflection;\n" + -"\n" + -"[assembly: AssemblyTitle(\"\")]\n" + -"[assembly: AssemblyDescription(\"\")]\n" + -"[assembly: AssemblyCompany(\"\")]\n" + -"[assembly: AssemblyProduct(\"\")]\n" + -"[assembly: AssemblyCopyright(\"\")]\n" + -"[assembly: AssemblyTrademark(\"\")]\n" + -"[assembly: AssemblyCulture(\"\")]\n" + -"[assembly: AssemblyVersion(\"13.1.1.976\")]", "UTF-8"); + build.getWorkspace().child("AssemblyVersion.cs").write(""" +using System.Reflection; + +[assembly: AssemblyTitle("")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: AssemblyVersion("13.1.1.976")]""", "UTF-8"); return true; } }); @@ -116,9 +117,10 @@ public void testResolveEnvironmentVariables_recursively_excludingSvn() throws In FreeStyleProject project = j.createFreeStyleProject(); final String f1 = "myassembly/properties/AssemblyInfo.cs"; final String f2 = ".svn/myassembly/properties/AssemblyInfo.cs"; - final String c = "using System.Reflection;\n" + -"\n" + -"[assembly: AssemblyVersion(\"13.1.1.976\")]"; + final String c = """ +using System.Reflection; + +[assembly: AssemblyVersion("13.1.1.976")]"""; project.getBuildersList().add(new TestBuilder() { public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { @@ -150,16 +152,17 @@ public boolean perform(AbstractBuild build, Launcher launcher, outputStream.write(fileBom.getBytes()); OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8"); try{ - w.write("using System.Reflection;\n" + - "\n" + - "[assembly: AssemblyTitle(\"\")]\n" + - "[assembly: AssemblyDescription(\"\")]\n" + - "[assembly: AssemblyCompany(\"\")]\n" + - "[assembly: AssemblyProduct(\"\")]\n" + - "[assembly: AssemblyCopyright(\"\")]\n" + - "[assembly: AssemblyTrademark(\"\")]\n" + - "[assembly: AssemblyCulture(\"\")]\n" + - "[assembly: AssemblyVersion(\"13.1.1.976\")]"); + w.write(""" + using System.Reflection; + + [assembly: AssemblyTitle("")] + [assembly: AssemblyDescription("")] + [assembly: AssemblyCompany("")] + [assembly: AssemblyProduct("")] + [assembly: AssemblyCopyright("")] + [assembly: AssemblyTrademark("")] + [assembly: AssemblyCulture("")] + [assembly: AssemblyVersion("13.1.1.976")]"""); } finally { w.close(); @@ -198,16 +201,17 @@ public boolean perform(AbstractBuild build, Launcher launcher, @Test public void testBOMFileNotModified() throws IOException, ExecutionException, InterruptedException { FreeStyleProject project = j.createFreeStyleProject(); - final byte[] originFileBinary = ("using System.Reflection;\n" + - "\n" + - "[assembly: AssemblyTitle(\"\")]\n" + - "[assembly: AssemblyDescription(\"\")]\n" + - "[assembly: AssemblyCompany(\"\")]\n" + - "[assembly: AssemblyProduct(\"\")]\n" + - "[assembly: AssemblyCopyright(\"\")]\n" + - "[assembly: AssemblyTrademark(\"\")]\n" + - "[assembly: AssemblyCulture(\"\")]\n" + - "[assembly: AssemblyVersion(\"13.1.1.976\")]").getBytes(); + final byte[] originFileBinary = (""" + using System.Reflection; + + [assembly: AssemblyTitle("")] + [assembly: AssemblyDescription("")] + [assembly: AssemblyCompany("")] + [assembly: AssemblyProduct("")] + [assembly: AssemblyCopyright("")] + [assembly: AssemblyTrademark("")] + [assembly: AssemblyCulture("")] + [assembly: AssemblyVersion("13.1.1.976")]""").getBytes(); project.getBuildersList().add(new TestBuilder() { public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { @@ -242,16 +246,17 @@ public void testBOMFileNotModified2() throws IOException, ExecutionException, In FreeStyleProject project = j.createFreeStyleProject(); ByteOrderMark bom = ByteOrderMark.UTF_8; byte[] bomBinary = bom.getBytes(); - final byte[] originFileContentBinary = ("using System.Reflection;\n" + - "\n" + - "[assembly: AssemblyTitle(\"\")]\n" + - "[assembly: AssemblyDescription(\"\")]\n" + - "[assembly: AssemblyCompany(\"\")]\n" + - "[assembly: AssemblyProduct(\"\")]\n" + - "[assembly: AssemblyCopyright(\"\")]\n" + - "[assembly: AssemblyTrademark(\"\")]\n" + - "[assembly: AssemblyCulture(\"\")]\n" + - "[assembly: AssemblyVersion(\"13.1.1.976\")]").getBytes(); + final byte[] originFileContentBinary = (""" + using System.Reflection; + + [assembly: AssemblyTitle("")] + [assembly: AssemblyDescription("")] + [assembly: AssemblyCompany("")] + [assembly: AssemblyProduct("")] + [assembly: AssemblyCopyright("")] + [assembly: AssemblyTrademark("")] + [assembly: AssemblyCulture("")] + [assembly: AssemblyVersion("13.1.1.976")]""").getBytes(); final byte[] originFileBinary = Bytes.concat(bomBinary, originFileContentBinary); project.getBuildersList().add(new TestBuilder() { public boolean perform(AbstractBuild build, Launcher launcher, From d9951618ba0b0d6ebd93d9495e3a85e3de770725 Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 3 Dec 2024 19:28:09 -0700 Subject: [PATCH 2/3] Apply spotless Apply spotless fixup for source. --- pom.xml | 173 +++--- .../AssemblyVersion.java | 43 +- .../ChangeAssemblyVersion.java | 138 ++--- .../changeassemblyversion/ChangeTools.java | 25 +- .../ReplacementsTest.java | 535 +++++++++--------- 5 files changed, 468 insertions(+), 446 deletions(-) diff --git a/pom.xml b/pom.xml index 9c623c3..fd23222 100644 --- a/pom.xml +++ b/pom.xml @@ -1,99 +1,100 @@ + - 4.0.0 - - org.jenkins-ci.plugins - plugin - 5.3 - - + 4.0.0 + + org.jenkins-ci.plugins + plugin + 5.3 + + + org.jenkinsci.plugins + change-assembly-version-plugin + ${revision}${changelist} + hpi - - 1.11 - -SNAPSHOT - jenkinsci/change-assembly-version-plugin - 2.479 - ${jenkins.baseline}.1 - Max - false - Low - + https://wiki.jenkins-ci.org/display/JENKINS/Change+Assembly+Version - - scm:git:https://github.com/${gitHubRepo}.git - scm:git:git@github.com:${gitHubRepo}.git - https://github.com/${gitHubRepo} - ${scmTag} - + + + MIT License + http://opensource.org/licenses/MIT + + - - - slide - Alex Earl - slide.o.mix@gmail.com - - + + + slide + Alex Earl + slide.o.mix@gmail.com + + - https://wiki.jenkins-ci.org/display/JENKINS/Change+Assembly+Version - org.jenkinsci.plugins - change-assembly-version-plugin - ${revision}${changelist} - hpi + + scm:git:https://github.com/${gitHubRepo}.git + scm:git:git@github.com:${gitHubRepo}.git + ${scmTag} + https://github.com/${gitHubRepo} + - - - MIT License - http://opensource.org/licenses/MIT - - + + + false + maven.jenkins-ci.org + https://repo.jenkins-ci.org/releases/ + + + maven.jenkins-ci.org + https://repo.jenkins-ci.org/snapshots/ + + + + 1.11 + -SNAPSHOT + jenkinsci/change-assembly-version-plugin + 2.479 + ${jenkins.baseline}.1 + Max + false + Low + + + - - org.jenkins-ci.plugins - structs - - - org.bouncycastle - bcprov-jdk15on - 1.70 - test - + + io.jenkins.tools.bom + bom-2.479.x + 3761.vd922730f0fd2 + pom + import + + - - - - io.jenkins.tools.bom - bom-2.479.x - 3761.vd922730f0fd2 - import - pom - - - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - + + + org.jenkins-ci.plugins + structs + + + org.bouncycastle + bcprov-jdk15on + 1.70 + test + + - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + - - - false - maven.jenkins-ci.org - https://repo.jenkins-ci.org/releases/ - - - maven.jenkins-ci.org - https://repo.jenkins-ci.org/snapshots/ - - + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + diff --git a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/AssemblyVersion.java b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/AssemblyVersion.java index 68dbe5a..55125ad 100644 --- a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/AssemblyVersion.java +++ b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/AssemblyVersion.java @@ -3,29 +3,24 @@ import hudson.EnvVars; import hudson.model.BuildListener; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import hudson.model.BuildListener; - public class AssemblyVersion { - - - private String version; - private EnvVars envVars; - private BuildListener listener; - - /** - * The instance of this class gonna return in the property version the value to be used on ChangeTools. - * @param version - * @param envVars - */ - public AssemblyVersion(String version, EnvVars envVars){ - this.envVars = envVars; - - this.version = envVars.expand(version); - } - - public String getVersion(){ - return this.version; - } + + private String version; + private EnvVars envVars; + private BuildListener listener; + + /** + * The instance of this class gonna return in the property version the value to be used on ChangeTools. + * @param version + * @param envVars + */ + public AssemblyVersion(String version, EnvVars envVars) { + this.envVars = envVars; + + this.version = envVars.expand(version); + } + + public String getVersion() { + return this.version; + } } diff --git a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java index 2e084a9..9fcb510 100644 --- a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java +++ b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeAssemblyVersion.java @@ -1,9 +1,5 @@ package org.jenkinsci.plugins.changeassemblyversion; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; - import hudson.AbortException; import hudson.EnvVars; import hudson.Extension; @@ -16,10 +12,11 @@ import hudson.model.TaskListener; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Builder; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import jenkins.tasks.SimpleBuildStep; - import org.apache.commons.lang.StringUtils; - import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; @@ -42,18 +39,18 @@ public class ChangeAssemblyVersion extends Builder implements SimpleBuildStep { private String assemblyCulture; @Deprecated - public ChangeAssemblyVersion(String versionPattern, - String assemblyFile, - String regexPattern, - String replacementPattern, - String assemblyTitle, - String assemblyDescription, - String assemblyCompany, - String assemblyProduct, - String assemblyCopyright, - String assemblyTrademark, - String assemblyCulture - ) { + public ChangeAssemblyVersion( + String versionPattern, + String assemblyFile, + String regexPattern, + String replacementPattern, + String assemblyTitle, + String assemblyDescription, + String assemblyCompany, + String assemblyProduct, + String assemblyCopyright, + String assemblyTrademark, + String assemblyCulture) { this.versionPattern = versionPattern; this.assemblyFile = assemblyFile; this.regexPattern = regexPattern; @@ -66,7 +63,7 @@ public ChangeAssemblyVersion(String versionPattern, this.assemblyTrademark = assemblyTrademark; this.assemblyCulture = assemblyCulture; } - + @DataBoundConstructor public ChangeAssemblyVersion(String versionPattern) { this.versionPattern = versionPattern; @@ -86,26 +83,26 @@ public void setRegexPattern(String regexPattern) { public void setReplacementPattern(String pattern) { this.replacementPattern = pattern; } - + @DataBoundSetter public void setAssemblyTitle(String title) { this.assemblyTitle = title; } - + @DataBoundSetter public void setAssemblyDescription(String description) { this.assemblyDescription = description; } - + @DataBoundSetter public void setAssemblyCompany(String company) { this.assemblyCompany = company; } - + @DataBoundSetter public void setAssemblyProduct(String product) { this.assemblyProduct = product; - } + } @DataBoundSetter public void setAssemblyCopyright(String copyright) { @@ -116,12 +113,11 @@ public void setAssemblyCopyright(String copyright) { public void setAssemblyTrademark(String trademark) { this.assemblyTrademark = trademark; } - + @DataBoundSetter public void setAssemblyCulture(String culture) { this.assemblyCulture = culture; } - public String getVersionPattern() { return this.versionPattern; @@ -138,22 +134,22 @@ public String getRegexPattern() { public String getReplacementPattern() { return this.replacementPattern; } - + public String getAssemblyTitle() { return this.assemblyTitle; } - + public String getAssemblyDescription() { return this.assemblyDescription; } - + public String getAssemblyCompany() { return this.assemblyCompany; } - + public String getAssemblyProduct() { return this.assemblyProduct; - } + } public String getAssemblyCopyright() { return this.assemblyCopyright; @@ -162,28 +158,27 @@ public String getAssemblyCopyright() { public String getAssemblyTrademark() { return this.assemblyTrademark; } - + public String getAssemblyCulture() { return this.assemblyCulture; } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) - throws InterruptedException, IOException { - try - { - perform(build, build.getWorkspace(), launcher, listener); - } catch (AbortException ex) { - return false; - } catch (Exception ex) { - StringWriter sw = new StringWriter(); - ex.printStackTrace(new PrintWriter(sw)); - listener.getLogger().println(sw.toString()); - } + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + try { + perform(build, build.getWorkspace(), launcher, listener); + } catch (AbortException ex) { + return false; + } catch (Exception ex) { + StringWriter sw = new StringWriter(); + ex.printStackTrace(new PrintWriter(sw)); + listener.getLogger().println(sw.toString()); + } - return true; + return true; } - + /** * * The perform method is gonna search all the file named "Assemblyinfo.cs" @@ -202,10 +197,12 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis @Override public void perform(Run run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException { - + try { - String assemblyGlob = this.assemblyFile == null || this.assemblyFile.equals("") ? "**/AssemblyInfo.cs" : this.assemblyFile; - + String assemblyGlob = this.assemblyFile == null || this.assemblyFile.equals("") + ? "**/AssemblyInfo.cs" + : this.assemblyFile; + EnvVars envVars = run.getEnvironment(listener); if (run instanceof AbstractBuild build) { @@ -213,12 +210,11 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi } String version = new AssemblyVersion(this.versionPattern, envVars).getVersion(); - if (versionPattern == null || StringUtils.isEmpty(versionPattern)) - { + if (versionPattern == null || StringUtils.isEmpty(versionPattern)) { listener.getLogger().println("Please provide a valid version pattern."); throw new AbortException("Please provide a valid version pattern."); } - + // Expand env variables String assemblyTitle = envVars.expand(this.assemblyTitle); String assemblyDescription = envVars.expand(this.assemblyDescription); @@ -227,8 +223,7 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi String assemblyCopyright = envVars.expand(this.assemblyCopyright); String assemblyTrademark = envVars.expand(this.assemblyTrademark); String assemblyCulture = envVars.expand(this.assemblyCulture); - - + // Log new expanded values listener.getLogger().println("Changing File(s): %s".formatted(assemblyGlob)); listener.getLogger().println("Assembly Version : %s".formatted(version)); @@ -239,21 +234,27 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi listener.getLogger().println("Assembly Copyright : %s".formatted(assemblyCopyright)); listener.getLogger().println("Assembly Trademark : %s".formatted(assemblyTrademark)); listener.getLogger().println("Assembly Culture : %s".formatted(assemblyCulture)); - - for (FilePath f : workspace.list(assemblyGlob)) - { + + for (FilePath f : workspace.list(assemblyGlob)) { // Update the AssemblyVerion and AssemblyFileVersion new ChangeTools(f, this.regexPattern, this.replacementPattern).Replace(version, listener); - + // Set new things, empty string being ok for them. // TODO: Would we need a regex for these or just blast as we are doing now? - new ChangeTools(f, "AssemblyTitle[(]\".*\"[)]", "AssemblyTitle(\"%s\")").Replace(assemblyTitle, listener); - new ChangeTools(f, "AssemblyDescription[(]\".*\"[)]", "AssemblyDescription(\"%s\")").Replace(assemblyDescription, listener); - new ChangeTools(f, "AssemblyCompany[(]\".*\"[)]", "AssemblyCompany(\"%s\")").Replace(assemblyCompany, listener); - new ChangeTools(f, "AssemblyProduct[(]\".*\"[)]", "AssemblyProduct(\"%s\")").Replace(assemblyProduct, listener); - new ChangeTools(f, "AssemblyCopyright[(]\".*\"[)]", "AssemblyCopyright(\"%s\")").Replace(assemblyCopyright, listener); - new ChangeTools(f, "AssemblyTrademark[(]\".*\"[)]", "AssemblyTrademark(\"%s\")").Replace(assemblyTrademark, listener); - new ChangeTools(f, "AssemblyCulture[(]\".*\"[)]", "AssemblyCulture(\"%s\")").Replace(assemblyCulture, listener); + new ChangeTools(f, "AssemblyTitle[(]\".*\"[)]", "AssemblyTitle(\"%s\")") + .Replace(assemblyTitle, listener); + new ChangeTools(f, "AssemblyDescription[(]\".*\"[)]", "AssemblyDescription(\"%s\")") + .Replace(assemblyDescription, listener); + new ChangeTools(f, "AssemblyCompany[(]\".*\"[)]", "AssemblyCompany(\"%s\")") + .Replace(assemblyCompany, listener); + new ChangeTools(f, "AssemblyProduct[(]\".*\"[)]", "AssemblyProduct(\"%s\")") + .Replace(assemblyProduct, listener); + new ChangeTools(f, "AssemblyCopyright[(]\".*\"[)]", "AssemblyCopyright(\"%s\")") + .Replace(assemblyCopyright, listener); + new ChangeTools(f, "AssemblyTrademark[(]\".*\"[)]", "AssemblyTrademark(\"%s\")") + .Replace(assemblyTrademark, listener); + new ChangeTools(f, "AssemblyCulture[(]\".*\"[)]", "AssemblyCulture(\"%s\")") + .Replace(assemblyCulture, listener); } } catch (Exception ex) { StringWriter sw = new StringWriter(); @@ -262,10 +263,10 @@ public void perform(Run run, FilePath workspace, Launcher launcher, TaskLi throw new AbortException(sw.toString()); } - } - - @Extension @Symbol("changeAsmVer") + + @Extension + @Symbol("changeAsmVer") public static class DescriptorImpl extends BuildStepDescriptor { @Override @@ -278,5 +279,4 @@ public String getDisplayName() { return "Change Assembly Version"; } } - } diff --git a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java index e3f431c..6c32289 100644 --- a/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java +++ b/src/main/java/org/jenkinsci/plugins/changeassemblyversion/ChangeTools.java @@ -1,15 +1,13 @@ package org.jenkinsci.plugins.changeassemblyversion; import hudson.FilePath; -import hudson.model.BuildListener; import hudson.model.TaskListener; -import org.apache.commons.io.ByteOrderMark; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.BOMInputStream; - import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; +import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.BOMInputStream; public class ChangeTools { @@ -33,8 +31,7 @@ public class ChangeTools { } public void Replace(String replacement, TaskListener listener) throws IOException, InterruptedException { - if (replacement != null && !replacement.isEmpty()) - { + if (replacement != null && !replacement.isEmpty()) { BOMInputStream inputStream = new BOMInputStream(file.read()); String content; ByteOrderMark bom; @@ -46,25 +43,23 @@ public void Replace(String replacement, TaskListener listener) throws IOExceptio } content = IOUtils.toString(inputStream, fileEncoding); - } - finally { + } finally { inputStream.close(); } - listener.getLogger().println("Updating file : %s, Replacement : %s".formatted(file.getRemote(), replacement)); + listener.getLogger() + .println("Updating file : %s, Replacement : %s".formatted(file.getRemote(), replacement)); content = content.replaceAll(regexPattern, replacementPattern.formatted(replacement)); - //listener.getLogger().println(String.format("Updating file : %s", file.getRemote())); + // listener.getLogger().println(String.format("Updating file : %s", file.getRemote())); OutputStream os = file.write(); try { - if (bom != null){ + if (bom != null) { os.write(bom.getBytes()); } os.write(content.getBytes(fileEncoding)); } finally { os.close(); } - } - else - { + } else { listener.getLogger().println("Skipping replacement because value is empty.".formatted()); } } diff --git a/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java b/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java index fa482b9..1f68097 100644 --- a/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java +++ b/src/test/java/org/jenkinsci/plugins/changeassemblyversion/ReplacementsTest.java @@ -1,79 +1,77 @@ -/* - * The MIT License - * - * Copyright 2014 BELLINSALARIN. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.jenkinsci.plugins.changeassemblyversion; - -import com.google.common.primitives.Bytes; -import hudson.EnvVars; -import hudson.Launcher; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.slaves.EnvironmentVariablesNodeProperty; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.util.concurrent.ExecutionException; - -import org.apache.commons.io.ByteOrderMark; -import org.apache.commons.io.FileUtils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.BOMInputStream; -import org.bouncycastle.util.Arrays; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.TestBuilder; - -/** - * - * @author BELLINSALARIN - */ -public class ReplacementsTest { - - @Rule - public JenkinsRule j = new JenkinsRule(); - - @Test - public void testResolveEnvironmentVariables() throws InterruptedException, IOException, Exception { - - EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); - EnvVars envVars = prop.getEnvVars(); - envVars.put("PREFIX", "1.1.0"); - j.jenkins.getGlobalNodeProperties().add(prop); - FreeStyleProject project = j.createFreeStyleProject(); - project.getBuildersList().add(new TestBuilder() { - public boolean perform(AbstractBuild build, Launcher launcher, - BuildListener listener) throws InterruptedException, IOException { - build.getWorkspace().child("AssemblyVersion.cs").write(""" +/* + * The MIT License + * + * Copyright 2014 BELLINSALARIN. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.jenkinsci.plugins.changeassemblyversion; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.primitives.Bytes; +import hudson.EnvVars; +import hudson.Launcher; +import hudson.model.AbstractBuild; +import hudson.model.BuildListener; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.slaves.EnvironmentVariablesNodeProperty; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.concurrent.ExecutionException; +import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.BOMInputStream; +import org.bouncycastle.util.Arrays; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.TestBuilder; + +/** + * + * @author BELLINSALARIN + */ +public class ReplacementsTest { + + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void testResolveEnvironmentVariables() throws InterruptedException, IOException, Exception { + + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); + EnvVars envVars = prop.getEnvVars(); + envVars.put("PREFIX", "1.1.0"); + j.jenkins.getGlobalNodeProperties().add(prop); + FreeStyleProject project = j.createFreeStyleProject(); + project.getBuildersList().add(new TestBuilder() { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + build.getWorkspace() + .child("AssemblyVersion.cs") + .write( + """ using System.Reflection; [assembly: AssemblyTitle("")] @@ -83,78 +81,94 @@ public boolean perform(AbstractBuild build, Launcher launcher, [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("13.1.1.976")]""", "UTF-8"); - return true; - } - }); - ChangeAssemblyVersion builder = new ChangeAssemblyVersion("$PREFIX.${BUILD_NUMBER}", "AssemblyVersion.cs", "", "", "MyTitle", "MyDescription", "MyCompany", "MyProduct", "MyCopyright", "MyTrademark", "MyCulture"); - project.getBuildersList().add(builder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - //String s = FileUtils.readFileToString(build.getLogFile()); - String content = build.getWorkspace().child("AssemblyVersion.cs").readToString(); - assertTrue(content.contains("AssemblyVersion(\"1.1.0.")); - - // Check that we update additional assembly info - assertTrue(content.contains("AssemblyTitle(\"MyTitle")); - assertTrue(content.contains("AssemblyDescription(\"MyDescription")); - assertTrue(content.contains("AssemblyCompany(\"MyCompany")); - assertTrue(content.contains("AssemblyProduct(\"MyProduct")); - assertTrue(content.contains("AssemblyCopyright(\"MyCopyright")); - assertTrue(content.contains("AssemblyTrademark(\"MyTrademark")); - assertTrue(content.contains("AssemblyCulture(\"MyCulture")); - - assertTrue(builder.getVersionPattern().equals("$PREFIX.${BUILD_NUMBER}")); - } - - @Test - public void testResolveEnvironmentVariables_recursively_excludingSvn() throws InterruptedException, IOException, Exception { - - EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); - EnvVars envVars = prop.getEnvVars(); - envVars.put("PREFIX", "1.1.0"); - j.jenkins.getGlobalNodeProperties().add(prop); - FreeStyleProject project = j.createFreeStyleProject(); - final String f1 = "myassembly/properties/AssemblyInfo.cs"; - final String f2 = ".svn/myassembly/properties/AssemblyInfo.cs"; +[assembly: AssemblyVersion("13.1.1.976")]""", + "UTF-8"); + return true; + } + }); + ChangeAssemblyVersion builder = new ChangeAssemblyVersion( + "$PREFIX.${BUILD_NUMBER}", + "AssemblyVersion.cs", + "", + "", + "MyTitle", + "MyDescription", + "MyCompany", + "MyProduct", + "MyCopyright", + "MyTrademark", + "MyCulture"); + project.getBuildersList().add(builder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // String s = FileUtils.readFileToString(build.getLogFile()); + String content = build.getWorkspace().child("AssemblyVersion.cs").readToString(); + assertTrue(content.contains("AssemblyVersion(\"1.1.0.")); + + // Check that we update additional assembly info + assertTrue(content.contains("AssemblyTitle(\"MyTitle")); + assertTrue(content.contains("AssemblyDescription(\"MyDescription")); + assertTrue(content.contains("AssemblyCompany(\"MyCompany")); + assertTrue(content.contains("AssemblyProduct(\"MyProduct")); + assertTrue(content.contains("AssemblyCopyright(\"MyCopyright")); + assertTrue(content.contains("AssemblyTrademark(\"MyTrademark")); + assertTrue(content.contains("AssemblyCulture(\"MyCulture")); + + assertTrue(builder.getVersionPattern().equals("$PREFIX.${BUILD_NUMBER}")); + } + + @Test + public void testResolveEnvironmentVariables_recursively_excludingSvn() + throws InterruptedException, IOException, Exception { + + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); + EnvVars envVars = prop.getEnvVars(); + envVars.put("PREFIX", "1.1.0"); + j.jenkins.getGlobalNodeProperties().add(prop); + FreeStyleProject project = j.createFreeStyleProject(); + final String f1 = "myassembly/properties/AssemblyInfo.cs"; + final String f2 = ".svn/myassembly/properties/AssemblyInfo.cs"; final String c = """ using System.Reflection; -[assembly: AssemblyVersion("13.1.1.976")]"""; - project.getBuildersList().add(new TestBuilder() { - public boolean perform(AbstractBuild build, Launcher launcher, - BuildListener listener) throws InterruptedException, IOException { - build.getWorkspace().child(f1).write(c, "UTF-8"); - build.getWorkspace().child(f2).write(c, "UTF-8"); - return true; - } - }); - ChangeAssemblyVersion builder = new ChangeAssemblyVersion("$PREFIX.${BUILD_NUMBER}", "", "", "", "", "", "", "", "", "", ""); - project.getBuildersList().add(builder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - //String s = FileUtils.readFileToString(build.getLogFile()); - String content = build.getWorkspace().child(f1).readToString(); - assertTrue(content.contains("AssemblyVersion(\"1.1.0.")); - content = build.getWorkspace().child(f2).readToString(); - assertTrue(content.contains("AssemblyVersion(\"13.1.1.976")); - assertTrue(builder.getVersionPattern().equals("$PREFIX.${BUILD_NUMBER}")); - } - - @Test - public void testBOMFileReplace() throws IOException, ExecutionException, InterruptedException { - FreeStyleProject project = j.createFreeStyleProject(); - final ByteOrderMark fileBom = ByteOrderMark.UTF_8; - project.getBuildersList().add(new TestBuilder() { - public boolean perform(AbstractBuild build, Launcher launcher, - BuildListener listener) throws InterruptedException, IOException { - OutputStream outputStream = build.getWorkspace().child("AssemblyVersion.cs").write(); - outputStream.write(fileBom.getBytes()); - OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8"); - try{ - w.write(""" +[assembly: AssemblyVersion("13.1.1.976")]"""; + project.getBuildersList().add(new TestBuilder() { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + build.getWorkspace().child(f1).write(c, "UTF-8"); + build.getWorkspace().child(f2).write(c, "UTF-8"); + return true; + } + }); + ChangeAssemblyVersion builder = + new ChangeAssemblyVersion("$PREFIX.${BUILD_NUMBER}", "", "", "", "", "", "", "", "", "", ""); + project.getBuildersList().add(builder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // String s = FileUtils.readFileToString(build.getLogFile()); + String content = build.getWorkspace().child(f1).readToString(); + assertTrue(content.contains("AssemblyVersion(\"1.1.0.")); + content = build.getWorkspace().child(f2).readToString(); + assertTrue(content.contains("AssemblyVersion(\"13.1.1.976")); + assertTrue(builder.getVersionPattern().equals("$PREFIX.${BUILD_NUMBER}")); + } + + @Test + public void testBOMFileReplace() throws IOException, ExecutionException, InterruptedException { + FreeStyleProject project = j.createFreeStyleProject(); + final ByteOrderMark fileBom = ByteOrderMark.UTF_8; + project.getBuildersList().add(new TestBuilder() { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + OutputStream outputStream = + build.getWorkspace().child("AssemblyVersion.cs").write(); + outputStream.write(fileBom.getBytes()); + OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8"); + try { + w.write( + """ using System.Reflection; - + [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyCompany("")] @@ -162,48 +176,60 @@ public boolean perform(AbstractBuild build, Launcher launcher, [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] - [assembly: AssemblyVersion("13.1.1.976")]"""); - } - finally { - w.close(); - outputStream.close(); - } - - return true; - } - }); - ChangeAssemblyVersion builder = new ChangeAssemblyVersion("1.2.3", "AssemblyVersion.cs", "", "", "MyTitle", "MyDescription", "MyCompany", "MyProduct", "MyCopyright", "MyTrademark", "MyCulture"); - project.getBuildersList().add(builder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - //String s = FileUtils.readFileToString(build.getLogFile()); - InputStream readStream = build.getWorkspace().child("AssemblyVersion.cs").read(); - BOMInputStream bomInputStream = new BOMInputStream(readStream); - ByteOrderMark bomAfterChange = bomInputStream.getBOM(); - String content = org.apache.commons.io.IOUtils.toString(bomInputStream); - bomInputStream.close(); - readStream.close(); - - // the replaced file should have the same BOM as the origin. - assertEquals(bomAfterChange.getCharsetName(), fileBom.getCharsetName()); - // the first bytes should be readable characters. - assertTrue(content.matches("(?s)^using.*")); - // Check that we update additional assembly info - assertTrue(content.contains("AssemblyTitle(\"MyTitle")); - assertTrue(content.contains("AssemblyDescription(\"MyDescription")); - assertTrue(content.contains("AssemblyCompany(\"MyCompany")); - assertTrue(content.contains("AssemblyProduct(\"MyProduct")); - assertTrue(content.contains("AssemblyCopyright(\"MyCopyright")); - assertTrue(content.contains("AssemblyTrademark(\"MyTrademark")); - assertTrue(content.contains("AssemblyCulture(\"MyCulture")); - } - - @Test - public void testBOMFileNotModified() throws IOException, ExecutionException, InterruptedException { - FreeStyleProject project = j.createFreeStyleProject(); - final byte[] originFileBinary = (""" + [assembly: AssemblyVersion("13.1.1.976")]"""); + } finally { + w.close(); + outputStream.close(); + } + + return true; + } + }); + ChangeAssemblyVersion builder = new ChangeAssemblyVersion( + "1.2.3", + "AssemblyVersion.cs", + "", + "", + "MyTitle", + "MyDescription", + "MyCompany", + "MyProduct", + "MyCopyright", + "MyTrademark", + "MyCulture"); + project.getBuildersList().add(builder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // String s = FileUtils.readFileToString(build.getLogFile()); + InputStream readStream = + build.getWorkspace().child("AssemblyVersion.cs").read(); + BOMInputStream bomInputStream = new BOMInputStream(readStream); + ByteOrderMark bomAfterChange = bomInputStream.getBOM(); + String content = org.apache.commons.io.IOUtils.toString(bomInputStream); + bomInputStream.close(); + readStream.close(); + + // the replaced file should have the same BOM as the origin. + assertEquals(bomAfterChange.getCharsetName(), fileBom.getCharsetName()); + // the first bytes should be readable characters. + assertTrue(content.matches("(?s)^using.*")); + // Check that we update additional assembly info + assertTrue(content.contains("AssemblyTitle(\"MyTitle")); + assertTrue(content.contains("AssemblyDescription(\"MyDescription")); + assertTrue(content.contains("AssemblyCompany(\"MyCompany")); + assertTrue(content.contains("AssemblyProduct(\"MyProduct")); + assertTrue(content.contains("AssemblyCopyright(\"MyCopyright")); + assertTrue(content.contains("AssemblyTrademark(\"MyTrademark")); + assertTrue(content.contains("AssemblyCulture(\"MyCulture")); + } + + @Test + public void testBOMFileNotModified() throws IOException, ExecutionException, InterruptedException { + FreeStyleProject project = j.createFreeStyleProject(); + final byte[] originFileBinary = + (""" using System.Reflection; - + [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyCompany("")] @@ -211,44 +237,47 @@ public void testBOMFileNotModified() throws IOException, ExecutionException, Int [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] - [assembly: AssemblyVersion("13.1.1.976")]""").getBytes(); - project.getBuildersList().add(new TestBuilder() { - public boolean perform(AbstractBuild build, Launcher launcher, - BuildListener listener) throws InterruptedException, IOException { - OutputStream outputStream = build.getWorkspace().child("AssemblyVersion.cs").write(); - try{ - outputStream.write(originFileBinary); - } - finally { - outputStream.close(); - } - - return true; - } - }); - - ChangeAssemblyVersion builder = new ChangeAssemblyVersion(""); - project.getBuildersList().add(builder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - //String s = FileUtils.readFileToString(build.getLogFile()); - InputStream readStream = build.getWorkspace().child("AssemblyVersion.cs").read(); - - byte[] binaryAfterChange = IOUtils.toByteArray(readStream); - String content = new String(binaryAfterChange, "UTF-8"); - - // the replaced file should have the same BOM as the origin. - assertTrue(Arrays.areEqual(originFileBinary, binaryAfterChange)); - } - - @Test - public void testBOMFileNotModified2() throws IOException, ExecutionException, InterruptedException { - FreeStyleProject project = j.createFreeStyleProject(); - ByteOrderMark bom = ByteOrderMark.UTF_8; - byte[] bomBinary = bom.getBytes(); - final byte[] originFileContentBinary = (""" + [assembly: AssemblyVersion("13.1.1.976")]""") + .getBytes(); + project.getBuildersList().add(new TestBuilder() { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + OutputStream outputStream = + build.getWorkspace().child("AssemblyVersion.cs").write(); + try { + outputStream.write(originFileBinary); + } finally { + outputStream.close(); + } + + return true; + } + }); + + ChangeAssemblyVersion builder = new ChangeAssemblyVersion(""); + project.getBuildersList().add(builder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // String s = FileUtils.readFileToString(build.getLogFile()); + InputStream readStream = + build.getWorkspace().child("AssemblyVersion.cs").read(); + + byte[] binaryAfterChange = IOUtils.toByteArray(readStream); + String content = new String(binaryAfterChange, "UTF-8"); + + // the replaced file should have the same BOM as the origin. + assertTrue(Arrays.areEqual(originFileBinary, binaryAfterChange)); + } + + @Test + public void testBOMFileNotModified2() throws IOException, ExecutionException, InterruptedException { + FreeStyleProject project = j.createFreeStyleProject(); + ByteOrderMark bom = ByteOrderMark.UTF_8; + byte[] bomBinary = bom.getBytes(); + final byte[] originFileContentBinary = + (""" using System.Reflection; - + [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyCompany("")] @@ -256,33 +285,35 @@ public void testBOMFileNotModified2() throws IOException, ExecutionException, In [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] - [assembly: AssemblyVersion("13.1.1.976")]""").getBytes(); - final byte[] originFileBinary = Bytes.concat(bomBinary, originFileContentBinary); - project.getBuildersList().add(new TestBuilder() { - public boolean perform(AbstractBuild build, Launcher launcher, - BuildListener listener) throws InterruptedException, IOException { - OutputStream outputStream = build.getWorkspace().child("AssemblyVersion.cs").write(); - try{ - outputStream.write(originFileBinary); - } - finally { - outputStream.close(); - } - - return true; - } - }); - - ChangeAssemblyVersion builder = new ChangeAssemblyVersion(""); - project.getBuildersList().add(builder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - //String s = FileUtils.readFileToString(build.getLogFile()); - InputStream readStream = build.getWorkspace().child("AssemblyVersion.cs").read(); - byte[] binaryAfterChange = IOUtils.toByteArray(readStream); - readStream.close(); - - // the replaced file should have the same BOM as the origin. - assertTrue(Arrays.areEqual(originFileBinary, binaryAfterChange)); - } -} + [assembly: AssemblyVersion("13.1.1.976")]""") + .getBytes(); + final byte[] originFileBinary = Bytes.concat(bomBinary, originFileContentBinary); + project.getBuildersList().add(new TestBuilder() { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) + throws InterruptedException, IOException { + OutputStream outputStream = + build.getWorkspace().child("AssemblyVersion.cs").write(); + try { + outputStream.write(originFileBinary); + } finally { + outputStream.close(); + } + + return true; + } + }); + + ChangeAssemblyVersion builder = new ChangeAssemblyVersion(""); + project.getBuildersList().add(builder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // String s = FileUtils.readFileToString(build.getLogFile()); + InputStream readStream = + build.getWorkspace().child("AssemblyVersion.cs").read(); + byte[] binaryAfterChange = IOUtils.toByteArray(readStream); + readStream.close(); + + // the replaced file should have the same BOM as the origin. + assertTrue(Arrays.areEqual(originFileBinary, binaryAfterChange)); + } +} From 0948914db9f14d7c337dc13858feea576bda9f1f Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 3 Dec 2024 19:29:11 -0700 Subject: [PATCH 3/3] Fixup configurations --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e6a4a5c..08b541f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,7 +3,7 @@ * https://github.com/jenkins-infra/pipeline-library/ */ buildPlugin(useContainerAgent: true, configurations: [ - [ platform: 'linux', jdk: '11' ], - [ platform: 'windows', jdk: '11' ], + [ platform: 'windows', jdk: '17' ], [ platform: 'linux', jdk: '17' ], + [ platform: 'linux', jdk: '21' ], ])