Skip to content

Commit

Permalink
Add version number and commit hash to CLI output (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 authored Dec 16, 2020
1 parent b3b84d7 commit de1fe32
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
28 changes: 28 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,39 @@ subprojects {

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

sourceSets {
main.kotlin.srcDirs = ["src"]
test.kotlin.srcDirs = ["test"]

main {
output.dir(generatedVersionDir, builtBy: 'generateVersionAndHashProperties')
}
}

// generates a build properties file with the current PartiQL version and most recent commit hash
task generateVersionAndHashProperties {
doLast {
def propertiesFile = file "$generatedVersionDir/partiql.properties"
propertiesFile.parentFile.mkdirs()
def properties = new Properties()

// get current PartiQL version
properties.setProperty("version", version.toString())

// get most recent short commit hash
def commitHash = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = commitHash
}
properties.setProperty("commit", commitHash.toString().trim())

propertiesFile.withWriter { properties.store(it, null) }
}
}
processResources.dependsOn generateVersionAndHashProperties
})

}
Expand Down
17 changes: 14 additions & 3 deletions cli/src/org/partiql/cli/Repl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ import com.amazon.ion.system.*
import com.amazon.ionelement.api.toIonValue
import org.partiql.cli.ReplState.*
import org.partiql.lang.*
import org.partiql.lang.ast.*
import org.partiql.lang.ast.passes.MetaStrippingRewriter
import org.partiql.lang.eval.*
import org.partiql.lang.syntax.*
import org.partiql.lang.util.*
import java.io.*
import java.util.Properties
import java.util.concurrent.*

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

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

fun retrievePartiQLVersionAndHash(): String {
val properties = Properties()
properties.load(this.javaClass.getResourceAsStream("/partiql.properties"))
return "${properties.getProperty("version")}-${properties.getProperty("commit")}"
}

private fun printVersionNumber() {
outputWriter.write("Using version: ${retrievePartiQLVersionAndHash()}")
outputWriter.write("\n")
}

private fun printPrompt() {
when {
buffer.isEmpty() -> outputWriter.write(PROMPT_1)
Expand Down Expand Up @@ -268,6 +278,7 @@ internal class Repl(private val valueFactory: ExprValueFactory,
state = when (state) {
INIT -> {
printWelcomeMessage()
printVersionNumber()
READY
}

Expand Down
65 changes: 17 additions & 48 deletions cli/test/org/partiql/cli/ReplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private class ReplTester(bindings: Bindings<ExprValue> = Bindings.empty()) {

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

val partiqlVersionAndHash = repl.retrievePartiQLVersionAndHash()

private val actualReplPrompt = StringBuilder()

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

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

@Test
fun singleQuery() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> 1+1
# |
#==='
Expand All @@ -167,6 +171,7 @@ class ReplTest {
fun querySemiColon() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> 1+1;
#==='
#2
Expand All @@ -180,6 +185,7 @@ class ReplTest {
fun multipleQuery() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> 1 + 1
# |
#==='
Expand All @@ -201,66 +207,24 @@ class ReplTest {
fun astWithoutMetas() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> 1 + 1
# | !!
#==='
#
#(
# plus
# (
# lit
# 1
# )
# (
# lit
# 1
# )
#)
#---
#OK!
#PartiQL>
""".trimMargin("#"))
}

@Test
fun astWithMetas() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#PartiQL> 1 + 1
# | !?
#==='
#
#(
# meta
# query
# (
# plus
# (
# meta
# (
# lit
# 1
# )
# {
# line:1,
# column:1
# }
# lit
# 1
# )
# (
# meta
# (
# lit
# 1
# )
# {
# line:1,
# column:5
# }
# lit
# 1
# )
# )
# {
# line:1,
# column:3
# }
#)
#---
#OK!
Expand All @@ -272,6 +236,7 @@ class ReplTest {
fun addToGlobalEnvAndQuery() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> !add_to_global_env {'myTable': <<{'a':1}, {'a': 2}>>}
# |
#==='
Expand Down Expand Up @@ -314,6 +279,7 @@ class ReplTest {

ReplTester(initialBindings).assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> !global_env
# |
#==='
Expand All @@ -334,6 +300,7 @@ class ReplTest {
fun dumpEmptyInitialEnv() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> !global_env
# |
#==='
Expand All @@ -348,6 +315,7 @@ class ReplTest {
fun dumpEnvAfterAltering() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> !add_to_global_env {'myTable': <<{'a':1}, {'a': 2}>>}
# |
#==='
Expand Down Expand Up @@ -386,6 +354,7 @@ class ReplTest {
fun listCommands() {
ReplTester().assertReplPrompt("""
#Welcome to the PartiQL REPL!
#Using version: $partiqlVersionAndHash
#PartiQL> !list_commands
# |
#
Expand Down

0 comments on commit de1fe32

Please sign in to comment.