@@ -5,26 +5,29 @@ import com.github.plokhotnyuk.jsoniter_scala.macros.*
5
5
import org .eclipse .jgit .api .Git
6
6
import org .eclipse .jgit .lib .{Constants , Ref }
7
7
8
- import scala .build .Positioned
8
+ import scala .build .{ Position , Positioned }
9
9
import scala .build .errors .{BuildException , MalformedInputError }
10
10
import scala .io .Codec
11
11
import scala .jdk .CollectionConverters .*
12
12
import scala .util .{Success , Try , Using }
13
13
14
14
sealed abstract class ComputeVersion extends Product with Serializable {
15
15
def get (workspace : os.Path ): Either [BuildException , String ]
16
+
17
+ val positions : Seq [Position ]
16
18
}
17
19
18
20
object ComputeVersion {
19
21
20
- final case class Command (command : Seq [String ]) extends ComputeVersion {
22
+ final case class Command (positions : Seq [ Position ], command : Seq [String ]) extends ComputeVersion {
21
23
def get (workspace : os.Path ): Either [BuildException , String ] = {
22
24
val maybeRes = Try (os.proc(command).call(stdin = os.Inherit , cwd = workspace, check = false ))
23
25
maybeRes match {
24
26
case Success (res) if res.exitCode == 0 =>
25
27
Right (res.out.trim(Codec .default))
26
28
case _ =>
27
29
Left (new Command .ComputeVersionCommandError (
30
+ positions,
28
31
command,
29
32
maybeRes.map(_.exitCode).getOrElse(1 )
30
33
))
@@ -33,13 +36,18 @@ object ComputeVersion {
33
36
}
34
37
35
38
object Command {
36
- final class ComputeVersionCommandError (command : Seq [String ], exitCode : Int )
37
- extends BuildException (
38
- s " Error running command ${command.mkString(" " )} (exit code: $exitCode) "
39
+ final class ComputeVersionCommandError (
40
+ positions : Seq [Position ],
41
+ command : Seq [String ],
42
+ exitCode : Int
43
+ ) extends BuildException (
44
+ s " Error running command ${command.mkString(" " )} (exit code: $exitCode) " ,
45
+ positions = positions
39
46
)
40
47
}
41
48
42
49
final case class GitTag (
50
+ positions : Seq [Position ],
43
51
repo : os.FilePath ,
44
52
dynVer : Boolean ,
45
53
defaultFirstVersion : String = " 0.1.0-SNAPSHOT"
@@ -115,17 +123,19 @@ object ComputeVersion {
115
123
Option (tagOrNull) match {
116
124
case None =>
117
125
Left (new GitTagError (
126
+ positions,
118
127
s " Unexpected error when running git describe from Git repository $repo0 (git describe doesn't find back tag $tag) "
119
128
))
120
129
case Some (tag) =>
121
130
versionOf(tag).map(_ + " -SNAPSHOT" ).toRight(
122
131
new GitTagError (
132
+ positions,
123
133
s " Unexpected error when running git describe from Git repository $repo0 (git describe-provided tag $tag doesn't have the expected shape) "
124
134
)
125
135
)
126
136
}
127
137
case (Some (_), None ) =>
128
- Left (new GitTagError (s " No stable tag found in Git repository $repo0" ))
138
+ Left (new GitTagError (positions, s " No stable tag found in Git repository $repo0" ))
129
139
case (_, Some ((tag, name))) =>
130
140
val idx = name.lastIndexOf('.' )
131
141
if (
@@ -134,6 +144,7 @@ object ComputeVersion {
134
144
Right (name.take(idx + 1 ) + (name.drop(idx + 1 ).toInt + 1 ).toString + " -SNAPSHOT" )
135
145
else
136
146
Left (new GitTagError (
147
+ positions,
137
148
s " Don't know how to bump version in tag $tag in Git repository $repo0"
138
149
))
139
150
}
@@ -142,32 +153,38 @@ object ComputeVersion {
142
153
Right (defaultFirstVersion)
143
154
}
144
155
else
145
- Left (new GitTagError (s " $repo0 doesn't look like a Git repository " ))
156
+ Left (new GitTagError (positions, s " $repo0 doesn't look like a Git repository " ))
146
157
}
147
158
}
148
159
object GitTag {
149
- final class GitTagError (message : String ) extends BuildException (message)
160
+ final class GitTagError (positions : Seq [Position ], message : String )
161
+ extends BuildException (message, positions)
150
162
}
151
163
152
164
private lazy val commandCodec : JsonValueCodec [List [String ]] =
153
165
JsonCodecMaker .make
154
166
155
167
def parse (input : Positioned [String ]): Either [BuildException , ComputeVersion ] =
156
168
if (input.value == " git" || input.value == " git:tag" )
157
- Right (ComputeVersion .GitTag (os.rel, dynVer = false ))
169
+ Right (ComputeVersion .GitTag (input.positions, os.rel, dynVer = false ))
158
170
else if (input.value.startsWith(" git:tag:" ))
159
- Right (ComputeVersion .GitTag (os.FilePath (input.value.stripPrefix(" git:tag:" )), dynVer = false ))
171
+ Right (ComputeVersion .GitTag (
172
+ input.positions,
173
+ os.FilePath (input.value.stripPrefix(" git:tag:" )),
174
+ dynVer = false
175
+ ))
160
176
else if (input.value == " git:dynver" )
161
- Right (ComputeVersion .GitTag (os.rel, dynVer = true ))
177
+ Right (ComputeVersion .GitTag (input.positions, os.rel, dynVer = true ))
162
178
else if (input.value.startsWith(" git:dynver:" ))
163
179
Right (ComputeVersion .GitTag (
180
+ input.positions,
164
181
os.FilePath (input.value.stripPrefix(" git:dynver:" )),
165
182
dynVer = true
166
183
))
167
184
else if (input.value.startsWith(" command:[" ))
168
185
try {
169
186
val command = readFromString(input.value.stripPrefix(" command:" ))(commandCodec)
170
- Right (ComputeVersion .Command (command))
187
+ Right (ComputeVersion .Command (input.positions, command))
171
188
}
172
189
catch {
173
190
case e : JsonReaderException =>
@@ -183,7 +200,7 @@ object ComputeVersion {
183
200
}
184
201
else if (input.value.startsWith(" command:" )) {
185
202
val command = input.value.stripPrefix(" command:" ).split(" \\ s+" ).toSeq
186
- Right (ComputeVersion .Command (command))
203
+ Right (ComputeVersion .Command (input.positions, command))
187
204
}
188
205
else
189
206
Left (
0 commit comments