-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement package manager abstraction
- Loading branch information
Showing
23 changed files
with
261 additions
and
198 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 |
---|---|---|
|
@@ -20,6 +20,8 @@ jobs: | |
node-version: 16.14.2 | ||
- name: Setup yarn | ||
run: npm install -g [email protected] | ||
- name: Setup pnpm | ||
run: npm install -g [email protected] | ||
- name: Unit tests | ||
run: sbt test | ||
- name: Scripted tests | ||
|
126 changes: 0 additions & 126 deletions
126
sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala
This file was deleted.
Oops, something went wrong.
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
163 changes: 163 additions & 0 deletions
163
sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.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,163 @@ | ||
package scalajsbundler | ||
|
||
import java.io.File | ||
|
||
import sbt._ | ||
import scalajsbundler.util.Commands | ||
import scalajsbundler.util.JSON | ||
|
||
abstract class PackageManager { | ||
|
||
val name: String | ||
val installCommand: String | ||
val installArgs: Seq[String] | ||
|
||
/** | ||
* Runs the command `cmd` | ||
* @param args Command arguments | ||
* @param workingDir Working directory of the process | ||
* @param logger Logger | ||
*/ | ||
protected def run(args: String*)(workingDir: File, logger: Logger): Unit = | ||
Commands.run(cmd ++: args, workingDir, logger) | ||
|
||
private val cmd = sys.props("os.name").toLowerCase match { | ||
case os if os.contains("win") => Seq("cmd", "/c", name) | ||
case _ => Seq(name) | ||
} | ||
|
||
def install(baseDir: File, | ||
installDir: File, | ||
logger: Logger): Unit = { | ||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileRead(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
|
||
run(installCommand +: installArgs: _*)(installDir, logger) | ||
|
||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileWrite(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
} | ||
|
||
val packageJsonContents: Map[String, JSON] | ||
} | ||
|
||
trait AddPackagesSupport { this: PackageManager => | ||
|
||
val addPackagesCommand: String | ||
val addPackagesArgs: Seq[String] | ||
|
||
/** | ||
* Locally install NPM packages | ||
* | ||
* @param baseDir The (sub-)project directory which contains yarn.lock | ||
* @param installDir The directory in which to install the packages | ||
* @param logger sbt logger | ||
* @param npmPackages Packages to install (e.g. "webpack", "[email protected]") | ||
*/ | ||
def addPackages(baseDir: File, | ||
installDir: File, | ||
logger: Logger, | ||
)(npmPackages: String*): Unit = { | ||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileRead(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
|
||
run(addPackagesCommand +: (addPackagesArgs ++ npmPackages): _*)(installDir, logger) | ||
|
||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileWrite(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
} | ||
} | ||
|
||
trait LockFileSupport { | ||
val lockFileName: String | ||
|
||
def lockFileRead( | ||
baseDir: File, | ||
installDir: File, | ||
logger: Logger | ||
): Unit = { | ||
val sourceLockFile = baseDir / lockFileName | ||
val targetLockFile = installDir / lockFileName | ||
|
||
if (sourceLockFile.exists()) { | ||
logger.info("Using lockfile " + sourceLockFile) | ||
IO.copyFile(sourceLockFile, targetLockFile) | ||
} | ||
} | ||
|
||
def lockFileWrite( | ||
baseDir: File, | ||
installDir: File, | ||
logger: Logger | ||
): Unit = { | ||
val sourceLockFile = baseDir / lockFileName | ||
val targetLockFile = installDir / lockFileName | ||
|
||
if (targetLockFile.exists()) { | ||
logger.debug("Wrote lockfile to " + sourceLockFile) | ||
IO.copyFile(targetLockFile, sourceLockFile) | ||
} | ||
} | ||
} | ||
|
||
case class Npm( | ||
name: String = "npm", | ||
lockFileName: String = "package-lock.json", | ||
installCommand: String = "install", | ||
installArgs: Seq[String] = Seq.empty, | ||
addPackagesCommand: String = "install", | ||
addPackagesArgs: Seq[String] = Seq.empty, | ||
) extends PackageManager | ||
with LockFileSupport | ||
with AddPackagesSupport { | ||
override val packageJsonContents: Map[String, JSON] = Map.empty | ||
} | ||
|
||
case class Yarn( | ||
name: String = "yarn", | ||
version: Option[String] = None, | ||
lockFileName: String = "yarn.lock", | ||
installCommand: String = "install", | ||
installArgs: Seq[String] = Yarn.DefaultArgs, | ||
addPackagesCommand: String = "add", | ||
addPackagesArgs: Seq[String] = Yarn.DefaultArgs | ||
) extends PackageManager | ||
with LockFileSupport | ||
with AddPackagesSupport { | ||
override val packageJsonContents: Map[String, JSON] = | ||
version.map(v => Map("packageManager" -> JSON.str(s"$name@$v"))).getOrElse(Map.empty) | ||
} | ||
object Yarn { | ||
val DefaultArgs: Seq[String] = Seq("--non-interactive", "--mutex", "network") | ||
} | ||
|
||
case class Pnpm( | ||
name: String = "pnpm", | ||
version: Option[String] = None, | ||
lockFileName: String = "pnpm-lock.yaml", | ||
installCommand: String = "install", | ||
installArgs: Seq[String] = Seq.empty, | ||
addPackagesCommand: String = "add", | ||
addPackagesArgs: Seq[String] = Seq.empty | ||
) extends PackageManager | ||
with LockFileSupport | ||
with AddPackagesSupport { | ||
override val packageJsonContents: Map[String, JSON] = | ||
version.map(v => Map("packageManager" -> JSON.str(s"$name@$v"))).getOrElse(Map.empty) | ||
} |
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
Oops, something went wrong.