139 lines
4.3 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2016-08-10 23:19:09 -07:00
module Utils
module Shell
module_function
# Take a path and heuristically convert it to a shell name,
# return `nil` if there's no match.
sig { params(path: String).returns(T.nilable(Symbol)) }
def from_path(path)
2016-08-10 23:19:09 -07:00
# we only care about the basename
shell_name = File.basename(path)
# handle possible version suffix like `zsh-5.2`
shell_name.sub!(/-.*\z/m, "")
shell_name.to_sym if %w[bash csh fish ksh mksh rc sh tcsh zsh].include?(shell_name)
2016-08-10 23:19:09 -07:00
end
sig { params(default: String).returns(String) }
def preferred_path(default: "")
ENV.fetch("SHELL", default)
end
sig { returns(T.nilable(Symbol)) }
def preferred
from_path(preferred_path)
2016-08-10 23:19:09 -07:00
end
sig { returns(T.nilable(Symbol)) }
def parent
from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
2016-08-10 23:19:09 -07:00
end
# Quote values. Quoting keys is overkill.
sig { params(key: String, value: String, shell: T.nilable(Symbol)).returns(T.nilable(String)) }
def export_value(key, value, shell = preferred)
2016-08-10 23:19:09 -07:00
case shell
2019-05-12 20:45:09 +02:00
when :bash, :ksh, :mksh, :sh, :zsh
2016-08-10 23:19:09 -07:00
"export #{key}=\"#{sh_quote(value)}\""
when :fish
# fish quoting is mostly Bourne compatible except that
# a single quote can be included in a single-quoted string via \'
# and a literal \ can be included via \\
"set -gx #{key} \"#{sh_quote(value)}\""
when :rc
"#{key}=(#{sh_quote(value)})"
2016-08-10 23:19:09 -07:00
when :csh, :tcsh
"setenv #{key} #{csh_quote(value)};"
2016-08-10 23:19:09 -07:00
end
end
# Return the shell profile file based on user's preferred shell.
sig { returns(String) }
def profile
case preferred
when :bash
2022-05-30 04:48:54 +01:00
bash_profile = "#{Dir.home}/.bash_profile"
return bash_profile if File.exist? bash_profile
when :rc
rc_profile = "#{Dir.home}/.rcrc"
return rc_profile if File.exist? rc_profile
when :zsh
2024-06-12 16:24:45 +02:00
return "#{ENV["HOMEBREW_ZDOTDIR"]}/.zshrc" if ENV["HOMEBREW_ZDOTDIR"].present?
end
SHELL_PROFILE_MAP.fetch(preferred, "~/.profile")
2016-08-10 23:19:09 -07:00
end
2016-05-22 16:03:51 -07:00
sig { params(variable: String, value: String).returns(T.nilable(String)) }
2018-05-12 11:47:12 -05:00
def set_variable_in_profile(variable, value)
case preferred
when :bash, :ksh, :sh, :zsh, nil
"echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}"
when :rc
"echo '#{variable}=(#{sh_quote(value)})' >> #{profile}"
2018-05-12 11:47:12 -05:00
when :csh, :tcsh
"echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}"
when :fish
"echo 'set -gx #{variable} #{sh_quote(value)}' >> #{profile}"
end
end
sig { params(path: String).returns(T.nilable(String)) }
def prepend_path_in_profile(path)
case preferred
2019-05-12 20:45:09 +02:00
when :bash, :ksh, :mksh, :sh, :zsh, nil
"echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}"
when :rc
"echo 'path=(#{sh_quote(path)} $path)' >> #{profile}"
2016-05-22 16:03:51 -07:00
when :csh, :tcsh
"echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}"
2016-05-22 16:03:51 -07:00
when :fish
"fish_add_path #{sh_quote(path)}"
2016-05-22 16:03:51 -07:00
end
end
SHELL_PROFILE_MAP = {
bash: "~/.profile",
2018-11-02 17:18:07 +00:00
csh: "~/.cshrc",
fish: "~/.config/fish/config.fish",
2018-11-02 17:18:07 +00:00
ksh: "~/.kshrc",
2019-05-12 20:45:09 +02:00
mksh: "~/.kshrc",
rc: "~/.rcrc",
sh: "~/.profile",
tcsh: "~/.tcshrc",
2018-11-02 17:18:07 +00:00
zsh: "~/.zshrc",
}.freeze
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~+\n])}
sig { params(str: String).returns(String) }
def csh_quote(str)
2024-04-30 11:10:23 +02:00
# Ruby's implementation of `shell_escape`.
str = str.to_s
return "''" if str.empty?
2018-09-17 02:45:00 +02:00
str = str.dup
2024-04-30 11:10:23 +02:00
# Anything that isn't a known safe character is padded.
2020-08-19 17:12:32 +01:00
str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation
2024-04-30 11:10:23 +02:00
# Newlines have to be specially quoted in `csh`.
str.gsub!("\n", "'\\\n'")
str
end
sig { params(str: String).returns(String) }
def sh_quote(str)
2024-04-30 11:10:23 +02:00
# Ruby's implementation of `shell_escape`.
str = str.to_s
return "''" if str.empty?
2018-09-17 02:45:00 +02:00
str = str.dup
2024-04-30 11:10:23 +02:00
# Anything that isn't a known safe character is padded.
2020-08-19 17:12:32 +01:00
str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation
str.gsub!("\n", "'\n'")
str
end
2016-08-10 23:19:09 -07:00
end
end