Skip to content

Commit

Permalink
Refactoring versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Jul 7, 2017
1 parent 92f1fb8 commit 07f5290
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 215 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# revoice
# Revoice [![Build Status](http://teamcity.rehlds.org/app/rest/builds/buildType:(id:Revoice_Publish)/statusIcon)](http://teamcity.rehlds.org/viewType.html?buildTypeId=Revoice_Publish&guest=1) [![Download](https://camo.githubusercontent.com/089706eb2571f262bb23afc9434d85d52a423cc3/687474703a2f2f7265686c64732e6f72672f76657273696f6e2f7265766f6963652e737667)](http://teamcity.rehlds.org/guestAuth/downloadArtifacts.html?buildTypeId=Revoice_Publish&buildId=lastSuccessful)
28 changes: 15 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import versioning.GitVersioner
import versioning.RevoiceVersionInfo
import org.joda.time.DateTime

apply plugin: 'maven-publish'
apply from: 'shared.gradle'
group = 'revoice'

Expand All @@ -13,13 +15,8 @@ idea {
}

def gitInfo = GitVersioner.versionForDir(project.rootDir)
if (!gitInfo) {
throw new RuntimeException('Running outside git repository')
}

RevoiceVersionInfo versionInfo
if (gitInfo.tag && gitInfo.tag[0] == 'v') {

if (gitInfo && gitInfo.tag && gitInfo.tag[0] == 'v') {
def m = gitInfo.tag =~ /^v(\d+)\.(\d+)(\.(\d+))?$/
if (!m.find()) {
throw new RuntimeException("Invalid git version tag name ${gitInfo.tag}")
Expand All @@ -29,22 +26,27 @@ if (gitInfo.tag && gitInfo.tag[0] == 'v') {
majorVersion: m.group(1) as int,
minorVersion: m.group(2) as int,
maintenanceVersion: m.group(4) ? (m.group(4) as int) : null,
countCommit: gitInfo.countCommit,
lastCommitDate: gitInfo.lastCommitDate
localChanges: gitInfo.localChanges,
commitDate: gitInfo.commitDate,
commitSHA: gitInfo.commitSHA,
commitURL: gitInfo.commitURL
)
} else {

versionInfo = new RevoiceVersionInfo(
majorVersion: project.majorVersion as int,
minorVersion: project.minorVersion as int,
specialVersion: project.specialVersion,
countCommit: gitInfo.countCommit,
lastCommitDate: gitInfo.lastCommitDate
maintenanceVersion: project.maintenanceVersion as int,
suffix: '',
localChanges: gitInfo ? gitInfo.localChanges : true,
commitDate: gitInfo ? gitInfo.commitDate : new DateTime(),
commitSHA: gitInfo ? gitInfo.commitSHA : "",
commitURL: gitInfo ? gitInfo.commitURL : "",
commitCount: gitInfo ? (gitInfo.commitCount as int) : null
)
}

project.ext.revoiceVersionInfo = versionInfo
project.version = versionInfo.asVersion()
project.version = versionInfo.asMavenVersion()

apply from: 'publish.gradle'

Expand Down
21 changes: 3 additions & 18 deletions buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.apache.velocity.Template
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.joda.time.format.DateTimeFormat
import versioning.RevoiceVersionInfo

class VelocityUtils {

Expand All @@ -20,29 +19,15 @@ class VelocityUtils {

Velocity.init(p);
}
static String renderTemplate(File tplFile, RevoiceVersionInfo ctx) {

static String renderTemplate(File tplFile, Map<String, ? extends Object> ctx) {
Template tpl = Velocity.getTemplate(tplFile.absolutePath)
if (!tpl) {
throw new RuntimeException("Failed to load velocity template ${tplFile.absolutePath}: not found")
}

def templateCtx = [
verInfo: ctx
]

def velocityContext = new VelocityContext(templateCtx)

if (ctx.specialVersion.length() > 0) {
velocityContext.put("appFlags", 0x0L)
velocityContext.put("formatSpecialVersion", "-" + ctx.specialVersion)
} else {

velocityContext.put("appFlags", "VS_FF_SPECIALBUILD")
velocityContext.put("formatSpecialVersion", "")
}

velocityContext.put("current_version", ctx.asVersion())

def velocityContext = new VelocityContext(ctx)
velocityContext.put("_DateTimeFormat", DateTimeFormat)

def sw = new StringWriter()
Expand Down
7 changes: 5 additions & 2 deletions buildSrc/src/main/groovy/versioning/GitInfo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import org.joda.time.DateTime

@CompileStatic @TypeChecked
class GitInfo {
DateTime lastCommitDate
boolean localChanges
DateTime commitDate
String branch
String tag
Integer countCommit
String commitSHA
String commitURL
Integer commitCount
}
90 changes: 81 additions & 9 deletions buildSrc/src/main/groovy/versioning/GitVersioner.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package versioning

import java.util.Set;

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.StoredConfig
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
Expand All @@ -26,28 +30,96 @@ class GitVersioner {

return count;
}
static String prepareUrlToCommits(String url) {
if (url == null) {
// default remote url
return "https://github.com/s1lentq/Revoice/commit/";
}

StringBuilder sb = new StringBuilder();
String childPath;
int pos = url.indexOf('@');
if (pos != -1) {
childPath = url.substring(pos + 1, url.lastIndexOf('.git')).replace(':', '/');
sb.append('https://');
} else {
pos = url.lastIndexOf('.git');
childPath = (pos == -1) ? url : url.substring(0, pos);
}

// support for different links to history of commits
if (url.indexOf('bitbucket.org') != -1) {
sb.append(childPath).append('/commits/');
} else {
sb.append(childPath).append('/commit/');
}
return sb.toString();
}
// check uncommited changes
static boolean getUncommittedChanges(Repository repo) {
Git git = new Git(repo);
Status status = git.status().call();

Set<String> uncommittedChanges = status.getUncommittedChanges();
for(String uncommitted : uncommittedChanges) {
return true;
}

return false;
}
static GitInfo versionForDir(File dir) {
FileRepositoryBuilder builder = new FileRepositoryBuilder()
Repository repo = builder.setWorkTree(dir).findGitDir().build()
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setWorkTree(dir)
.findGitDir()
.build()

ObjectId head = repo.resolve('HEAD')
ObjectId head = repo.resolve('HEAD');
if (!head) {
return null
}

def commit = new RevWalk(repo).parseCommit(head)
def branch = repo.getBranch()
def commitDate = new DateTime(1000L * commit.commitTime, DateTimeZone.UTC)
final StoredConfig cfg = repo.getConfig();
def commit = new RevWalk(repo).parseCommit(head);
if (!commit) {
throw new RuntimeException("Can't find last commit.");
}

def localChanges = getUncommittedChanges(repo);
def commitDate = new DateTime(1000L * commit.commitTime, DateTimeZone.UTC);
if (localChanges) {
commitDate = new DateTime();
}

int commitCount = getCountCommit(repo);
def branch = repo.getBranch();

String url = null;
String remote_name = cfg.getString("branch", branch, "remote");

if (remote_name == null) {
for (String remotes : cfg.getSubsections("remote")) {
if (url != null) {
println 'Found a second remote: (' + remotes + '), url: (' + cfg.getString("remote", remotes, "url") + ')'
continue;
}

url = cfg.getString("remote", remotes, "url");
}
} else {
url = cfg.getString("remote", remote_name, "url");
}

String commitURL = prepareUrlToCommits(url);
String tag = repo.tags.find { kv -> kv.value.objectId == commit.id }?.key
String commitSHA = commit.getId().abbreviate(7).name();

return new GitInfo(
lastCommitDate: commitDate,
localChanges: localChanges,
commitDate: commitDate,
branch: branch,
tag: tag,
countCommit: commitCount
commitSHA: commitSHA,
commitURL: commitURL,
commitCount: getCountCommit(repo)
)
}
}
51 changes: 34 additions & 17 deletions buildSrc/src/main/groovy/versioning/RevoiceVersionInfo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,56 @@ package versioning
import groovy.transform.CompileStatic
import groovy.transform.ToString
import groovy.transform.TypeChecked
import org.joda.time.format.DateTimeFormat
import org.joda.time.DateTime

@CompileStatic @TypeChecked
@ToString(includeNames = true)
class RevoiceVersionInfo {
int majorVersion
int minorVersion
Integer majorVersion
Integer minorVersion
Integer maintenanceVersion
String specialVersion
Integer countCommit
DateTime lastCommitDate
String suffix

String format(String versionSeparator, String suffixSeparator, boolean includeSuffix) {
boolean localChanges
DateTime commitDate
String commitSHA
String commitURL
Integer commitCount

String asMavenVersion(boolean extra = true, String separator = ".") {
StringBuilder sb = new StringBuilder()
sb.append(majorVersion).append(versionSeparator).append(minorVersion)
sb.append(majorVersion).append(separator).append(minorVersion);
if (maintenanceVersion != null) {
sb.append(versionSeparator).append(maintenanceVersion)
sb.append(separator).append(maintenanceVersion);
}

if (commitCount != null) {
sb.append(separator).append(commitCount)
}

if (specialVersion && includeSuffix) {
sb.append(suffixSeparator).append(specialVersion)
if (extra && suffix) {
sb.append('-' + suffix)
}

// do mark for this build like a modified version
if (extra && localChanges) {
sb.append('+m');
}

return sb.toString()
}
String asVersion() {
if (specialVersion.length() > 0) {
sprintf("%d.%d.%d-%s", majorVersion, minorVersion, countCommit, specialVersion)
String asCommitDate(String pattern = null) {
if (pattern == null) {
pattern = "MMM d yyyy";
if (commitDate.getDayOfMonth() >= 10) {
pattern = "MMM d yyyy";
}
}
else
sprintf("%d.%d.%d", majorVersion, minorVersion, countCommit)

return DateTimeFormat.forPattern(pattern).withLocale(Locale.ENGLISH).print(commitDate);
}
String asMavenVersion() {
format('.', '-', true)
String asCommitTime() {
return DateTimeFormat.forPattern('HH:mm:ss').withLocale(Locale.ENGLISH).print(commitDate);
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
majorVersion=0
minorVersion=1
specialVersion=
maintenanceVersion=0
Empty file modified gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion msvc/ReVoice.sln → msvc/revoice.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReVoice", "..\revoice\msvc\ReVoice.vcxproj", "{DAEFE371-7D77-4B72-A8A5-3CD3D1A55786}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "revoice", "..\revoice\msvc\revoice.vcxproj", "{DAEFE371-7D77-4B72-A8A5-3CD3D1A55786}"
ProjectSection(ProjectDependencies) = postProject
{E1AC990E-C012-4167-80C2-84C98AA7070C} = {E1AC990E-C012-4167-80C2-84C98AA7070C}
{966DE7A9-EC15-4C1D-8B46-EA813A845723} = {966DE7A9-EC15-4C1D-8B46-EA813A845723}
Expand Down
25 changes: 13 additions & 12 deletions publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ void _copyFile(String from, String to) {
GradleCppUtils.copyFile(project.file(from), project.file(to), false)
}

task publishPrepareFiles << {
def pubRootDir = project.file('publish/publishRoot')
if (pubRootDir.exists()) {
if (!pubRootDir.deleteDir()) {
throw new RuntimeException("Failed to delete ${pubRootDir}")
task publishPrepareFiles {
doLast {
def pubRootDir = project.file('publish/publishRoot')
if (pubRootDir.exists()) {
if (!pubRootDir.deleteDir()) {
throw new RuntimeException("Failed to delete ${pubRootDir}")
}
}
}

pubRootDir.mkdirs()
pubRootDir.mkdirs()

project.file('publish/publishRoot/revoice/bin/Windows').mkdirs()
project.file('publish/publishRoot/revoice/bin/Linux').mkdirs()
project.file('publish/publishRoot/revoice/bin/win32').mkdirs()
project.file('publish/publishRoot/revoice/bin/linux32').mkdirs()

_copyFileToDir('publish/revoice_mm.dll', 'publish/publishRoot/revoice/bin/Windows/')
_copyFile('publish/librevoice_mm_i386.so', 'publish/publishRoot/revoice/bin/Linux/revoice_mm_i386.so')
_copyFileToDir('publish/revoice_mm.dll', 'publish/publishRoot/revoice/bin/win32/')
_copyFile('publish/librevoice_mm_i386.so', 'publish/publishRoot/revoice/bin/linux32/revoice_mm_i386.so')
}
}

task publishPackage(type: Zip, dependsOn: 'publishPrepareFiles') {
Expand All @@ -35,5 +37,4 @@ task publishPackage(type: Zip, dependsOn: 'publishPrepareFiles') {

task doPackage {
dependsOn 'publishPackage'

}
Loading

0 comments on commit 07f5290

Please sign in to comment.