generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Download morphir-tools-cli from launcher
- Loading branch information
Justin Corn
authored and
Justin Corn
committed
Oct 10, 2022
1 parent
31ef501
commit eb3cfcf
Showing
11 changed files
with
267 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.1-M01 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
morphir/tools/launcher/src/org/finos/morphir/launcher/Coursier.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
morphir/tools/launcher/src/org/finos/morphir/launcher/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
30 changes: 30 additions & 0 deletions
30
morphir/tools/launcher/src/org/finos/morphir/launcher/MorphirVersion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
8 changes: 8 additions & 0 deletions
8
morphir/tools/launcher/test/src/org/finos/morphir/launcher/CoursierTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
25 changes: 25 additions & 0 deletions
25
morphir/tools/launcher/test/src/org/finos/morphir/launcher/MainSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
morphir/tools/launcher/test/src/org/finos/morphir/launcher/MorphirVersionSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
morphir/tools/launcher/test/src/org/finos/morphir/launcher/MorphirVersionTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters