Skip to content

Commit

Permalink
Update CLI to ignore dependencies when wrapped by Gradle
Browse files Browse the repository at this point in the history
When using Gradle, Gradle should handle dependency resolution, not the
CLI. If we detect that Gradle is wrapping the CLI, dependency
resolution is ignored. This helps ensure that previous versions of
the Gradle plugin do not allow dependency resolution to occur, which
would not work well with Gradle and could break users when they upgrade
to a newer version of the Gradle plugin.
  • Loading branch information
mtdowling committed Dec 14, 2022
1 parent 51ef7bf commit 0f720f9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.cli;

import java.util.logging.Logger;

/**
* Environment variables used by the Smithy CLI.
*/
Expand All @@ -41,12 +43,28 @@ public enum EnvironmentVariable {
* <li>forbid: forbids dependencies from being declared and will fail the CLI if dependencies are declared.</li>
* <li>standard: the assumed default, will automatically resolve dependencies using Apache Maven.</li>
* </ul>
*
* <p>If the CLI detects that Gradle is calling the CLI, dependency resolution is disabled and defaults to
* {@code ignore}. You can explicitly enable dependency resolution by setting {@code SMITHY_DEPENDENCY_MODE} to
* {@code standard}.
*/
SMITHY_DEPENDENCY_MODE {
@Override
public String get() {
String result = super.get();
return result == null ? "standard" : result;
if (result != null) {
return result;
} else if (System.getProperty("org.gradle.appname") != null) {
// Maintainer's note: Ideally this would just be in the Gradle plugin itself; however, previous
// versions of the Gradle plugin can use newer versions of Smithy. This means that even if Gradle is
// updated to disable dependency resolution, previous versions could inadvertently use it.
LOGGER.info("Detected that the Smithy CLI is running in Gradle, so dependency resolution is disabled. "
+ "This can be overridden by setting SMITHY_DEPENDENCY_MODE environment variable to "
+ "'standard'.");
return "ignore";
} else {
return "standard";
}
}
},

Expand All @@ -73,6 +91,8 @@ public String get() {
*/
TERM;

private static final Logger LOGGER = Logger.getLogger(EnvironmentVariable.class.getName());

/**
* Gets a system property or environment variable by name, in that order.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package software.amazon.smithy.cli;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Test;

public class EnvironmentVariableTest {
@Test
public void ignoreDependenciesWhenRunningInGradle() {
Object originalValue = System.getProperty("org.gradle.appname");

try {
System.setProperty("org.gradle.appname", "foo");
assertThat(EnvironmentVariable.SMITHY_DEPENDENCY_MODE.get(), equalTo("ignore"));
} finally {
if (originalValue == null) {
System.clearProperty("org.gradle.appname");
} else {
System.setProperty("org.gradle.appname", originalValue.toString());
}
}
}

@Test
public void useStandardDependencyModeWhenRunningInGradle() {
Object originalValue = System.getProperty("org.gradle.appname");

try {
System.clearProperty("org.gradle.appname");
assertThat(EnvironmentVariable.SMITHY_DEPENDENCY_MODE.get(), equalTo("standard"));
} finally {
if (originalValue != null) {
System.setProperty("org.gradle.appname", originalValue.toString());
}
}
}
}

0 comments on commit 0f720f9

Please sign in to comment.