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 20, 2017
1 parent bc20f0f commit 030220e
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"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.io"/>
<allow pkg="java.nio"/>
<allow pkg="org.apache.commons.io"/>
Expand Down
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,55 @@
</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.puppycrawl.tools.checkstyle.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</profile>
<profile>
<id>cobertura-check</id>
<activation>
Expand Down
123 changes: 121 additions & 2 deletions src/main/java/com/github/checkstyle/regression/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,140 @@

package com.github.checkstyle.regression;

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

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 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 repository path. */
private static final String OPT_REPO = "repo";

/** Option name of branch name. */
private static final String OPT_BRANCH = "branch";

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

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

/** Prevents instantiation. */
private Main() {
}

/**
* Executes CLI command.
* @param args the CLI arguments
* @throws Exception execute failure
* @throws Exception generateConfig 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();
final CommandLine cmd;

try {
cmd = parser.parse(options, args);
final String repoPath = cmd.getOptionValue(OPT_REPO);
final String branch = cmd.getOptionValue(OPT_BRANCH);
final boolean stopAfterGenerateConfig = cmd.hasOption(OPT_STOP_AFTER_GENERATE_CONFIG);

final File config = generateConfig(repoPath, branch);
System.out.println("config generated at " + config.getAbsolutePath());
if (!stopAfterGenerateConfig) {
final String testerPath = cmd.getOptionValue(OPT_TESTER);
final File report = ReportGenerator.generate(testerPath, repoPath, branch, config);
System.out.println("report generated at " + report.getAbsolutePath());
}
}
catch (ParseException ex) {
System.err.println(ex.getMessage());
formatter.printHelp(null, options);
System.exit(1);
}
}

/**
* 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_REPO)
.required()
.hasArg()
.desc("the path of the checkstyle repository")
.build();
repo.setRequired(true);
options.addOption(repo);

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

final Option tester = Option.builder("t")
.longOpt(OPT_TESTER)
.required()
.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;
}

/**
* Generates the config file.
* @param repoPath the path of the checkstyle repository
* @param branch the name of the PR branch
* @return the generated config file
* @throws Exception generation failure
*/
private static File generateConfig(String repoPath, String branch)
throws Exception {
final List<GitChange> changes = DiffParser.parse(repoPath, branch);
final Map<String, ModuleExtractInfo> extractInfos =
ExtractInfoProcessor.getModuleExtractInfos(repoPath, branch);
ModuleUtils.setNameToModuleExtractInfo(extractInfos);
final List<ModuleInfo> moduleInfos = ModuleCollector.generate(changes);
final String configFileName =
String.format("config-%s-%d.xml", branch, System.currentTimeMillis());
return ConfigGenerator.generateConfig(configFileName, moduleInfos);
}
}

0 comments on commit 030220e

Please sign in to comment.