lib/mkalias.rb in mkalias-1.0.3 vs lib/mkalias.rb in mkalias-1.0.4

- old
+ new

@@ -1,19 +1,18 @@ require 'mkalias/version' require 'set' module Mkalias - SIGNAL_NAME = 'USR1' + SIGNAL_NAME = 'USR1' BASHRC_PATH = "#{File.expand_path('~')}/.bashrc" def self.new_alias(alias_name, commands, file_path=BASHRC_PATH) alias_names = Mkalias.list_alias(file_path) return false if alias_names.include?(alias_name) - commands = commands.join('; ') if commands.kind_of?(Array) - commands = commands.tr('#', '$') + commands = Mkalias.prepare_commands(commands) function_name = "mkalias_#{alias_name}" bash_function = "function #{function_name}(){ #{commands}; }" bash_alias = "alias #{alias_name}='#{function_name}'" @@ -21,11 +20,11 @@ file.puts("\n") file.puts(bash_alias) file.puts(bash_function) end - true + return true end def self.list_alias(file_path=BASHRC_PATH) alias_names = Set.new @@ -48,11 +47,11 @@ alias_commands[alias_name] = Mkalias.get_alias_command(alias_name, file_path) end alias_commands.select!{ |_, value| !value.nil? } - return alias_commands + alias_commands end def self.remove_alias(alias_names, file_path=BASHRC_PATH) alias_names = [alias_names] unless alias_names.kind_of?(Array) @@ -60,40 +59,40 @@ alias_names.each do |alias_name| removed = Mkalias.remove_one_alias(alias_name, file_path) removed_alias << alias_name if removed end - return removed_alias + removed_alias end - def self.add_signal(file_path=BASHRC_PATH) - return false if Mkalias.has_signal?(file_path) + def self.add_signal(file_path=BASHRC_PATH) + return false if Mkalias.has_signal?(file_path) - trap_command = "trap 'source #{file_path}' #{SIGNAL_NAME}" + trap_command = "trap 'source #{file_path}' #{SIGNAL_NAME}" open(file_path, 'a') do |file| file.puts("\n") file.puts(trap_command) end - true - end + return true + end - def self.remove_signal(file_path=BASHRC_PATH) - return false unless has_signal?(file_path) + def self.remove_signal(file_path=BASHRC_PATH) + return false unless has_signal?(file_path) - trap_regex = /\btrap\s'source\s(.*)\sUSR1/ + trap_regex = /\btrap\s'source\s(.*)\sUSR1/ lines = File.readlines(file_path).reject{ |line| line =~ trap_regex } File.open(file_path, "w"){ |f| lines.each { |line| f.puts line } } - true - end + return true + end - def self.has_signal?(file_path=BASHRC_PATH) - trap_regex = /\btrap\s'source\s(.*)\sUSR1/ - !File.foreach(file_path).grep(trap_regex).empty? - end + def self.has_signal?(file_path=BASHRC_PATH) + trap_regex = /\btrap\s'source\s(.*)\sUSR1/ + !File.foreach(file_path).grep(trap_regex).empty? + end def self.get_alias_command(alias_name, file_path=BASHRC_PATH) alias_names = Mkalias.list_alias(file_path) return nil unless alias_names.include?(alias_name) @@ -105,11 +104,11 @@ alias_commands.each do |command| result = command.match(command_regex) return result.captures.first.split(';').each{ |c| c.strip! } if result end - nil + return nil end def self.remove_one_alias(alias_name, file_path=BASHRC_PATH) alias_names = Mkalias.list_alias(file_path) return false unless alias_names.include?(alias_name) @@ -119,7 +118,17 @@ lines = File.readlines(file_path).reject{ |line| line =~ alias_regex } File.open(file_path, "w"){ |f| lines.each { |line| f.puts line } } return true + end + + def self.prepare_commands(commands) + commands = commands.join('; ') if commands.kind_of?(Array) + unless commands.include?(';') or commands.include?('#') + commands = "#{commands} $@" + end + commands = commands.tr('#', '$') + + commands end end