Skip to content

Commit

Permalink
wip: Add a RuntimeManager for mise
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Jan 24, 2024
1 parent 6daac19 commit d29c385
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/mstrap/runtime_managers/asdf.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
module MStrap
module RuntimeManagers
class ASDF < RuntimeManager
def asdf?
true
end

def name : String
"asdf"
end
Expand Down
57 changes: 57 additions & 0 deletions src/mstrap/runtime_managers/mise.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module MStrap
module RuntimeManagers
class Mise < RuntimeManager
def name : String
"mise"
end

def current_version(language_name : String) : String?
`mise current #{language_name}`.chomp
end

# Returns whether the mise plugin has been installed for a language runtime
# or not
def has_plugin?(language_name : String) : Bool
`mise plugins ls --core --user`.chomp.split("\n").includes?(plugin_name(language_name))
end

def install_plugin(language_name : String) : Bool
cmd("mise plugins install #{plugin_name(language_name)}", quiet: true)
end

def install_version(language_name : String, version : String) : Bool
cmd("mise install #{plugin_name(language_name)} #{version}", quiet: true)
end

# Returns a list of the versions of the language runtime installed
# by mise.
def installed_versions(language_name : String) : Array(String)
mise_json_output = `mise ls -i #{plugin_name(language_name)} --json`
mise_installed = JSON.parse(mise_json_output)
mise_installed[language_name].as_a.map { |version| version["version"].as_s }
end

def latest_version(language_name : String) : String
`mise latest #{plugin_name(language_name)}`.chomp
end

# Name of the mise plugin for a particular language
def plugin_name(language_name : String) : String?
language_name
end

def set_global_version(language_name, version : String) : Bool
cmd "mise use -g #{plugin_name(language_name)}@#{version}", quiet: true
end

# :nodoc:
def version_env_var(language_name) : String
if asdf_plugin_name = plugin_name(language_name)
"MISE_#{asdf_plugin_name.upcase}_VERSION"
else
"MISE_#{language_name.upcase}_VERSION"
end
end
end
end
end
2 changes: 1 addition & 1 deletion src/mstrap/steps/init_step.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module MStrap
if !File.exists?(Paths::BREWFILE) || force?
logw "No Brewfile found or update requested with --force"
log "--> Copying default Brewfile to #{Paths::BREWFILE}: "
brewfile_contents = Templates::Brewfile.new.to_s
brewfile_contents = Templates::Brewfile.new(runtime_manager).to_s
File.write(Paths::BREWFILE, brewfile_contents)
success "OK"
end
Expand Down
2 changes: 1 addition & 1 deletion src/mstrap/steps/shell_step.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module MStrap
def bootstrap
Dir.mkdir_p(MStrap::Paths::RC_DIR)

contents = Templates::EnvSh.new.to_s
contents = Templates::EnvSh.new(login_shell, runtime_manager).to_s
File.write(env_sh_path, contents, perm: 0o600)

exit_if_shell_changed!
Expand Down
5 changes: 5 additions & 0 deletions src/mstrap/templates/Brewfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ module MStrap
module Templates
class Brewfile
ECR.def_to_s "#{__DIR__}/Brewfile.ecr"

getter :runtime_manager

def initialize(@runtime_manager : RuntimeManager)
end
end
end
end
4 changes: 4 additions & 0 deletions src/mstrap/templates/Brewfile.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ brew 'pkg-config'
brew 'zlib'

# Language runtime managers
<% if runtime_manager.name == "asdf" %>
brew 'asdf'
<% elsif runtime_manager.name == "mise" %>
brew 'mise'
<% end %>

if /darwin/ =~ RUBY_PLATFORM
brew 'autoconf'
Expand Down
10 changes: 9 additions & 1 deletion src/mstrap/templates/env.sh.ecr
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#!/bin/bash
#!/usr/bin/env <%= shell_name %>

export MSTRAP=true
export MSTRAP_PROJECT_SOCKETS="<%= MStrap::Paths::PROJECT_SOCKETS %>"
export MSTRAP_SRC_DIR="<%= MStrap::Paths::SRC_DIR %>"
export MSTRAP_RC_DIR="<%= MStrap::Paths::RC_DIR %>"

<%- if needs_homebrew_shellenv? %>
# Load Homebrew
test -d <%= MStrap::Paths::HOMEBREW_PREFIX %> && eval $(<%= MStrap::Paths::HOMEBREW_PREFIX %>/bin/brew shellenv)
<% end -%>

<% if runtime_manager.name == "asdf" %>
# Activate asdf for language runtime version management
if [ -d "$(brew --prefix asdf)" ]; then
source "$(brew --prefix asdf)/libexec/asdf.sh"

if [ ! -f "$HOME/.asdfrc" ]; then
echo "legacy_version_file = yes\n" > "$HOME/.asdfrc"
fi
fi
<% elsif runtime_manager.name == "mise" %>
# Activate mise for language runtime version management
export MISE_ASDF_COMPAT=1
eval "$(mise activate <%= shell_name %>)"
<% end %>
6 changes: 6 additions & 0 deletions src/mstrap/templates/env_sh.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module MStrap
class EnvSh
ECR.def_to_s "#{__DIR__}/env.sh.ecr"

getter :shell_name
getter :runtime_manager

def initialize(@shell_name : String, @runtime_manager : RuntimeManager)
end

def needs_homebrew_shellenv?
{{ flag?(:linux) || (flag?(:aarch64) && flag?(:darwin)) }}
end
Expand Down

0 comments on commit d29c385

Please sign in to comment.