forked from checkstyle/regression-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#37: inject, invoke maven command, read file and clea…
…n up
- Loading branch information
Showing
8 changed files
with
234 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/main/java/com/github/checkstyle/regression/extract/CheckstyleInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library 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 | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.checkstyle.regression.extract; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
|
||
/** | ||
* Injects files to checkstyle repository, which would be invoked by maven command | ||
* to generate module extract info. | ||
* @author LuoLiangchen | ||
*/ | ||
final class CheckstyleInjector implements Closeable { | ||
/** The path to checkstyle repository. */ | ||
private final String repoPath; | ||
|
||
/** The name of PR branch. */ | ||
private final String branch; | ||
|
||
/** The checkstyle repository. */ | ||
private final Repository repository; | ||
|
||
/** | ||
* Creates a new instance of CheckstyleInjector. | ||
* @param repoPath the path to checkstyle repository | ||
* @param branch the name of PR branch | ||
*/ | ||
CheckstyleInjector(String repoPath, String branch) { | ||
this.repoPath = repoPath; | ||
this.branch = branch; | ||
|
||
final File gitDir = new File(repoPath, ".git"); | ||
final Repository repo; | ||
try { | ||
repo = new FileRepositoryBuilder().setGitDir(gitDir) | ||
.readEnvironment().findGitDir().build(); | ||
} | ||
catch (IOException ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
repository = repo; | ||
} | ||
|
||
/** | ||
* Generates the module extract info file. | ||
* @return the module extract info file | ||
* @throws IOException failure of generation | ||
* @throws GitAPIException JGit library exception | ||
*/ | ||
public File generateExtractInfoFile() throws IOException, GitAPIException { | ||
checkoutToPrBranch(); | ||
copyInjectFilesToCheckstyleRepo(); | ||
invokeMavenCommand(); | ||
return null; | ||
} | ||
|
||
/** | ||
* Clears the injected files and the generated info file in checkstyle repository. | ||
* @throws GitAPIException JGit library exception | ||
*/ | ||
public void clearInjections() throws GitAPIException { | ||
final Git git = new Git(repository); | ||
|
||
try { | ||
git.checkout().addPath(".").call(); | ||
} | ||
finally { | ||
git.close(); | ||
} | ||
} | ||
|
||
/** Closes the repository resource. */ | ||
@Override | ||
public void close() { | ||
repository.close(); | ||
} | ||
|
||
/** | ||
* Invokes Maven command to generate the extract info file in checkstyle repository. | ||
*/ | ||
private static void invokeMavenCommand() { | ||
// invoke maven command to generate the extract info file | ||
} | ||
|
||
/** | ||
* Copies the injection files to checkstyle repository. | ||
* @throws IOException failure of copying | ||
*/ | ||
private void copyInjectFilesToCheckstyleRepo() throws IOException { | ||
final File srcDir = | ||
new File("src/main/resources/com/github/checkstyle/regression/extract/"); | ||
final File destDir = new File(repoPath, "src/test/java/com/puppycrawl/tools/checkstyle/"); | ||
final String[] injections = {"ExtractInfoGeneratorTest.java", "Json.java"}; | ||
for (String injection : injections) { | ||
final File srcFile = new File(srcDir, injection); | ||
final File destFile = new File(destDir, injection); | ||
FileUtils.copyFile(srcFile, destFile); | ||
} | ||
} | ||
|
||
/** | ||
* Checkouts to the PR branch in the given repository. | ||
* @throws GitAPIException JGit library exception | ||
*/ | ||
private void checkoutToPrBranch() | ||
throws GitAPIException { | ||
final Git git = new Git(repository); | ||
|
||
try { | ||
git.checkout().setName(branch).call(); | ||
} | ||
finally { | ||
git.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters