From e32432bcb31362b714eb45a0bc63308eb5f298ad Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Tue, 22 Sep 2020 13:36:52 -0700 Subject: [PATCH] Use System's path separator for classpaths --- .../smithy/cli/BuildParameterBuilder.java | 16 ++++++---- .../smithy/cli/commands/CommandUtils.java | 4 ++- .../smithy/cli/BuildParameterBuilderTest.java | 32 +++++++++++++++++-- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/BuildParameterBuilder.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/BuildParameterBuilder.java index 93f875e186c..f7058f7c06c 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/BuildParameterBuilder.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/BuildParameterBuilder.java @@ -65,6 +65,7 @@ public final class BuildParameterBuilder { private static final Logger LOGGER = Logger.getLogger(BuildParameterBuilder.class.getName()); private static final String SMITHY_TAG_PROPERTY = "Smithy-Tags"; private static final String SOURCE = "source"; + private static final String PATH_SEPARATOR = "path.separator"; private String projectionSource = SOURCE; private Set projectionSourceTags = new LinkedHashSet<>(); @@ -135,7 +136,7 @@ public BuildParameterBuilder addSourcesIfExists(Collection sources) { * @return Returns the builder. */ public BuildParameterBuilder buildClasspath(String buildClasspath) { - this.buildClasspath.addAll(splitAndFilterString(":", buildClasspath)); + this.buildClasspath.addAll(splitAndFilterString(System.getProperty(PATH_SEPARATOR), buildClasspath)); return this; } @@ -157,7 +158,7 @@ private static Set splitAndFilterString(String delimiter, String value) * @return Returns the builder. */ public BuildParameterBuilder libClasspath(String libClasspath) { - this.libClasspath.addAll(splitAndFilterString(":", libClasspath)); + this.libClasspath.addAll(splitAndFilterString(System.getProperty(PATH_SEPARATOR), libClasspath)); return this; } @@ -357,7 +358,9 @@ private Result configureSourceProjection() { Set combined = new LinkedHashSet<>(libClasspath); combined.addAll(buildClasspath); - return new Result(this, String.join(":", computedDiscovery), String.join(":", combined), sources); + String discoveryClasspath = String.join(System.getProperty(PATH_SEPARATOR), computedDiscovery); + String classpath = String.join(System.getProperty(PATH_SEPARATOR), combined); + return new Result(this, discoveryClasspath, classpath, sources); } /** @@ -373,7 +376,7 @@ private Result configureProjection() { LOGGER.warning("No projection source tags were set for the projection `" + projection + "`, so the " + "projection will not have any sources in it other than files found in the sources of " + "the package being built."); - String buildCp = String.join(":", buildClasspath); + String buildCp = String.join(System.getProperty(PATH_SEPARATOR), buildClasspath); return new Result(this, buildCp, buildCp, sources); } @@ -390,8 +393,9 @@ private Result configureProjection() { Set computedDiscovery = new LinkedHashSet<>(buildClasspath); computedDiscovery.removeAll(computedSources); - return new Result(this, String.join(":", computedDiscovery), - String.join(":", buildClasspath), computedSources); + String discoveryClasspath = String.join(System.getProperty(PATH_SEPARATOR), computedDiscovery); + String classpath = String.join(System.getProperty(PATH_SEPARATOR), buildClasspath); + return new Result(this, discoveryClasspath, classpath, computedSources); } /** diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java index 52fcd3c0ec7..3545e664844 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/CommandUtils.java @@ -70,7 +70,9 @@ private static void handleModelDiscovery(Arguments arguments, ModelAssembler ass private static void discoverModelsWithClasspath(Arguments arguments, ModelAssembler assembler) { String rawClasspath = arguments.parameter(SmithyCli.DISCOVER_CLASSPATH); LOGGER.finer("Discovering models with classpath: " + rawClasspath); - String[] classpath = rawClasspath.split(":"); + + // Use System.getProperty here each time since it allows the value to be changed. + String[] classpath = rawClasspath.split(System.getProperty("path.separator")); URL[] urls = new URL[classpath.length]; for (int i = 0; i < classpath.length; i++) { diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java index 3fca472bbd7..1fa5a0178f1 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/BuildParameterBuilderTest.java @@ -217,7 +217,8 @@ public void findsProjectionJarsWithSourceTags() { String a = getClass().getResource("jars/a/a.jar").getPath(); String b = getClass().getResource("jars/b/b.jar").getPath(); String c = getClass().getResource("jars/c/c.jar").getPath(); - String buildCp = a + ":" + b + ":" + c; + String separator = System.getProperty("path.separator"); + String buildCp = a + separator + b + separator + c; BuildParameterBuilder.Result result = new BuildParameterBuilder() .projectionSource("foo") @@ -230,8 +231,35 @@ public void findsProjectionJarsWithSourceTags() { // The classpath keeps all of the JARs. assertThat(result.classpath, equalTo(buildCp)); // The discovery classpath removes a because it's JAR matched the source tag. - assertThat(result.discoveryClasspath, equalTo(b + ":" + c)); + assertThat(result.discoveryClasspath, equalTo(b + separator + c)); // The sources now contains a because it matched a source tag. assertThat(result.sources, contains(a)); } + + @Test + public void usesCustomSeparator() { + String currentSeparator = System.getProperty("path.separator"); + + try { + System.setProperty("path.separator", "|"); + String a = getClass().getResource("jars/a/a.jar").getPath(); + String b = getClass().getResource("jars/b/b.jar").getPath(); + String c = getClass().getResource("jars/c/c.jar").getPath(); + String buildCp = a + "|" + b + "|" + c; + + BuildParameterBuilder.Result result = new BuildParameterBuilder() + .projectionSource("foo") + .projectionSourceTags("X, Blah") + .libClasspath("abc.jar") + .buildClasspath(buildCp) + .discover(true) + .build(); + + assertThat(result.classpath, equalTo(buildCp)); + assertThat(result.discoveryClasspath, equalTo(b + "|" + c)); + assertThat(result.sources, contains(a)); + } finally { + System.setProperty("path.separator", currentSeparator); + } + } }