Skip to content

Commit

Permalink
Use modern API to get property values
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 22, 2024
1 parent 706f18e commit 1f5efd1
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 37 deletions.
23 changes: 13 additions & 10 deletions gradle/build-scans.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
File acceptFile = new File(gradle.gradleUserHomeDir, "build-scans/spotless/gradle-scans-license-agree.txt")
acceptFile.parentFile.mkdirs()
def env = System.getenv()
boolean isCI = env.CI || env.TRAVIS
boolean hasAccepted = isCI || env.SPOTLESS_GRADLE_SCANS_ACCEPT=='yes' || acceptFile.exists() && acceptFile.text.trim() == 'yes'
boolean hasRefused = env.SPOTLESS_GRADLE_SCANS_ACCEPT=='no' || acceptFile.exists() && acceptFile.text.trim() == 'no'

def env(String key) {
return providers.environmentVariable(key).orNull
}
boolean isCI = env('CI') != null
boolean hasAccepted = isCI || env('SPOTLESS_GRADLE_SCANS_ACCEPT') =='yes' || acceptFile.exists() && acceptFile.text.trim() == 'yes'
boolean hasRefused = env('SPOTLESS_GRADLE_SCANS_ACCEPT') =='no' || acceptFile.exists() && acceptFile.text.trim() == 'no'

buildScan {
if (hasAccepted) {
termsOfServiceAgree = 'yes'
} else if (!hasRefused) {
gradle.buildFinished {
println """
This build uses Gradle Build Scans to gather statistics, share information about
This build uses Gradle Build Scans to gather statistics, share information about
failures, environmental issues, dependencies resolved during the build and more.
Build scans will be published after each build, if you accept the terms of
Build scans will be published after each build, if you accept the terms of
service, and in particular the privacy policy.
Please read
https://gradle.com/terms-of-service
Please read
https://gradle.com/terms-of-service
https://gradle.com/legal/privacy
and then:
Expand All @@ -32,7 +35,7 @@ and then:
echo 'no' >> ${acceptFile}
And we'll not bother you again. Note that build scans are only made public if
And we'll not bother you again. Note that build scans are only made public if
you share the URL at the end of the build.
"""
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/changelog.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (project == rootProject) {
if (isPlugin) {
// make sure there aren't any unreleased changes in lib
if (!rootProject.spotlessChangelog.parsedChangelog.noUnreleasedChanges()) {
if (!("true".equals(rootProject.findProperty('ignoreUnreleasedLib')))) {
if (!("true" == rootProject.providers.gradleProperty('ignoreUnreleasedLib').orNull)) {
throw new IllegalArgumentException(
"You're going to publish ${changelogPushTasks[0]}, but there are unreleased features in lib!\n" +
"You should run :changelogPush first! Else you'll be missing out on:\n" +
Expand Down
14 changes: 8 additions & 6 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import java.nio.charset.StandardCharsets
def env(String key) {
return providers.environmentVariable(key).orNull
}

def decode64(String varName) {
String envValue = System.env[varName]
String envValue = env(varName)
if (envValue == null) {
return ""
} else {
Expand All @@ -11,7 +13,7 @@ def decode64(String varName) {

if (project.parent == null) {
group = 'com.diffplug.spotless'
def pass = System.env['ORG_GRADLE_PROJECT_nexus_pass64']
def pass = env('ORG_GRADLE_PROJECT_nexus_pass64')
if (pass != null) {
pass = pass.decodeBase64()
}
Expand All @@ -22,7 +24,7 @@ if (project.parent == null) {
sonatype {
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
username = System.env['ORG_GRADLE_PROJECT_nexus_user']
username = env('ORG_GRADLE_PROJECT_nexus_user')
password = decode64('ORG_GRADLE_PROJECT_nexus_pass64')
}
}
Expand Down Expand Up @@ -169,14 +171,14 @@ model {
}
}

if (System.env['JITPACK'] == 'true' || version.endsWith('-SNAPSHOT')) {
if (env('JITPACK') == 'true' || version.endsWith('-SNAPSHOT')) {
signing {
setRequired(false)
}
} else {
signing {
String gpg_key = decode64('ORG_GRADLE_PROJECT_gpg_key64')
useInMemoryPgpKeys(gpg_key, System.env['ORG_GRADLE_PROJECT_gpg_passphrase'])
useInMemoryPgpKeys(gpg_key, env('ORG_GRADLE_PROJECT_gpg_passphrase'))
sign(publishing.publications)
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/special-tests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def special = [
'Shfmt'
]

boolean isCiServer = System.getenv().containsKey("CI")
boolean isCiServer = providers.environmentVariable("CI").isPresent()
tasks.withType(Test).configureEach {
if (isCiServer) {
retry {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -558,8 +558,8 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {

FormatterStep createStep() {
return builder.withYearModeLazy(() -> {
if ("true".equals(spotless.project
.findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
if ("true".equals(spotless.project.getProviders()
.gradleProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()).getOrNull())) {
return YearMode.SET_FROM_GIT;
} else {
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@ private static void dumpIsClean() {
}

static void performHook(SpotlessTaskImpl spotlessTask) {
String path = (String) spotlessTask.getProject().property(PROPERTY);
String path = spotlessTask.getProject().getProviders().gradleProperty(PROPERTY).get();
File file = new File(path);
if (!file.isAbsolute()) {
System.err.println("Argument passed to " + PROPERTY + " must be an absolute path");
Expand All @@ -50,7 +50,7 @@ static void performHook(SpotlessTaskImpl spotlessTask) {
}
}
byte[] bytes;
if (spotlessTask.getProject().hasProperty(USE_STD_IN)) {
if (spotlessTask.getProject().getProviders().gradleProperty(USE_STD_IN).isPresent()) {
bytes = ByteStreams.toByteArray(System.in);
} else {
bytes = Files.readAllBytes(file.toPath());
Expand All @@ -63,7 +63,7 @@ static void performHook(SpotlessTaskImpl spotlessTask) {
System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md");
} else {
System.err.println("IS DIRTY");
if (spotlessTask.getProject().hasProperty(USE_STD_OUT)) {
if (spotlessTask.getProject().getProviders().gradleProperty(USE_STD_OUT).isPresent()) {
dirty.writeCanonicalTo(System.out);
} else {
dirty.writeCanonicalTo(file);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +48,7 @@ public SpotlessExtensionImpl(Project project) {

@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
boolean isIdeHook = project.hasProperty(IdeHook.PROPERTY);
boolean isIdeHook = project.getProviders().gradleProperty(IdeHook.PROPERTY).isPresent();
TaskContainer tasks = project.getTasks();

// create the SpotlessTask
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +41,7 @@ public void apply(Project project) {
+ "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation");
}
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
if (project.hasProperty(SPOTLESS_MODERN)) {
if (project.getProviders().gradleProperty(SPOTLESS_MODERN).isPresent()) {
project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it.");
}
// make sure there's a `clean` and a `check`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ prettierConfig['printWidth'] = 20
prettierConfig['parser'] = 'typescript'

// the executable names
def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'
def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'
def npmExec = providers.systemProperty('os.name').get().toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'
def nodeExec = providers.systemProperty('os.name').get().toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'

spotless {
format 'mytypescript', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ prettierConfig['printWidth'] = 20
prettierConfig['parser'] = 'typescript'

// the executable name
def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'
def npmExec = providers.systemProperty('os.name').get().toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'

spotless {
typescript {
Expand Down
14 changes: 9 additions & 5 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ dependencyResolutionManagement {
}
}

if (System.env['CI'] != null) {
def env(String key) {
return providers.environmentVariable(key).orNull
}

if (env('CI') != null) {
// use the remote buildcache on all CI builds
buildCache {
def cred = {
if (System.env[it] != null) {
return System.env[it]
if (env(it) != null) {
return env(it)
} else {
return System.getProperty(it)
return providers.systemProperty(it)
}
}
remote(HttpBuildCache) {
Expand Down Expand Up @@ -92,6 +96,6 @@ def getStartProperty(java.lang.String name) {
return userProperties.get(name)
}

if (System.getenv('SPOTLESS_EXCLUDE_MAVEN') != 'true' && getStartProperty('SPOTLESS_EXCLUDE_MAVEN') != 'true') {
if (env('SPOTLESS_EXCLUDE_MAVEN') != 'true' && getStartProperty('SPOTLESS_EXCLUDE_MAVEN') != 'true') {
include 'plugin-maven' // maven-specific glue code
}

0 comments on commit 1f5efd1

Please sign in to comment.