Created
April 14, 2024 23:51
-
-
Save fayimora/296dbedd6cc0bb6a14cc38dca0462039 to your computer and use it in GitHub Desktop.
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
//> using toolkit latest | |
//> using dep com.lihaoyi::pprint::0.8.1 | |
//> using dep com.lihaoyi::mainargs::0.5.0 | |
//> using dep com.lihaoyi::fansi::0.4.0 | |
import mainargs.{arg, main, Flag, ParserForMethods} | |
import pprint.{pprintln => pp} | |
import scala.util.Try | |
import scala.util.Failure | |
import scala.util.Success | |
object Install { | |
@main | |
def run() = | |
installZsh() | |
// installHomebrew() | |
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args) | |
def installZsh() = { | |
info("Checking for ZSH") | |
downloadAndInstall( | |
"https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/HEAD/tools/install.sh", | |
os.pwd / "installzsh.sh", | |
"zsh" | |
) | |
} | |
def installHomebrew() = { | |
info("Checking for Homebrew") | |
downloadAndInstall( | |
"https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh", | |
os.pwd / "installbrew.sh", | |
"brew" | |
) | |
} | |
def downloadAndInstall(url: String, dest: os.Path, cmd: String) = { | |
val call = Try(os.proc("which", cmd).call().out.text().trim()) | |
call match | |
case Failure(exception) => | |
error(s"$cmd not found, installing...") | |
downloadInstallationFile(url, dest) | |
os.proc("/bin/bash", "-c", s""""./${dest.last}"""").call() | |
case Success(value) => | |
warn(s"$cmd found at $value, skipping installation.") | |
} | |
def downloadInstallationFile(url: String, dest: os.Path) = { | |
val temp = os.temp() | |
val curl = os.proc("curl", "-fsSL", url).call(stdout = temp) | |
if (curl.exitCode != 0) { | |
error(s"Error downloading $url") | |
sys.exit(1) | |
} | |
os.write.over(dest, os.read(temp), perms = os.PermSet.fromInt(755)) | |
} | |
def warn(msg: String, color: fansi.Attrs = fansi.Color.LightYellow) = { | |
pp(color(msg)) | |
} | |
def error(msg: String, color: fansi.Attrs = fansi.Color.Red) = { | |
pp(color(msg)) | |
} | |
def info(msg: String, color: fansi.Attrs = fansi.Color.LightBlue) = { | |
pp(color(msg)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment