Skip to content

Commit

Permalink
Merge pull request #50 from maxfierke/mf-asdf_to_mise
Browse files Browse the repository at this point in the history
Add runtime manager abstraction & experimental mise support (mstrap = mise-en-strap???)
  • Loading branch information
maxfierke authored Jan 28, 2024
2 parents 5263f77 + f66c11c commit 2e056d0
Show file tree
Hide file tree
Showing 31 changed files with 465 additions and 149 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Support for using [mise](https://mise.jdx.dev) to manage language runtime versions (#50). `config.hcl` can be configured as such to enable it:
```
version = "1.1"
# ...
runtimes {
default_manager = "mise"
}
```

To switch, you'll need to re-run `mstrap` and restart your terminal windows.
Then, you can run `brew uninstall asdf --force` to uninstall asdf (`mstrap`
will have removed `asdf`'s activation from mstrap's `env.sh` already)

### Changed

### Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project('mstrap',
'c',
meson_version : '>= 0.60.0',
license : 'MIT',
version : '0.6.0',
version : '0.7.0.dev',
default_options : [
'buildtype=debugoptimized',
'default_library=static'
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mstrap
version: 0.6.0
version: 0.7.0.dev

authors:
- Max Fierke <[email protected]>
Expand Down
42 changes: 41 additions & 1 deletion spec/mstrap/configuration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def delete_profile(name)
end

Spectator.describe MStrap::Configuration do
let(config_def) do
let(config_def_v1_0) do
MStrap::Defs::ConfigDef.from_hcl(<<-HCL)
version = "1.0"
Expand All @@ -50,6 +50,8 @@ Spectator.describe MStrap::Configuration do
HCL
end

let(config_def) { config_def_v1_0 }

let(personal_profile_def) do
MStrap::Defs::ProfileDef.from_hcl(<<-HCL)
version = "1.0"
Expand Down Expand Up @@ -315,6 +317,44 @@ Spectator.describe MStrap::Configuration do
end
end

describe "#runtime_manager" do
context "for v1.0 configs" do
subject { MStrap::Configuration.new(config_def_v1_0) }

it "defaults to asdf" do
expect(subject.runtime_manager).to be_a(MStrap::RuntimeManagers::ASDF)
end
end

context "for v1.1 configs" do
let(config_def_v1_1) do
MStrap::Defs::ConfigDef.from_hcl(<<-HCL)
version = "1.1"
runtimes {
default_manager = "mise"
}
user {
name = "Reginald Testington"
email = "[email protected]"
}
profile "personal" {
url = "ssh://[email protected]/reggiemctest/mstrap-personal.git"
}
HCL
end

subject { MStrap::Configuration.new(config_def_v1_1) }

it "can be set through runtimes.default_manager" do
expect(subject.runtime_manager).to be_a(MStrap::RuntimeManagers::Mise)
end
end
end

describe "#save!" do
it "saves the loaded configuration back to disk" do
subject = MStrap::Configuration.new(config_def)
Expand Down
2 changes: 2 additions & 0 deletions src/mstrap.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ require "./mstrap/user"
require "./mstrap/configuration"
require "./mstrap/supports/**"
require "./mstrap/web_bootstrapper"
require "./mstrap/runtime_manager"
require "./mstrap/runtime_managers/**"
require "./mstrap/runtime"
require "./mstrap/runtimes/**"
require "./mstrap/project"
Expand Down
4 changes: 3 additions & 1 deletion src/mstrap/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ DESC
project_def.runtimes = options.string["runtimes"].split(',') if options.string.has_key?("runtimes")

project = MStrap::Project.for(project_def)
project.bootstrap
project.bootstrap(config.runtime_manager)
end
end

Expand Down Expand Up @@ -235,6 +235,8 @@ DESC
Signal::TERM.trap { exit 1 }

Commander.run(cli, options.argv)
rescue e : MStrapError
logc e.message
rescue e
{% if flag?(:debug) %}
raise e
Expand Down
5 changes: 5 additions & 0 deletions src/mstrap/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module MStrap
@loaded_profiles : Array(Defs::ProfileDef)
@known_profile_configs : Array(Defs::ProfileConfigDef)
@resolved_profile : Defs::ProfileDef
@runtime_manager : RuntimeManager
@user : User

DEFAULT_PROFILE_CONFIG_DEF = Defs::DefaultProfileConfigDef.new
Expand All @@ -27,6 +28,9 @@ module MStrap
# profiles with the default profiles.
getter :resolved_profile

# Returns the runtime manager specified by the configuration
getter :runtime_manager

# Returns the mstrap user
getter :user

Expand All @@ -40,6 +44,7 @@ module MStrap
@loaded_profiles = [] of Defs::ProfileDef
@known_profile_configs = config.profiles + [DEFAULT_PROFILE_CONFIG_DEF]
@resolved_profile = Defs::ProfileDef.new
@runtime_manager = RuntimeManager.for(config.runtimes.default_manager)
@user = User.new(user: config.user)
end

Expand Down
13 changes: 10 additions & 3 deletions src/mstrap/defs/config_def.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ module MStrap
include HCL::Serializable

@[HCL::Attribute]
property version = "1.0"
property version = "1.1"

@[HCL::Block(key: "profile")]
property profiles = [] of ::MStrap::Defs::ProfileConfigDef

@[HCL::Block]
property runtimes = ::MStrap::Defs::RuntimesConfigDef.new

@[HCL::Block]
property user = ::MStrap::Defs::UserDef.new

def_equals_and_hash @version, @profiles, @user
def_equals_and_hash @version, @profiles, @runtimes, @user

def self.from_url(url : String)
HTTP::Client.get(url, tls: MStrap.tls_client) do |response|
self.from_hcl(response.body_io.gets_to_end)
end
end

def initialize(@user = UserDef.new, @profiles = Array(ProfileConfigDef).new)
def initialize(
@user = UserDef.new,
@profiles = Array(ProfileConfigDef).new,
@runtimes = RuntimesConfigDef.new
)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions src/mstrap/defs/runtimes_config_def.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module MStrap
module Defs
class RuntimesConfigDef
include HCL::Serializable

@[HCL::Attribute]
property default_manager = "asdf"

def_equals_and_hash @default_manager

def initialize
end
end
end
end
6 changes: 6 additions & 0 deletions src/mstrap/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ module MStrap
end
end

class InvalidRuntimeManagerError < MStrapError
def initialize(manager_name)
super("Runtime manager is not recognized or supported by mstrap: #{manager_name}")
end
end

# Exception class to indicate a failure involving language runtime setup
class RuntimeSetupError < MStrapError
def initialize(language_name, message)
Expand Down
12 changes: 7 additions & 5 deletions src/mstrap/project.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ module MStrap
# Executes `script/bootstrap` and `script/setup` (if either exists and are
# configured to run) or executes conventional runtime bootstrapping as
# determined by mstrap.
def bootstrap
def bootstrap(runtime_manager : RuntimeManager)
if has_scripts? && run_scripts?
logd "Found bootstrapping scripts, executing instead of using defaults."
begin
Expand All @@ -183,7 +183,7 @@ module MStrap
end
else
logd "Bootstrapping '#{name}' with runtime defaults."
default_bootstrap
default_bootstrap(runtime_manager)
end
end

Expand All @@ -192,12 +192,14 @@ module MStrap
# This **does not** run any bootstrapping scripts, and is used mainly for
# calling into conventional bootstrapping within a project's
# `script/bootstrap` or `script/setup` from `mstrap project`.
protected def default_bootstrap
#
# TODO: Move this somewhere more appropriate
protected def default_bootstrap(runtime_manager : RuntimeManager)
runtime_impls =
if runtimes.empty?
MStrap::Runtime.all
runtime_manager.runtimes
else
MStrap::Runtime.all.select do |runtime|
runtime_manager.runtimes.select do |runtime|
runtimes.includes?(runtime.language_name)
end
end
Expand Down
Loading

0 comments on commit 2e056d0

Please sign in to comment.