Merge pull request #878 from mistydemeo/fix_shell_profile
Fix shell profile
This commit is contained in:
		
						commit
						efd29ebaca
					
				| @ -3,6 +3,7 @@ require "formula_lock" | |||||||
| require "formula_pin" | require "formula_pin" | ||||||
| require "hardware" | require "hardware" | ||||||
| require "utils/bottles" | require "utils/bottles" | ||||||
|  | require "utils/shell" | ||||||
| require "build_environment" | require "build_environment" | ||||||
| require "build_options" | require "build_options" | ||||||
| require "formulary" | require "formulary" | ||||||
| @ -41,6 +42,7 @@ require "migrator" | |||||||
| class Formula | class Formula | ||||||
|   include FileUtils |   include FileUtils | ||||||
|   include Utils::Inreplace |   include Utils::Inreplace | ||||||
|  |   include Utils::Shell | ||||||
|   extend Enumerable |   extend Enumerable | ||||||
| 
 | 
 | ||||||
|   # @!method inreplace(paths, before = nil, after = nil) |   # @!method inreplace(paths, before = nil, after = nil) | ||||||
|  | |||||||
| @ -14,23 +14,26 @@ module Utils | |||||||
| 
 | 
 | ||||||
|     # take a path and heuristically convert it |     # take a path and heuristically convert it | ||||||
|     # to a shell name, return nil if there's no match |     # to a shell name, return nil if there's no match | ||||||
|     def self.path_to_shell(path) |     def path_to_shell(path) | ||||||
|       # we only care about the basename |       # we only care about the basename | ||||||
|       shell_name = File.basename(path) |       shell_name = File.basename(path) | ||||||
|       # handle possible version suffix like `zsh-5.2` |       # handle possible version suffix like `zsh-5.2` | ||||||
|       shell_name.sub!(/-.*\z/m, "") |       shell_name.sub!(/-.*\z/m, "") | ||||||
|       shell_name.to_sym if %w[bash csh fish ksh sh tcsh zsh].include?(shell_name) |       shell_name.to_sym if %w[bash csh fish ksh sh tcsh zsh].include?(shell_name) | ||||||
|     end |     end | ||||||
|  |     module_function :path_to_shell | ||||||
| 
 | 
 | ||||||
|     def self.preferred_shell |     def preferred_shell | ||||||
|       path_to_shell(ENV.fetch("SHELL", "")) |       path_to_shell(ENV.fetch("SHELL", "")) | ||||||
|     end |     end | ||||||
|  |     module_function :preferred_shell | ||||||
| 
 | 
 | ||||||
|     def self.parent_shell |     def parent_shell | ||||||
|       path_to_shell(`ps -p #{Process.ppid} -o ucomm=`.strip) |       path_to_shell(`ps -p #{Process.ppid} -o ucomm=`.strip) | ||||||
|     end |     end | ||||||
|  |     module_function :parent_shell | ||||||
| 
 | 
 | ||||||
|     def self.csh_quote(str) |     def csh_quote(str) | ||||||
|       # ruby's implementation of shell_escape |       # ruby's implementation of shell_escape | ||||||
|       str = str.to_s |       str = str.to_s | ||||||
|       return "''" if str.empty? |       return "''" if str.empty? | ||||||
| @ -41,8 +44,9 @@ module Utils | |||||||
|       str.gsub!(/\n/, "'\\\n'") |       str.gsub!(/\n/, "'\\\n'") | ||||||
|       str |       str | ||||||
|     end |     end | ||||||
|  |     module_function :csh_quote | ||||||
|      |      | ||||||
|     def self.sh_quote(str) |     def sh_quote(str) | ||||||
|       # ruby's implementation of shell_escape |       # ruby's implementation of shell_escape | ||||||
|       str = str.to_s |       str = str.to_s | ||||||
|       return "''" if str.empty? |       return "''" if str.empty? | ||||||
| @ -52,9 +56,10 @@ module Utils | |||||||
|       str.gsub!(/\n/, "'\n'") |       str.gsub!(/\n/, "'\n'") | ||||||
|       str |       str | ||||||
|     end |     end | ||||||
|  |     module_function :sh_quote | ||||||
| 
 | 
 | ||||||
|     # quote values. quoting keys is overkill |     # quote values. quoting keys is overkill | ||||||
|     def self.export_value(shell, key, value) |     def export_value(shell, key, value) | ||||||
|       case shell |       case shell | ||||||
|       when :bash, :ksh, :sh, :zsh |       when :bash, :ksh, :sh, :zsh | ||||||
|         "export #{key}=\"#{sh_quote(value)}\"" |         "export #{key}=\"#{sh_quote(value)}\"" | ||||||
| @ -67,13 +72,15 @@ module Utils | |||||||
|         "setenv #{key} #{csh_quote(value)};" |         "setenv #{key} #{csh_quote(value)};" | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |     module_function :export_value | ||||||
| 
 | 
 | ||||||
|     # return the shell profile file based on users' preferred shell |     # return the shell profile file based on users' preferred shell | ||||||
|     def self.shell_profile |     def shell_profile | ||||||
|       SHELL_PROFILE_MAP.fetch(preferred_shell, "~/.bash_profile") |       SHELL_PROFILE_MAP.fetch(preferred_shell, "~/.bash_profile") | ||||||
|     end |     end | ||||||
|  |     module_function :shell_profile | ||||||
| 
 | 
 | ||||||
|     def self.prepend_path_in_shell_profile(path) |     def prepend_path_in_shell_profile(path) | ||||||
|       case preferred_shell  |       case preferred_shell  | ||||||
|       when :bash, :ksh, :sh, :zsh, nil |       when :bash, :ksh, :sh, :zsh, nil | ||||||
|         "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{shell_profile}" |         "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{shell_profile}" | ||||||
| @ -83,5 +90,6 @@ module Utils | |||||||
|         "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{shell_profile}" |         "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{shell_profile}" | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |     module_function :prepend_path_in_shell_profile | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Misty De Meo
						Misty De Meo