Method: Morpheus::Cli::DotFile#export_aliases
- Defined in:
- lib/morpheus/cli/dot_file.rb
#export_aliases(alias_definitions) ⇒ Object
this saves the source file, upserting alias definitions at the bottom todo: smarter logic to allow the user to put stuff AFTER this section too under the section titled ‘# exported aliases’
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/morpheus/cli/dot_file.rb', line 80 def export_aliases(alias_definitions) if !@filename print "#{Term::ANSIColor.dark}Skipping source file save because filename has not been set#{Term::ANSIColor.reset}\n" if Morpheus::Logging.debug? return false end if !Dir.exist?(File.dirname(@filename)) FileUtils.mkdir_p(File.dirname(@filename)) end if !File.exist?(@filename) print "#{Term::ANSIColor.dark}Initializing source file #{@filename}#{Term::ANSIColor.reset}\n" if Morpheus::Logging.debug? FileUtils.touch(@filename) else print "#{dark} #=> Saving source file #{@filename}#{reset}\n" if Morpheus::Logging.debug? end config_text = File.read(@filename) config_lines = config_text.split(/\n/) new_config_lines = [] existing_alias_definitions = {} header_line_index = config_lines.index {|line| line.strip.include?(EXPORTED_ALIASES_HEADER) } # JD: there's some bad bug here where it can clear all your aliases! # it would be safer to export to another file at .morpheus/aliases or something. if header_line_index # keep everything before the exported alias section new_config_lines = config_lines[0..header_line_index-1] existing_alias_lines = config_lines[header_line_index..config_lines.size-1] # parse out the existing alias definitions existing_alias_lines.each do |line| if line =~ /^alias\s+/ alias_name, command_string = Morpheus::Cli::CliRegistry.parse_alias_definition(line) if alias_name.empty? || command_string.empty? print "#{dark} #=> removing bad config line #{line_num} invalid alias definition: #{line}\n" if Morpheus::Logging.debug? else existing_alias_definitions[alias_name] = command_string end end end else new_config_lines = config_lines new_config_lines << "" # blank line before alias header end # append header line new_config_lines << EXPORTED_ALIASES_HEADER new_config_lines << "# Do not put anything below here, or it will be lost when aliases are exported" #new_config_lines << "" # update aliases, sort them, and append the lines new_alias_definitions = existing_alias_definitions.merge(alias_definitions) new_alias_definitions.keys.sort.each do |alias_name| new_config_lines << "alias #{alias_name}='#{new_alias_definitions[alias_name]}'" end # include a blank line after this section new_config_lines << "" new_config_lines << "" new_config_text = new_config_lines.join("\n") File.open(@filename, 'w') {|f| f.write(new_config_text) } return true end |