Skip to content

Commit 7f09e6e

Browse files
authored
Add install home command (#137)
* Add install home command
1 parent 4a3cc9b commit 7f09e6e

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

modules/build/src/main/scala/scala/build/Directories.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dev.dirs.{GetWinDirs, ProjectDirectories}
44

55
trait Directories {
66
def localRepoDir: os.Path
7+
def binRepoDir: os.Path
78
def completionsDir: os.Path
89
def virtualProjectsDir: os.Path
910
def bspSocketDir: os.Path
@@ -14,6 +15,8 @@ object Directories {
1415
final case class OsLocations(projDirs: ProjectDirectories) extends Directories {
1516
lazy val localRepoDir: os.Path =
1617
os.Path(projDirs.cacheDir, Os.pwd) / "local-repo"
18+
lazy val binRepoDir: os.Path =
19+
os.Path(localRepoDir, Os.pwd) / "bin"
1720
lazy val completionsDir: os.Path =
1821
os.Path(projDirs.dataLocalDir, Os.pwd) / "completions"
1922
lazy val virtualProjectsDir: os.Path =
@@ -27,6 +30,8 @@ object Directories {
2730
final case class SubDir(dir: os.Path) extends Directories {
2831
lazy val localRepoDir: os.Path =
2932
dir / "cache" / "local-repo"
33+
lazy val binRepoDir: os.Path =
34+
localRepoDir / "bin"
3035
lazy val completionsDir: os.Path =
3136
dir / "data-local" / "completions"
3237
lazy val virtualProjectsDir: os.Path =

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ object ScalaCli extends CommandsEntryPoint {
2323
Export,
2424
Fmt,
2525
InstallCompletions,
26+
InstallHome,
2627
Metabrowse,
2728
Repl,
2829
Package,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package scala.cli.commands
2+
3+
import caseapp._
4+
import coursier.env.{EnvironmentUpdate, ProfileUpdater}
5+
import scala.io.StdIn.readLine
6+
7+
import scala.util.Properties
8+
9+
object InstallHome extends ScalaCommand[InstallHomeOptions] {
10+
override def hidden: Boolean = true
11+
def run(options: InstallHomeOptions, args: RemainingArgs): Unit = {
12+
13+
val binDirPath = scala.build.Directories.default().binRepoDir
14+
val scalaCliBinPath = binDirPath / "scala-cli"
15+
16+
if (os.exists(scalaCliBinPath)) {
17+
if (options.force) os.remove.all(scalaCliBinPath)
18+
else if (coursier.paths.Util.useAnsiOutput()) {
19+
println(
20+
"scala-cli already exists. Do you want to override it [Y/n]"
21+
)
22+
val replace = readLine()
23+
if (replace == "Y") {
24+
os.remove.all(scalaCliBinPath)
25+
}
26+
else {
27+
System.err.println("Abort")
28+
sys.exit(1)
29+
}
30+
}
31+
else {
32+
System.err.println(
33+
s"Error: scala-cli already exists. Pass -f or --force to force erasing it."
34+
)
35+
sys.exit(1)
36+
}
37+
}
38+
39+
os.copy(
40+
from = os.Path(options.scalaCliBinaryPath, os.pwd),
41+
to = scalaCliBinPath / "scala-cli",
42+
createFolders = true
43+
)
44+
if (!Properties.isWin)
45+
os.perms.set(scalaCliBinPath / "scala-cli", os.PermSet.fromString("rwxrwxr-x"))
46+
47+
val update = EnvironmentUpdate(Nil, Seq("PATH" -> scalaCliBinPath.toString()))
48+
49+
val didUpdate =
50+
if (Properties.isWin) {
51+
val updater = CustomWindowsEnvVarUpdater().withUseJni(Some(coursier.paths.Util.useJni()))
52+
updater.applyUpdate(update)
53+
}
54+
else {
55+
val updater = ProfileUpdater()
56+
updater.applyUpdate(update)
57+
}
58+
59+
if (didUpdate) {
60+
println("Successfully installed scala-cli")
61+
}
62+
else {
63+
System.err.println(s"scala-cli is already installed")
64+
}
65+
66+
}
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package scala.cli.commands
2+
3+
import caseapp._
4+
5+
// format: off
6+
@HelpMessage("Install scala-cli in a sub-directory of the home directory")
7+
final case class InstallHomeOptions(
8+
@Group("InstallHome")
9+
scalaCliBinaryPath: String,
10+
@Group("InstallHome")
11+
@Name("f")
12+
@HelpMessage("Overwrite scala-cli if exists")
13+
force: Boolean = false,
14+
)
15+
// format: on
16+
17+
object InstallHomeOptions {
18+
implicit val parser = Parser[InstallHomeOptions]
19+
implicit val help = Help[InstallHomeOptions]
20+
}

website/docs/reference/cli-options.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Available in commands:
237237
- [`export`](./commands#export)
238238
- [`fmt`](./commands#fmt)
239239
- [`install completions`](./commands#install-completions)
240+
- [`install-home`](./commands#install-home)
240241
- [`browse` / `metabrowse`](./commands#browse)
241242
- [`package`](./commands#package)
242243
- [`console` / `repl`](./commands#console)
@@ -285,6 +286,22 @@ Aliases: `--shell`
285286

286287
#### `--env`
287288

289+
## Install home options
290+
291+
Available in commands:
292+
- [`install-home`](./commands#install-home)
293+
294+
295+
<!-- Automatically generated, DO NOT EDIT MANUALLY -->
296+
297+
#### `--scala-cli-binary-path`
298+
299+
#### `--force`
300+
301+
Aliases: `-f`
302+
303+
Overwrite scala-cli if exists
304+
288305
## Java options
289306

290307
Available in commands:

website/docs/reference/commands.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,10 @@ Accepts options:
222222
- [jvm](./cli-options.md#jvm-options)
223223
- [logging](./cli-options.md#logging-options)
224224

225+
### `install-home`
226+
227+
Install scala-cli in a sub-directory of the home directory
228+
229+
Accepts options:
230+
- [install home](./cli-options.md#install-home-options)
231+

0 commit comments

Comments
 (0)