Skip to content

Commit

Permalink
Add circleci plugin (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 authored and iamdanfox committed Jul 30, 2018
1 parent b71950e commit e344eed
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ apply plugin: 'com.palantir.baseline-idea'
apply plugin: 'org.inferred.processors' // installs the "processor" configuration needed for baseline-error-prone
apply plugin: 'com.palantir.baseline-error-prone'
apply plugin: 'com.palantir.baseline-class-uniqueness'
apply plugin: 'com.palantir.baseline-circleci'
```

- Run ``./gradlew baselineUpdateConfig`` to download the config files
Expand Down Expand Up @@ -275,6 +276,12 @@ configurations.all {
}
```

### CircleCi Plugin (com.palantir.baseline-circleci)

Applies [`com.palantir.circle.style`](https://github.com/palantir/gradle-circle-style) plugin which configures junit xml test output to be written to $CIRCLE_TEST_REPORTS directory.
Additionally enables html reports for tests and stores the output in $CIRCLE_ARTIFACTS and if gradle is run with `--profile`
the profiling output is also persisted in $CIRCLE_ARTIFACTS.


### Copyright Checks

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
repositories {
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
gradlePluginPortal()
maven { url "http://palantir.bintray.com/releases" }
}

dependencies {
classpath 'gradle.plugin.com.palantir:gradle-circle-style:1.1.2'
classpath 'gradle.plugin.com.palantir:gradle-circle-style:1.1.4'
classpath 'com.gradle.publish:plugin-publish-plugin:0.9.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3'
classpath 'com.netflix.nebula:nebula-dependency-recommender:6.0.0'
Expand Down Expand Up @@ -37,8 +37,8 @@ dependencies {
allprojects {
repositories {
jcenter()
gradlePluginPortal()
maven { url "http://palantir.bintray.com/releases" }
maven { url "https://plugins.gradle.org/m2/" }
}

apply plugin: 'org.inferred.processors'
Expand Down
16 changes: 11 additions & 5 deletions gradle-baseline-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ apply plugin: 'java-gradle-plugin'
apply from: "${rootDir}/gradle/publish-libs.gradle"
apply from: "${rootDir}/gradle/java.gradle"

repositories {
jcenter()
}

dependencies {
compile gradleApi()
compile 'net.ltgt.gradle:gradle-errorprone-plugin'
compile 'com.google.guava:guava'
compile 'gradle.plugin.com.palantir:gradle-circle-style'
compile 'net.ltgt.gradle:gradle-errorprone-plugin'

testCompile gradleTestKit()
testCompile 'com.netflix.nebula:nebula-test' // for better temp directory junit rule only
testCompile 'net.lingala.zip4j:zip4j'
testCompile 'org.apache.commons:commons-io'
}

test {
environment 'CIRCLE_ARTIFACTS', "${buildDir}/artifacts"
environment 'CIRCLE_TEST_REPORTS', "${buildDir}/circle-reports"
}

// Create version file included in jar so that the plugin can know about its own version
task createVersionFile {
def versionFile = file("src/main/resources/baseline-version.txt")
Expand Down Expand Up @@ -58,6 +60,10 @@ pluginBundle {
id = 'com.palantir.baseline-class-uniqueness'
displayName = 'Palantir Baseline Class Uniqueness Plugin'
}
baselineCircleCiPlugin {
id = 'com.palantir.baseline-circleci'
displayName = 'Palantir Baseline CircleCi Plugin'
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Baseline implements Plugin<Project> {
project.plugins.apply BaselineEclipse
project.plugins.apply BaselineIdea
project.plugins.apply BaselineErrorProne
project.plugins.apply BaselineCircleCi

// TODO(dfox): enable this when it has been validated on a few real projects
// project.plugins.apply BaselineClassUniquenessPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* (c) Copyright 2018 Palantir Technologies 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.palantir.baseline.plugins;

import com.palantir.gradle.circlestyle.CircleStylePlugin;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import org.gradle.api.Project;
import org.gradle.api.tasks.testing.Test;
import org.gradle.profile.ProfileListener;
import org.gradle.profile.ProfileReportRenderer;

public final class BaselineCircleCi extends AbstractBaselinePlugin {
private static final SimpleDateFormat FILE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
private static final FileAttribute<Set<PosixFilePermission>> PERMS_ATTRIBUTE =
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x"));

@Override
public void apply(Project project) {
project.getPluginManager().apply(CircleStylePlugin.class);
String circleArtifactsDir = System.getenv("CIRCLE_ARTIFACTS");
if (circleArtifactsDir == null) {
return;
}

try {
Files.createDirectories(Paths.get(circleArtifactsDir), PERMS_ATTRIBUTE);
} catch (IOException e) {
throw new RuntimeException("failed to create CIRCLE_ARTIFACTS directory", e);
}

project.getRootProject().allprojects(proj ->
proj.getTasks().withType(Test.class, test -> {
Path junitReportsDir = Paths.get(circleArtifactsDir, "junit");
for (String component : test.getPath().substring(1).split(":")) {
junitReportsDir = junitReportsDir.resolve(component);
}
test.getReports().getHtml().setEnabled(true);
test.getReports().getHtml().setDestination(junitReportsDir.toFile());
}));

if (project.getGradle().getStartParameter().isProfile()) {
project.getGradle().addListener((ProfileListener) buildProfile -> {
ProfileReportRenderer renderer = new ProfileReportRenderer();
File file = Paths.get(circleArtifactsDir, "profile", "profile-"
+ FILE_DATE_FORMAT.format(new Date(buildProfile.getBuildStarted())) + ".html").toFile();
renderer.writeTo(buildProfile, file);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.palantir.baseline.plugins

import groovy.text.SimpleTemplateEngine
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.plugins.ide.eclipse.EclipsePlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.palantir.baseline.plugins

import net.ltgt.gradle.errorprone.ErrorPronePlugin
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=com.palantir.baseline.plugins.BaselineCircleCi
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class AbstractPluginTest extends Specification {
TemporaryFolder folder = new TemporaryFolder();

File buildFile
File settingsFile
MultiProjectIntegrationHelper multiProject
File projectDir

def setup() {
projectDir = folder.getRoot()
buildFile = file('build.gradle')
settingsFile = file('settings.gradle')
println("Build directory: \n" + projectDir.absolutePath)
multiProject = new MultiProjectIntegrationHelper(projectDir, file("settings.gradle"))
multiProject = new MultiProjectIntegrationHelper(projectDir, settingsFile)
}

GradleRunner with(String... tasks) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2018 Palantir Technologies, 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.palantir.baseline


import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome

class BaselineCircleCiIntegrationTest extends AbstractPluginTest {
def standardBuildFile = '''
plugins {
id 'java'
id 'com.palantir.baseline-circleci'
}
repositories {
jcenter()
}
dependencies {
testCompile 'junit:junit:4.12'
}
'''.stripIndent()

def javaFile = '''
package test;
import org.junit.Test;
public class TestClass {
@Test
public void test() {}
}
'''.stripIndent()

def setup() {
new File(System.getenv('CIRCLE_ARTIFACTS')).toPath().deleteDir()
}

def 'collects html reports'() {
when:
buildFile << standardBuildFile
file('src/test/java/test/TestClass.java') << javaFile

String artifacts = System.getenv('CIRCLE_ARTIFACTS')
then:
BuildResult result = with('test').build()
result.task(':test').outcome == TaskOutcome.SUCCESS
new File(new File(artifacts, 'junit'), 'test').list().toList().toSet() == ['classes', 'css', 'index.html', 'js', 'packages'].toSet()
}

def 'collects build profiles'() {
when:
buildFile << standardBuildFile

String artifacts = System.getenv('CIRCLE_ARTIFACTS')
Set<String> defaultDirs = ['js', 'css'].toSet()
then:
BuildResult result = with('build', '--profile').build()
result.task(':build').outcome == TaskOutcome.SUCCESS
Set<String> files = new File(artifacts, 'profile').list().toList().toSet()
files.size() == 3
files.containsAll(defaultDirs)
files.removeAll(defaultDirs)
String profileFile = files.iterator().next()
profileFile.startsWith("profile-")
profileFile.endsWith(".html")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2018 Palantir Technologies, 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.palantir.baseline


import com.palantir.baseline.plugins.BaselineCircleCi
import com.palantir.gradle.circlestyle.CircleStylePlugin
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class BaselineCircleCiTest extends Specification {
private Project project

def setup() {
project = ProjectBuilder.builder().build()
project.plugins.apply 'java'
project.plugins.apply BaselineCircleCi
}

def baselineCircleCiApplied() {
expect:
project.plugins.hasPlugin(BaselineCircleCi.class)
}

def circleStylePluginApplied() {
expect:
project.plugins.hasPlugin(CircleStylePlugin.class)
}

}
1 change: 1 addition & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ com.google.guava:guava = 21.0
com.palantir.baseline:* = 0.24.0
com.palantir.safe-logging:* = 1.4.0
org.slf4j:slf4j-api = 1.7.24
gradle.plugin.com.palantir:gradle-circle-style = 1.1.4
net.ltgt.gradle:gradle-errorprone-plugin = 0.0.14

# test deps
Expand Down

0 comments on commit e344eed

Please sign in to comment.