Class: MicroInstall::Installer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hl = HighLine.new($stdin, $stdout)) ⇒ Installer

Returns a new instance of Installer.



14
15
16
# File 'lib/micro_install.rb', line 14

def initialize(hl = HighLine.new($stdin, $stdout))
  @hl = hl
end

Instance Attribute Details

#archObject (readonly)

Returns the value of attribute arch.



13
14
15
# File 'lib/micro_install.rb', line 13

def arch
  @arch
end

Instance Method Details

#download_micro_tar(hl = @hl) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/micro_install.rb', line 80

def download_micro_tar(hl = @hl)
  hl.say "Downloading... "
  MicroInstall.show_wait_spinner {
    File.open("micro-#{@tag}-#{@arch}.tar.gz", "wb") do |saved_file|
      # the following "open" is provided by open-uri
      URI.open("#{@download_url}") do |read_file|
        saved_file.write(read_file.read)
      end
    end
  }
  print "\n"
end

#download_url(hl = @hl) ⇒ Object



75
76
77
78
# File 'lib/micro_install.rb', line 75

def download_url(hl = @hl)
  @download_url = "https://github.com/zyedidia/micro/releases/download/v#{@tag}/micro-#{@tag}-#{@arch}.tar.gz"
  hl.say("#{Paint['URL', 'yellow']}: #{@download_url}")
end

#extract_micro(hl = @hl) ⇒ Object



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
# File 'lib/micro_install.rb', line 93

def extract_micro(hl = @hl)
  begin
    hl.say Paint["Extracting micro-#{@tag}-#{@arch}.tar.gz", 'yellow']
    micro_tar = Gem::Package::TarReader.new(Zlib::GzipReader.open("micro-#{@tag}-#{@arch}.tar.gz"))
    micro_tar.rewind
    dest = nil
    micro_tar.each do |entry|
      dest ||= Pathname(Dir.home).realdirpath.join(entry.full_name).to_path
      if entry.directory?
        File.delete dest if File.file? dest
        FileUtils::Verbose.mkdir_p dest, :mode => entry.header.mode
      elsif entry.file?
        FileUtils::Verbose.rm_rf dest if File.directory? dest
        File.open dest, "wb" do |f|
          f.print entry.read
        end
        FileUtils::Verbose.chmod entry.header.mode, dest
      elsif entry.header.typeflag == '2' #Symlink!
        File.symlink entry.header.linkname, dest
      end
      dest = nil
    end
  rescue
    hl.say "#{Paint['Error']}: Could not extract micro due to an error."

  end

end

#get_arch(hl = @hl) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/micro_install.rb', line 30

def get_arch(hl = @hl)
  begin
    host_os = OS.config['host_os']
    bits    = OS.bits.to_s
    case host_os
    when "linux-gnu"
      if bits == "64"
        @arch = 'linux64'
      elsif bits == "32"
        @arch = 'linux32'
      end
    when "darwin"
      @arch = 'osx'
    when "freebsd"
      if bits == "64"
        @arch = 'freebsd64'
      elsif bits == "32"
        @arch = 'freebsd32'
      end
    when "openbsd"
      if bits == "64"
        @arch = 'openbsd64'
      elsif bits == "32"
        @arch = 'openbsd32'
      end
    when "netbsd"
      if bits == "64"
        @arch = 'netbsd64'
      elsif bits == "32"
        @arch = 'netbsd32'
      end

    end
    if OS.config['host_cpu'] == 'arm' or OS.config['host_os'] =~ /linux-arm/ or OS.config['host_os'] =~ /arm-linux/
      @arch = 'linux-arm'
    end
    if @arch.nil?
      raise MicroInstall::LookupError.new 'Unable to determine your system'
    end
  rescue MicroInstall::LookupError => e
    hl.say "#{Paint['Error', 'red']}: #{e}"
  end

end

#install_micro(hl = @hl) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/micro_install.rb', line 122

def install_micro(hl = @hl)
  hl.say "#{Paint['Checking if ~/.local/bin exists.', 'yellow']}"
  if Dir.exist? Pathname(Dir.home).join('.local/bin')
    hl.say "'~/.local/bin' exists."
  else
    hl.say "'~/.local/bin' doesn't exist, creating."
    begin
      FileUtils.mkdir_p(Pathname(Dir.home).join('.local/bin'))
      hl.say "created '~/.local/bin'"
    rescue Errno::ENOENT => e
      hl.say "#{Paint['Error', 'red']}: #{e}"
    end
  end
  begin
    FileUtils.cp(Pathname(Dir.home).join("micro-#{@tag}/micro"), Pathname(Dir.home).join('.local/bin/'))
    hl.say Paint["Installed 'micro' to ~/.local/bin/"]
  rescue Errno::ENOENT => e
    hl.say "#{Paint['Error', 'red']}: #{e}"
  end
  begin
    FileUtils.rm("micro-#{@tag}-#{@arch}.tar.gz")
  rescue Errno::ENOENT => e
    hl.say "#{Paint['Error', 'red']}: #{e}"
  end
  begin
    FileUtils.rm_r(Pathname(Dir.home).join("micro-#{@tag}"), :secure => true)
  rescue Errno::ENOENT => e
    hl.say "#{Paint['Error', 'red']}: #{e}"
  end
end

#is_installed(hl = @hl) ⇒ Object



153
154
155
156
157
158
# File 'lib/micro_install.rb', line 153

def is_installed(hl = @hl)
  hl.say [
             "Micro has been installed to your ~/.local/bin/ directory. You can run it with:",
             "'micro'"
         ].join
end

#is_installed_but_no_bin(hl = @hl) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/micro_install.rb', line 160

def is_installed_but_no_bin(hl = @hl)
  hl.say [
             "Micro is installed to ~/.local/bin/, but can't run,",
             "Please check your ~/.bashrc and/or ~/.profile and/or ~/.zshrc and see",
             "if '~/.local/bin/' is being added to the PATH variable"
         ]
end

#latesttag(hl = @hl) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/micro_install.rb', line 18

def latesttag(hl = @hl)
  begin
    hl.say "#{Paint["Getting Latest Tag", 'green']}"
    MicroInstall.show_wait_spinner {
      @tag = "2.0.13"
    }
    hl.say "#{Paint['Latest Tag', 'green']}: #{Paint[@tag, 'yellow']}"
  rescue
    hl.say "#{Paint['Error', 'red']}: Unable to retrieve latest release."
  end
end