1+ plugins {
2+ id(" pmd" )
3+ id(" com.diffplug.spotless" ) version " 8.0.0"
4+ }
5+
6+ fun runCommand (vararg args : String ): String {
7+ val process = ProcessBuilder (* args)
8+ .directory(rootDir)
9+ .redirectErrorStream(true )
10+ .start()
11+
12+ val output = process.inputStream.bufferedReader().readText()
13+ val exitCode = process.waitFor()
14+
15+ if (exitCode != 0 ) {
16+ throw GradleException (" Command failed with exit code $exitCode : ${args.joinToString(" " )} " )
17+ }
18+
19+ return output.trim()
20+ }
21+
22+ val gitHash: String by lazy {
23+ runCommand(" git" , " rev-parse" , " --short" , " HEAD" )
24+ }
25+
26+ val gitTag: String? by lazy {
27+ runCatching {
28+ runCommand(" git" , " describe" , " --abbrev=0" , " --tags" )
29+ }.getOrNull()
30+ }
31+
32+ val commitCount: Int by lazy {
33+ val range = if (gitTag.isNullOrEmpty()) " HEAD" else " $gitTag ..HEAD"
34+ runCommand(" git" , " rev-list" , " --count" , range).toInt()
35+ }
36+
37+ val branch: String by lazy {
38+ // First, try GitHub Actions environment variable
39+ val githubBranch = System .getenv(" GITHUB_REF_NAME" )
40+ if (! githubBranch.isNullOrBlank()) githubBranch
41+
42+ // Fallback to local git command
43+ else {
44+ runCommand(" git" , " rev-parse" , " --abbrev-ref" , " HEAD" )
45+ }
46+ }
47+
48+ fun parseTag (tag : String ): Triple <Int , Int , Int >? {
49+ val regex = Regex (""" v?(\d+)\.(\d+)\.(\d+)""" )
50+ val match = regex.matchEntire(tag.trim()) ? : return null
51+ val (major, minor, patch) = match.destructured
52+ return Triple (major.toInt(), minor.toInt(), patch.toInt())
53+ }
54+
55+ fun calcVersion (): String {
56+ var (major, minor, patch) = parseTag(gitTag ? : " " ) ? : Triple (0 , 1 , 0 )
57+
58+ if (branch == " main" ) {
59+ return " $major .${minor + 1 } .$patch -m$commitCount "
60+ }
61+
62+ if (branch.startsWith(" release/v" )) {
63+ if (commitCount == 0 ) {
64+ return " $major .$minor .$patch "
65+ } else {
66+ return " $major .$minor .${patch + 1 } -rc$commitCount "
67+ }
68+ }
69+
70+ return " $major .${minor + 1 } .$patch -a$commitCount -g$gitHash "
71+ }
72+
73+ val calculatedVersion: String by lazy {
74+ calcVersion()
75+ }
76+
77+ // prints when Gradle evaluates the build
78+ println (" DBOS Transact version: $calculatedVersion " )
79+
80+ allprojects {
81+ group = " dev.dbos"
82+ version = calculatedVersion
83+
84+ repositories {
85+ mavenCentral()
86+ gradlePluginPortal()
87+ }
88+ }
89+
90+ subprojects {
91+ apply (plugin = " java" )
92+ apply (plugin = " pmd" )
93+ apply (plugin = " com.diffplug.spotless" ) // Spotless plugin
94+
95+ // PMD configuration
96+ extensions.configure< org.gradle.api.plugins.quality.PmdExtension > {
97+ toolVersion = " 7.16.0"
98+ ruleSets = listOf () // disable defaults
99+ ruleSetFiles = files(" ${rootDir} /config/pmd/ruleset.xml" )
100+ isConsoleOutput = true
101+ }
102+
103+ // Spotless configuration
104+ extensions.configure< com.diffplug.gradle.spotless.SpotlessExtension > {
105+ java {
106+ googleJavaFormat()
107+ importOrder(" dev.dbos" , " java" , " javax" , " " )
108+ removeUnusedImports()
109+ trimTrailingWhitespace()
110+ endWithNewline()
111+ }
112+ }
113+
114+ plugins.withId(" java" ) {
115+ extensions.configure<JavaPluginExtension > {
116+ toolchain {
117+ languageVersion.set(JavaLanguageVersion .of(17 ))
118+ }
119+ }
120+
121+ tasks.named<Jar >(" jar" ) {
122+ manifest {
123+ attributes[" Implementation-Version" ] = project.version
124+ attributes[" Implementation-Title" ] = project.name
125+ attributes[" Implementation-Vendor" ] = " DBOS, Inc"
126+ attributes[" Implementation-Vendor-Id" ] = project.group
127+ attributes[" SCM-Revision" ] = gitHash
128+ }
129+ }
130+ }
131+ }
0 commit comments