Class: Yay::Installer

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

Overview

installs .yay files from a remote url in to either ~/.yay or /etc/yay depending on the users permissions

Instance Method Summary collapse

Constructor Details

#initialize(file_name, url) ⇒ Installer

initialize an installer for the specified url. the .install method will attempt the actual installation process



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

def initialize file_name, url
  # directories to iterate. begin with /etc/yay, which should only be
  # accessible to sudo. then try a local install.
  paths     = Yay::Paths.new
  validate_filename file_name
  @file_name = append_filetype file_name
  @dirs = [
    paths.global_yay_path,
    paths.local_yay_path
  ]
  @url = url
end

Instance Method Details

#append_filetype(string) ⇒ Object

add the .yay part to the filename if it’s missing



33
34
35
36
# File 'lib/yay/installer.rb', line 33

def append_filetype string
  return "#{string}.yay" unless /\.yay$/.match(string)
  return string
end

#can_write_to_dir(dir, create) ⇒ Object

see if we can write to a directory. will try to create the folder (and its parents) unless asked otherwise



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yay/installer.rb', line 40

def can_write_to_dir dir, create

    begin
      stat = File.stat(dir)
      return true if stat && stat.writable?
    rescue Errno::ENOENT
    end

    # create and try again if it doesn't exist
    if create
      begin
        FileUtils.mkdir_p dir
        #FileUtils.chmod 0644, dir 
        result = can_write_to_dir dir, false
        puts "Created #{dir}" if result
        return result
      rescue Errno::EACCES
      end
    end

    return false
end

#get_install_directoryObject

try to find the install directory



64
65
66
67
68
69
70
# File 'lib/yay/installer.rb', line 64

def get_install_directory
  # search globally and then locally to see if we can write
  @dirs.each { |dir| 
    return dir if can_write_to_dir dir, true
  }
  return nil
end

#get_remote_string(url, limit = 5) ⇒ Object

make a curl-ish request and get the contents



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yay/installer.rb', line 73

def get_remote_string url, limit=5
  raise  InstallFailedError.new url, 'HTTP redirect too deep' if limit == 0

  begin
    puts "Requesting #{url}"
    url = URI.parse(url)

    request = Net::HTTP::Get.new(url.path || '/')
    
    http    = Net::HTTP.new url.host, url.port
    #http.set_debug_output($stderr)
    http.use_ssl = url.scheme == 'https' 

    response     = http.start { |http2| 
      http2.request(request)
    }

  rescue StandardError => error
    raise InstallFailedError.new url, error.to_s
  end
  
  case response
    when Net::HTTPSuccess then 
      return response.body
    when Net::HTTPRedirection then
      puts "Redirecting to #{response['location']}"; 
      return get_remote_string(response['location'], limit - 1)
  end

  raise InstallFailedError.new url, "#{response.code} \"#{response.message}\"", response.body
end

#installObject

attempt the installation process



127
128
129
130
131
132
133
# File 'lib/yay/installer.rb', line 127

def install
  dest_folder = get_install_directory
  raise "Couldn't write to or create directories #{@dirs.join(' or ')} something is up with your system!" unless dest_folder
  string = get_remote_string @url
  verify_rules @url, string
  install_string string, dest_folder
end

#install_string(string, dest_folder) ⇒ Object

store a string



118
119
120
121
122
123
124
# File 'lib/yay/installer.rb', line 118

def install_string string, dest_folder
  dest = "#{dest_folder}/#{@file_name}"
  File.open(dest, 'w') {|file|
    file.write(string)
  }
  puts "#{ColourWheel::success}Installed to #{dest}#{ColourWheel::end_colour}"
end

#validate_filename(string) ⇒ Object

some filenames just aren’t a good idea



28
29
30
# File 'lib/yay/installer.rb', line 28

def validate_filename string
  raise BadFilenameError.new string if /[\?\*\\\/]/.match(string)
end

#verify_rules(url, string) ⇒ Object

ensure the rules we’ve downloaded parse correctly



106
107
108
109
110
111
112
113
114
115
# File 'lib/yay/installer.rb', line 106

def verify_rules url, string
  begin
    parser = Yay::Parser.new
    parser.parse(string)
  rescue Yay::Error => error
    raise InstallFailedError.new url, error.printable_message, string
  end
  raise InstallFailedError.new url, "No rules in downloaded file", string if parser.get_rules == []
  return parser
end