Class: C0Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/c0_setup.rb

Instance Method Summary collapse

Instance Method Details

#check_precedenceObject

check for things that don’t exist yet



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/c0_setup.rb', line 46

def check_precedence
  Dir.chdir(File.expand_path("~")) do
    not_yet = []
    if which("cc0").nil?
      not_yet.push("cc0") 
    end
    if which("coin").nil?
      not_yet.push("coin") 
    end
    if !Dir.exist?(".c0")
      not_yet.push("runpath")
    end
    return not_yet
  end
end

#create_runpathObject

create folder for binaries and set executable path



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/c0_setup.rb', line 63

def create_runpath
  Dir.chdir(File.expand_path("~")) do
    Dir.mkdir ".c0"
    File.new ".bashrc" unless File.exist? ".bashrc"
    File.open(".bashrc", "a") do |file|
      file.puts "export PATH=$PATH:~/.c0"
    end
    in_path = File.open(".bashrc", "r").read.include? "export PATH=$PATH:~/.c0"
    return (in_path and Dir.exist? ".c0")
  end
end

#get_osObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/c0_setup.rb', line 114

def get_os
    result = ""
    os = RbConfig::CONFIG['host_os']
    if os.downcase.include?('linux')
    result = 'linux'
    elsif os.downcase.include?('darwin')
    result = 'darwin'
  end
  return result
end

#install_cc0Object



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

def install_cc0
# copy the cc0 binary to "~/.c0"
  user = File.expand_path("~")
  Dir.chdir(File.dirname __dir__) do
    if get_os == "darwin"
      FileUtils.cp("./cc0_mac", "#{user}/.c0/cc0")
    elsif get_os == "linux"
      FileUtils.cp("./cc0_linux", "#{user}/.c0/cc0")
    end 
  end
  return File.exist?("#{user}/.c0/cc0")
end

#install_coinObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/c0_setup.rb', line 88

def install_coin
# copy the coin binary to "~/.c0"
  user = File.expand_path("~")
  Dir.chdir(File.dirname __dir__) do
    if get_os == "darwin"
      FileUtils.cp("./coin_mac", "#{user}/.c0/coin")
    elsif get_os == "linux"
      FileUtils.cp("./coin_linux", "#{user}/.c0/coin")
    end 
  end
  return File.exist?("#{user}/.c0/coin")
end

#setupObject

do the setup yo



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/c0_setup.rb', line 10

def setup
  to_do = check_precedence()
  result = []
  if to_do.include? "runpath"
    if create_runpath
      result.push "successfully created runpath"
    else
      result.push "failed to create runpath"
      return result
    end
  else
    result.push ".c0 folder already exists, remove it and retry if broken"
    return result
  end
  if to_do.include? "cc0"
    if install_cc0
      result.push "successfully installed cc0"
    else
      result.push "failed to install cc0"
    end
  else
    result.push "cc0 is already installed somewhere, remove it and retry if broken"
  end
  if to_do.include? "coin"
    if install_coin
      result.push "successfully installed coin"
    else
      result.push "failed to install coin"
    end
  else
    result.push "cc0 is already installed somewhere, remove it and retry if broken"
  end
  return result
end

#which(cmd) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/c0_setup.rb', line 101

def which(cmd)
# check for the presence of a binary in $PATH
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  return nil
end