class Dotstrap::Shell
Attributes
repo_path[RW]
TODO: cleanup shell class & split it into 3+ classes with inheritence
Public Class Methods
new(repo_path)
click to toggle source
TODO: how to handle load order? ie. load path.fish first
# File lib/dotstrap/shell.rb, line 9 def initialize(repo_path) @repo_path = repo_path end
Public Instance Methods
config_file(sh, dir = Dotstrap.config_home)
click to toggle source
# File lib/dotstrap/shell.rb, line 79 def config_file(sh, dir = Dotstrap.config_home) File.join(dir, "config.#{sh}") end
configure(repo_dir = @repo_path)
click to toggle source
# File lib/dotstrap/shell.rb, line 13 def configure(repo_dir = @repo_path) configure_fish(repo_dir) if should_configure_fish? configure_zsh(repo_dir) configure_bash(repo_dir) $LOG.unknown { "configuration complete" } end
configure_bash(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 67 def configure_bash(repo_dir) write_config_file(bash_configs(repo_dir), config_file('bash')) end
configure_fish(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 29 def configure_fish(repo_dir) fish_functions(repo_dir).each do |f| link_config_file(f, File.join(Dotstrap.shell_config_home('fish'), 'functions')) end fish_completions(repo_dir).each do |f| link_config_file(f, File.join(Dotstrap.shell_config_home('fish'), 'completions')) end write_config_file(fish_configs(repo_dir), config_file('fish')) end
configure_zsh(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 59 def configure_zsh(repo_dir) write_config_file(zsh_configs(repo_dir), config_file('zsh')) end
rm_config_file(file)
click to toggle source
# File lib/dotstrap/shell.rb, line 75 def rm_config_file(file) FileUtils.rm_rf file end
unconfigure(repo_dir = @repo_path)
click to toggle source
# File lib/dotstrap/shell.rb, line 20 def unconfigure(repo_dir = @repo_path) return unless Dir.exist?(repo_dir) unconfigure_fish(repo_dir) if should_configure_fish? unconfigure_zsh(repo_dir) unconfigure_bash(repo_dir) FileUtils.rm_r(repo_dir, force: true, secure: true) $LOG.unknown { "removed: #{repo_dir}\n" } end
unconfigure_bash(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 71 def unconfigure_bash(repo_dir) unwrite_config_file(bash_configs(repo_dir), config_file('bash')) end
unconfigure_fish(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 43 def unconfigure_fish(repo_dir) fish_functions(repo_dir).each do |f| file = File.join(Dotstrap.shell_config_home('fish'), 'functions', File.basename(f)) rm_config_file(file) end fish_completions(repo_dir).each do |f| file = File.join(Dotstrap.shell_config_home('fish'), 'completions', File.basename(f)) rm_config_file(file) end unwrite_config_file(fish_configs(repo_dir), config_file('fish')) end
unconfigure_zsh(repo_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 63 def unconfigure_zsh(repo_dir) unwrite_config_file(zsh_configs(repo_dir), config_file('zsh')) end
Private Instance Methods
append_config_file(file_to_source, config_file)
click to toggle source
TODO: only open config file once, write all source strings at once
# File lib/dotstrap/shell.rb, line 102 def append_config_file(file_to_source, config_file) source_str = "source \"#{file_to_source}\"\n" if File.exist?(config_file) # Do not write source_str if we have already written it before return if File.readlines(config_file).grep(source_str).any? end File.open(config_file, 'a') { |f| f.write source_str } end
bash_configs(dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 161 def bash_configs(dir) c = Dir.glob(File.join(dir, '**', '*.{sh,bash}')) $LOG.debug { "BASH_CONFIGS:#{c}" } c end
fish_completions(dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 155 def fish_completions(dir) c = Dir.glob(File.join(dir, 'completions', '*.fish')) $LOG.debug { "FISH_COMPLETIONS:#{c}" } c end
fish_configs(dir, excl = 'fish_prompt.fish')
click to toggle source
# File lib/dotstrap/shell.rb, line 137 def fish_configs(dir, excl = 'fish_prompt.fish') c = [] Dir.glob(File.join(dir, '*.fish')) { |f| c << f unless f.end_with?(excl) } $LOG.debug { "FISH_CONFIGS:#{c}" } c end
fish_functions(dir, incl = ['fish_prompt.fish'])
click to toggle source
# File lib/dotstrap/shell.rb, line 144 def fish_functions(dir, incl = ['fish_prompt.fish']) funcs = [] funcs << Dir.glob(File.join(dir, 'functions', '*.fish')) incl.each do |f| f = File.join(dir, f) funcs << f if File.exist?(f) end $LOG.debug { "FISH_FUNCTIONS:#{funcs.flatten}" } funcs.flatten end
link_config_file(src, dest_dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 125 def link_config_file(src, dest_dir) return unless File.exist?(src) $LOG.debug { "LINK_CONFIG_FILE:#{src} to #{dest_dir}" } dst = File.join(dest_dir, File.basename(src)) # FIXME: fails if dest_dir for link is a broken symbolic link # parent = Pathname.new(dest_dir).parent # FileUtils.mkdir parent unless Dir.exist? parent FileUtils.mkdir_p dest_dir unless Dir.exist? dest_dir FileUtils.ln_s src, dst, force: true $LOG.info { "linked: #{src} to #{dst}" } end
should_configure_fish?()
click to toggle source
# File lib/dotstrap/shell.rb, line 173 def should_configure_fish? Dir.exist?(Dotstrap.shell_config_home('fish')) end
unappend_config_file(file_to_unsource, config_file)
click to toggle source
# File lib/dotstrap/shell.rb, line 111 def unappend_config_file(file_to_unsource, config_file) return unless File.exist?(config_file) Dir.mktmpdir do |tmp_dir| source_str = "source \"#{file_to_unsource}\"" tmp_config_file = File.join(tmp_dir, File.basename(config_file)) File.open(tmp_config_file, 'w') do |tmp_file| File.foreach(file_to_unsource) do |line| tmp_file.write line unless line.chomp == source_str end end FileUtils.mv(tmp_config_file, config_file) end end
unwrite_config_file(repo_config_files, dotstrap_config_file)
click to toggle source
# File lib/dotstrap/shell.rb, line 93 def unwrite_config_file(repo_config_files, dotstrap_config_file) return if repo_config_files.empty? repo_config_files.each do |file| unappend_config_file(file, dotstrap_config_file) $LOG.info { "removed: #{file}" } end end
write_config_file(repo_config_files, dotstrap_config_file)
click to toggle source
# File lib/dotstrap/shell.rb, line 85 def write_config_file(repo_config_files, dotstrap_config_file) return if repo_config_files.empty? repo_config_files.each do |file| append_config_file(file, dotstrap_config_file) $LOG.info { "configured: #{file}" } end end
zsh_configs(dir)
click to toggle source
# File lib/dotstrap/shell.rb, line 167 def zsh_configs(dir) c = Dir.glob(File.join(dir, '**', '*.{sh,zsh}')) $LOG.debug { "ZSH_CONFIGS:#{c}" } c end