Last active
May 20, 2018 14:58
-
-
Save Orange168/a94d5fee6eeaba2d708ad56387ac2a90 to your computer and use it in GitHub Desktop.
gradle 版本控制
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
def getVersionCode = { -> | |
try { | |
def stdout = new ByteArrayOutputStream() | |
exec { | |
commandLine 'git', 'rev-list', '--all', '--count' | |
standardOutput = stdout | |
} | |
return Integer.parseInt(stdout.toString().trim())+400 | |
} | |
catch (ignored) { | |
return -1; | |
} | |
} | |
def getVersionName = { -> | |
try { | |
def stdout = new ByteArrayOutputStream() | |
exec { | |
commandLine 'git', 'describe', '--tags', '--dirty' | |
standardOutput = stdout | |
} | |
return stdout.toString().trim() | |
} | |
catch (ignored) { | |
return null; | |
} | |
} |
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
// 命名为common.gradle 引用apply from: 'common.gradle' | |
/** | |
* defaultConfig { | |
.... | |
versionName "2.0_" + getGitHeadRefsSuffix() | |
.... | |
} | |
git checkout 14e1535 [-b $branchname] | |
*/ | |
gradle.allprojects { | |
ext.getGitHeadRefsSuffix = { | |
try { | |
// .git/HEAD描述当前目录所指向的分支信息,内容示例:"ref: refs/heads/master\n" | |
def headFile = new File('.git/HEAD') | |
if (headFile.exists()) { | |
String[] strings = headFile.getText('UTF-8').split(" "); | |
if (strings.size() > 1) { | |
String refFilePath = '.git/' + strings[1]; | |
// 根据HEAD读取当前指向的hash值,路径示例为:".git/refs/heads/master" | |
def refFile = new File(refFilePath.replace("\n", "")); | |
// 索引文件内容为hash值+"\n", | |
// 示例:"90312cd9157587d11779ed7be776e3220050b308\n" | |
return "_" + refFile.getText('UTF-8').substring(0, 7) | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace() | |
} | |
return "" | |
} | |
} | |
// 参考博客: https://www.jianshu.com/p/582939dfd73e |
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
class TimingsListener implements TaskExecutionListener, BuildListener { | |
private Clock clock | |
private timings = [] | |
@Override | |
void beforeExecute(Task task) { | |
clock = new org.gradle.util.Clock() | |
} | |
@Override | |
void afterExecute(Task task, TaskState taskState) { | |
def ms = clock.timeInMs | |
timings.add([ms, task.path]) | |
task.project.logger.warn "${task.path} took ${ms}ms" | |
} | |
@Override | |
void buildFinished(BuildResult result) { | |
println "Task timings:" | |
for (timing in timings) { | |
if (timing[0] >= 50) { | |
printf "%7sms %s\n", timing | |
} | |
} | |
} | |
@Override | |
void buildStarted(Gradle gradle) {} | |
@Override | |
void projectsEvaluated(Gradle gradle) {} | |
@Override | |
void projectsLoaded(Gradle gradle) {} | |
@Override | |
void settingsEvaluated(Settings settings) {} | |
} | |
gradle.addListener new TimingsListener() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment