Skip to content

Commit

Permalink
Download morphir-tools-cli from launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Corn authored and Justin Corn committed Oct 10, 2022
1 parent 31ef501 commit eb3cfcf
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 16 deletions.
1 change: 1 addition & 0 deletions .morphir-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.1-M01
25 changes: 15 additions & 10 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object morphir extends Module {
object contrib extends Module {
object flowz extends mill.Cross[FlowzModule](ScalaVersions.all: _*) {}
class FlowzModule(val crossScalaVersion: String) extends MorphirCrossScalaModule with MorphirPublishModule {
def ivyDeps = Agg(Deps.dev.zio.zio, Deps.dev.zio.`zio-json`)
def ivyDeps = Agg(dev.zio.zio, dev.zio.`zio-json`)
object test extends Tests with MorphirTestModule {

def moduleDeps = super.moduleDeps ++ Seq(morphir.testing(crossScalaVersion))
Expand Down Expand Up @@ -67,7 +67,7 @@ object morphir extends Module {
def scalacOptions = super.scalacOptions() ++ Seq("-Yretain-trees")
def crossScalaVersion = morphirScalaVersion
def moduleDeps = Seq(morphir.experimental.formats.core, ir)
def ivyDeps = Agg(Deps.dev.zio.`zio-json`)
def ivyDeps = Agg(dev.zio.`zio-json`)
object test extends Tests with MorphirTestModule {
def moduleDeps = super.moduleDeps ++ Seq(morphir.testing(crossScalaVersion))
}
Expand Down Expand Up @@ -120,7 +120,7 @@ object morphir extends Module {
}

class TestingModule(val crossScalaVersion: String) extends MorphirCrossScalaModule {
def ivyDeps = Agg(Deps.dev.zio.zio, Deps.dev.zio.`zio-test`)
def ivyDeps = Agg(dev.zio.zio, dev.zio.`zio-test`)
object test extends Tests with MorphirTestModule
}

Expand Down Expand Up @@ -180,18 +180,23 @@ object morphir extends Module {
}

def ivyDeps = Agg(
Deps.dev.zio.zio,
Deps.dev.zio.`zio-cli`,
Deps.dev.zio.`zio-json`,
Deps.dev.zio.`zio-process`
dev.zio.zio,
dev.zio.`zio-cli`,
dev.zio.`zio-json`,
dev.zio.`zio-process`
)
def packageDescription = "A command line interface for Morphir"
object test extends Tests with MorphirTestModule {}
}

object launcher extends MorphirScalaModule with MorphirPublishModule {
def crossScalaVersion = morphirScalaVersion
def ivyDeps = Agg(com.lihaoyi.mainargs, com.lihaoyi.`os-lib`)
object launcher extends MorphirScalaModule with BuildInfo with MorphirPublishModule {
def crossScalaVersion = ScalaVersions.scala213 // Coursier not available for Scala 3
def ivyDeps = Agg(com.lihaoyi.mainargs, com.lihaoyi.`os-lib`, io.`get-coursier`.coursier)
def buildInfoPackageName = Some("org.finos.morphir.launcher")
def buildInfoMembers = T {
val maybeLastTaggedVersion = VcsVersion.vcsState().lastTag.map(_.stripPrefix("v"))
Map("version" -> maybeLastTaggedVersion.getOrElse("0.0.0"))
}
object test extends Tests with MorphirTestModule {}
}

Expand Down
92 changes: 92 additions & 0 deletions launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash

# Download and run the Morphir launcher jar.
#
# The launcher jar downloads the other jars that are required for Morphir development.
#
# The version of the launcher jar can be set from MORPHIR_VERSION or the .morphir-version file.
#
# Order of precedence for launcher jar version is
# - MORPHIR_VERSION env variable
# - contents of version file .morphir-version
# - the default from this file

set -euo pipefail
#set -x

FALLBACK_MORPHIR_VERSION=0.1.1-M01
MORPHIR_VERSION_FILE=.morphir-version
CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}/morphir/launcher"

_log() {
echo $* >/dev/stderr
echo >/dev/stderr
}

_advise_version_is_from() {
local ver="$1"
local src="$2"

_log Using Morphir launcher version $ver set from $src...
}

_version_from_env() {
if [[ -n "${MORPHIR_VERSION+x}" ]]; then
_advise_version_is_from $MORPHIR_VERSION "MORPHIR_VERSION environment variable"
echo $MORPHIR_VERSION
else
return 1
fi
}

_version_from_file() {
if [[ -r "$MORPHIR_VERSION_FILE" ]]; then
local ver="$(cat "$MORPHIR_VERSION_FILE")"
_advise_version_is_from $ver "file $MORPHIR_VERSION_FILE"
echo $ver
else
return 1
fi
}

_version_fallback() {
_advise_version_is_from $FALLBACK_MORPHIR_VERSION "hard-coded default"
echo $FALLBACK_MORPHIR_VERSION
}

morphir_version() {
_version_from_env || _version_from_file || _version_fallback
}

curl_download() {
local url="$1"
local target="$2"

mkdir -p "$(dirname $target)"

_log Downloading $url to $target...

curl -Sf -o "$target" "$url"

_log
}

launcher_jar_url() {
local ver="$1"

echo https://repo1.maven.org/maven2/org/finos/morphir/morphir-tools-launcher_3/$ver/morphir-tools-launcher_3-$ver.jar
}

main() {
local ver="$(morphir_version)"
local url="$(launcher_jar_url "$ver")"
local jar="$CACHE_HOME/$ver.jar"

[[ ! -r "$jar" ]] && curl_download "$url" "$jar"

_log Running $jar...

java -jar "$jar"
}

main
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.finos.morphir.launcher

import coursier.cache.FileCache
import coursier.cache.loggers.RefreshLogger
import coursier.util.Task
import coursier.{Dependency, Fetch}

/**
* Wrapper for Coursier Fetch to facilitate logging progress to stderr and testing.
*/
trait Coursier {
def fetch(deps: Dependency*): Unit
}

// Avoid using, for instance, ZIO, for module implementation to keep dependencies minimal.
case object CoursierLive extends Coursier {
private def loggingFetch = {
val logger = RefreshLogger.create()
val cacheWithLogger = FileCache[Task]().withLogger(logger)
Fetch().withCache(cacheWithLogger)
}

def fetch(deps: Dependency*): Unit = loggingFetch.withDependencies(deps).run()
}
29 changes: 23 additions & 6 deletions morphir/tools/launcher/src/org/finos/morphir/launcher/Main.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package org.finos.morphir.launcher
import mainargs.{main, arg, ParserForMethods, Flag, Leftover}

import coursier.{Dependency, Module, ModuleName, Organization}
import mainargs.{ParserForMethods, main}

/**
* Fetch the necessary JARs for a Morphir development environment using Coursier.
*/
// Avoid using, for instance, ZIO, for dependency injection to keep dependencies minimal.
final case class Main(morphirVersion: MorphirVersion, coursier: Coursier) {
private val morphirCliDep =
Dependency(
Module(Organization("org.finos.morphir"), ModuleName("morphir-tools-cli_3")),
morphirVersion.version
)
def run(): Unit =
coursier.fetch(
morphirCliDep
)
}

object Main {
@main
def run(rest: Leftover[String]) = {
println("TODO: Implement")
println(s"rest: ${rest.value}")
}
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
def run(): Unit = Main(MorphirVersionLive, CoursierLive).run()

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args.toIndexedSeq)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.finos.morphir.launcher

import scala.io.Source
import scala.util.Try

/**
* Resolve the Morphir version.
*
* Order of precedence is
* - MORPHIR_VERSION env variable
* - contents of version file .morphir-version
* - the current project version from mill
*/
trait MorphirVersion {
def defaultVersion: String
def versionFromEnv: Option[String]
def versionFromFile: Option[String]

final def version: String = versionFromEnv.getOrElse(versionFromFile.getOrElse(defaultVersion))
}

// Avoid using, for instance, ZIO, for module implementation to keep dependencies minimal.
case object MorphirVersionLive extends MorphirVersion {
val morphirVersionKey: String = "MORPHIR_VERSION"
val morphirVersionFilePath: String = ".morphir-version"

override val defaultVersion: String = BuildInfo.version
override def versionFromEnv: Option[String] = sys.env.get(morphirVersionKey)
override def versionFromFile: Option[String] = Try(Source.fromFile(morphirVersionFilePath).mkString.strip).toOption
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.finos.morphir.launcher

import coursier.Dependency

case class CoursierTest() extends Coursier {
var fetched: Vector[Dependency] = Vector.empty
override def fetch(deps: Dependency*): Unit = fetched ++= deps
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.finos.morphir.launcher

import coursier.{Dependency, Module, ModuleName, Organization}
import zio.test._

object CoursierSpec extends ZIOSpecDefault {

def fixture = new {
val morphirVersion = MorphirVersionTest("dummyVersion", None, None)
val coursier = CoursierTest()
val main = Main(morphirVersion, coursier)
}

def spec = suite("Launcher")(
test("should fetch morphir cli") {
val f = fixture
val expectedMorphirCliDep = Dependency(
Module(Organization("org.finos.morphir"), ModuleName("morphir-tools-cli_3")),
"dummyVersion"
)
f.main.run()
assertTrue(f.coursier.fetched.contains(expectedMorphirCliDep))
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.finos.morphir.launcher

import zio.test._

object MorphirVersionSpec extends ZIOSpecDefault {

def spec = suite("MorphirVersion")(
suite("version resolution order")(
test("should resolve from env first") {
val morphirVersion = MorphirVersionTest(
defaultVersion = "versionA",
versionFromEnv = Some("versionB"),
versionFromFile = Some("versionC")
)

assertTrue(morphirVersion.version == morphirVersion.versionFromEnv.get)
},
test("should resolve from file second") {
val morphirVersion = MorphirVersionTest(
defaultVersion = "versionA",
versionFromEnv = None,
versionFromFile = Some("versionC")
)

assertTrue(morphirVersion.version == morphirVersion.versionFromFile.get)
},
test("should resolve from default third") {
val morphirVersion = MorphirVersionTest(
defaultVersion = "versionA",
versionFromEnv = None,
versionFromFile = None
)

assertTrue(morphirVersion.version == morphirVersion.defaultVersion)
}
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.finos.morphir.launcher

case class MorphirVersionTest(
defaultVersion: String,
versionFromEnv: Option[String],
versionFromFile: Option[String]
) extends MorphirVersion
4 changes: 4 additions & 0 deletions project/deps.sc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ object Deps {
def `borer-derivation`(scalaVersionParts: Seq[String]): Dep =
ivy"io.bullet::borer-derivation::${Versions.borer(scalaVersionParts)}"
}
case object `get-coursier` {
val coursier = ivy"io.get-coursier::coursier::${Versions.coursier}"
}
case object lemonlabs {
val `scala-uri` = ivy"io.lemonlabs::scala-uri:4.0.2"
}
Expand Down Expand Up @@ -73,6 +76,7 @@ object Versions {
case _ => "4.5.11"
}

val coursier = "2.1.0-M7"
val munit = "1.0.0-M4"
val mainargs = "0.3.0"
val `os-lib` = "0.8.1"
Expand Down

0 comments on commit eb3cfcf

Please sign in to comment.