提交 4a3c0808 编写于 作者: B Blankj

Merge branch '1.25.9'

import org.apache.commons.io.FileUtils
import org.apache.groovy.json.internal.ArrayUtils
import org.gradle.BuildListener
import org.gradle.BuildResult
import org.gradle.api.Project
......@@ -10,7 +9,6 @@ import org.gradle.api.execution.TaskExecutionListener
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle
import org.gradle.api.tasks.TaskState
import org.gradle.internal.impldep.org.apache.commons.collections.MapUtils
import java.text.SimpleDateFormat
......@@ -63,6 +61,7 @@ class ConfigUtils {
static addBuildListener(Gradle gradle) {
gradle.addBuildListener(new ConfigBuildListener())
GitUtils.init(gradle)
}
private static class ConfigBuildListener implements BuildListener {
......
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.invocation.Gradle
import java.text.SimpleDateFormat
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/08/16
* desc :
* </pre>
*/
class GitUtils {
private static String sCurBranchName;
static void init(Gradle gradle) {
gradle.rootProject(new Action<Project>() {
@Override
void execute(Project project) {
sCurBranchName = getGitBranch()
addGitPushTask(project)
addGitPushAndMerge2MasterTask(project)
addGitNewBranchTask(project)
}
})
}
static def getGitBranch() {
return ShellUtils.execCmd('git symbolic-ref --short -q HEAD').successMsg
}
static void addGitPushTask(Project project) {
project.task("gitPush", new Action<Task>() {
@Override
void execute(Task task) {
task.doLast {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd")
String date = simpleDateFormat.format(new Date())
GLog.d(ShellUtils.execCmd([
"git add -A",
"git commit -m \"see $date log\"",
"git push origin $sCurBranchName"
] as String[]))
}
}
})
}
static void addGitPushAndMerge2MasterTask(Project project) {
project.task("gitPushAndMerge2Master", new Action<Task>() {
@Override
void execute(Task task) {
task.doLast {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd")
String date = simpleDateFormat.format(new Date())
GLog.d(ShellUtils.execCmd([
"git add -A",
"git commit -m \"see $date log\"",
"git push origin $sCurBranchName",
"git checkout master",
"git merge $sCurBranchName",
"git push origin master",
"git checkout $sCurBranchName",
] as String[]))
}
}
})
}
static void addGitNewBranchTask(Project project) {
project.task("gitNewBranch", new Action<Task>() {
@Override
void execute(Task task) {
task.doLast {
GLog.d(ShellUtils.execCmd([
"git checkout master",
"git checkout -b ${Config.versionName}",
"git push origin ${Config.versionName}:${Config.versionName}",
] as String[]))
}
}
})
}
}
package PACKAGE_NAME;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/08/16
* desc :
* </pre>
*/
public class GitUtils {
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/07
* desc : utils about shell
* </pre>
*/
public final class ShellUtils {
private static final String LINE_SEP = System.getProperty("line.separator");
private ShellUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Execute the command.
*
* @param command The command.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String command) {
return execCmd(new String[]{command}, false, true);
}
/**
* Execute the command.
*
* @param command The command.
* @param isRooted True to use root, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String command, final boolean isRooted) {
return execCmd(new String[]{command}, isRooted, true);
}
/**
* Execute the command.
*
* @param commands The commands.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final List<String> commands) {
return execCmd(commands == null ? null : commands.toArray(new String[]{}), false, true);
}
/**
* Execute the command.
*
* @param commands The commands.
* @param isRooted True to use root, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final List<String> commands, final boolean isRooted) {
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true);
}
/**
* Execute the command.
*
* @param commands The commands.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String[] commands) {
return execCmd(commands, false, true);
}
/**
* Execute the command.
*
* @param commands The commands.
* @param isRooted True to use root, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String[] commands, final boolean isRooted) {
return execCmd(commands, isRooted, true);
}
/**
* Execute the command.
*
* @param command The command.
* @param isRooted True to use root, false otherwise.
* @param isNeedResultMsg True to return the message of result, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String command,
final boolean isRooted,
final boolean isNeedResultMsg) {
return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
}
/**
* Execute the command.
*
* @param commands The commands.
* @param isRooted True to use root, false otherwise.
* @param isNeedResultMsg True to return the message of result, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final List<String> commands,
final boolean isRooted,
final boolean isNeedResultMsg) {
return execCmd(commands == null ? null : commands.toArray(new String[]{}),
isRooted,
isNeedResultMsg);
}
/**
* Execute the command.
*
* @param commands The commands.
* @param isRooted True to use root, false otherwise.
* @param isNeedResultMsg True to return the message of result, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String[] commands,
final boolean isRooted,
final boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, "", "");
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh");
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) continue;
os.write(command.getBytes());
os.writeBytes(LINE_SEP);
os.flush();
}
os.writeBytes("exit" + LINE_SEP);
os.flush();
result = process.waitFor();
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8")
);
errorResult = new BufferedReader(
new InputStreamReader(process.getErrorStream(), "UTF-8")
);
String line;
if ((line = successResult.readLine()) != null) {
successMsg.append(line);
while ((line = successResult.readLine()) != null) {
successMsg.append(LINE_SEP).append(line);
}
}
if ((line = errorResult.readLine()) != null) {
errorMsg.append(line);
while ((line = errorResult.readLine()) != null) {
errorMsg.append(LINE_SEP).append(line);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (successResult != null) {
successResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(
result,
successMsg == null ? "" : successMsg.toString(),
errorMsg == null ? "" : errorMsg.toString()
);
}
/**
* The result of command.
*/
public static class CommandResult {
public int result;
public String successMsg;
public String errorMsg;
public CommandResult(final int result, final String successMsg, final String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
@Override
public String toString() {
return "result: " + result + "\n" +
"successMsg: " + successMsg + "\n" +
"errorMsg: " + errorMsg;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册