Skip to content

Commit

Permalink
Enable to force using jvm signing launcher for native launcher of sca…
Browse files Browse the repository at this point in the history
…la-cli
  • Loading branch information
lwronski committed Nov 21, 2022
1 parent 7ee0b52 commit 6691b6e
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.util.Base64
import scala.build.Logger
import scala.build.Ops.*
import scala.build.errors.{BuildException, CompositeBuildException, MalformedCliInputError}
import scala.cli.commands.pgp.PgpScalaSigningOptions
import scala.cli.commands.publish.ConfigUtil.*
import scala.cli.commands.util.JvmUtils
import scala.cli.commands.{ScalaCommand, SpecificationLevel}
Expand Down Expand Up @@ -75,7 +76,8 @@ object Config extends ScalaCommand[ConfigOptions] {
ArchiveCache().withCache(coursierCache),
coursierCache,
logger.verbosity
).value.javaCommand
).value.javaCommand,
options.scalaSigning.cliOptions()
).orExit(logger)
val pgpSecretBase64 = pgpSecret0.map(Base64.getEncoder.encodeToString)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scala.cli.commands.config

import caseapp.*

import scala.cli.commands.pgp.PgpScalaSigningOptions
import scala.cli.commands.shared.{
CoursierOptions,
HasLoggingOptions,
Expand All @@ -20,7 +21,8 @@ final case class ConfigOptions(
coursier: CoursierOptions = CoursierOptions(),
@Recurse
jvm: SharedJvmOptions = SharedJvmOptions(),

@Recurse
scalaSigning: PgpScalaSigningOptions = PgpScalaSigningOptions(),
@Group("Config")
@HelpMessage("Dump config DB as JSON")
@Hidden
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package scala.cli.commands.config

import coursier.cache.Cache
import coursier.cache.{Cache, FileCache}
import coursier.util.Task

import java.security.SecureRandom

import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.BuildException
import scala.cli.commands.pgp.PgpProxyMaker
import scala.build.{Logger, options => bo}
import scala.cli.commands.pgp.{PgpProxyMaker, PgpScalaSigningOptions}
import scala.cli.errors.PgpError
import scala.cli.signing.shared.Secret
import scala.util.Properties
Expand All @@ -32,8 +32,9 @@ object ThrowawayPgpSecret {
mail: String,
password: Secret[String],
logger: Logger,
cache: Cache[Task],
javaCommand: () => String
cache: FileCache[Task],
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, (Secret[String], Secret[Array[Byte]])] = either {

val dir = os.temp.dir(perms = if (Properties.isWin) null else "rwx------")
Expand All @@ -48,7 +49,8 @@ object ThrowawayPgpSecret {
password.value,
cache,
logger,
javaCommand
javaCommand,
signingCliOptions
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package scala.cli.commands.pgp

import coursier.cache.{ArchiveCache, Cache}
import coursier.Repositories
import coursier.cache.{ArchiveCache, Cache, FileCache}
import coursier.core.Version
import coursier.util.Task
import dependency._

import java.io.File

import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.errors.{BuildException, ScalaJsLinkingError}
import scala.build.internal.Util.{DependencyOps, ModuleOps}
import scala.build.internal.{
Constants,
ExternalBinary,
ExternalBinaryParams,
FetchExternalBinary,
Runner
Runner,
ScalaJsLinkerConfig
}
import scala.build.options.scalajs.ScalaJsLinkerOptions
import scala.build.{Logger, Positioned, options => bo}
import scala.cli.ScalaCli
import scala.cli.commands.util.JvmUtils
import scala.util.Properties
Expand All @@ -23,20 +30,26 @@ abstract class PgpExternalCommand extends ExternalCommand {
def externalCommand: Seq[String]

def tryRun(
cache: Cache[Task],
versionOpt: Option[String],
cache: FileCache[Task],
args: Seq[String],
extraEnv: Map[String, String],
logger: Logger,
allowExecve: Boolean,
javaCommand: () => String
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, Int] = either {

val archiveCache = ArchiveCache().withCache(cache)

val binary = value(PgpExternalCommand.launcher(archiveCache, versionOpt, logger, javaCommand))
val binary = value(PgpExternalCommand.launcher(
cache,
archiveCache,
logger,
javaCommand,
signingCliOptions
))

val command = binary.command ++ externalCommand ++ args
val command = binary ++ externalCommand ++ args

Runner.run0(
progName,
Expand All @@ -49,19 +62,25 @@ abstract class PgpExternalCommand extends ExternalCommand {
}

def output(
cache: Cache[Task],
versionOpt: Option[String],
cache: FileCache[Task],
args: Seq[String],
extraEnv: Map[String, String],
logger: Logger,
javaCommand: () => String
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, String] = either {

val archiveCache = ArchiveCache().withCache(cache)

val binary = value(PgpExternalCommand.launcher(archiveCache, versionOpt, logger, javaCommand))
val binary = value(PgpExternalCommand.launcher(
cache,
archiveCache,
logger,
javaCommand,
signingCliOptions
))

val command = binary.command ++ externalCommand ++ args
val command = binary ++ externalCommand ++ args

os.proc(command).call(stdin = os.Inherit, env = extraEnv)
.out.text()
Expand All @@ -82,7 +101,6 @@ abstract class PgpExternalCommand extends ExternalCommand {
val cache = options.coursier.coursierCache(logger.coursierLogger(""))
val retCode = tryRun(
cache,
options.signingCliVersion.map(_.trim).filter(_.nonEmpty),
remainingArgs,
Map(),
logger,
Expand All @@ -92,7 +110,8 @@ abstract class PgpExternalCommand extends ExternalCommand {
ArchiveCache().withCache(cache),
cache,
logger.verbosity
).value.javaCommand
).value.javaCommand,
options.scalaSigning.cliOptions()
).orExit(logger)

if (retCode != 0)
Expand All @@ -102,33 +121,69 @@ abstract class PgpExternalCommand extends ExternalCommand {

object PgpExternalCommand {
def launcher(
cache: FileCache[Task],
archiveCache: ArchiveCache[Task],
versionOpt: Option[String],
logger: Logger,
javaCommand: () => String
): Either[BuildException, ExternalBinary] = {

val platformSuffix = FetchExternalBinary.platformSuffix()
val version = versionOpt
.getOrElse(Constants.scalaCliSigningVersion)
val (tag, changing) =
if (version == "latest") ("launchers", true)
else ("v" + version, false)
val ext = if (Properties.isWin) ".zip" else ".gz"
val url =
s"https://github.com/scala-cli/scala-cli-signing/releases/download/$tag/scala-cli-signing-$platformSuffix$ext"

val ver = if (version.startsWith("latest")) "latest.release" else version
val params = ExternalBinaryParams(
url,
changing,
"scala-cli-signing",
Seq(
dep"${Constants.scalaCliSigningOrganization}:${Constants.scalaCliSigningName}:$ver"
),
"scala.cli.signing.ScalaCliSigning"
)

FetchExternalBinary.fetch(params, archiveCache, logger, javaCommand)
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, Seq[String]] = either {

val version =
signingCliOptions.signingCliVersion
.getOrElse(Constants.scalaCliSigningVersion)
val ver = if (version.startsWith("latest")) "latest.release" else version
val signingMainClass = "scala.cli.signing.ScalaCliSigning"
val jvmSigningDep =
dep"${Constants.scalaCliSigningOrganization}:${Constants.scalaCliSigningName}_3:$ver"

if (signingCliOptions.useJvm.getOrElse(false)) {
val extraRepos =
if (version.endsWith("SNAPSHOT"))
Seq(Repositories.sonatype("snapshots").root)
else
Nil

val signingClassPath = value {
scala.build.Artifacts.fetch0(
Positioned.none(Seq(jvmSigningDep.toCs)),
extraRepos,
None,
Nil,
logger,
cache,
None
)
}.files

val command = Seq[os.Shellable](
javaCommand(),
signingCliOptions.javaArgs,
"-cp",
signingClassPath.map(_.getAbsolutePath).mkString(File.pathSeparator),
signingMainClass
)

command.flatMap(_.value)
}
else {
val platformSuffix = FetchExternalBinary.platformSuffix()
val (tag, changing) =
if (version == "latest") ("launchers", true)
else ("v" + version, false)
val ext = if (Properties.isWin) ".zip" else ".gz"
val url =
s"https://github.com/scala-cli/scala-cli-signing/releases/download/$tag/scala-cli-signing-$platformSuffix$ext"
val params = ExternalBinaryParams(
url,
changing,
"scala-cli-signing",
Seq(jvmSigningDep),
signingMainClass
)
val binary = value {
FetchExternalBinary.fetch(params, archiveCache, logger, javaCommand)
}
binary.command
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final case class PgpExternalOptions(
jvm: SharedJvmOptions = SharedJvmOptions(),
@Recurse
coursier: CoursierOptions = CoursierOptions(),
@Hidden
signingCliVersion: Option[String] = None
@Recurse
scalaSigning: PgpScalaSigningOptions = PgpScalaSigningOptions()
) extends HasLoggingOptions
// format: on

Expand Down
22 changes: 12 additions & 10 deletions modules/cli/src/main/scala/scala/cli/commands/pgp/PgpProxy.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package scala.cli.commands.pgp

import coursier.cache.Cache
import coursier.cache.{Cache, FileCache}
import coursier.util.Task

import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.{Logger, options => bo}
import scala.cli.commands.pgp.{PgpCreateExternal, PgpKeyIdExternal}
import scala.cli.errors.PgpError
import scala.util.Properties
Expand All @@ -16,14 +16,14 @@ class PgpProxy {
mail: String,
quiet: Boolean,
password: String,
cache: Cache[Task],
cache: FileCache[Task],
logger: Logger,
javaCommand: () => String
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, Int] = {
val quietOptions = Nil
(new PgpCreateExternal).tryRun(
cache,
None,
Seq(
"pgp",
"create",
Expand All @@ -39,16 +39,18 @@ class PgpProxy {
Map("SCALA_CLI_RANDOM_KEY_PASSWORD" -> password),
logger,
allowExecve = false,
javaCommand
javaCommand,
signingCliOptions
)
}

def keyId(
key: String,
keyPrintablePath: String,
cache: Cache[Task],
cache: FileCache[Task],
logger: Logger,
javaCommand: () => String
javaCommand: () => String,
signingCliOptions: bo.ScalaSigningCliOptions
): Either[BuildException, String] = {
val keyPath =
if (Properties.isWin)
Expand All @@ -59,11 +61,11 @@ class PgpProxy {
try {
(new PgpKeyIdExternal).output(
cache,
None,
Seq(keyPath.toString),
Map(),
logger,
javaCommand
javaCommand,
signingCliOptions
).map(_.trim)
}
finally os.remove(keyPath)
Expand Down
Loading

0 comments on commit 6691b6e

Please sign in to comment.