Skip to content

Commit

Permalink
Version 2.0.1
Browse files Browse the repository at this point in the history
- Reduced verbosity
- Added grabverDebug dummy task to print debug steps
  • Loading branch information
davideas committed Jun 25, 2019
1 parent fb1d2a3 commit 922866b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 48 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ only 2 variables.
Inspired from <a href='https://andreborud.com/android-studio-automatic-incremental-gradle-versioning/'>Android Studio
Automatic Incremental Gradle Versioning</a>. Customized into library with PreRelease, Auto-Reset and Sub-Modules features.</p>

> Easy to apply, it _works with **any** project type with sub modules too._
> :speech_balloon: Easy to apply, it _works with **any** project type with sub modules too._
## Rules
###### User values
Expand Down Expand Up @@ -44,18 +44,18 @@ buildscript {
}
dependencies {
// Using Bintray or Gradle Plugins repository
classpath "eu.davidea:grabver:2.0.0"
classpath "eu.davidea:grabver:2.0.1"
}
}
```

## Usage
### 1. Version configuration
Apply the plugin in the _module_ you desire, it will create a properties file under that module!
Apply the plugin in the _module_ you desire, it will create a `version.properties` file under that module!
``` gradle
plugins {
...
id: 'eu.davidea.grabver'
id 'eu.davidea.grabver'
}
versioning {
Expand All @@ -72,6 +72,8 @@ versioning {
saveOn "<task-name>"
}
```
> :speech_balloon: **Note:** The file `version.properties` is auto-generated, but once it's created, you can modify its content
as of your convenience. Just remember to add it to your Version Control System (from time to time).

### 2. Grab your new version
``` gradle
Expand All @@ -82,11 +84,14 @@ versioning.build
versioning.preRelease
versioning.code // needed for all Android projects
versioning.name // output: "major.minor.patch[-preRelease]"
versioning.fullName // output: "major.minor.patch[-preRelease] #build built on yyyy.mm.dd"
versioning.builtOn // output: " built on yyyy.mm.dd"
versioning.date // or versioning.getDate([format]) - default "yyyy.mm.dd"
versioning.fullName // output: "major.minor.patch[-preRelease] #buildNr built on yyyy.MM.dd"
versioning.builtOn // output: " built on yyyy.MM.dd"
versioning.date // or versioning.getDate([format]) - default "yyyy.MM.dd"
// Example: grab the version name
version = versioning.name
```
> **Note:** To trigger the evaluation, the user must grab one of the above attribute.
> :warning: **Note:** To trigger the evaluation, user must grab one of the above attribute!
### 3. Run it
- Via command line:
Expand Down Expand Up @@ -125,13 +130,10 @@ tasks out there will not trigger the versioning evaluation. Example:
|`gradle clean build`|Evaluation triggered and new values saved|
|`gradle clean war grabverRelease`|Evaluation triggered, versioning increased and new values saved|

> **Note:** File `version.properties` is auto-generated, but once it's created, you can modify its content
as of your convenience. Just remember to add it to your Version Control System (from time to time).

# Contributions
Everybody is welcome to improve existing solution.

> **Note:** Unit tests work fine if you open the project with IntelliJ Idea, while with Android Studio
> :speech_balloon: **Note:** Unit tests work fine if you open the project with IntelliJ Idea, while with Android Studio
they don't. Alternatively, you can simulate a real build script by running `gradle install`
and `gradle -b build-test.gradle [grabverRelease]` OR testing with modules `gradle build [grabverRelease]`.

Expand Down
2 changes: 1 addition & 1 deletion build-test.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ buildscript {
}
//noinspection GroovyAssignabilityCheck
dependencies {
classpath 'eu.davidea:grabver:2.0.0'
classpath 'eu.davidea:grabver:2.0.1'
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ext {

// Library Repository
libraryName = 'GrabVer'
libraryVersion = '2.0.0'
libraryVersion = '2.0.1'
displayName = 'Gradle Automatic Build Versioning Plugin'
libraryDescription = 'An easy Gradle plugin that follows semver.org rules to automatically generate the Patch version, Build number and Code version, while Major, Minor and Pre-Release suffix remain under our control.'
libraryLabels = ['semver', 'version', 'versioning', 'build-versioning', 'automatic-versioning', 'intellij-idea', 'android-studio', 'auto-reset']
Expand Down
2 changes: 1 addition & 1 deletion module_a/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenLocal()
}
dependencies {
classpath 'eu.davidea:grabver:2.0.0'
classpath 'eu.davidea:grabver:2.0.1'
}
}

Expand Down
2 changes: 1 addition & 1 deletion module_b/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenLocal()
}
dependencies {
classpath 'eu.davidea:grabver:2.0.0'
classpath 'eu.davidea:grabver:2.0.1'
}
}

Expand Down
47 changes: 24 additions & 23 deletions src/main/groovy/eu/davidea/gradle/GrabVer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,25 @@ import static eu.davidea.gradle.ConsoleColors.*
*/
class GrabVer implements Plugin<Project> {

private static String GRABVER_VERSION = "2.0.0"
private static String GRABVER_VERSION = "2.0.1"
private static String[] RELEASE_TASKS = ["assembleRelease", "bundleRelease", "grabverRelease"]
private static String[] SAVE_TASKS = ["build", "assembleDebug", "assembleRelease", "bundleDebug", "bundleRelease", "grabverRelease", "jar", "war", "explodedWar"]

// Extension reference
private VersioningExtension versioning
// Internal references
private Project project
private File versionFile
private OrderedProperties versionProps
protected boolean isFirstRun = false
protected Project project
protected boolean firstRun = false
protected boolean debug = false

void apply(Project project) {
project.task('grabverRelease') {
// Dummy task to force trigger release versioning (also used in unit test)
// Dummy task to force release versioning calculation (also used in unit test)
}
project.task('grabverDebug') {
// Dummy task to log/display debug steps
}

// Create new empty versioning instance
Expand All @@ -87,8 +91,8 @@ class GrabVer implements Plugin<Project> {
// Gradle build complete
project.gradle.buildFinished() { BuildResult result ->
println("") // Print empty line
if (isFirstRun || !succeededTasks.isEmpty()) {
println(bold("> Module: ${project.name}"))
if (firstRun || !succeededTasks.isEmpty()) {
println(bold("> Module: ${project.name} ")) // Fix for dirty print
for (Task task in succeededTasks) {
String state = task.state.skipMessage != null
? styler(YELLOW, task.state.skipMessage)
Expand All @@ -115,6 +119,7 @@ class GrabVer implements Plugin<Project> {
*/
protected boolean readUserConfiguration() {
List<String> runTasks = project.gradle.startParameter.taskNames
this.debug = runTasks.contains("grabverDebug")

// Silent evaluation looking for activation/save tasks
if (!shouldSave(runTasks, project.name, versioning.saveOn)) {
Expand All @@ -123,20 +128,18 @@ class GrabVer implements Plugin<Project> {
: println(styler(YELLOW, "> GrabVer - No save task detected"))
// Load existing properties file to provide last values
loadProperties(true)
return isFirstRun
return firstRun
}

// Silent evaluation done. If passes, at least one save task was detected.
// Plugin info
String rootProject = project.rootProject.name
println(bold("> Plugin GrabVer v${GRABVER_VERSION}"))
println("INFO - Root project ${rootProject}")

// Load existing properties file
loadProperties(false)
// Display extra info
println("INFO - runTasks=" + runTasks)
println("INFO - Save task detected") // Real detection was done in silent mode
printDebug("runTasks=" + runTasks)
printDebug("Save task detected") // Real detection was done in silent mode

// Patch and Code increment depending on release task
if (isRelease(runTasks, project.name, versioning)) {
Expand Down Expand Up @@ -166,26 +169,18 @@ class GrabVer implements Plugin<Project> {
String filename = 'version.properties'

// Root or Module versioning
if (rootProject == module) {
if (!silent) {
println("INFO - Versioning root project ${module}")
}
} else {
if (!silent) {
println("INFO - Versioning module ${module}")
}
if (rootProject != module) {
filename = module + File.separator + filename
}
filename = project.rootDir.toString() + File.separator + filename

File file = new File(filename)
if (!file.canRead()) {
this.isFirstRun = true
println(styler(YELLOW, "WARN - Could not find properties file ${filename}"))
println(styler(YELLOW, "WARN - Creating new properties file!"))
this.firstRun = true
println(styler(YELLOW, "WARN - Creating new properties file ${filename}"))
file.createNewFile()
} else if (!silent) {
println("INFO - Versioning file ${filename}")
printDebug("Versioning file ${filename}")
}
return file
}
Expand Down Expand Up @@ -240,4 +235,10 @@ class GrabVer implements Plugin<Project> {
return (lastIndex > 0) ? task.substring(lastIndex + 1) : task
}

protected printDebug(String message) {
if (debug) {
println("DEBUG - ${message}")
}
}

}
18 changes: 9 additions & 9 deletions src/main/groovy/eu/davidea/gradle/VersioningExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@ class VersioningExtension {
println("INFO - Evaluating user values: {${version}${tasks}}")
// Auto reset Patch in case they differ or preRelease is set
if (major != propMajor || minor != propMinor || isPreRelease()) {
if (!grabver.isFirstRun && propMajor != 0 && major > propMajor && minor != 0) {
if (!grabver.firstRun && propMajor != 0 && major > propMajor && minor != 0) {
println(styler(RED, "ERROR - Expected minor to be 0 if major has increased"))
throw new IllegalArgumentException("Inconsistent minor value: major has changed but minor is not 0")
}
if (!grabver.isFirstRun && propMinor != 0 && minor > propMinor && patch > 0) {
if (!grabver.firstRun && propMinor != 0 && minor > propMinor && patch > 0) {
println(styler(RED, "ERROR - Expected patch to be 0 if minor has increased"))
throw new IllegalArgumentException("Inconsistent patch value: minor has changed but patch is not 0")
}
if (patch < 0) {
println("INFO - Auto resetting patch version")
grabver.printDebug("Auto resetting patch version")
patch = 0
}
} else if (isRelease && patch < 0) {
println("INFO - Auto incrementing patch version")
grabver.printDebug("Auto incrementing patch version")
// Auto-increment Patch if Major or Minor do not differ from user
patch = propPatch + 1
}
// Always auto-increment build number
println("INFO - Auto incrementing build number")
grabver.printDebug("Auto incrementing build number")
build++
// Auto-increment Code only in case of release
if (isRelease) {
println("INFO - Auto incrementing code version")
grabver.printDebug("Auto incrementing code version")
code += 1
}
propPreRelease = preRelease
println("INFO - Evaluation complete!")
grabver.printDebug("Evaluation complete: ${bold(toString())}")
}
}

Expand All @@ -97,7 +97,7 @@ class VersioningExtension {
build = Integer.valueOf(versionProps.getProperty(VersionType.BUILD.toString(), "0"))
code = Integer.valueOf(versionProps.getProperty(VersionType.CODE.toString(), "1"))
if (!silent) {
println("INFO - Current versioning: " + bold(toStringCurrent()))
println("INFO - Current version: " + bold(toStringCurrent()))
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ class VersioningExtension {
}

/**
* @return "<code>major.minor.patch[-preRelease] #build built on date</code>"
* @return "<code>major.minor.patch[-preRelease] #buildNr built on yyyy.MM.dd</code>"
*/
String getFullName() {
return getName() + " #" + build + getBuiltOn()
Expand Down

0 comments on commit 922866b

Please sign in to comment.