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,6 +4,7 @@
require "utils/link" require "utils/link"
require "settings" require "settings"
module Homebrew
# Helper functions for generating shell completions. # Helper functions for generating shell completions.
# #
# @api private # @api private
@ -12,6 +13,8 @@ module Completions
module_function module_function
SHELLS = %w[bash fish zsh].freeze
sig { void } sig { void }
def link! def link!
Settings.write :linkcompletions, true Settings.write :linkcompletions, true
@ -37,11 +40,10 @@ module Completions
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def completions_to_link? def completions_to_link?
shells = %w[bash fish zsh]
Tap.each do |tap| Tap.each do |tap|
next if tap.official? next if tap.official?
shells.each do |shell| SHELLS.each do |shell|
return true if (tap.path/"completions/#{shell}").exist? return true if (tap.path/"completions/#{shell}").exist?
end end
end end
@ -54,8 +56,8 @@ module Completions
return if Settings.read(:completionsmessageshown) == "true" return if Settings.read(:completionsmessageshown) == "true"
return unless completions_to_link? return unless completions_to_link?
T.unsafe(self).ohai "Homebrew completions for external commands are unlinked by default!" ohai "Homebrew completions for external commands are unlinked by default!"
T.unsafe(self).puts <<~EOS puts <<~EOS
To opt-in to automatically linking external tap shell competion files, run: To opt-in to automatically linking external tap shell competion files, run:
brew completions link brew completions link
Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")} Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")}
@ -64,3 +66,4 @@ module Completions
Settings.write :completionsmessageshown, true 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,6 +1,7 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew
# Helper functions for reading and writing settings. # Helper functions for reading and writing settings.
# #
# @api private # @api private
@ -23,7 +24,7 @@ module Settings
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
@ -33,7 +34,8 @@ module Settings
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 "brew completions" do
describe "Homebrew.completions_args" do describe "Homebrew.completions_args" do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
end 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)