Skip to content

Commit

Permalink
Merge pull request #1432 from alexarchambault/publish-tweaks
Browse files Browse the repository at this point in the history
Publish / publish setup tweaks
  • Loading branch information
alexarchambault authored Mar 3, 2023
2 parents 1429de5 + 9f15e05 commit e327ca4
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 164 deletions.
89 changes: 55 additions & 34 deletions modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import scala.cli.config.{ConfigDb, Keys, PublishCredentials}
import scala.cli.errors.{
FailedToSignFileError,
MalformedChecksumsError,
MissingConfigEntryError,
MissingPublishOptionError,
UploadError
}
Expand Down Expand Up @@ -847,6 +848,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
if (repoParams.supportsSig)
if (publishOptions.contextual(isCi).secretKey.isDefined) Some(PSigner.BouncyCastle)
else if (publishOptions.contextual(isCi).gpgSignatureId.isDefined) Some(PSigner.Gpg)
else if (repoParams.shouldSign) Some(PSigner.BouncyCastle)
else None
else None
}
Expand All @@ -861,52 +863,71 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
case None => NopSigner
}
case Some(PSigner.BouncyCastle) =>
publishOptions.contextual(isCi).secretKey match {
val getLauncher: Supplier[Array[String]] = { () =>
val buildOptions = builds.headOption.map(_.options)
val archiveCache = buildOptions.map(_.archiveCache)
.getOrElse(ArchiveCache())
val fileCache = buildOptions.map(_.finalCache).getOrElse(FileCache())
PgpExternalCommand.launcher(
fileCache,
archiveCache,
logger,
() => builds.head.options.javaHome().value.javaCommand,
publishOptions.signingCli
) match {
case Left(e) => throw new Exception(e)
case Right(binaryCommand) => binaryCommand.toArray
}
}
val secretKeyDetailsOpt = publishOptions.contextual(isCi).secretKey match {
case Some(secretKey0) =>
val getLauncher: Supplier[Array[String]] = { () =>
val buildOptions = builds.headOption.map(_.options)
val archiveCache = buildOptions.map(_.archiveCache)
.getOrElse(ArchiveCache())
val fileCache = buildOptions.map(_.finalCache).getOrElse(FileCache())
PgpExternalCommand.launcher(
fileCache,
archiveCache,
logger,
() => builds.head.options.javaHome().value.javaCommand,
publishOptions.signingCli
) match {
case Left(e) => throw new Exception(e)
case Right(binaryCommand) => binaryCommand.toArray
}
val secretKey = secretKey0.get(configDb()).orExit(logger).toCliSigning
val secretKeyPassword = publishOptions
.contextual(isCi)
.secretKeyPassword
.orNull
.get(configDb())
.orExit(logger)
.toCliSigning
Some((secretKey, secretKeyPassword))
case None =>
configDb().get(Keys.pgpSecretKey).wrapConfigException.orExit(logger) match {
case Some(secretKey) =>
val secretKeyPassword =
configDb().get(Keys.pgpSecretKeyPassword).wrapConfigException
.flatMap {
case None =>
Left(new MissingConfigEntryError(Keys.pgpSecretKeyPassword.fullName))
case Some(p) => Right(p)
}
.orExit(logger)
Some((secretKey.toCliSigning, secretKeyPassword.toCliSigning))
case None =>
None
}
val secretKey = secretKey0.get(configDb()).orExit(logger)
}
secretKeyDetailsOpt match {
case Some((secretKey, secretKeyPassword)) =>
if (forceSigningBinary)
(new scala.cli.internal.BouncycastleSignerMakerSubst).get(
publishOptions
.contextual(isCi)
.secretKeyPassword
.orNull
.get(configDb())
.orExit(logger)
.toCliSigning,
secretKey.toCliSigning,
secretKeyPassword,
secretKey,
getLauncher,
logger
)
else
(new BouncycastleSignerMaker).get(
publishOptions
.contextual(isCi)
.secretKeyPassword
.orNull
.get(configDb())
.orExit(logger)
.toCliSigning,
secretKey.toCliSigning,
secretKeyPassword,
secretKey,
getLauncher,
logger
)
case None => NopSigner
case None =>
if (repoParams.shouldSign)
logger.diagnostic(
"PGP signatures are disabled, while these are recommended for this repository."
)
NopSigner
}
case Some(PSigner.Nop) => NopSigner
case None => NopSigner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ object PublishSetup extends ScalaCommand[PublishSetupOptions] {
written = written :+ dest
}

if (options.checkWorkflow.getOrElse(options.publishParams.isCi)) {
if (options.checkWorkflow.getOrElse(options.publishParams.setupCi)) {
val workflowDir = inputs.workspace / ".github" / "workflows"
val hasWorkflows = os.isDir(workflowDir) &&
os.list(workflowDir)
Expand Down Expand Up @@ -311,17 +311,41 @@ object PublishSetup extends ScalaCommand[PublishSetupOptions] {
}
}

if (options.checkGitignore.getOrElse(true)) {
val dest = inputs.workspace / ".gitignore"
val hasGitignore = os.exists(dest)
if (hasGitignore)
logger.message(
s"Found .gitignore under ${CommandUtils.printablePath(inputs.workspace)}, not writing one"
)
else {
val content = {
val resourcePath = Constants.defaultFilesResourcePath + "/gitignore"
val cl = Thread.currentThread().getContextClassLoader
val resUrl = cl.getResource(resourcePath)
if (resUrl == null)
sys.error(s"Should not happen - resource $resourcePath not found")
val is = resUrl.openStream()
try is.readAllBytes()
finally is.close()
}
os.write(dest, content, createFolders = true)
logger.message(s"Wrote gitignore in ${CommandUtils.printablePath(dest)}")
written = written :+ dest
}
}

if (written.nonEmpty)
logger.message("") // printing an empty line, for readability

if (options.publishParams.isCi && written.nonEmpty)
if (options.publishParams.setupCi && written.nonEmpty)
logger.message(
s"Commit and push ${written.map(CommandUtils.printablePath).mkString(", ")}, to enable publishing from CI"
)
else
logger.message("Project is ready for publishing!")

if (!options.publishParams.isCi) {
if (!options.publishParams.setupCi) {
logger.message("To publish your project, run")
logger.message {
val inputs = inputArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ final case class PublishSetupOptions(
@HelpMessage("Whether to check if a GitHub workflow already exists (one for publishing is written if none is found)")
checkWorkflow: Option[Boolean] = None,
@Group(HelpGroup.Publishing.toString)
@Tag(tags.restricted)
@HelpMessage("Whether to check if a .gitignore file already exists (one is written if none is found)")
checkGitignore: Option[Boolean] = None,
@Group(HelpGroup.Publishing.toString)
@Tag(tags.implementation)
@HelpMessage("Dummy mode - don't upload any secret to GitHub")
dummy: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final case class RepoParams(
defaultParallelUpload: Boolean,
supportsSig: Boolean,
acceptsChecksums: Boolean,
shouldSign: Boolean,
shouldAuthenticate: Boolean
) {
def withAuth(auth: Authentication): RepoParams =
Expand Down Expand Up @@ -87,6 +88,7 @@ object RepoParams {
true,
true,
true,
false,
false
)
}
Expand All @@ -112,6 +114,7 @@ object RepoParams {
true,
true,
true,
true,
true
)
}
Expand All @@ -125,6 +128,7 @@ object RepoParams {
false,
false,
false,
false,
true
)

Expand All @@ -151,6 +155,7 @@ object RepoParams {
true,
true,
true,
false,
false
)
}
Expand Down
Loading

0 comments on commit e327ca4

Please sign in to comment.