diff --git a/cli/src/main/scala/coursier/cli/Coursier.scala b/cli/src/main/scala/coursier/cli/Coursier.scala index 9bc06a63b..dbd8ec866 100644 --- a/cli/src/main/scala/coursier/cli/Coursier.scala +++ b/cli/src/main/scala/coursier/cli/Coursier.scala @@ -32,11 +32,27 @@ case class CommonOptions( repository: List[String], @HelpMessage("Maximum number of parallel downloads (default: 6)") @ExtraName("n") - parallel: Int = 6 + parallel: Int = 6, + @Recurse + cacheOptions: CacheOptions ) { val verbose0 = verbose.length + (if (quiet) 1 else 0) } +object CacheOptions { + def default = + sys.env.getOrElse( + "COURSIER_CACHE", + sys.props("user.home") + "/.coursier/cache" + ) +} + +case class CacheOptions( + @HelpMessage("Cache directory (defaults to environment variable COURSIER_CACHE or ~/.coursier/cache)") + @ExtraName("C") + cache: String = CacheOptions.default +) + @AppName("Coursier") @ProgName("coursier") sealed trait CoursierCommand extends Command @@ -176,7 +192,9 @@ case class Repository( list: Boolean, @ExtraName("l") defaultList: Boolean, - ivyLike: Boolean + ivyLike: Boolean, + @Recurse + cacheOptions: CacheOptions ) extends CoursierCommand { if (add.exists(!_.contains(":"))) { @@ -200,7 +218,7 @@ case class Repository( } - val cache = Cache.default + val cache = Cache(new File(cacheOptions.cache)) if (cache.cache.exists() && !cache.cache.isDirectory) { Console.err.println(s"Error: ${cache.cache} not a directory") @@ -263,6 +281,10 @@ case class Bootstrap( @HelpMessage(s"Internal use - prepend base classpath options to arguments") @ExtraName("b") prependClasspath: Boolean, + @HelpMessage("Set environment variables in the generated launcher. No escaping is done. Value is simply put between quotes in the launcher preamble.") + @ValueDescription("NAME=VALUE") + @ExtraName("e") + env: List[String], @Recurse common: CommonOptions ) extends CoursierCommand { @@ -280,6 +302,18 @@ case class Bootstrap( sys.exit(255) } + val (validEnv, wrongEnv) = env.partition(_.contains("=")) + if (wrongEnv.nonEmpty) { + Console.err.println(s"Wrong -e / --env option(s):\n${wrongEnv.mkString("\n")}") + sys.exit(255) + } + + val env0 = validEnv.map { s => + val idx = s.indexOf('=') + assert(idx >= 0) + (s.take(idx), s.drop(idx + 1)) + } + val downloadDir0 = if (downloadDir.isEmpty) "$HOME/" @@ -316,11 +350,16 @@ case class Bootstrap( sys.exit(1) } - val shellPreamble = Seq( - "#!/usr/bin/env sh", - "exec java -jar \"$0\" " + (if (prependClasspath) "-B " else "") + "\"" + mainClass + "\" \"" + downloadDir + "\" " + urls.map("\"" + _ + "\"").mkString(" ") + " -- \"$@\"", - "" - ).mkString("\n") + val shellPreamble = { + Seq( + "#!/usr/bin/env sh" + ) ++ + env0.map { case (k, v) => "export " + k + "=\"" + v + "\"" } ++ + Seq( + "exec java -jar \"$0\" " + (if (prependClasspath) "-B " else "") + "\"" + mainClass + "\" \"" + downloadDir + "\" " + urls.map("\"" + _ + "\"").mkString(" ") + " -- \"$@\"", + "" + ) + }.mkString("\n") try NIOFiles.write(output0.toPath, shellPreamble.getBytes("UTF-8") ++ bootstrapJar) catch { case e: IOException => diff --git a/cli/src/main/scala/coursier/cli/Helper.scala b/cli/src/main/scala/coursier/cli/Helper.scala index e256e2d99..ae28c8b83 100644 --- a/cli/src/main/scala/coursier/cli/Helper.scala +++ b/cli/src/main/scala/coursier/cli/Helper.scala @@ -117,7 +117,7 @@ class Helper( else CachePolicy.Default - val cache = Cache.default + val cache = Cache(new File(cacheOptions.cache)) cache.init(verbose = verbose0 >= 0) val repositoryIds = {