Dev-Wiki
8/13/2016 - 9:44 AM

AndroidStudio中获取SVN信息

AndroidStudio中获取SVN信息

import org.tmatesoft.svn.core.wc.ISVNOptions
import org.tmatesoft.svn.core.wc.SVNClientManager
import org.tmatesoft.svn.core.wc.SVNStatus
import org.tmatesoft.svn.core.wc.SVNStatusClient
import org.tmatesoft.svn.core.wc.SVNWCUtil

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url "http://dl.bintray.com/dodola/maven"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.dodola:rocoofix:1.0'
        classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

def getSvnRevision(){
    SVNStatus status = getSvnStatus();
    return status.getRevision().number;
}

def getSvnName() {
    SVNStatus status = getSvnStatus();
    String url = status.getURL();
    String[] names = url.split("/");
    String name = project.name;
    if (names != null && names.length > 0) {
        name = names[names.length - 1];
        if (name.contains("@")) {
            int index = name.indexOf("@");
            name = name.substring(0, index);
        }
    }
    return name;
}

def getSvnStatus() {
    ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
    SVNClientManager clientManager = SVNClientManager.newInstance(options);
    SVNStatusClient statusClient = clientManager.getStatusClient();
    SVNStatus status = statusClient.doStatus(projectDir, false);
    return status;
}

ext {
    svnName = getSvnName();
    svnRevision = getSvnRevision();
}