Completions, Settings: move to Homebrew namespace

This commit is contained in:
Rylan Polster 2021-01-13 11:16:09 -05:00
parent eb3a662841
commit 4b8477ba70
11 changed files with 114 additions and 99 deletions

View File

@ -92,7 +92,7 @@ begin
internal_cmd ||= begin internal_cmd ||= begin
internal_dev_cmd = Commands.valid_internal_dev_cmd?(cmd) internal_dev_cmd = Commands.valid_internal_dev_cmd?(cmd)
if internal_dev_cmd && !Homebrew::EnvConfig.developer? if internal_dev_cmd && !Homebrew::EnvConfig.developer?
Settings.write "devcmdrun", true Homebrew::Settings.write "devcmdrun", true
ENV["HOMEBREW_DEV_CMD_RUN"] = "1" ENV["HOMEBREW_DEV_CMD_RUN"] = "1"
end end
internal_dev_cmd internal_dev_cmd

View File

@ -4,63 +4,66 @@
require "utils/link" require "utils/link"
require "settings" require "settings"
# Helper functions for generating shell completions. module Homebrew
# # Helper functions for generating shell completions.
# @api private #
module Completions # @api private
extend T::Sig module Completions
extend T::Sig
module_function module_function
sig { void } SHELLS = %w[bash fish zsh].freeze
def link!
Settings.write :linkcompletions, true
Tap.each do |tap|
Utils::Link.link_completions tap.path, "brew completions link"
end
end
sig { void } sig { void }
def unlink! def link!
Settings.write :linkcompletions, false Settings.write :linkcompletions, true
Tap.each do |tap| Tap.each do |tap|
next if tap.official? Utils::Link.link_completions tap.path, "brew completions link"
Utils::Link.unlink_completions tap.path
end
end
sig { returns(T::Boolean) }
def link_completions?
Settings.read(:linkcompletions) == "true"
end
sig { returns(T::Boolean) }
def completions_to_link?
shells = %w[bash fish zsh]
Tap.each do |tap|
next if tap.official?
shells.each do |shell|
return true if (tap.path/"completions/#{shell}").exist?
end end
end end
false sig { void }
end def unlink!
Settings.write :linkcompletions, false
Tap.each do |tap|
next if tap.official?
sig { void } Utils::Link.unlink_completions tap.path
def show_completions_message_if_needed end
return if Settings.read(:completionsmessageshown) == "true" end
return unless completions_to_link?
T.unsafe(self).ohai "Homebrew completions for external commands are unlinked by default!" sig { returns(T::Boolean) }
T.unsafe(self).puts <<~EOS def link_completions?
To opt-in to automatically linking external tap shell competion files, run: Settings.read(:linkcompletions) == "true"
brew completions link end
Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")}
EOS
Settings.write :completionsmessageshown, true sig { returns(T::Boolean) }
def completions_to_link?
Tap.each do |tap|
next if tap.official?
SHELLS.each do |shell|
return true if (tap.path/"completions/#{shell}").exist?
end
end
false
end
sig { void }
def show_completions_message_if_needed
return if Settings.read(:completionsmessageshown) == "true"
return unless completions_to_link?
ohai "Homebrew completions for external commands are unlinked by default!"
puts <<~EOS
To opt-in to automatically linking external tap shell competion files, run:
brew completions link
Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")}
EOS
Settings.write :completionsmessageshown, true
end
end end
end end

View File

@ -0,0 +1,7 @@
# typed: strict
module Homebrew
module Completions
include Kernel
end
end

View File

@ -1,39 +1,41 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
# Helper functions for reading and writing settings. module Homebrew
# # Helper functions for reading and writing settings.
# @api private #
module Settings # @api private
extend T::Sig module Settings
extend T::Sig
module_function module_function
sig { params(setting: T.any(String, Symbol), repo: Pathname).returns(T.nilable(String)) } sig { params(setting: T.any(String, Symbol), repo: Pathname).returns(T.nilable(String)) }
def read(setting, repo: HOMEBREW_REPOSITORY) def read(setting, repo: HOMEBREW_REPOSITORY)
return unless (repo/".git/config").exist? return unless (repo/".git/config").exist?
repo.cd do repo.cd do
Utils.popen_read("git", "config", "--get", "homebrew.#{setting}").chomp.presence Utils.popen_read("git", "config", "--get", "homebrew.#{setting}").chomp.presence
end
end end
end
sig { params(setting: T.any(String, Symbol), value: T.any(String, T::Boolean), repo: Pathname).void } sig { params(setting: T.any(String, Symbol), value: T.any(String, T::Boolean), repo: Pathname).void }
def write(setting, value, repo: HOMEBREW_REPOSITORY) def write(setting, value, repo: HOMEBREW_REPOSITORY)
return unless (repo/".git/config").exist? return unless (repo/".git/config").exist?
repo.cd do repo.cd do
T.unsafe(self).safe_system "git", "config", "--replace-all", "homebrew.#{setting}", value.to_s safe_system "git", "config", "--replace-all", "homebrew.#{setting}", value.to_s
end
end end
end
sig { params(setting: T.any(String, Symbol), repo: Pathname).void } sig { params(setting: T.any(String, Symbol), repo: Pathname).void }
def delete(setting, repo: HOMEBREW_REPOSITORY) def delete(setting, repo: HOMEBREW_REPOSITORY)
return unless (repo/".git/config").exist? return unless (repo/".git/config").exist?
return if read(setting, repo: repo).blank? return if read(setting, repo: repo).blank?
repo.cd do repo.cd do
T.unsafe(self).safe_system "git", "config", "--unset-all", "homebrew.#{setting}" safe_system "git", "config", "--unset-all", "homebrew.#{setting}"
end
end end
end end
end end

View File

@ -0,0 +1,7 @@
# typed: strict
module Homebrew
module Settings
include Kernel
end
end

View File

@ -343,8 +343,8 @@ class Tap
command = "brew tap --repair" command = "brew tap --repair"
Utils::Link.link_manpages(path, command) Utils::Link.link_manpages(path, command)
Completions.show_completions_message_if_needed Homebrew::Completions.show_completions_message_if_needed
if official? || Completions.link_completions? if official? || Homebrew::Completions.link_completions?
Utils::Link.link_completions(path, command) Utils::Link.link_completions(path, command)
else else
Utils::Link.unlink_completions(path) Utils::Link.unlink_completions(path)
@ -822,14 +822,14 @@ class TapConfig
return unless tap.git? return unless tap.git?
return unless Utils::Git.available? return unless Utils::Git.available?
Settings.read key, repo: tap.path Homebrew::Settings.read key, repo: tap.path
end end
def []=(key, value) def []=(key, value)
return unless tap.git? return unless tap.git?
return unless Utils::Git.available? return unless Utils::Git.available?
Settings.write key, value.to_s, repo: tap.path Homebrew::Settings.write key, value.to_s, repo: tap.path
end end
end end

View File

@ -22,7 +22,7 @@ RSpec/InstanceVariable:
- 'utils/git_spec.rb' - 'utils/git_spec.rb'
- 'version_spec.rb' - 'version_spec.rb'
# Offense count: 81 # Offense count: 76
RSpec/MultipleDescribes: RSpec/MultipleDescribes:
Exclude: Exclude:
- 'ENV_spec.rb' - 'ENV_spec.rb'
@ -37,7 +37,6 @@ RSpec/MultipleDescribes:
- 'cmd/autoremove_spec.rb' - 'cmd/autoremove_spec.rb'
- 'cmd/cleanup_spec.rb' - 'cmd/cleanup_spec.rb'
- 'cmd/commands_spec.rb' - 'cmd/commands_spec.rb'
- 'cmd/completions_spec.rb'
- 'cmd/config_spec.rb' - 'cmd/config_spec.rb'
- 'cmd/deps_spec.rb' - 'cmd/deps_spec.rb'
- 'cmd/desc_spec.rb' - 'cmd/desc_spec.rb'

View File

@ -3,12 +3,12 @@
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
describe "Homebrew.completions_args" do describe "brew completions" do
it_behaves_like "parseable arguments" describe "Homebrew.completions_args" do
end it_behaves_like "parseable arguments"
end
describe "brew completions", :integration_test do it "runs the status subcommand correctly", :integration_test do
it "runs the status subcommand correctly" do
HOMEBREW_REPOSITORY.cd do HOMEBREW_REPOSITORY.cd do
system "git", "init" system "git", "init"
end end

View File

@ -3,7 +3,7 @@
require "completions" require "completions"
describe Completions do describe Homebrew::Completions do
let(:internal_path) { HOMEBREW_REPOSITORY/"Library/Taps/homebrew/homebrew-bar" } let(:internal_path) { HOMEBREW_REPOSITORY/"Library/Taps/homebrew/homebrew-bar" }
let(:external_path) { HOMEBREW_REPOSITORY/"Library/Taps/foo/homebrew-bar" } let(:external_path) { HOMEBREW_REPOSITORY/"Library/Taps/foo/homebrew-bar" }
@ -132,12 +132,9 @@ describe Completions do
setup_completions external: true setup_completions external: true
delete_completions_setting setting: :completionsmessageshown delete_completions_setting setting: :completionsmessageshown
# This will fail because the method calls `puts`. message = /Homebrew completions for external commands are unlinked by default!/
# If we output the `ohai` andcatch the error, we can be usre that the message is showing.
error_message = "private method `puts' called for Completions:Module"
expect { described_class.show_completions_message_if_needed } expect { described_class.show_completions_message_if_needed }
.to output.to_stdout .to output(message).to_stdout
.and raise_error(NoMethodError, error_message)
end end
end end
end end

View File

@ -3,7 +3,7 @@
require "settings" require "settings"
describe Settings do describe Homebrew::Settings do
before do before do
HOMEBREW_REPOSITORY.cd do HOMEBREW_REPOSITORY.cd do
system "git", "init" system "git", "init"

View File

@ -103,27 +103,27 @@ module Utils
end end
def uuid def uuid
Settings.read :analyticsuuid Homebrew::Settings.read :analyticsuuid
end end
def messages_displayed! def messages_displayed!
Settings.write :analyticsmessage, true Homebrew::Settings.write :analyticsmessage, true
Settings.write :caskanalyticsmessage, true Homebrew::Settings.write :caskanalyticsmessage, true
end end
def enable! def enable!
Settings.write :analyticsdisabled, false Homebrew::Settings.write :analyticsdisabled, false
messages_displayed! messages_displayed!
end end
def disable! def disable!
Settings.write :analyticsdisabled, true Homebrew::Settings.write :analyticsdisabled, true
regenerate_uuid! regenerate_uuid!
end end
def regenerate_uuid! def regenerate_uuid!
# it will be regenerated in next run unless disabled. # it will be regenerated in next run unless disabled.
Settings.delete :analyticsuuid Homebrew::Settings.delete :analyticsuuid
end end
def output(args:, filter: nil) def output(args:, filter: nil)
@ -314,7 +314,7 @@ module Utils
end end
def config_true?(key) def config_true?(key)
Settings.read(key) == "true" Homebrew::Settings.read(key) == "true"
end end
def formulae_brew_sh_json(endpoint) def formulae_brew_sh_json(endpoint)