Skip to content
This repository has been archived by the owner on Mar 21, 2019. It is now read-only.

Commit

Permalink
Setting default class path if not provided by user
Browse files Browse the repository at this point in the history
  • Loading branch information
manupsunny committed Apr 13, 2016
1 parent 21123d3 commit e1f05f5
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 178 deletions.
2 changes: 1 addition & 1 deletion plugin.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
siteUrl='https://github.com/manupsunny/gauge-gradle-plugin'
gitUrl='https://github.com/manupsunny/gauge-gradle-plugin.git'

version=1.2.0
version=1.4.0
name=gauge-gradle-plugin
description='Gradle plugin for Gauge, the open source test automation tool developed by ThoughtWorks.'

Expand Down
1 change: 0 additions & 1 deletion plugin/bintray.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

uploadArchives {
repositories {
mavenDeployer {
Expand Down
4 changes: 3 additions & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
apply plugin: 'maven'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'com.jfrog.bintray'

dependencies {
compile gradleApi()
compile 'org.apache.commons:commons-io:1.3.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile 'org.mockito:mockito-core:1.10.19'
compile gradleApi()
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;

public class GaugePlugin implements Plugin<Project> {

public static final String GAUGE = "gauge";
private static final String GAUGE = "gauge";

@Override
public void apply(Project project) {
project.getExtensions().create(GAUGE, GaugeExtension.class);
project.getTasks().create(GAUGE, GaugeTask.class);
GaugeTask gaugeTask = project.getTasks().create(GAUGE, GaugeTask.class);
Task compileTestJava = project.getTasks().findByName("testClasses");

if (compileTestJava != null) {
gaugeTask.dependsOn(compileTestJava);
}
}
}
28 changes: 7 additions & 21 deletions plugin/src/main/java/com/thoughtworks/gauge/gradle/GaugeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@

import java.io.IOException;

@SuppressWarnings("WeakerAccess")
public class GaugeTask extends DefaultTask {
private final Logger log = LoggerFactory.getLogger("gauge");
private GaugeExtension extension;

@TaskAction
public void gauge() {
Project project = getProject();
extension = project.getExtensions().findByType(GaugeExtension.class);
GaugeExtension extension = project.getExtensions().findByType(GaugeExtension.class);
PropertyManager propertyManager = new PropertyManager(project, extension);
propertyManager.setProperties();

ProcessBuilder builder = new ProcessBuilderFactory(extension).create();
info("Executing command => " + builder.command());
ProcessBuilderFactory processBuilderFactory = new ProcessBuilderFactory(extension);
ProcessBuilder builder = processBuilderFactory.create();
log.info("Executing command => " + builder.command());

try {
Process process = builder.start();
executeGaugeSpecs(process);
Expand All @@ -52,7 +54,7 @@ public void gauge() {
}
}

public void executeGaugeSpecs(Process process) throws GaugeExecutionFailedException {
void executeGaugeSpecs(Process process) throws GaugeExecutionFailedException {
try {
Util.inheritIO(process.getInputStream(), System.out);
Util.inheritIO(process.getErrorStream(), System.err);
Expand All @@ -63,20 +65,4 @@ public void executeGaugeSpecs(Process process) throws GaugeExecutionFailedExcept
throw new GaugeExecutionFailedException(e);
}
}

private void warn(String format, String... args) {
log.warn(String.format(format, args));
}

private void debug(String format, String... args) {
log.debug(String.format(format, args));
}

private void error(String format, String... args) {
log.error(String.format(format, args));
}

private void info(String format, String... args) {
log.info(String.format(format, args));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge-maven-plugin.

// Gauge-maven-plugin is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge-maven-plugin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
/*
* Copyright 2015 Manu Sunny
*
* This file is part of Gauge-gradle-plugin.
*
* Gauge-gradle-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gauge-gradle-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gauge-gradle-plugin. If not, see <http://www.gnu.org/licenses/>.
*/

package com.thoughtworks.gauge.gradle.exception;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright 2015 Manu Sunny
*
* This file is part of Gauge-gradle-plugin.
*
* Gauge-gradle-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gauge-gradle-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gauge-gradle-plugin. If not, see <http://www.gnu.org/licenses/>.
*/

package com.thoughtworks.gauge.gradle.util;

import com.thoughtworks.gauge.gradle.GaugeExtension;
Expand All @@ -10,40 +29,37 @@
import java.util.Arrays;

public class ProcessBuilderFactory {
private final Logger log = LoggerFactory.getLogger("gauge");
private GaugeExtension extension;
private ProcessBuilder builder;

private static final String TAGS_FLAG = "--tags";
private static final String GAUGE = "gauge";
private static final String PARALLEL_FLAG = "--parallel";
private static final String DEFAULT_SPECS_DIR = "specs";
private static final String NODES_FLAG = "-n";
private static final String ENV_FLAG = "--env";
private static final String GAUGE_CUSTOM_CLASSPATH_ENV = "gauge_custom_classpath";
private static final String NODE_FLAG = "-n";
private static final String TAGS_FLAG = "--tags";
private static final String SPECS_FLAG = "specs";
private static final String PARALLEL_FLAG = "--parallel";
private static final String CUSTOM_CLASSPATH = "gauge_custom_classpath";

private final Logger log = LoggerFactory.getLogger(GAUGE);
private GaugeExtension extension;

public ProcessBuilderFactory(GaugeExtension extension) {
this.extension = extension;
}

public ProcessBuilder create() {
builder = new ProcessBuilder();
ProcessBuilder builder = new ProcessBuilder();
builder.command(createGaugeCommand());

setClasspath(builder);
return builder;
}

private void setClasspath(java.lang.ProcessBuilder builder) {
private void setClasspath(ProcessBuilder builder) {
String classpath = extension.getClasspath();
if (classpath == null) {
classpath = "";
}
debug("Setting Custom classpath => %s", classpath);
builder.environment().put(GAUGE_CUSTOM_CLASSPATH_ENV, classpath);

log.debug("Setting Custom classpath => %s", classpath);
builder.environment().put(CUSTOM_CLASSPATH, classpath);
}

public ArrayList<String> createGaugeCommand() {
private ArrayList<String> createGaugeCommand() {
ArrayList<String> command = new ArrayList<>();
command.add(GAUGE);
addTags(command);
Expand Down Expand Up @@ -75,7 +91,7 @@ private void addParallelFlags(ArrayList<String> command) {
if (inParallel != null && inParallel) {
command.add(PARALLEL_FLAG);
if (nodes != null && nodes != 0) {
command.add(NODES_FLAG);
command.add(NODE_FLAG);
command.add(Integer.toString(nodes));
}
}
Expand All @@ -88,15 +104,15 @@ private void addSpecsDir(ArrayList<String> command) {
validateSpecsDirectory(specsDirectoryPath);
command.add(specsDirectoryPath);
} else {
warn("Property 'specsDir' not set. Using default value => '%s'", DEFAULT_SPECS_DIR);
command.add(DEFAULT_SPECS_DIR);
log.warn("Property 'specsDir' not set. Using default value => '%s'", "specs");
command.add(SPECS_FLAG);
}
}

private void validateSpecsDirectory(String specsDirectoryPath) {
File specsDirectory = new File(specsDirectoryPath);
if (!specsDirectory.exists()) {
error("Specs directory specified is not existing!");
log.error("Specs directory specified is not existing!");
throw new GaugeExecutionFailedException("Specs directory specified is not existing!");
}
}
Expand All @@ -108,20 +124,4 @@ private void addTags(ArrayList<String> command) {
command.add(tags);
}
}

private void warn(String format, String... args) {
log.warn(String.format(format, args));
}

private void debug(String format, String... args) {
log.debug(String.format(format, args));
}

private void error(String format, String... args) {
log.error(String.format(format, args));
}

private void info(String format, String... args) {
log.info(String.format(format, args));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
/*
* Copyright 2015 Manu Sunny
*
* This file is part of Gauge-gradle-plugin.
*
* Gauge-gradle-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gauge-gradle-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gauge-gradle-plugin. If not, see <http://www.gnu.org/licenses/>.
*/

package com.thoughtworks.gauge.gradle.util;

import com.thoughtworks.gauge.gradle.GaugeExtension;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;

import java.io.File;
import java.util.List;
import java.util.Map;

public class PropertyManager {
public static final String SPECS_DIR = "specsDir";
public static final String IN_PARALLEL = "inParallel";
public static final String TAGS = "tags";
public static final String ENV = "env";
public static final String NODES = "nodes";
public static final String ADDITIONAL_FLAGS = "additionalFlags";
public static final String CLASSPATH = "classpath";
private static final String ENV = "env";
private static final String TAGS = "tags";
private static final String NODES = "nodes";
private static final String SPECS_DIR = "specsDir";
private static final String CLASSPATH = "classpath";
private static final String IN_PARALLEL = "inParallel";
private static final String ADDITIONAL_FLAGS = "additionalFlags";

private Project project;
private GaugeExtension extension;
Expand Down Expand Up @@ -43,7 +66,7 @@ private void setSpecsDir(Map<String, ?> properties) {
private void setInParallel(Map<String, ?> properties) {
String inParallel = (String) properties.get(IN_PARALLEL);
if (inParallel != null) {
extension.setInParallel("true".equals(inParallel));
extension.setInParallel(inParallel.equals("true"));
}
}

Expand Down Expand Up @@ -76,9 +99,23 @@ private void setAdditionalFlags(Map<String, ?> properties) {
}

private void setClasspath(Map<String, ?> properties) {
String classpath = (String) properties.get(CLASSPATH);
List<String> classpath = (List<String>) properties.get(CLASSPATH);

if (classpath != null) {
extension.setClasspath(classpath);
extension.setClasspath(StringUtils.join(classpath, File.pathSeparator));
} else {
extension.setClasspath(getClasspath(project));
}
}

private String getClasspath(Project project) {
Configuration runtime = project.getConfigurations().getByName("runtime");
String jarPaths = runtime.getAsFileTree().getAsPath();

String mainClassPath = new File(System.getProperty("user.dir") + "/build/classes/main").getAbsolutePath();
String testClassPath = new File(System.getProperty("user.dir") + "/build/classes/test").getAbsolutePath();
String javaClassPath = mainClassPath + File.pathSeparator + testClassPath;

return javaClassPath + File.pathSeparator + jarPaths;
}
}
Loading

0 comments on commit e1f05f5

Please sign in to comment.