Skip to content

Commit

Permalink
Issue checkstyle#32: Main
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc committed Jul 27, 2017
1 parent bc20f0f commit 0a07ca2
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ matrix:
- CMD8="mvn test -Dtest=ExtractInfoGeneratorTest#generateExtractInfoFile"
- CMD="$CMD1$CMD2$CMD3$CMD4$CMD5$CMD6$CMD7$CMD8"

# package no error (oraclejdk8)
- jdk: oraclejdk8
env:
- DESC="package no error"
- CMD="mvn clean package -Passembly"

script: eval $CMD

after_success:
Expand Down
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ environment:
- JAVA_HOME: C:\Program Files\Java\jdk1.8.0
DESC: "verify without checkstyle"
CMD1: "mvn -e verify -Dcheckstyle.ant.skip=true -Dcheckstyle.skip=true"
# package no error
- JAVA_HOME: C:\Program Files\Java\jdk1.8.0
DESC: "package no error"
CMD1: "mvn clean package -Passembly"

build_script:
- ps: >
Expand Down
3 changes: 3 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"http://www.puppycrawl.com/dtds/import_control_1_1.dtd">

<import-control pkg="com.github.checkstyle.regression">
<allow pkg="org.apache.commons.cli" local-only="true"/>
<allow pkg="com.github.checkstyle.regression" local-only="true"/>
<allow pkg="java.text" local-only="true"/>
<allow pkg="java.io"/>
<allow pkg="java.nio"/>
<allow pkg="org.apache.commons.io"/>
Expand Down
81 changes: 81 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@
</execution>
</executions>
</plugin>

<!-- Ensure the manifest has all the gory details -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -261,6 +283,65 @@
</reporting>

<profiles>
<profile>
<!-- To be used during development. Run the command-->
<!-- mvn -Passembly package -->
<id>assembly</id>
<properties>
<skipTests>true</skipTests>
<checkstyle.ant.skip>true</checkstyle.ant.skip>
<checkstyle.skip>true</checkstyle.skip>
<pmd.skip>true</pmd.skip>
<findbugs.skip>true</findbugs.skip>
<xml.skip>true</xml.skip>
<forbiddenapis.skip>true</forbiddenapis.skip>
<cobertura.skip>true</cobertura.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
<linkcheck.skip>true</linkcheck.skip>
<!-- difference from "no-validations" -->
<maven.site.skip>true</maven.site.skip>
</properties>

<build>
<plugins>
<!-- Creates the all inclusive uber jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>all</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.github.checkstyle.regression.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</profile>
<profile>
<id>cobertura-check</id>
<activation>
Expand Down
204 changes: 203 additions & 1 deletion src/main/java/com/github/checkstyle/regression/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,51 @@

package com.github.checkstyle.regression;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.immutables.value.Value;

import com.github.checkstyle.regression.configuration.ConfigGenerator;
import com.github.checkstyle.regression.data.GitChange;
import com.github.checkstyle.regression.data.ModuleExtractInfo;
import com.github.checkstyle.regression.data.ModuleInfo;
import com.github.checkstyle.regression.extract.ExtractInfoProcessor;
import com.github.checkstyle.regression.git.DiffParser;
import com.github.checkstyle.regression.module.ModuleCollector;
import com.github.checkstyle.regression.module.ModuleUtils;
import com.github.checkstyle.regression.report.ReportGenerator;

/**
* Utility class, contains main function and its auxiliary routines.
* @author LuoLiangchen
*/
public final class Main {
/** Option name of the local checkstyle repository path. */
private static final String OPT_LOCAL_GIT_REPO = "localGitRepo";

/** Option name of the PR branch name. */
private static final String OPT_PATCH_BRANCH = "patchBranch";

/** Option name of checkstyle-tester path. */
private static final String OPT_CHECKSTYLE_TESTER_PATH = "checkstyleTesterPath";

/** Option name of whether to stop after generating config. */
private static final String OPT_STOP_AFTER_GENERATE_CONFIG = "stopAfterGenerateConfig";

/** Prevents instantiation. */
private Main() {
}
Expand All @@ -34,6 +74,168 @@ private Main() {
* @throws Exception execute failure
*/
public static void main(String[] args) throws Exception {
// empty for now
final Options options = createOptions();
final CommandLineParser parser = new DefaultParser();
final HelpFormatter formatter = new HelpFormatter();
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
}
catch (ParseException ex) {
System.err.println(ex.getMessage());
formatter.printHelp("java -jar regression-tool.jar", options, true);
System.exit(1);
}

final Arguments arguments = ImmutableArguments.builder()
.repoPath(cmd.getOptionValue(OPT_LOCAL_GIT_REPO))
.branch(cmd.getOptionValue(OPT_PATCH_BRANCH))
.checkstyleTesterPath(
Optional.ofNullable(cmd.getOptionValue(OPT_CHECKSTYLE_TESTER_PATH)))
.stopAfterGenerateConfig(cmd.hasOption(OPT_STOP_AFTER_GENERATE_CONFIG))
.build();

validateArguments(arguments);
runRegression(arguments);
}

/**
* Creates and initializes the {@link Options} instance.
* @return the initialized options
*/
private static Options createOptions() {
final Options options = new Options();

final Option repo = Option.builder("r")
.longOpt(OPT_LOCAL_GIT_REPO)
.required()
.hasArg()
.desc("the path of the checkstyle repository")
.build();
repo.setRequired(true);
options.addOption(repo);

final Option branch = Option.builder("p")
.longOpt(OPT_PATCH_BRANCH)
.required()
.hasArg()
.desc("the name of the PR branch")
.build();
options.addOption(branch);

final Option tester = Option.builder("t")
.longOpt(OPT_CHECKSTYLE_TESTER_PATH)
.required(false)
.hasArg()
.desc("the path of the checkstyle-tester directory")
.build();
options.addOption(tester);

final Option stopAfterGenerateConfig = Option.builder()
.longOpt(OPT_STOP_AFTER_GENERATE_CONFIG)
.required(false)
.hasArg(false)
.desc("indicates that regression tool would stop after generating config")
.build();
options.addOption(stopAfterGenerateConfig);

return options;
}

/**
* Validates the parsed CLI arguments.
* @param args the parsed CLI arguments.
* @throws IllegalArgumentException the arguments are invalid
*/
private static void validateArguments(Arguments args) {
if (!existAndIsDirectory(args.repoPath())) {
throw new IllegalArgumentException(
"path of local git repo must exist and be a directory");
}
if (!args.stopAfterGenerateConfig()) {
if (args.checkstyleTesterPath().isPresent()) {
if (!existAndIsDirectory(args.checkstyleTesterPath().get())) {
throw new IllegalArgumentException(
"path of checkstyle tester must exist and be a directory");
}
}
else {
throw new IllegalArgumentException("missing checkstyleTesterPath, "
+ "which is required if you are not using --stopAfterGenerateConfig mode");
}
}
}

/**
* Runs the regression tool.
* @param args the parsed CLI arguments.
* @throws Exception execute failure
*/
private static void runRegression(Arguments args) throws Exception {
final File config = generateConfig(args);
System.out.println("config generated at " + config.getAbsolutePath());
if (!args.stopAfterGenerateConfig()) {
final File report = ReportGenerator.generate(
args.checkstyleTesterPath().get(), args.repoPath(), args.branch(), config);
System.out.println("report generated at " + report.getAbsolutePath());
}
}

/**
* Generates the config file.
* @param args the parsed CLI arguments.
* @return the generated config file
* @throws Exception generation failure
*/
private static File generateConfig(Arguments args)
throws Exception {
final List<GitChange> changes = DiffParser.parse(args.repoPath(), args.branch());
final Map<String, ModuleExtractInfo> extractInfos =
ExtractInfoProcessor.getModuleExtractInfos(args.repoPath(), args.branch());
ModuleUtils.setNameToModuleExtractInfo(extractInfos);
final List<ModuleInfo> moduleInfos = ModuleCollector.generate(changes);
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
final String configFileName =
String.format("config-%s-%s.xml", args.branch(), format.format(new Date()));
return ConfigGenerator.generateConfig(configFileName, moduleInfos);
}

/**
* Checks whether the file in given path exists and is a directory.
* @param path the path to check
* @return true if file exists and is a directory
*/
private static boolean existAndIsDirectory(String path) {
final File file = new File(path);
return !path.isEmpty() && file.exists() && file.isDirectory();
}

/** Represents the CLI arguments. */
@Value.Immutable
/* default */ interface Arguments {
/**
* The local checkstyle repository path.
* @return the local checkstyle repository path
*/
String repoPath();

/**
* The PR branch name.
* @return the PR branch name
*/
String branch();

/**
* Checkstyle-tester path.
* @return Checkstyle-tester path
*/
Optional<String> checkstyleTesterPath();

/**
* Whether to stop after generating config.
* @return whether to stop after generating config
*/
boolean stopAfterGenerateConfig();
}
}

0 comments on commit 0a07ca2

Please sign in to comment.