Skip to content

Commit

Permalink
Merge pull request #1331 from Homebrew/bundle_dump_exchange_uid
Browse files Browse the repository at this point in the history
vscode_extension_dumper: also use exchange_uid.
  • Loading branch information
MikeMcQuaid authored Apr 1, 2024
2 parents 2935b4d + 334db4d commit 01e47fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/bundle/brew_services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def started?(name)
def started_services
@started_services ||= if Bundle.services_installed?
states_to_skip = %w[stopped none]
Utils.safe_popen_read(HOMEBREW_BREW_FILE, "services", "list").lines.filter_map do |line|
Utils.safe_popen_read("brew", "services", "list").lines.filter_map do |line|
name, state, _plist = line.split(/\s+/)
next if states_to_skip.include? state

Expand Down
24 changes: 24 additions & 0 deletions lib/bundle/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ def which_formula(name)
ENV["PATH"] = "#{formula.opt_bin}:#{ENV.fetch("PATH", nil)}" if formula.any_version_installed?
which(name).present?
end

def exchange_uid(&block)
euid = Process.euid
uid = Process.uid
return yield if euid == uid

old_euid = euid
process_reexchangeable = Process::UID.re_exchangeable?
if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(uid)
end

return_value = with_env("HOME" => Etc.getpwuid(Process.uid).dir, &block)

if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(old_euid)
end

return_value
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/bundle/vscode_extension_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def reset!

def extensions
@extensions ||= if Bundle.vscode_installed?
`code --list-extensions 2>/dev/null`.split("\n").map(&:downcase)
Bundle.exchange_uid do
`code --list-extensions 2>/dev/null`
end.split("\n").map(&:downcase)
else
[]
end
Expand Down
26 changes: 1 addition & 25 deletions lib/bundle/vscode_extension_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,13 @@ def preinstall(name, no_upgrade: false, verbose: false)
true
end

def exchange_uid(&block)
euid = Process.euid
uid = Process.uid
return yield if euid == uid

old_euid = euid
process_reexchangeable = Process::UID.re_exchangeable?
if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(uid)
end

return_value = with_env("HOME" => Etc.getpwuid(Process.uid).dir, &block)

if process_reexchangeable
Process::UID.re_exchange
else
Process::Sys.seteuid(old_euid)
end

return_value
end

def install(name, preinstall: true, no_upgrade: false, verbose: false, force: false)
return true unless preinstall
return true if extension_installed?(name)

puts "Installing #{name} VSCode extension. It is not currently installed." if verbose

return false unless exchange_uid do
return false unless Bundle.exchange_uid do
Bundle.system("code", "--install-extension", name, verbose:)
end

Expand Down

0 comments on commit 01e47fa

Please sign in to comment.