2024-08-09 10:35:28 -07:00
|
|
|
// Package versions handles all operations pertaining to specific versions.
|
|
|
|
// Install, uninstall, etc...
|
|
|
|
package versions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2024-08-17 11:10:16 -07:00
|
|
|
"regexp"
|
2024-08-09 10:35:28 -07:00
|
|
|
"strings"
|
|
|
|
|
2024-09-05 06:31:30 -07:00
|
|
|
"asdf/internal/config"
|
|
|
|
"asdf/internal/hook"
|
2024-09-05 06:55:13 -07:00
|
|
|
"asdf/internal/installs"
|
2024-09-05 06:31:30 -07:00
|
|
|
"asdf/internal/plugins"
|
2024-08-17 13:27:18 -07:00
|
|
|
"asdf/internal/resolve"
|
2024-09-05 19:12:08 -07:00
|
|
|
"asdf/internal/shims"
|
2024-10-05 14:49:28 -07:00
|
|
|
"asdf/internal/toolversions"
|
2024-08-09 10:35:28 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
systemVersion = "system"
|
|
|
|
latestVersion = "latest"
|
2024-09-05 19:12:08 -07:00
|
|
|
uninstallableVersionMsg = "uninstallable version: %s"
|
2024-08-17 11:10:16 -07:00
|
|
|
latestFilterRegex = "(?i)(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)"
|
|
|
|
noLatestVersionErrMsg = "no latest version found"
|
2024-08-09 10:35:28 -07:00
|
|
|
)
|
|
|
|
|
2024-08-22 06:16:28 -07:00
|
|
|
// UninstallableVersionError is an error returned if someone tries to install the
|
2024-08-09 10:35:28 -07:00
|
|
|
// system version.
|
2024-09-05 19:12:08 -07:00
|
|
|
type UninstallableVersionError struct {
|
|
|
|
versionType string
|
|
|
|
}
|
2024-08-09 10:35:28 -07:00
|
|
|
|
2024-08-22 06:16:28 -07:00
|
|
|
func (e UninstallableVersionError) Error() string {
|
2024-09-05 19:12:08 -07:00
|
|
|
return fmt.Sprintf(uninstallableVersionMsg, e.versionType)
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|
|
|
|
|
2024-08-22 06:16:28 -07:00
|
|
|
// NoVersionSetError is returned whenever an operation that requires a version
|
|
|
|
// is not able to resolve one.
|
|
|
|
type NoVersionSetError struct {
|
|
|
|
toolName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e NoVersionSetError) Error() string {
|
|
|
|
// Eventually switch this to a more friendly error message, BATS tests fail
|
|
|
|
// with this improvement
|
|
|
|
// return fmt.Sprintf("no version set for plugin %s", e.toolName)
|
|
|
|
return "no version set"
|
|
|
|
}
|
|
|
|
|
2024-08-17 13:27:18 -07:00
|
|
|
// InstallAll installs all specified versions of every tool for the current
|
|
|
|
// directory. Typically this will just be a single version, if not already
|
|
|
|
// installed, but it may be multiple versions if multiple versions for the tool
|
|
|
|
// are specified in the .tool-versions file.
|
|
|
|
func InstallAll(conf config.Config, dir string, stdOut io.Writer, stdErr io.Writer) (failures []error) {
|
|
|
|
plugins, err := plugins.List(conf, false, false)
|
|
|
|
if err != nil {
|
|
|
|
return []error{fmt.Errorf("unable to list plugins: %w", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally we should install these in the order they are specified in the
|
|
|
|
// closest .tool-versions file, but for now that is too complicated to
|
|
|
|
// implement.
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
err := Install(conf, plugin, dir, stdOut, stdErr)
|
|
|
|
if err != nil {
|
|
|
|
failures = append(failures, err)
|
|
|
|
}
|
|
|
|
}
|
2024-08-09 10:35:28 -07:00
|
|
|
|
2024-08-17 13:27:18 -07:00
|
|
|
return failures
|
|
|
|
}
|
2024-08-09 10:35:28 -07:00
|
|
|
|
2024-08-17 13:27:18 -07:00
|
|
|
// Install installs all specified versions of a tool for the current directory.
|
|
|
|
// Typically this will just be a single version, if not already installed, but
|
|
|
|
// it may be multiple versions if multiple versions for the tool are specified
|
|
|
|
// in the .tool-versions file.
|
|
|
|
func Install(conf config.Config, plugin plugins.Plugin, dir string, stdOut io.Writer, stdErr io.Writer) error {
|
2024-08-09 10:35:28 -07:00
|
|
|
err := plugin.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-17 13:27:18 -07:00
|
|
|
versions, found, err := resolve.Version(conf, plugin, dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found || len(versions.Versions) == 0 {
|
2024-08-22 06:16:28 -07:00
|
|
|
return NoVersionSetError{toolName: plugin.Name}
|
2024-08-17 13:27:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, version := range versions.Versions {
|
2024-12-03 06:35:37 -07:00
|
|
|
err := InstallOneVersion(conf, plugin, version, false, stdOut, stdErr)
|
2024-08-17 13:27:18 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstallVersion installs a version of a specific tool, the version may be an
|
|
|
|
// exact version, or it may be `latest` or `latest` a regex query in order to
|
|
|
|
// select the latest version matching the provided pattern.
|
2024-12-03 06:35:37 -07:00
|
|
|
func InstallVersion(conf config.Config, plugin plugins.Plugin, version toolversions.Version, stdOut io.Writer, stdErr io.Writer) error {
|
2024-08-17 13:27:18 -07:00
|
|
|
err := plugin.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|
|
|
|
|
2024-12-03 06:35:37 -07:00
|
|
|
resolvedVersion := ""
|
|
|
|
if version.Type == latestVersion {
|
|
|
|
resolvedVersion, err = Latest(plugin, version.Value)
|
2024-08-17 11:10:16 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|
|
|
|
|
2024-12-03 06:35:37 -07:00
|
|
|
return InstallOneVersion(conf, plugin, resolvedVersion, false, stdOut, stdErr)
|
2024-08-17 13:27:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstallOneVersion installs a specific version of a specific tool
|
2024-12-03 06:35:37 -07:00
|
|
|
func InstallOneVersion(conf config.Config, plugin plugins.Plugin, versionStr string, keepDownload bool, stdOut io.Writer, stdErr io.Writer) error {
|
2024-08-17 13:27:18 -07:00
|
|
|
err := plugin.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
if versionStr == systemVersion {
|
|
|
|
return UninstallableVersionError{versionType: systemVersion}
|
2024-08-17 13:27:18 -07:00
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
version := toolversions.Parse(versionStr)
|
2024-08-09 10:35:28 -07:00
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
if version.Type == "path" {
|
2024-09-05 19:12:08 -07:00
|
|
|
return UninstallableVersionError{versionType: "path"}
|
|
|
|
}
|
2024-10-31 06:30:48 -07:00
|
|
|
downloadDir := installs.DownloadPath(conf, plugin, version)
|
|
|
|
installDir := installs.InstallPath(conf, plugin, version)
|
2024-09-05 19:12:08 -07:00
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
if installs.IsInstalled(conf, plugin, version) {
|
2024-08-09 10:35:28 -07:00
|
|
|
return fmt.Errorf("version %s of %s is already installed", version, plugin.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
env := map[string]string{
|
2024-10-31 06:30:48 -07:00
|
|
|
"ASDF_INSTALL_TYPE": version.Type,
|
|
|
|
"ASDF_INSTALL_VERSION": version.Value,
|
2024-08-09 10:35:28 -07:00
|
|
|
"ASDF_INSTALL_PATH": installDir,
|
|
|
|
"ASDF_DOWNLOAD_PATH": downloadDir,
|
2024-08-22 06:16:28 -07:00
|
|
|
"ASDF_CONCURRENCY": asdfConcurrency(conf),
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(downloadDir, 0o777)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create download dir: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
err = hook.RunWithOutput(conf, fmt.Sprintf("pre_asdf_download_%s", plugin.Name), []string{version.Value}, stdOut, stdErr)
|
2024-08-09 10:35:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to run pre-download hook: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.RunCallback("download", []string{}, env, stdOut, stdErr)
|
|
|
|
if _, ok := err.(plugins.NoCallbackError); err != nil && !ok {
|
|
|
|
return fmt.Errorf("failed to run download callback: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
err = hook.RunWithOutput(conf, fmt.Sprintf("pre_asdf_install_%s", plugin.Name), []string{version.Value}, stdOut, stdErr)
|
2024-08-09 10:35:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to run pre-install hook: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(installDir, 0o777)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create install dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.RunCallback("install", []string{}, env, stdOut, stdErr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to run install callback: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-05 19:12:08 -07:00
|
|
|
// Reshim
|
|
|
|
err = shims.GenerateAll(conf, stdOut, stdErr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to generate shims post-install: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
err = hook.RunWithOutput(conf, fmt.Sprintf("post_asdf_install_%s", plugin.Name), []string{version.Value}, stdOut, stdErr)
|
2024-08-09 10:35:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to run post-install hook: %w", err)
|
|
|
|
}
|
2024-12-03 06:35:37 -07:00
|
|
|
|
|
|
|
// delete download dir
|
|
|
|
keep, err := conf.AlwaysKeepDownload()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if keep || keepDownload {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(downloadDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to remove download dir: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:35:28 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-22 06:16:28 -07:00
|
|
|
func asdfConcurrency(conf config.Config) string {
|
|
|
|
val, ok := os.LookupEnv("ASDF_CONCURRENCY")
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
val, err := conf.Concurrency()
|
|
|
|
if err != nil {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-08-17 11:10:16 -07:00
|
|
|
// Latest invokes the plugin's latest-stable callback if it exists and returns
|
|
|
|
// the version it returns. If the callback is missing it invokes the list-all
|
|
|
|
// callback and returns the last version matching the query, if a query is
|
|
|
|
// provided.
|
2024-08-20 05:15:05 -07:00
|
|
|
func Latest(plugin plugins.Plugin, query string) (version string, err error) {
|
2024-08-17 11:10:16 -07:00
|
|
|
var stdOut strings.Builder
|
|
|
|
var stdErr strings.Builder
|
|
|
|
|
|
|
|
err = plugin.RunCallback("latest-stable", []string{query}, map[string]string{}, &stdOut, &stdErr)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(plugins.NoCallbackError); !ok {
|
2024-08-20 05:15:05 -07:00
|
|
|
return version, err
|
2024-08-17 11:10:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
allVersions, err := AllVersionsFiltered(plugin, query)
|
|
|
|
if err != nil {
|
2024-08-20 05:15:05 -07:00
|
|
|
return version, err
|
2024-08-17 11:10:16 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
versions := filterOutByRegex(allVersions, latestFilterRegex)
|
2024-08-17 11:10:16 -07:00
|
|
|
|
|
|
|
if len(versions) < 1 {
|
2024-08-20 05:15:05 -07:00
|
|
|
return version, errors.New(noLatestVersionErrMsg)
|
2024-08-17 11:10:16 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
return versions[len(versions)-1], nil
|
2024-08-17 11:10:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse stdOut and return version
|
2024-08-20 05:15:05 -07:00
|
|
|
allVersions := parseVersions(stdOut.String())
|
|
|
|
versions := filterOutByRegex(allVersions, latestFilterRegex)
|
|
|
|
if len(versions) < 1 {
|
|
|
|
return version, errors.New(noLatestVersionErrMsg)
|
|
|
|
}
|
|
|
|
return versions[len(versions)-1], nil
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|
|
|
|
|
2024-08-17 11:10:16 -07:00
|
|
|
// AllVersions returns a slice of all available versions for the tool managed by
|
|
|
|
// the given plugin by invoking the plugin's list-all callback
|
|
|
|
func AllVersions(plugin plugins.Plugin) (versions []string, err error) {
|
|
|
|
var stdout strings.Builder
|
|
|
|
var stderr strings.Builder
|
|
|
|
|
|
|
|
err = plugin.RunCallback("list-all", []string{}, map[string]string{}, &stdout, &stderr)
|
|
|
|
if err != nil {
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
versions = parseVersions(stdout.String())
|
|
|
|
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllVersionsFiltered returns a list of existing versions that match a regex
|
|
|
|
// query provided by the user.
|
|
|
|
func AllVersionsFiltered(plugin plugins.Plugin, query string) (versions []string, err error) {
|
|
|
|
all, err := AllVersions(plugin)
|
|
|
|
if err != nil {
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
return filterByExactMatch(all, query), err
|
|
|
|
}
|
|
|
|
|
2024-10-07 06:06:50 -07:00
|
|
|
// Uninstall uninstalls a specific tool version. It invokes pre and
|
|
|
|
// post-uninstall hooks if set, and runs the plugin's uninstall callback if
|
|
|
|
// defined.
|
|
|
|
func Uninstall(conf config.Config, plugin plugins.Plugin, rawVersion string, stdout, stderr io.Writer) error {
|
2024-10-31 06:30:48 -07:00
|
|
|
version := toolversions.ParseFromCliArg(rawVersion)
|
2024-10-07 06:06:50 -07:00
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
if version.Type == "latest" {
|
2024-10-07 06:06:50 -07:00
|
|
|
return errors.New("'latest' is a special version value that cannot be used for uninstall command")
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
if !installs.IsInstalled(conf, plugin, version) {
|
2024-10-07 06:06:50 -07:00
|
|
|
return errors.New("No such version")
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
err := hook.RunWithOutput(conf, fmt.Sprintf("pre_asdf_uninstall_%s", plugin.Name), []string{version.Value}, stdout, stderr)
|
2024-10-07 06:06:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoke uninstall callback if available
|
2024-10-31 06:30:48 -07:00
|
|
|
installDir := installs.InstallPath(conf, plugin, version)
|
2024-10-07 06:06:50 -07:00
|
|
|
env := map[string]string{
|
2024-10-31 06:30:48 -07:00
|
|
|
"ASDF_INSTALL_TYPE": version.Type,
|
|
|
|
"ASDF_INSTALL_VERSION": version.Value,
|
2024-10-07 06:06:50 -07:00
|
|
|
"ASDF_INSTALL_PATH": installDir,
|
|
|
|
}
|
|
|
|
err = plugin.RunCallback("uninstall", []string{}, env, stdout, stderr)
|
|
|
|
if _, ok := err.(plugins.NoCallbackError); !ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(installDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:30:48 -07:00
|
|
|
err = hook.RunWithOutput(conf, fmt.Sprintf("post_asdf_uninstall_%s", plugin.Name), []string{version.Value}, stdout, stderr)
|
2024-10-07 06:06:50 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
func filterByExactMatch(allVersions []string, pattern string) (versions []string) {
|
|
|
|
for _, version := range allVersions {
|
|
|
|
if strings.HasPrefix(version, pattern) {
|
|
|
|
versions = append(versions, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions
|
2024-08-17 11:10:16 -07:00
|
|
|
}
|
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
func filterOutByRegex(allVersions []string, pattern string) (versions []string) {
|
2024-08-17 11:10:16 -07:00
|
|
|
for _, version := range allVersions {
|
|
|
|
match, _ := regexp.MatchString(pattern, version)
|
2024-08-20 05:15:05 -07:00
|
|
|
if !match {
|
2024-08-17 11:10:16 -07:00
|
|
|
versions = append(versions, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions
|
|
|
|
}
|
|
|
|
|
|
|
|
// future refactoring opportunity: this function is an exact copy of
|
|
|
|
// resolve.parseVersion
|
|
|
|
func parseVersions(rawVersions string) []string {
|
|
|
|
var versions []string
|
|
|
|
for _, version := range strings.Split(rawVersions, " ") {
|
|
|
|
version = strings.TrimSpace(version)
|
|
|
|
if len(version) > 0 {
|
|
|
|
versions = append(versions, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return versions
|
2024-08-09 10:35:28 -07:00
|
|
|
}
|