8000 show update info in About command on linux · Pull Request #663 · VirtusLab/scala-cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

show update info in About command on linux #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/cli/src/main/scala/scala/cli/commands/About.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ class About(isSipScala: Boolean) extends ScalaCommand[AboutOptions] {
if (isSipScala) "Scala command"
else "Scala CLI"
println(s"$appName version $version" + detailedVersionOpt.fold("")(" (" + _ + ")"))
if (Version.isOutdated(None)) println(Update.updateInstructions)
}
}
27 changes: 27 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/CommandUtils.scala
8000
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package scala.cli.commands

import java.io.File
import java.nio.file.Paths

import scala.build.Os
import scala.util.Try

object CommandUtils {

def isOutOfDateVersion(newVersion: String, oldVersion: String): Boolean = {
Expand All @@ -8,5 +14,26 @@ object CommandUtils {
Version(newVersion) > Version(oldVersion)
}

// Ensure the path to the CLI is absolute
def getAbsolutePathToScalaCli(programName: String): String =
if (programName.contains(File.separator))
os.Path(programName, Os.pwd).toString
else
/*
In order to get absolute path we first try to get it from coursier.mainJar (this works for standalone launcher)
If this fails we fallback to getting it from this class and finally we may also use rawArg if there is nothing left
*/
sys.props.get("coursier.mainJar")
.map(Paths.get(_).toAbsolutePath.toString)
.orElse {
Try(
// This is weird but on windows we get /D:\a\scala-cli...
Paths.get(getClass.getProtectionDomain.getCodeSource.getLocation.toURI)
.toAbsolutePath
.toString
).toOption
}
.getOrElse(programName)

lazy val shouldCheckUpdate: Boolean = scala.util.Random.nextInt() % 10 == 1
}
28 changes: 2 additions & 26 deletions modules/cli/src/main/scala/scala/cli/commands/SetupIde.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import ch.epfl.scala.bsp4j.BspConnectionDetails
import com.google.gson.GsonBuilder
import upickle.default._

import java.io.File
import java.nio.charset.Charset
import java.nio.file.Paths

import scala.build.EitherCps.{either, value}
import scala.build.Inputs.WorkspaceOrigin
import scala.build.errors.{BuildException, WorkspaceError}
import scala.build.internal.{Constants, CustomCodeWrapper}
import scala.build.options.{BuildOptions, Scope}
import scala.build.{Artifacts, CrossSources, Inputs, Logger, Os, Sources}
import scala.build.{Artifacts, CrossSources, Inputs, Logger, Sources}
import scala.cli.CurrentParams
import scala.cli.errors.FoundVirtualInputsError
import scala.jdk.CollectionConverters._
import scala.util.Try

object SetupIde extends ScalaCommand[SetupIdeOptions] {

Expand Down Expand Up @@ -98,27 +95,6 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
val scalaCliBspJsonDestination =
inputs.workspace / Constants.workspaceDirName / "ide-options.json"

// Ensure the path to the CLI is absolute
val absolutePathToScalaCli: String =
if (progName.contains(File.separator))
os.Path(progName, Os.pwd).toString
else
/*
In order to get absolute path we first try to get it from coursier.mainJar (this works for standalone launcher)
If this fails we fallback to getting it from this class and finally we may also use rawArg if there is nothing left
*/
sys.props.get("coursier.mainJar")
.map(Paths.get(_).toAbsolutePath.toString)
.orElse {
Try(
// This is weird but on windows we get /D:\a\scala-cli...
Paths.get(getClass.getProtectionDomain.getCodeSource.getLocation.toURI)
.toAbsolutePath
.toString
).toOption
}
.getOrElse(progName)

val inputArgs = inputs.elements.collect {
case d: Inputs.>
val path = d.path
Expand All @@ -128,7 +104,7 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
}

val bspArgs =
List(absolutePathToScalaCli, "bsp") ++
List(CommandUtils.getAbsolutePathToScalaCli(progName), "bsp") ++
List("--json-options", scalaCliBspJsonDestination.toString) ++
inputArgs
val details = new BspConnectionDetails(
Expand Down
38 changes: 16 additions & 22 deletions modules/cli/src/main/scala/scala/cli/commands/Update.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caseapp._

import scala.build.Logger
import scala.cli.CurrentParams
import scala.cli.commands.Version.{getCurrentVersion, newestScalaCliVersion}
import scala.cli.internal.ProcUtil
import scala.io.StdIn.readLine
import scala.util.{Failure, Properties, Success, Try}
Expand Down Expand Up @@ -50,23 +51,13 @@ object Update extends ScalaCommand[UpdateOptions] {
}
}

def update(options: UpdateOptions, scalaCliBinPath: os.Path) = {
val currentVersion = {
val res = os.proc(scalaCliBinPath, "version").call(cwd = os.pwd, check = false)
if (res.exitCode == 0)
res.out.text().trim
else
"0.0.0"
}
lazy val updateInstructions: String =
s"""Your Scala CLI version is outdated. The newest version is $newestScalaCliVersion
|It is recommended that you update Scala CLI through the same tool or method you used for its initial installation for avoiding the creation of outdated duplicates.""".stripMargin

lazy val newestScalaCliVersion = {
val resp = ProcUtil.downloadFile("https://github.com/VirtusLab/scala-cli/releases/latest")
def update(options: UpdateOptions, maybeScalaCliBinPath: Option[os.Path]): Unit = {

val scalaCliVersionRegex = "tag/v(.*?)\"".r
scalaCliVersionRegex.findFirstMatchIn(resp).map(_.group(1))
}.getOrElse(
sys.error("Can not resolve ScalaCLI version to update")
)
val currentVersion = getCurrentVersion(maybeScalaCliBinPath)

val isOutdated = CommandUtils.isOutOfDateVersion(newestScalaCliVersion, currentVersion)

Expand All @@ -85,13 +76,14 @@ object Update extends ScalaCommand[UpdateOptions] {

val scalaCliBinPath = options.installDirPath / options.binaryName

lazy val execScalaCliPath = os.proc("which", "scala-cli").call(
cwd = os.pwd,
mergeErrIntoOut = true,
check = false
).out.text().trim
val programName = argvOpt.flatMap(_.headOption).getOrElse {
sys.error("update called in a non-standard way :|")
}

lazy val isScalaCliInPath = // if binDir is non empty, we not except scala-cli in PATH, it is useful in tests
execScalaCliPath.contains(options.installDirPath.toString()) || options.binDir.isDefined
CommandUtils.getAbsolutePathToScalaCli(programName).contains(
options.installDirPath.toString()
) || options.binDir.isDefined

if (!os.exists(scalaCliBinPath) || !isScalaCliInPath) {
if (!options.isInternalRun) {
Expand All @@ -107,7 +99,9 @@ object Update extends ScalaCommand[UpdateOptions] {
sys.exit(1)
}
}
else update(options, scalaCliBinPath)
else if (options.binaryName == "scala-cli") update(options, None)
else
update(options, Some(scalaCliBinPath))
}

def run(options: UpdateOptions, args: RemainingArgs): Unit = {
Expand Down
28 changes: 28 additions & 0 deletions modules/cli/src/main/scala/scala/cli/commands/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@ import caseapp._

import scala.build.internal.Constants
import scala.cli.CurrentParams
import scala.cli.internal.ProcUtil

object Version extends ScalaCommand[VersionOptions] {
override def group = "Miscellaneous"
def run(options: VersionOptions, args: RemainingArgs): Unit = {
CurrentParams.verbosity = options.verbosity.verbosity
println(Constants.version)
}

lazy val newestScalaCliVersion = {
val scalaCliVersionRegex = "tag/v(.*?)\"".r

val resp = ProcUtil.downloadFile("https://github.com/VirtusLab/scala-cli/releases/latest")

scalaCliVersionRegex.findFirstMatchIn(resp).map(_.group(1))
}.getOrElse(
sys.error("Can not resolve ScalaCLI version to update")
)

def isOutdated(maybeScalaCliBinPath: Option[os.Path]): Boolean =
CommandUtils.isOutOfDateVersion(newestScalaCliVersion, getCurrentVersion(maybeScalaCliBinPath))

def getCurrentVersion(maybeScalaCliBinPath: Option[os.Path]): String = {
val maybeCurrentVersion = maybeScalaCliBinPath.map {
scalaCliBinPath =>
val res = os.proc(scalaCliBinPath, "version").call(cwd = os.pwd, check = false)
if (res.exitCode == 0)
res.out.text().trim
else
"0.0.0"
}
maybeCurrentVersion.getOrElse(Constants.version)

}

}
0