diff --git a/.gitignore b/.gitignore index e2358ac..ad9846b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ out .arcconfig /meerkat_election_params_tempfile.dat /meerkat_booth_system_messages.dat +local.properties diff --git a/build.gradle b/build.gradle index ed7d229..17b07c2 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,5 @@ -plugins { - id "com.google.osdetector" version "1.4.0" -} +apply plugin: com.google.gradle.osdetector.OsDetectorPlugin + subprojects { proj -> proj.afterEvaluate { diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 0000000..378559d --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,25 @@ +apply plugin: 'groovy' + +description = 'A Gradle plugin that detects the OS name and architecture, providing a uniform\ + classifier to be used in the names of native artifacts.' +group = 'com.google.gradle' + +// The major and minor versions are aligned with the Maven plugin's. +version = '1.5.0-SNAPSHOT' + +dependencies { + compile gradleApi(), + localGroovy(), + 'kr.motd.maven:os-maven-plugin:1.5.0.Final' + // Newer versions of gradle conflict with the default guava 10. dependency of the maven + // plugin. + compile("com.google.guava:guava:19.0") { + force = true + } + testCompile 'junit:junit:4.12' +} + +repositories { + mavenLocal() + mavenCentral() +} diff --git a/buildSrc/gradlew b/buildSrc/gradlew new file mode 120000 index 0000000..502f5a2 --- /dev/null +++ b/buildSrc/gradlew @@ -0,0 +1 @@ +../gradlew \ No newline at end of file diff --git a/buildSrc/src/main/groovy/com/google/gradle/osdetector/OsDetectorPlugin.groovy b/buildSrc/src/main/groovy/com/google/gradle/osdetector/OsDetectorPlugin.groovy new file mode 100644 index 0000000..915a1f1 --- /dev/null +++ b/buildSrc/src/main/groovy/com/google/gradle/osdetector/OsDetectorPlugin.groovy @@ -0,0 +1,25 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gradle.osdetector + +import org.gradle.api.Plugin +import org.gradle.api.Project + +class OsDetectorPlugin implements Plugin { + void apply(final Project project) { + project.extensions.create('osdetector', OsDetector) + } +} diff --git a/buildSrc/src/main/java/com/google/gradle/osdetector/OsDetector.java b/buildSrc/src/main/java/com/google/gradle/osdetector/OsDetector.java new file mode 100644 index 0000000..941d59b --- /dev/null +++ b/buildSrc/src/main/java/com/google/gradle/osdetector/OsDetector.java @@ -0,0 +1,120 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gradle.osdetector; + +import kr.motd.maven.os.Detector; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +public class OsDetector { + private static final Logger logger = LoggerFactory.getLogger(OsDetector.class.getName()); + + private final List classifierWithLikes = new ArrayList(); + private Impl impl; + + public String getOs() { + return (String) getImpl().detectedProperties.get(Detector.DETECTED_NAME); + } + + public String getArch() { + return (String) getImpl().detectedProperties.get(Detector.DETECTED_ARCH); + } + + public String getClassifier() { + return (String) getImpl().detectedProperties.get(Detector.DETECTED_CLASSIFIER); + } + + public Release getRelease() { + Impl impl = getImpl(); + Object releaseId = impl.detectedProperties.get(Detector.DETECTED_RELEASE); + if (releaseId == null) { + return null; + } + return new Release(impl); + } + + public synchronized void setClassifierWithLikes(List classifierWithLikes) { + if (impl != null) { + throw new IllegalStateException("classifierWithLikes must be set before osdetector is read."); + } + this.classifierWithLikes.clear(); + this.classifierWithLikes.addAll(classifierWithLikes); + } + + private synchronized Impl getImpl() { + if (impl == null) { + impl = new Impl(classifierWithLikes); + } + return impl; + } + + /** + * Accessor to information about the current OS release. + */ + public static class Release { + private final Impl impl; + + private Release(Impl impl) { + this.impl = impl; + } + + /** + * Returns the release ID. + */ + public String getId() { + return (String) impl.detectedProperties.get(Detector.DETECTED_RELEASE); + } + + /** + * Returns the version ID. + */ + public String getVersion() { + return (String) impl.detectedProperties.get(Detector.DETECTED_RELEASE_VERSION); + } + + /** + * Returns {@code true} if this release is a variant of the given base release (for example, + * ubuntu is "like" debian). + */ + public boolean isLike(String baseRelease) { + return impl.detectedProperties.containsKey( + Detector.DETECTED_RELEASE_LIKE_PREFIX + baseRelease); + } + } + + private static class Impl extends Detector { + final Properties detectedProperties = System.getProperties(); + + @Override + protected void log(String message) { + logger.info(message); + } + + @Override + protected void logProperty(String name, String value) { + logger.info(name + "=" + value); + } + + Impl(List classifierWithLikes) { + detect(detectedProperties, classifierWithLikes); + } + } +} diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/com.google.osdetector.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/com.google.osdetector.properties new file mode 100644 index 0000000..b412a19 --- /dev/null +++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/com.google.osdetector.properties @@ -0,0 +1 @@ +implementation-class=com.google.gradle.osdetector.OsDetectorPlugin diff --git a/buildSrc/src/test/groovy/com/google/gradle/osdetector/OsDetectorPluginTest.groovy b/buildSrc/src/test/groovy/com/google/gradle/osdetector/OsDetectorPluginTest.groovy new file mode 100644 index 0000000..3d8a20a --- /dev/null +++ b/buildSrc/src/test/groovy/com/google/gradle/osdetector/OsDetectorPluginTest.groovy @@ -0,0 +1,62 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gradle.osdetector + +import org.gradle.testfixtures.ProjectBuilder +import org.gradle.api.Project +import org.junit.Test +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertNotNull +import static org.junit.Assert.fail + +class OsDetectorPluginTest { + @Test + public void pluginAddsExtensionToProject() { + Project project = ProjectBuilder.builder().build() + project.apply plugin: 'com.google.osdetector' + assertNotNull(project.osdetector) + assertNotNull(project.osdetector.os) + assertNotNull(project.osdetector.arch) + assertEquals(project.osdetector.os + '-' + project.osdetector.arch, + project.osdetector.classifier) + System.err.println('classifier=' + project.osdetector.classifier) + if (project.osdetector.os == 'linux') { + assertNotNull(project.osdetector.release.id) + assertNotNull(project.osdetector.release.version) + System.err.println('release.id=' + project.osdetector.release.id) + System.err.println('release.version=' + project.osdetector.release.version) + System.err.println('release.isLike(debian)=' + project.osdetector.release.isLike('debian')) + System.err.println('release.isLike(redhat)=' + project.osdetector.release.isLike('redhat')) + } else if (project.osdetector.release) { + fail("Should be null") + } + } + + @Test + public void setClassifierWithLikes() { + Project project = ProjectBuilder.builder().build() + project.apply plugin: 'com.google.osdetector' + project.osdetector.classifierWithLikes = ['debian', 'fedora'] + assertNotNull(project.osdetector.os) + assertNotNull(project.osdetector.arch) + System.err.println('classifier=' + project.osdetector.classifier) + try { + project.osdetector.classifierWithLikes = ['debian'] + fail("Should throw IllegalStateException") + } catch (IllegalStateException expected) { + } + } +}