Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version number to CLI output #339

Merged
merged 4 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,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
# | !?
Comment on lines -225 to -230
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was removed due to removal of printing ASTs without metas in #272.

#==='
#
#(
# meta
# query
Comment on lines 215 to +216
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to the existing test's AST.

# (
# 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