Skip to content

Commit de1fe32

Browse files
authored
Add version number and commit hash to CLI output (#339)
1 parent b3b84d7 commit de1fe32

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
lines changed

build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,39 @@ subprojects {
9191

9292
})
9393
plugins.withId('org.jetbrains.kotlin.jvm', { _ ->
94+
def generatedVersionDir = "${buildDir}/generated-version"
9495

9596
sourceSets {
9697
main.kotlin.srcDirs = ["src"]
9798
test.kotlin.srcDirs = ["test"]
99+
100+
main {
101+
output.dir(generatedVersionDir, builtBy: 'generateVersionAndHashProperties')
102+
}
103+
}
104+
105+
// generates a build properties file with the current PartiQL version and most recent commit hash
106+
task generateVersionAndHashProperties {
107+
doLast {
108+
def propertiesFile = file "$generatedVersionDir/partiql.properties"
109+
propertiesFile.parentFile.mkdirs()
110+
def properties = new Properties()
111+
112+
// get current PartiQL version
113+
properties.setProperty("version", version.toString())
114+
115+
// get most recent short commit hash
116+
def commitHash = new ByteArrayOutputStream()
117+
exec {
118+
commandLine 'git', 'rev-parse', '--short', 'HEAD'
119+
standardOutput = commitHash
120+
}
121+
properties.setProperty("commit", commitHash.toString().trim())
122+
123+
propertiesFile.withWriter { properties.store(it, null) }
124+
}
98125
}
126+
processResources.dependsOn generateVersionAndHashProperties
99127
})
100128

101129
}

cli/src/org/partiql/cli/Repl.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ import com.amazon.ion.system.*
1818
import com.amazon.ionelement.api.toIonValue
1919
import org.partiql.cli.ReplState.*
2020
import org.partiql.lang.*
21-
import org.partiql.lang.ast.*
22-
import org.partiql.lang.ast.passes.MetaStrippingRewriter
2321
import org.partiql.lang.eval.*
2422
import org.partiql.lang.syntax.*
2523
import org.partiql.lang.util.*
2624
import java.io.*
25+
import java.util.Properties
2726
import java.util.concurrent.*
2827

2928
internal const val PROMPT_1 = "PartiQL> "
3029
internal const val PROMPT_2 = " | "
3130
internal const val BAR_1 = "===' "
3231
internal const val BAR_2 = "--- "
33-
internal const val WELCOME_MSG = "Welcome to the PartiQL REPL!" // TODO: extract version from gradle.build and append to message
32+
internal const val WELCOME_MSG = "Welcome to the PartiQL REPL!"
3433

3534
private enum class ReplState {
3635
/** Initial state, first state as soon as you start the REPL */
@@ -192,6 +191,17 @@ internal class Repl(private val valueFactory: ExprValueFactory,
192191
outputWriter.write("\n")
193192
}
194193

194+
fun retrievePartiQLVersionAndHash(): String {
195+
val properties = Properties()
196+
properties.load(this.javaClass.getResourceAsStream("/partiql.properties"))
197+
return "${properties.getProperty("version")}-${properties.getProperty("commit")}"
198+
}
199+
200+
private fun printVersionNumber() {
201+
outputWriter.write("Using version: ${retrievePartiQLVersionAndHash()}")
202+
outputWriter.write("\n")
203+
}
204+
195205
private fun printPrompt() {
196206
when {
197207
buffer.isEmpty() -> outputWriter.write(PROMPT_1)
@@ -268,6 +278,7 @@ internal class Repl(private val valueFactory: ExprValueFactory,
268278
state = when (state) {
269279
INIT -> {
270280
printWelcomeMessage()
281+
printVersionNumber()
271282
READY
272283
}
273284

cli/test/org/partiql/cli/ReplTest.kt

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ private class ReplTester(bindings: Bindings<ExprValue> = Bindings.empty()) {
7979

8080
private val repl = Repl(valueFactory, input, output, parser, compiler, bindings, zeroTimer)
8181

82+
val partiqlVersionAndHash = repl.retrievePartiQLVersionAndHash()
83+
8284
private val actualReplPrompt = StringBuilder()
8385

8486
private val outputPhaser = Phaser()
@@ -148,11 +150,13 @@ private class ReplTester(bindings: Bindings<ExprValue> = Bindings.empty()) {
148150

149151
@Ignore("https://github.com/partiql/partiql-lang-kotlin/issues/266")
150152
class ReplTest {
153+
private val partiqlVersionAndHash = ReplTester().partiqlVersionAndHash
151154

152155
@Test
153156
fun singleQuery() {
154157
ReplTester().assertReplPrompt("""
155158
#Welcome to the PartiQL REPL!
159+
#Using version: $partiqlVersionAndHash
156160
#PartiQL> 1+1
157161
# |
158162
#==='
@@ -167,6 +171,7 @@ class ReplTest {
167171
fun querySemiColon() {
168172
ReplTester().assertReplPrompt("""
169173
#Welcome to the PartiQL REPL!
174+
#Using version: $partiqlVersionAndHash
170175
#PartiQL> 1+1;
171176
#==='
172177
#2
@@ -180,6 +185,7 @@ class ReplTest {
180185
fun multipleQuery() {
181186
ReplTester().assertReplPrompt("""
182187
#Welcome to the PartiQL REPL!
188+
#Using version: $partiqlVersionAndHash
183189
#PartiQL> 1 + 1
184190
# |
185191
#==='
@@ -201,66 +207,24 @@ class ReplTest {
201207
fun astWithoutMetas() {
202208
ReplTester().assertReplPrompt("""
203209
#Welcome to the PartiQL REPL!
210+
#Using version: $partiqlVersionAndHash
204211
#PartiQL> 1 + 1
205212
# | !!
206213
#==='
207214
#
208215
#(
209-
# plus
210-
# (
211-
# lit
212-
# 1
213-
# )
214-
# (
215-
# lit
216-
# 1
217-
# )
218-
#)
219-
#---
220-
#OK!
221-
#PartiQL>
222-
""".trimMargin("#"))
223-
}
224-
225-
@Test
226-
fun astWithMetas() {
227-
ReplTester().assertReplPrompt("""
228-
#Welcome to the PartiQL REPL!
229-
#PartiQL> 1 + 1
230-
# | !?
231-
#==='
232-
#
233-
#(
234-
# meta
216+
# query
235217
# (
236218
# plus
237219
# (
238-
# meta
239-
# (
240-
# lit
241-
# 1
242-
# )
243-
# {
244-
# line:1,
245-
# column:1
246-
# }
220+
# lit
221+
# 1
247222
# )
248223
# (
249-
# meta
250-
# (
251-
# lit
252-
# 1
253-
# )
254-
# {
255-
# line:1,
256-
# column:5
257-
# }
224+
# lit
225+
# 1
258226
# )
259227
# )
260-
# {
261-
# line:1,
262-
# column:3
263-
# }
264228
#)
265229
#---
266230
#OK!
@@ -272,6 +236,7 @@ class ReplTest {
272236
fun addToGlobalEnvAndQuery() {
273237
ReplTester().assertReplPrompt("""
274238
#Welcome to the PartiQL REPL!
239+
#Using version: $partiqlVersionAndHash
275240
#PartiQL> !add_to_global_env {'myTable': <<{'a':1}, {'a': 2}>>}
276241
# |
277242
#==='
@@ -314,6 +279,7 @@ class ReplTest {
314279

315280
ReplTester(initialBindings).assertReplPrompt("""
316281
#Welcome to the PartiQL REPL!
282+
#Using version: $partiqlVersionAndHash
317283
#PartiQL> !global_env
318284
# |
319285
#==='
@@ -334,6 +300,7 @@ class ReplTest {
334300
fun dumpEmptyInitialEnv() {
335301
ReplTester().assertReplPrompt("""
336302
#Welcome to the PartiQL REPL!
303+
#Using version: $partiqlVersionAndHash
337304
#PartiQL> !global_env
338305
# |
339306
#==='
@@ -348,6 +315,7 @@ class ReplTest {
348315
fun dumpEnvAfterAltering() {
349316
ReplTester().assertReplPrompt("""
350317
#Welcome to the PartiQL REPL!
318+
#Using version: $partiqlVersionAndHash
351319
#PartiQL> !add_to_global_env {'myTable': <<{'a':1}, {'a': 2}>>}
352320
# |
353321
#==='
@@ -386,6 +354,7 @@ class ReplTest {
386354
fun listCommands() {
387355
ReplTester().assertReplPrompt("""
388356
#Welcome to the PartiQL REPL!
357+
#Using version: $partiqlVersionAndHash
389358
#PartiQL> !list_commands
390359
# |
391360
#

0 commit comments

Comments
 (0)