Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#add_line_to_file(file, line, regex = nil) ⇒ Object



84
85
86
87
# File 'lib/ahalogy/install-helper.rb', line 84

def add_line_to_file(file, line, regex = nil)
  regex = Regexp.escape(line) if regex == nil
  run_cmd "grep '#{regex}' #{file} --quiet || echo '#{line}' >> #{file}", "Updating #{file}..."
end

#get_datadirObject



5
6
7
8
9
# File 'lib/ahalogy/install-helper.rb', line 5

def get_datadir
  datadir = Gem.datadir('ahalogy-automation')
  datadir = File.join(File.dirname(__FILE__), '../../data/ahalogy-automation') if datadir== nil
  return datadir
end

#install_brew(app, opts = '') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ahalogy/install-helper.rb', line 18

def install_brew(app, opts = '')
  if app.is_a? Array
    app.each { |a| install_brew a, opts }
    return
  end
  %x[brew list -1 | grep "^#{app}$" &> /dev/null]
  if $? == 0
    puts "(brew) updating #{app}...".colorize(:blue)
    system "brew upgrade #{app} #{opts}"
  else
    puts "(brew) install #{app} #{opts}".colorize(:blue)
    system "brew install #{app} #{opts}"
    raise "Failed with exit code: #{$?}" if $? != 0
  end
end

#install_cask(app) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ahalogy/install-helper.rb', line 49

def install_cask(app)
  if app.is_a? Array
    app.each { |a| install_cask a }
    return
  end
  %x[brew cask list #{app} &> /dev/null]
  if $? == 0
    puts "(cask) #{app} already installed...skipped.".colorize(:green)
  else
    puts "(cask) install #{app}".colorize(:blue)
    system 'brew', 'cask', 'install', app.to_s
    raise "Failed with exit code: #{$?}" if $? != 0
  end
end

#install_file(file, destination) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ahalogy/install-helper.rb', line 64

def install_file(file, destination)
  file = File.join(get_datadir, file)
  if not File.readable? file
    puts "(file) Attempted to install #{file} but the file was not found.".colorize(:red)
    return false
  end
  destination = File.expand_path(destination)
  filemd5 = Digest::MD5.file(file).hexdigest
  destmd5 = ''
  if File.readable? destination
    destmd5 = Digest::MD5.file(destination).hexdigest
  end
  if filemd5 == destmd5
    puts "(file) #{file} and #{destination} are the same file...skipped.".colorize(:green)
  else
    puts "(file) Copying #{file} to #{destination}...".colorize(:blue)
    FileUtils.cp file, destination
  end
end

#mkdir(dir, mode: nil, owner: nil, group: nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ahalogy/install-helper.rb', line 89

def mkdir(dir, mode: nil, owner: nil, group: nil)
  dir = File.expand_path dir
  if File.exists? dir
    if File.directory? dir
      puts "(mkdir) The directory #{dir} already exists...skipped.".colorize(:green)
    else
      fail "(mkdir) ERROR: Trying to create directory but file already exists at #{dir}".colorize(:red)
    end
  else
    puts "(mkdir) Creating directory #{dir}...".colorize(:blue)
    FileUtils.mkdir_p dir
  end
  FileUtils.chmod mode, dir unless mode.nil?
  FileUtils.chown owner, group, dir
end

#run_cmd(cmd, msg) ⇒ Object



11
12
13
14
15
16
# File 'lib/ahalogy/install-helper.rb', line 11

def run_cmd(cmd, msg)
  puts msg
  puts "(cmd) '#{cmd}'".colorize(:blue)
  system cmd
  raise "Failed with exit code: #{$?}" if $? != 0
end

#uninstall_brew(app) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ahalogy/install-helper.rb', line 34

def uninstall_brew(app)
  if app.is_a? Array
    app.each { |a| uninstall_brew a }
    return
  end
  %x[brew list -1 | grep "^#{app}$" &> /dev/null]
  if $? != 0
    puts "(brew) #{app} is not installed...skipped.".colorize(:green)
  else
    puts "(brew-uninstall) uninstall #{app}".colorize(:blue)
    system 'brew', 'uninstall', '--force', app.to_s
    raise "Failed with exit code: #{$?}" if $? != 0
  end
end