Skip to content

Commit 649de08

Browse files
committed
see 08/16 log
1 parent 1be7b42 commit 649de08

File tree

4 files changed

+308
-2
lines changed

4 files changed

+308
-2
lines changed

buildSrc/src/main/groovy/ConfigUtils.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import org.apache.commons.io.FileUtils
2-
import org.apache.groovy.json.internal.ArrayUtils
32
import org.gradle.BuildListener
43
import org.gradle.BuildResult
54
import org.gradle.api.Project
@@ -10,7 +9,6 @@ import org.gradle.api.execution.TaskExecutionListener
109
import org.gradle.api.initialization.Settings
1110
import org.gradle.api.invocation.Gradle
1211
import org.gradle.api.tasks.TaskState
13-
import org.gradle.internal.impldep.org.apache.commons.collections.MapUtils
1412

1513
import java.text.SimpleDateFormat
1614

@@ -63,6 +61,7 @@ class ConfigUtils {
6361

6462
static addBuildListener(Gradle gradle) {
6563
gradle.addBuildListener(new ConfigBuildListener())
64+
GitUtils.init(gradle)
6665
}
6766

6867
private static class ConfigBuildListener implements BuildListener {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import org.gradle.api.Action
2+
import org.gradle.api.Project
3+
import org.gradle.api.Task
4+
import org.gradle.api.invocation.Gradle
5+
6+
import java.text.SimpleDateFormat
7+
8+
/**
9+
* <pre>
10+
* author: blankj
11+
* blog : http://blankj.com
12+
* time : 2019/08/16
13+
* desc :
14+
* </pre>
15+
*/
16+
class GitUtils {
17+
18+
private static String sCurBranchName;
19+
20+
static void init(Gradle gradle) {
21+
gradle.rootProject(new Action<Project>() {
22+
@Override
23+
void execute(Project project) {
24+
sCurBranchName = getGitBranch()
25+
addGitPushTask(project)
26+
}
27+
})
28+
}
29+
30+
static def getGitBranch() {
31+
return ShellUtils.execCmd('git symbolic-ref --short -q HEAD').successMsg
32+
}
33+
34+
static void addGitPushTask(Project project) {
35+
project.task("gitPush", new Action<Task>() {
36+
@Override
37+
void execute(Task task) {
38+
task.doLast {
39+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd")
40+
String date = simpleDateFormat.format(new Date())
41+
GLog.d(ShellUtils.execCmd([
42+
"git add -A",
43+
"git commit -m \"see $date log\"",
44+
"git push origin $sCurBranchName"
45+
] as String[]))
46+
}
47+
}
48+
})
49+
}
50+
51+
static void addGitPush2MasterTask(Project project) {
52+
project.task("gitPush2Master", new Action<Task>() {
53+
@Override
54+
void execute(Task task) {
55+
task.doLast {
56+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd")
57+
String date = simpleDateFormat.format(new Date())
58+
GLog.d(ShellUtils.execCmd([
59+
"git add -A",
60+
"git commit -m \"see $date log\"",
61+
"git push origin $sCurBranchName"
62+
] as String[]))
63+
}
64+
}
65+
})
66+
}
67+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package PACKAGE_NAME;
2+
3+
/**
4+
* <pre>
5+
* author: blankj
6+
* blog : http://blankj.com
7+
* time : 2019/08/16
8+
* desc :
9+
* </pre>
10+
*/
11+
public class GitUtils {
12+
}
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
import java.io.BufferedReader;
2+
import java.io.DataOutputStream;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.List;
6+
7+
/**
8+
* <pre>
9+
* author: Blankj
10+
* blog : http://blankj.com
11+
* time : 2016/08/07
12+
* desc : utils about shell
13+
* </pre>
14+
*/
15+
public final class ShellUtils {
16+
17+
private static final String LINE_SEP = System.getProperty("line.separator");
18+
19+
private ShellUtils() {
20+
throw new UnsupportedOperationException("u can't instantiate me...");
21+
}
22+
23+
/**
24+
* Execute the command.
25+
*
26+
* @param command The command.
27+
* @return the single {@link CommandResult} instance
28+
*/
29+
public static CommandResult execCmd(final String command) {
30+
return execCmd(new String[]{command}, false, true);
31+
}
32+
33+
/**
34+
* Execute the command.
35+
*
36+
* @param command The command.
37+
* @param isRooted True to use root, false otherwise.
38+
* @return the single {@link CommandResult} instance
39+
*/
40+
public static CommandResult execCmd(final String command, final boolean isRooted) {
41+
return execCmd(new String[]{command}, isRooted, true);
42+
}
43+
44+
/**
45+
* Execute the command.
46+
*
47+
* @param commands The commands.
48+
* @return the single {@link CommandResult} instance
49+
*/
50+
public static CommandResult execCmd(final List<String> commands) {
51+
return execCmd(commands == null ? null : commands.toArray(new String[]{}), false, true);
52+
}
53+
54+
/**
55+
* Execute the command.
56+
*
57+
* @param commands The commands.
58+
* @param isRooted True to use root, false otherwise.
59+
* @return the single {@link CommandResult} instance
60+
*/
61+
public static CommandResult execCmd(final List<String> commands, final boolean isRooted) {
62+
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true);
63+
}
64+
65+
/**
66+
* Execute the command.
67+
*
68+
* @param commands The commands.
69+
* @return the single {@link CommandResult} instance
70+
*/
71+
public static CommandResult execCmd(final String[] commands) {
72+
return execCmd(commands, false, true);
73+
}
74+
75+
/**
76+
* Execute the command.
77+
*
78+
* @param commands The commands.
79+
* @param isRooted True to use root, false otherwise.
80+
* @return the single {@link CommandResult} instance
81+
*/
82+
public static CommandResult execCmd(final String[] commands, final boolean isRooted) {
83+
return execCmd(commands, isRooted, true);
84+
}
85+
86+
/**
87+
* Execute the command.
88+
*
89+
* @param command The command.
90+
* @param isRooted True to use root, false otherwise.
91+
* @param isNeedResultMsg True to return the message of result, false otherwise.
92+
* @return the single {@link CommandResult} instance
93+
*/
94+
public static CommandResult execCmd(final String command,
95+
final boolean isRooted,
96+
final boolean isNeedResultMsg) {
97+
return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
98+
}
99+
100+
/**
101+
* Execute the command.
102+
*
103+
* @param commands The commands.
104+
* @param isRooted True to use root, false otherwise.
105+
* @param isNeedResultMsg True to return the message of result, false otherwise.
106+
* @return the single {@link CommandResult} instance
107+
*/
108+
public static CommandResult execCmd(final List<String> commands,
109+
final boolean isRooted,
110+
final boolean isNeedResultMsg) {
111+
return execCmd(commands == null ? null : commands.toArray(new String[]{}),
112+
isRooted,
113+
isNeedResultMsg);
114+
}
115+
116+
/**
117+
* Execute the command.
118+
*
119+
* @param commands The commands.
120+
* @param isRooted True to use root, false otherwise.
121+
* @param isNeedResultMsg True to return the message of result, false otherwise.
122+
* @return the single {@link CommandResult} instance
123+
*/
124+
public static CommandResult execCmd(final String[] commands,
125+
final boolean isRooted,
126+
final boolean isNeedResultMsg) {
127+
int result = -1;
128+
if (commands == null || commands.length == 0) {
129+
return new CommandResult(result, "", "");
130+
}
131+
Process process = null;
132+
BufferedReader successResult = null;
133+
BufferedReader errorResult = null;
134+
StringBuilder successMsg = null;
135+
StringBuilder errorMsg = null;
136+
DataOutputStream os = null;
137+
try {
138+
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh");
139+
os = new DataOutputStream(process.getOutputStream());
140+
for (String command : commands) {
141+
if (command == null) continue;
142+
os.write(command.getBytes());
143+
os.writeBytes(LINE_SEP);
144+
os.flush();
145+
}
146+
os.writeBytes("exit" + LINE_SEP);
147+
os.flush();
148+
result = process.waitFor();
149+
if (isNeedResultMsg) {
150+
successMsg = new StringBuilder();
151+
errorMsg = new StringBuilder();
152+
successResult = new BufferedReader(
153+
new InputStreamReader(process.getInputStream(), "UTF-8")
154+
);
155+
errorResult = new BufferedReader(
156+
new InputStreamReader(process.getErrorStream(), "UTF-8")
157+
);
158+
String line;
159+
if ((line = successResult.readLine()) != null) {
160+
successMsg.append(line);
161+
while ((line = successResult.readLine()) != null) {
162+
successMsg.append(LINE_SEP).append(line);
163+
}
164+
}
165+
if ((line = errorResult.readLine()) != null) {
166+
errorMsg.append(line);
167+
while ((line = errorResult.readLine()) != null) {
168+
errorMsg.append(LINE_SEP).append(line);
169+
}
170+
}
171+
}
172+
} catch (Exception e) {
173+
e.printStackTrace();
174+
} finally {
175+
try {
176+
if (os != null) {
177+
os.close();
178+
}
179+
} catch (IOException e) {
180+
e.printStackTrace();
181+
}
182+
try {
183+
if (successResult != null) {
184+
successResult.close();
185+
}
186+
} catch (IOException e) {
187+
e.printStackTrace();
188+
}
189+
try {
190+
if (errorResult != null) {
191+
errorResult.close();
192+
}
193+
} catch (IOException e) {
194+
e.printStackTrace();
195+
}
196+
if (process != null) {
197+
process.destroy();
198+
}
199+
}
200+
return new CommandResult(
201+
result,
202+
successMsg == null ? "" : successMsg.toString(),
203+
errorMsg == null ? "" : errorMsg.toString()
204+
);
205+
}
206+
207+
/**
208+
* The result of command.
209+
*/
210+
public static class CommandResult {
211+
public int result;
212+
public String successMsg;
213+
public String errorMsg;
214+
215+
public CommandResult(final int result, final String successMsg, final String errorMsg) {
216+
this.result = result;
217+
this.successMsg = successMsg;
218+
this.errorMsg = errorMsg;
219+
}
220+
221+
@Override
222+
public String toString() {
223+
return "result: " + result + "\n" +
224+
"successMsg: " + successMsg + "\n" +
225+
"errorMsg: " + errorMsg;
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)