mise 2026.8.15

Dev tools, env vars, and tasks in one CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# frozen_string_literal: true

# mise's Homebrew cask lifecycle shim.
#
# This evaluates a sha-verified cask .rb file without requiring Homebrew and
# executes the lifecycle hook requested by Rust. It intentionally implements a
# small Cask DSL surface first; unsupported lifecycle code fails loudly instead
# of falling back to `brew`.

require "etc"
require "fileutils"
require "open3"
require "pathname"
require "rbconfig"
require "shellwords"

class Array
  def second
    self[1]
  end

  def third
    self[2]
  end

  def fourth
    self[3]
  end

  def fifth
    self[4]
  end
end

MISE_BREW_CASK_FILE = Pathname.new(ENV.fetch("MISE_BREW_CASK_FILE"))
MISE_BREW_CASK_TOKEN = ENV.fetch("MISE_BREW_CASK_TOKEN")
MISE_BREW_CASK_VERSION = ENV.fetch("MISE_BREW_CASK_VERSION")
MISE_BREW_CASK_STAGED_PATH = Pathname.new(ENV.fetch("MISE_BREW_CASK_STAGED_PATH"))
MISE_BREW_CASK_APPDIR = Pathname.new(ENV.fetch("MISE_BREW_CASK_APPDIR"))
MISE_BREW_PREFIX = Pathname.new(ENV.fetch("MISE_BREW_PREFIX"))
MISE_BREW_CASK_HOOK = ENV.fetch("MISE_BREW_CASK_HOOK")

def odie(message)
  $stderr.puts "Error: #{message}"
  exit 1
end

class ShimUnsupportedError < StandardError; end

def shim_unsupported!(feature)
  raise ShimUnsupportedError,
        "cask uses `#{feature}`, which mise's cask shim does not support"
end

module OS
  def self.mac?
    RbConfig::CONFIG["host_os"].include?("darwin")
  end

  def self.linux?
    RbConfig::CONFIG["host_os"].include?("linux")
  end
end

class MacOSVersion
  include Comparable

  SYMBOLS = {
    tahoe: "26", sequoia: "15", sonoma: "14", ventura: "13",
    monterey: "12", big_sur: "11", catalina: "10.15", mojave: "10.14",
    high_sierra: "10.13", sierra: "10.12", el_capitan: "10.11",
  }.freeze

  def self.host
    @host ||= begin
      version = OS.mac? ? `sw_vers -productVersion`.strip : "0"
      new(version.empty? ? "0" : version)
    end
  end

  def self.from_symbol(sym)
    new(SYMBOLS.fetch(sym.to_sym, "0"))
  end

  def initialize(version)
    @version = version.to_s
  end

  def <=>(other)
    other = self.class.from_symbol(other) if other.is_a?(Symbol)
    other = self.class.new(other.to_s) unless other.is_a?(MacOSVersion)
    Gem::Version.new(@version) <=> Gem::Version.new(other.to_s)
  end

  def same_release?(other)
    other = self.class.from_symbol(other) if other.is_a?(Symbol)
    other = self.class.new(other.to_s) unless other.is_a?(MacOSVersion)

    if major >= 11 || other.major >= 11
      major == other.major
    else
      major == other.major && minor == other.minor
    end
  end

  def to_s
    @version
  end

  protected

  def major
    version_parts[0] || 0
  end

  def minor
    version_parts[1] || 0
  end

  private

  def version_parts
    @version.split(".").map(&:to_i)
  end
end

module Hardware
  module CPU
    def self.arch
      RbConfig::CONFIG["host_cpu"] =~ /arm|aarch64/ ? :arm64 : :x86_64
    end

    def self.arm?
      arch == :arm64
    end

    def self.intel?
      arch == :x86_64
    end

    def self.cores
      Etc.respond_to?(:nprocessors) ? Etc.nprocessors : 4
    end
  end
end

class Version
  include Comparable

  def initialize(version)
    @version = version.to_s
  end

  def <=>(other)
    Gem::Version.new(@version.gsub(/[^0-9.].*\z/, "")) <=> Gem::Version.new(other.to_s.gsub(/[^0-9.].*\z/, ""))
  end

  def to_s
    @version
  end

  def to_str
    @version
  end

  def inspect
    @version.inspect
  end

  def major
    token(0)
  end

  def minor
    token(1)
  end

  def patch
    token(2)
  end

  def major_minor
    Version.new(@version.split(".")[0, 2].to_a.join("."))
  end

  def major_minor_patch
    Version.new(@version.split(".")[0, 3].to_a.join("."))
  end

  def csv
    @version.split(",").map { |part| Version.new(part) }
  end

  private

  def token(idx)
    part = @version.split(".")[idx]
    part.nil? ? nil : Version.new(part)
  end
end

class SystemCommandResult
  attr_reader :stdout, :stderr, :exit_status

  def initialize(stdout, stderr, exit_status)
    @stdout = stdout
    @stderr = stderr
    @exit_status = exit_status
  end

  def success?
    @exit_status.zero?
  end

  def merged_output
    @stdout + @stderr
  end
end

class CaskContext
  attr_reader :token

  def initialize(token)
    @token = token
    @version = Version.new(MISE_BREW_CASK_VERSION)
    @hooks = {}
    @arch = Hardware::CPU.arch.to_s
    @languages = {}
    @default_language = nil
  end

  def run_hook(name)
    hook = @hooks[name.to_sym]
    return unless hook

    instance_eval(&hook)
  end

  def arch(mapping = nil)
    return @arch if mapping.nil?

    @arch = if Hardware::CPU.arm?
      mapping.fetch(:arm, mapping.fetch("arm", @arch))
    else
      mapping.fetch(:intel, mapping.fetch("intel", @arch))
    end
  end

  def version(value = nil)
    @version = Version.new(value) unless value.nil?
    @version
  end

  def language(code = nil, default: false, &block)
    if code
      @languages[code.to_s] = instance_eval(&block)
      @default_language = code.to_s if default
      return
    end

    # The API metadata and downloaded artifact use the cask's default language,
    # so lifecycle evaluation must select the same variant.
    @languages[@default_language] || @languages.values.first
  end

  def on_system_conditional(values)
    key = OS.mac? ? :macos : :linux
    return values[key] if values.key?(key)
    return values[key.to_s] if values.key?(key.to_s)

    shim_unsupported!("on_system_conditional without #{key}")
  end

  def sha256(*) nil end
  def url(*) nil end
  def name(*) nil end
  def desc(*) nil end
  def homepage(*) nil end
  def livecheck(*) nil end
  def auto_updates(*) nil end
  def conflicts_with(*) nil end
  def depends_on(*) nil end
  def caveats(*) nil end
  def app(*) nil end
  def binary(*) nil end
  def pkg(*) nil end
  def font(*) nil end
  def manpage(*) nil end
  # Completion artifacts are linked natively by Rust; the stanzas only need to
  # parse so lifecycle hooks in the same cask can run.
  def bash_completion(*) nil end
  def zsh_completion(*) nil end
  def fish_completion(*) nil end
  def uninstall(*) nil end
  def zap(*) nil end

  def preflight(&block) @hooks[:preflight] = block end
  def postflight(&block) @hooks[:postflight] = block end
  def uninstall_preflight(&block) @hooks[:uninstall_preflight] = block end
  def uninstall_postflight(&block) @hooks[:uninstall_postflight] = block end

  def on_catalina(method = nil, &block) run_macos_condition(:catalina, method, &block) end
  def on_big_sur(method = nil, &block) run_macos_condition(:big_sur, method, &block) end
  def on_monterey(method = nil, &block) run_macos_condition(:monterey, method, &block) end
  def on_ventura(method = nil, &block) run_macos_condition(:ventura, method, &block) end
  def on_sonoma(method = nil, &block) run_macos_condition(:sonoma, method, &block) end
  def on_sequoia(method = nil, &block) run_macos_condition(:sequoia, method, &block) end
  def on_tahoe(method = nil, &block) run_macos_condition(:tahoe, method, &block) end
  def on_intel(&block) instance_eval(&block) if Hardware::CPU.intel? end
  def on_arm(&block) instance_eval(&block) if Hardware::CPU.arm? end
  def on_macos(&block) instance_eval(&block) if OS.mac? end
  def on_linux(&block) instance_eval(&block) if OS.linux? end

  # Mirrors Homebrew's Cask DSL `system_command` (SystemCommand.run!): raises
  # on failure unless must_succeed: false. Elevation follows mise's sudo
  # policy, passed down from Rust via MISE_BREW_CASK_SUDO:
  # - "interactive": run `sudo`, which may prompt on the controlling TTY
  # - "noninteractive": run `sudo -n`, so it fails instead of hanging on a
  #   password prompt that nothing can answer
  # - "deny": error with the exact command to run manually
  def system_command(executable, args: [], sudo: false, env: {}, must_succeed: true,
                     print_stdout: false, print_stderr: true, verbose: false,
                     input: nil, chdir: nil, **rest)
    shim_unsupported!("system_command #{rest.keys.join(", ")}") if rest.any?

    env = env.map { |k, v| [k.to_s, v.to_s] }.to_h
    argv = [executable.to_s, *args.map(&:to_s)]
    argv = sudo_argv(argv, env) if sudo && Process.euid != 0

    opts = {}
    opts[:chdir] = chdir.to_s if chdir
    opts[:stdin_data] = input.to_s if input
    # [argv[0], argv[0]] forces argv execution: a lone string would be treated
    # as a shell command line, breaking paths with spaces and inviting shell
    # interpretation.
    stdout, stderr, status = Open3.capture3(env, [argv[0], argv[0]], *argv[1..], **opts)
    $stdout.print(stdout) if print_stdout || verbose
    $stderr.print(stderr) if print_stderr || verbose

    result = SystemCommandResult.new(stdout, stderr, status.exitstatus || 1)
    if must_succeed && !result.success?
      odie "command failed (exit #{result.exit_status}): #{argv.shelljoin}\n#{stderr}"
    end
    result
  end

  def staged_path
    MISE_BREW_CASK_STAGED_PATH
  end

  def appdir
    MISE_BREW_CASK_APPDIR
  end

  def caskroom_path
    MISE_BREW_PREFIX + "Caskroom"
  end

  def method_missing(name, *args, &block)
    shim_unsupported!(name)
  end

  def respond_to_missing?(*args)
    false
  end

  private

  def sudo_argv(argv, env)
    # sudo resets the environment; pass env vars through `env` so they reach
    # the elevated command (same as mise's sudo::run on the Rust side)
    argv = ["env", *env.map { |k, v| "#{k}=#{v}" }, *argv] unless env.empty?
    case ENV.fetch("MISE_BREW_CASK_SUDO", "deny")
    when "interactive"
      ["sudo", *argv]
    when "noninteractive"
      ["sudo", "-n", *argv]
    else
      odie "cask lifecycle hook needs sudo, but mise is not allowed to elevate " \
           "(system_packages.sudo is disabled or sudo is unavailable). Run manually:\n" \
           "  #{(["sudo"] + argv).shelljoin}"
    end
  end

  def run_macos_condition(version, method, &block)
    target = MacOSVersion.from_symbol(version)
    host = MacOSVersion.host
    matched = case method
    when :or_older then host <= target
    when :or_newer then host >= target
    when nil then host.same_release?(target)
    else shim_unsupported!("on_#{version} #{method}")
    end
    instance_eval(&block) if matched
  end
end

def cask(token, &block)
  $mise_cask_context = CaskContext.new(token)
  $mise_cask_context.instance_eval(&block)
end

begin
  load MISE_BREW_CASK_FILE.to_s
  ctx = $mise_cask_context
  odie "no cask block found in #{MISE_BREW_CASK_FILE}" if ctx.nil?
  odie "expected cask #{MISE_BREW_CASK_TOKEN}, got #{ctx.token}" if ctx.token != MISE_BREW_CASK_TOKEN
  ctx.run_hook(MISE_BREW_CASK_HOOK)
rescue ShimUnsupportedError => e
  odie e.message
end