-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 新增了参数 是否编译打包Flutter 项目 2 新增了编译打包Flutter AArR 3 修改了日志打印提示
- Loading branch information
Showing
10 changed files
with
228 additions
and
208 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
127 changes: 42 additions & 85 deletions
127
app/src/main/java/com/trubitpro/uploadapkplugin/help/ProcessUtils.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 |
---|---|---|
@@ -1,108 +1,65 @@ | ||
package com.trubitpro.uploadapkplugin.help; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.logging.Logger; | ||
|
||
public class ProcessUtils { | ||
/** | ||
* @param timeout 超时时长 | ||
* @param fileDir 所运行程序路径 | ||
* @param command 程序所要执行的命令 | ||
* 运行一个外部命令,返回状态.若超过指定的超时时间,抛出TimeoutException | ||
*/ | ||
public static int executeProcess(final long timeout, File fileDir, final String command) | ||
throws IOException, InterruptedException, TimeoutException { | ||
Process process = Runtime.getRuntime().exec(command, null, fileDir); | ||
Worker worker = new Worker(process); | ||
worker.start(); | ||
try { | ||
worker.join(timeout); | ||
if (worker.exit != null){ | ||
return worker.exit; | ||
} else{ | ||
throw new TimeoutException(); | ||
} | ||
} catch (InterruptedException ex) { | ||
worker.interrupt(); | ||
Thread.currentThread().interrupt(); | ||
throw ex; | ||
} | ||
finally { | ||
process.destroy(); | ||
} | ||
} | ||
|
||
private static class Worker extends Thread { | ||
private final Process process; | ||
private Integer exit; | ||
|
||
private Worker(Process process) { | ||
this.process = process; | ||
} | ||
private static final Integer WAIT_TIME = 60; | ||
|
||
/** | ||
* 执行脚本命令 | ||
* @param commands | ||
* @throws | ||
*/ | ||
public static Integer exec(List<String> commands) throws Exception{ | ||
|
||
@Override | ||
public void run() { | ||
InputStream errorStream = null; | ||
InputStream inputStream = null; | ||
String[] arrCommands = list2Array(commands); | ||
ProcessBuilder processBuilder = new ProcessBuilder(arrCommands); | ||
processBuilder.redirectErrorStream(true); | ||
Process process = null; | ||
try { | ||
errorStream = process.getErrorStream(); | ||
inputStream = process.getInputStream(); | ||
readStreamInfo(errorStream, inputStream); | ||
exit = process.waitFor(); | ||
process.destroy(); | ||
if (exit == 0) { | ||
System.out.println("子进程正常完成"); | ||
} else { | ||
System.out.println("子进程异常结束"); | ||
processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE); // 将输出重定向到空的OutputStream | ||
process = processBuilder.start(); | ||
// 读取进程的输出流(可选) | ||
InputStream inputStream = process.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
// 处理输出(可选) | ||
System.out.println(line); | ||
} | ||
reader.close(); | ||
process.waitFor(WAIT_TIME, TimeUnit.SECONDS); | ||
return process.exitValue(); | ||
} finally { | ||
if(process != null){ | ||
process.destroy(); | ||
} | ||
} catch (InterruptedException ignore) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 读取RunTime.exec运行子进程的输入流 和 异常流 | ||
* @param inputStreams 输入流 | ||
* List转String | ||
* @param commands | ||
* @return | ||
*/ | ||
public static void readStreamInfo(InputStream... inputStreams){ | ||
ExecutorService executorService = Executors.newFixedThreadPool(inputStreams.length); | ||
for (InputStream in : inputStreams) { | ||
executorService.execute(new MyThread(in)); | ||
} | ||
executorService.shutdown(); | ||
private static String[] list2Array(List<String> commands){ | ||
String[] commends = new String[commands.size()]; | ||
commands.toArray(commends); | ||
return commends; | ||
} | ||
public static class MyThread implements Runnable { | ||
|
||
private InputStream in; | ||
public MyThread(InputStream in){ | ||
this.in = in; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(in, "GBK")); | ||
String line = null; | ||
while((line = br.readLine())!=null){ | ||
System.out.println(" inputStream: " + line); | ||
} | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
}finally { | ||
try { | ||
in.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.