Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.
Closed
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
5 changes: 4 additions & 1 deletion cmd/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def run
require_relative "../lib/bundle"

subcommand = args.named.first.presence
if ["exec", "add", "remove"].exclude?(subcommand) && args.named.size > 1
if %w[exec add remove services].exclude?(subcommand) && args.named.size > 1
raise UsageError, "This command does not take more than 1 subcommand argument."
end

Expand Down Expand Up @@ -273,6 +273,9 @@ def run
else
Bundle::Commands::Remove.run(*named_args, type: selected_types.first, global:, file:)
end
when "services"
_, *named_args = args.named
Bundle::Commands::Services.run(*named_args, global:, file:)
else
raise UsageError, "unknown subcommand: #{subcommand}"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require "bundle/dumper"
require "bundle/installer"
require "bundle/lister"
require "bundle/services"
require "bundle/commands/install"
require "bundle/commands/dump"
require "bundle/commands/cleanup"
Expand All @@ -35,6 +36,7 @@
require "bundle/commands/list"
require "bundle/commands/add"
require "bundle/commands/remove"
require "bundle/commands/services"
require "bundle/whalebrew_installer"
require "bundle/whalebrew_dumper"
require "bundle/vscode_extension_checker"
Expand Down
25 changes: 25 additions & 0 deletions lib/bundle/commands/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict
# frozen_string_literal: true

module Bundle
module Commands
module Services
sig { params(args: String, global: T::Boolean, file: T.nilable(String)).void }
def self.run(*args, global:, file:)
raise UsageError, "invalid `brew bundle services` arguments" if args.length != 1

parsed_entries = Brewfile.read(global:, file:).entries

subcommand = args.first
case subcommand
when "run"
Bundle::Services.run(parsed_entries)
when "stop"
Bundle::Services.stop(parsed_entries)
else
raise UsageError, "unknown services subcommand: #{subcommand}"
end
end
end
end
end
93 changes: 93 additions & 0 deletions lib/bundle/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# typed: strict
# frozen_string_literal: true

require "formula"
require "services/cli"
require "services/system"

module Bundle
module Services
sig {
params(
entries: T::Array[Bundle::Dsl::Entry],
_block: T.proc.params(wrapper: Homebrew::Services::FormulaWrapper, service_file: Pathname).void,
).void
}
private_class_method def self.map_entries(entries, &_block)
formula_versions = {}
ENV.each do |key, value|
match = key.match(/^HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_(.+)$/)
next if match.blank?

formula_name = match[1]
next if formula_name.blank?

ENV.delete(key)
formula_versions[formula_name.downcase] = value
end

entries.filter_map do |entry|
next if entry.type != :brew

formula = Formula[entry.name]
next unless formula.any_version_installed?

version = formula_versions[entry.name.downcase]
prefix = formula.rack/version if version

service_file = if prefix&.directory?
if Homebrew::Services::System.launchctl?
prefix/"#{formula.plist_name}.plist"
else
prefix/"#{formula.service_name}.service"
end
end

unless service_file&.file?
prefix = formula.any_installed_prefix
next if prefix.nil?

service_file = if Homebrew::Services::System.launchctl?
prefix/"#{formula.plist_name}.plist"
else
prefix/"#{formula.service_name}.service"
end
end

next unless service_file.file?

wrapper = Homebrew::Services::FormulaWrapper.new(formula)

yield wrapper, service_file
end
end

sig { params(entries: T::Array[Bundle::Dsl::Entry]).void }
def self.run(entries)
map_entries(entries) do |wrapper, service_file|
next if wrapper.pid? # already started

if Homebrew::Services::System.launchctl?
Homebrew::Services::Cli.launchctl_load(wrapper, file: service_file, enable: false)
elsif Homebrew::Services::System.systemctl?
Homebrew::Services::Cli.install_service_file(wrapper, service_file)
Homebrew::Services::Cli.systemd_load(wrapper, enable: false)
end

ohai "Running service `#{wrapper.name}` (label: #{wrapper.service_name})"
end
end

sig { params(entries: T::Array[Bundle::Dsl::Entry]).void }
def self.stop(entries)
map_entries(entries) do |wrapper, _service_file|
next unless wrapper.loaded?

# Try avoid services not started by `brew bundle services`
next if Homebrew::Services::System.launchctl? && wrapper.dest.exist?

Homebrew::Services::Cli.stop([wrapper])
end
end
end
end
Loading