Skip to content

Commit

Permalink
Add "publish local" command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed May 12, 2022
1 parent 5808eb3 commit 2533386
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scala.cli.commands.publish

import caseapp._

import scala.cli.commands.{CompileCrossOptions, MainClassOptions, SharedOptions, SharedWatchOptions}

// format: off
final case class PublishLocalOptions(
@Recurse
shared: SharedOptions = SharedOptions(),
@Recurse
watch: SharedWatchOptions = SharedWatchOptions(),
@Recurse
compileCross: CompileCrossOptions = CompileCrossOptions(),
@Recurse
mainClass: MainClassOptions = MainClassOptions(),
@Recurse
publishParams: PublishParamsOptions = PublishParamsOptions(),
@Recurse
sharedPublish: SharedPublishOptions = SharedPublishOptions()
)
// format: on

object PublishLocalOptions {
implicit lazy val parser: Parser[PublishLocalOptions] = Parser.derive
implicit lazy val help: Help[PublishLocalOptions] = Help.derive
}
3 changes: 2 additions & 1 deletion modules/cli/src/main/scala/scala/cli/ScalaCliCommands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.cli.commands._
import scala.cli.commands.bloop.BloopOutput
import scala.cli.commands.github.{SecretCreate, SecretList}
import scala.cli.commands.pgp.{PgpCommands, PgpCommandsSubst}
import scala.cli.commands.publish.Publish
import scala.cli.commands.publish.{Publish, PublishLocal}

class ScalaCliCommands(
val progName: String,
Expand Down Expand Up @@ -45,6 +45,7 @@ class ScalaCliCommands(
Repl,
Package,
Publish,
PublishLocal,
Run,
SecretCreate,
SecretList,
Expand Down
77 changes: 42 additions & 35 deletions modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ object Publish extends ScalaCommand[PublishOptions] {

lazy val workingDir = options.sharedPublish.workingDir
.filter(_.trim.nonEmpty)
.map(os.Path(_, Os.pwd))
.map(os.Path(_, os.pwd))
.getOrElse {
os.temp.dir(
prefix = "scala-cli-publish-",
Expand All @@ -174,6 +174,7 @@ object Publish extends ScalaCommand[PublishOptions] {
cross,
workingDir,
ivy2HomeOpt,
publishLocal = false,
options.watch.watch
)
}
Expand All @@ -187,6 +188,7 @@ object Publish extends ScalaCommand[PublishOptions] {
cross: Boolean,
workingDir: => os.Path,
ivy2HomeOpt: Option[os.Path],
publishLocal: Boolean,
watch: Boolean
): Unit = {

Expand All @@ -203,7 +205,7 @@ object Publish extends ScalaCommand[PublishOptions] {
postAction = () => WatchUtil.printWatchMessage()
) { res =>
res.orReport(logger).foreach { builds =>
maybePublish(builds, workingDir, ivy2HomeOpt, logger, allowExit = false)
maybePublish(builds, workingDir, ivy2HomeOpt, publishLocal, logger, allowExit = false)
}
}
try WatchUtil.waitForCtrlC()
Expand All @@ -221,7 +223,7 @@ object Publish extends ScalaCommand[PublishOptions] {
buildTests = false,
partial = None
).orExit(logger)
maybePublish(builds, workingDir, ivy2HomeOpt, logger, allowExit = true)
maybePublish(builds, workingDir, ivy2HomeOpt, publishLocal, logger, allowExit = true)
}
}

Expand All @@ -236,6 +238,7 @@ object Publish extends ScalaCommand[PublishOptions] {
builds: Builds,
workingDir: os.Path,
ivy2HomeOpt: Option[os.Path],
publishLocal: Boolean,
logger: Logger,
allowExit: Boolean
): Unit = {
Expand All @@ -257,7 +260,7 @@ object Publish extends ScalaCommand[PublishOptions] {
val docBuilds0 = builds.allDoc.collect {
case s: Build.Successful => s
}
val res = doPublish(builds0, docBuilds0, workingDir, ivy2HomeOpt, logger)
val res = doPublish(builds0, docBuilds0, workingDir, ivy2HomeOpt, publishLocal, logger)
if (allowExit)
res.orExit(logger)
else
Expand Down Expand Up @@ -486,6 +489,7 @@ object Publish extends ScalaCommand[PublishOptions] {
docBuilds: Seq[Build.Successful],
workingDir: os.Path,
ivy2HomeOpt: Option[os.Path],
publishLocal: Boolean,
logger: Logger
): Either[BuildException, Unit] = either {

Expand Down Expand Up @@ -544,37 +548,40 @@ object Publish extends ScalaCommand[PublishOptions] {
)
}

publishOptions.repository match {
case None =>
value(Left(new MissingPublishOptionError(
"repository",
"--publish-repository",
"publish.repository"
)))
case Some("ivy2-local") =>
ivy2Local
case Some("central" | "maven-central" | "mvn-central") =>
centralRepo("https://oss.sonatype.org")
case Some("central-s01" | "maven-central-s01" | "mvn-central-s01") =>
centralRepo("https://s01.oss.sonatype.org")
case Some(repoStr) =>
val repo0 = RepositoryParser.repositoryOpt(repoStr)
.collect {
case m: MavenRepository =>
m
}
.getOrElse {
val url =
if (repoStr.contains("://")) repoStr
else os.Path(repoStr, Os.pwd).toNIO.toUri.toASCIIString
MavenRepository(url)
}
(
PublishRepository.Simple(repo0),
Hooks.dummy,
publishOptions.repositoryIsIvy2LocalLike.getOrElse(false)
)
}
if (publishLocal)
ivy2Local
else
publishOptions.repository match {
case None =>
value(Left(new MissingPublishOptionError(
"repository",
"--publish-repository",
"publish.repository"
)))
case Some("ivy2-local") =>
ivy2Local
case Some("central" | "maven-central" | "mvn-central") =>
centralRepo("https://oss.sonatype.org")
case Some("central-s01" | "maven-central-s01" | "mvn-central-s01") =>
centralRepo("https://s01.oss.sonatype.org")
case Some(repoStr) =>
val repo0 = RepositoryParser.repositoryOpt(repoStr)
.collect {
case m: MavenRepository =>
m
}
.getOrElse {
val url =
if (repoStr.contains("://")) repoStr
else os.Path(repoStr, Os.pwd).toNIO.toUri.toASCIIString
MavenRepository(url)
}
(
PublishRepository.Simple(repo0),
Hooks.dummy,
publishOptions.repositoryIsIvy2LocalLike.getOrElse(false)
)
}
}

val now = Instant.now()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package scala.cli.commands.publish

import caseapp.core.RemainingArgs

import scala.cli.commands.ScalaCommand
import scala.cli.commands.util.SharedOptionsUtil._
import scala.cli.CurrentParams
import scala.build.BuildThreads

object PublishLocal extends ScalaCommand[PublishLocalOptions] {

override def group = "Main"
override def inSipScala = false
override def sharedOptions(options: PublishLocalOptions) =
Some(options.shared)

override def names = List(
List("publish", "local")
)

def run(options: PublishLocalOptions, args: RemainingArgs): Unit = {
maybePrintGroupHelp(options)

CurrentParams.verbosity = options.shared.logging.verbosity
val inputs = options.shared.inputsOrExit(args)
CurrentParams.workspaceOpt = Some(inputs.workspace)

val logger = options.shared.logger
val initialBuildOptions = Publish.mkBuildOptions(
options.shared,
options.publishParams,
options.sharedPublish,
PublishRepositoryOptions(),
options.mainClass,
None
).orExit(logger)
val threads = BuildThreads.create()

val compilerMaker = options.shared.compilerMaker(threads)
val docCompilerMaker = options.shared.compilerMaker(threads, scaladoc = true)

val cross = options.compileCross.cross.getOrElse(false)

lazy val workingDir = options.sharedPublish.workingDir
.filter(_.trim.nonEmpty)
.map(os.Path(_, os.pwd))
.getOrElse {
os.temp.dir(
prefix = "scala-cli-publish-",
deleteOnExit = true
)
}

val ivy2HomeOpt = options.sharedPublish.ivy2Home
.filter(_.trim.nonEmpty)
.map(os.Path(_, os.pwd))

Publish.doRun(
inputs,
logger,
initialBuildOptions,
compilerMaker,
docCompilerMaker,
cross,
workingDir,
ivy2HomeOpt,
publishLocal = true,
options.watch.watch
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,68 @@ import com.eed3si9n.expecty.Expecty.expect

class PublishTestsDefault extends PublishTestDefinitions(scalaVersionOpt = None) {

test("publish local") {
val testOrg = "test-local-org.sth"
val testName = "my-proj"
val testVersion = "1.5.6"
val inputs = TestInputs(
Seq(
os.rel / "Project.scala" ->
s"""//> using publish.organization "$testOrg"
|//> using publish.name "$testName"
|//> using publish.version "$testVersion"
|
|//> using scala "2.13"
|//> using lib "com.lihaoyi::os-lib:0.8.1"
|
|object Project {
| def message = "Hello"
|
| def main(args: Array[String]): Unit =
| println(message)
|}
|""".stripMargin
)
)

val expectedFiles = {
val modName = s"${testName}_2.13"
val base = os.rel / testOrg / modName / testVersion
val baseFiles = Seq(
base / "jars" / s"$modName.jar",
base / "docs" / s"$modName-javadoc.jar",
base / "srcs" / s"$modName-sources.jar",
base / "poms" / s"$modName.pom",
base / "ivys" / "ivy.xml"
)
baseFiles
.flatMap { f =>
val md5 = f / os.up / s"${f.last}.md5"
val sha1 = f / os.up / s"${f.last}.sha1"
Seq(f, md5, sha1)
}
.toSet
}

inputs.fromRoot { root =>
os.proc(TestUtil.cli, "publish", "local", "Project.scala", "--ivy2-home", os.rel / "ivy2")
.call(cwd = root)
val ivy2Local = root / "ivy2" / "local"
val foundFiles = os.walk(ivy2Local)
.filter(os.isFile(_))
.map(_.relativeTo(ivy2Local))
.toSet
val missingFiles = expectedFiles -- foundFiles
val unexpectedFiles = foundFiles -- expectedFiles
if (missingFiles.nonEmpty)
pprint.err.log(missingFiles)
if (unexpectedFiles.nonEmpty)
pprint.err.log(unexpectedFiles)
expect(missingFiles.isEmpty)
expect(unexpectedFiles.isEmpty)
}
}

test("Pure Java") {
val testOrg = "test-org.foo"
val testName = "foo"
Expand Down

0 comments on commit 2533386

Please sign in to comment.