Module: Commandify::Install

Extended by:
Tty
Defined in:
lib/commandify/install.rb

Class Method Summary collapse

Methods included from Tty

error, log, notify

Class Method Details

.bashrcObject

full path of .bashrc



37
38
39
# File 'lib/commandify/install.rb', line 37

def bashrc
  File.expand_path('~/.bashrc')
end

.command_alias(command) ⇒ Object

alias string for a single command



52
53
54
# File 'lib/commandify/install.rb', line 52

def command_alias command
  "alias #{command}='eval $(bundle exec commandify #{command})'\n"
end

.end_markerObject

default commandify end marker for .bashrc



47
48
49
# File 'lib/commandify/install.rb', line 47

def end_marker
  "# COMMANDIFY END #\n"
end

.installed?Boolean

checks for current Commandify version in .bashrc

Returns:

  • (Boolean)


32
33
34
# File 'lib/commandify/install.rb', line 32

def installed?
  File.read(bashrc).include? start_marker
end

.mod_bashrc!Object

write out aliases to .bashrc



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/commandify/install.rb', line 73

def mod_bashrc!
  brc = File.read bashrc
  if old_command_aliases.length > 0
    log 'Updating Commandify aliases ...'
    brc.gsub! old_command_aliases, new_command_aliases
  else
    log 'Adding Commandify aliases ...'
    brc << new_command_aliases
  end
  File.open bashrc, 'w' do |file| 
    file << brc
  end
  log 'Finished.'
end

.new_command_aliasesObject

generates new command aliases



57
58
59
60
61
62
63
64
# File 'lib/commandify/install.rb', line 57

def new_command_aliases
  aliases = ""
  aliases << start_marker
  Commandify::Command.commands.each do |command|
    aliases << command_alias(command)
  end
  aliases << end_marker
end

.old_command_aliasesObject

grabs the old command aliases by start and end markers



67
68
69
70
# File 'lib/commandify/install.rb', line 67

def old_command_aliases
  regex = Regexp.new "^# COMMANDIFY.*#{end_marker}$", Regexp::MULTILINE
  @@old_command_aliases ||= File.read(bashrc).match(regex).to_s
end

.runObject

write to .bashrc if havent already



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/commandify/install.rb', line 14

def run
  if !File.exists? bashrc
    log 'Creating ~/.bashrc'
    FileUtils.touch bashrc 
  end
  if installed?
    error 'Commandify aliases are already installed.'
  else
    run!
  end
end

.run!Object



26
27
28
29
# File 'lib/commandify/install.rb', line 26

def run!
  mod_bashrc!
  notify "Be sure to run 'source ~/.bashrc' or open a new terminal for the changes to take effect."
end

.start_markerObject

default commandify start marker for .bashrc



42
43
44
# File 'lib/commandify/install.rb', line 42

def start_marker
  "# COMMANDIFY #{Commandify::VERSION} #\n"
end