Skip to content

Commit

Permalink
feat: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lfvjimisola authored and jimisola committed May 27, 2024
1 parent ecc6146 commit bb1bbea
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
8 changes: 3 additions & 5 deletions versions-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -118,7 +116,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.9.0.202403050737-r</version>
<version>5.13.3.202401111512-r</version>
</dependency>

<!-- testing -->
Expand Down Expand Up @@ -308,4 +306,4 @@
</build>
</profile>
</profiles>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:set-version-from-scm-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//setup:

void exec(String command, File outputFile) {
def proc = command.execute(null, basedir)
proc.consumeProcessOutput(outputFile.newOutputStream(), outputFile.newOutputStream())
proc.waitFor()
assert proc.exitValue() == 0 : "command '${command}' return status: " + proc.exitValue()
}

def testFile = new File(basedir, 'test.txt')
testFile << 'content'

exec('git init', new File(basedir, 'setup.log'))
exec('git add test.txt', new File(basedir, 'setup.log'))
exec('git status', new File(basedir, 'setup.log'))
exec('git commit -m first-commit', new File(basedir, 'setup.log'))
exec('git tag first-tag', new File(basedir, 'setup.log'))

def mavenLogFile = new File(basedir, 'maven.log')
exec('mvn se.lfv.maven.plugins:ci-friendly-versions-maven-plugin:set-version-from-tag', mavenLogFile)

// verify

def mavenLogFile = new File(basedir, 'maven.log')
assert mavenLogFile.exists() : "Maven log file does not exist"

// Read the log file
def logContent = mavenLogFile.text

// Define a pattern to match the version output
def versionPattern = /\[INFO\] Setting property 'revision' to: (.+)/
def matcher = (logContent =~ versionPattern)
assert matcher.find() : "Version information not found in log file"

// Extract the version from the matched group
def actualVersion = matcher[0][1]

// Define the expected version (for example, based on your tag)
def expectedVersion = '0.0.1-1215-SNAPSHOT' // Replace with the expected version

assert actualVersion == expectedVersion : "Expected version '${expectedVersion}', but found '${actualVersion}'"

// full

void exec(String command, File outputFile) {
def proc = command.execute(null, basedir)
proc.consumeProcessOutput(outputFile.newOutputStream(), outputFile.newOutputStream())
proc.waitFor()
assert proc.exitValue() == 0 : "command '${command}' return status: " + proc.exitValue()
}

// Setup phase
def testFile = new File(basedir, 'test.txt')
testFile << 'content'

exec('git init', new File(basedir, 'setup.log'))
exec('git add test.txt', new File(basedir, 'setup.log'))
exec('git status', new File(basedir, 'setup.log'))
exec('git commit -m first-commit', new File(basedir, 'setup.log'))
exec('git tag first-tag', new File(basedir, 'setup.log'))

def mavenLogFile = new File(basedir, 'maven.log')
exec('mvn se.lfv.maven.plugins:ci-friendly-versions-maven-plugin:set-version-from-tag', mavenLogFile)

// Verification phase
assert mavenLogFile.exists() : "Maven log file does not exist"

// Read the log file
def logContent = mavenLogFile.text

// Define a pattern to match the version output (adjust according to your plugin's output format)
def versionPattern = /.*\[INFO\] Setting project version to (.+) from tag.*/
def matcher = (logContent =~ versionPattern)
assert matcher.find() : "Version information not found in log file"

// Extract the version from the matched group
def actualVersion = matcher[0][1]

// Define the expected version (for example, based on your tag)
def expectedVersion = '1.0.0' // Replace with the expected version

assert actualVersion == expectedVersion : "Expected version '${expectedVersion}', but found '${actualVersion}'"
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;

@Mojo(name = "set-version-from-tag", defaultPhase = LifecyclePhase.PACKAGE)
@Mojo(name = "set-version-from-scm-tag", defaultPhase = LifecyclePhase.PACKAGE)
public class CIFriendlyVersionsMojo extends AbstractMojo {

@Parameter(property = "propertyName", defaultValue = "revision")
Expand Down

0 comments on commit bb1bbea

Please sign in to comment.