com.sfhuskie.plugins.githooks.io.ScriptMaker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of githooks-maven-plugin Show documentation
Show all versions of githooks-maven-plugin Show documentation
Creates or appends git hook scripts
The newest version!
package com.sfhuskie.plugins.githooks.io;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
/*
* 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.
*/
/**
* @author Steven Tong
*
*/
import java.util.List;
import org.apache.maven.shared.utils.io.FileUtils;
import com.sfhuskie.plugins.githooks.MojoSettings;
/** Create standalone executable scripts
* @author Steven Tong
*
*/
public class ScriptMaker {
List tools;
List hooks;
/**
* @param tools list of tools
* @param hooks list of hooks
*/
public ScriptMaker(List tools, List hooks) {
this.tools = tools;
this.hooks = hooks;
}
/**
* @return List of lines
* @throws IOException i/o exception
*/
public List getCommands(File script) throws IOException {
// Checkstyle configured so get commands for checkstyle
if (this.tools.contains(MojoSettings.CHECKSTYLE)) {
List commands = new ArrayList();
List javaCheck = FileIO.readFileTemplateFromPath("java-check");
for (String line: javaCheck) {
commands.add(line);
}
commands.add(MojoSettings.getCheckstyleCommandToAdd());
return commands;
}
throw new RuntimeException("Tool not supported.");
}
/** Write scripts based on the values in the hooks attribute
* @param destDir Directory to write files to
* @throws IOException i/o exception
*/
public void generateScripts(File destDir) throws IOException {
if (this.hooks.contains(MojoSettings.PRECOMMIT)) {
File precommit = new File(destDir.getAbsolutePath()+"/"+MojoSettings.PRECOMMIT);
createScript(precommit, destDir);
}
if (this.hooks.contains(MojoSettings.PREPUSH)) {
File prepush = new File(destDir.getAbsolutePath()+"/"+MojoSettings.PREPUSH);
createScript(prepush, destDir);
}
}
protected void createScript(File script, File destDir) throws IOException {
List lines = getCommands(script);
FileUtils.forceMkdir(destDir);
FileIO.writeLines(script, lines);
if (!script.canExecute()) {
script.setExecutable(true);
}
}
}