Class: Geoffrey::Package

Inherits:
Object
  • Object
show all
Includes:
FileHelper, Reporter
Defined in:
lib/geoffrey/package.rb

Overview

This class helps installing packages and other apps into your system.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileHelper

#extract_filename, #remove_extension

Methods included from Reporter

#debug, #echo, #logger, #verbose

Constructor Details

#initializePackage

Returns a new instance of Package.



29
30
31
# File 'lib/geoffrey/package.rb', line 29

def initialize
  @options = {}
end

Instance Attribute Details

#createsObject

Returns the value of attribute creates.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def creates
  @creates
end

#descriptionObject

Returns the value of attribute description.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def description
  @description
end

#dirObject

Returns the value of attribute dir.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def dir
  @dir
end

#fileObject

Returns the value of attribute file.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def filename
  @filename
end

#formatObject

Returns the value of attribute format.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def format
  @format
end

#guessed_nameObject

Returns the value of attribute guessed_name.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def guessed_name
  @guessed_name
end

#options(val = nil) ⇒ Object

We will do some kind of symbolize_keys, but manually so we don’t have to use activesupport just for this. Pretty much blatantly copied from it :P.



43
44
45
# File 'lib/geoffrey/package.rb', line 43

def options
  @options
end

#urlObject

Returns the value of attribute url.



26
27
28
# File 'lib/geoffrey/package.rb', line 26

def url
  @url
end

Instance Method Details

#download_and_decompressObject

Uses the url provided to download the file. Checks the options first to see if it shouldn’t be done.



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
104
105
106
107
# File 'lib/geoffrey/package.rb', line 77

def download_and_decompress
  echo "URL not specified" unless @url && @url != ""

  get_filename and debug("filename detected: #{@filename}")

  return unless should_install

  echo @description if @description && @description != ""

  begin
    @file = Tempfile.new("geoffrey") and debug("downloading file into #{@file.path}")

    read_with_progress_bar
    @file.close

    get_format and debug("format detected: #{@format}")


    @dir = "#{Dir.tmpdir}/#{filename}" and debug("will put files into temporary dir: #{@dir}")

    FileUtils.rm_rf dir if File.exist?(dir)
    FileUtils.mkdir_p dir
  rescue Exception => e
    echo "Can't find file #{@url}"
    debug "raised an exception #{e}"

    raise NotFoundError
  end

  decompress
end

#file_to_installObject

If it was specified through options, then get that, otherwise get the first file with .pkg extension



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/geoffrey/package.rb', line 135

def file_to_install
  return @file_to_install if defined?(@file_to_install)
  @file_to_install = if @options[:file]
    "#{dir}/#{@options[:file]}"
  else
    candidate   = Dir.glob("#{dir}/**/*.pkg").first
    candidate ||= Dir.glob("#{dir}/**/*.app").first
    candidate ||= Dir.glob("#{dir}/**/*").first
    candidate
  end
end

#fits?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/geoffrey/package.rb', line 109

def fits?(pattern)
  description.to_s.downcase.match(pattern) || name.to_s.downcase.match(pattern)
end

#get_nameObject

Dumb method to get its already defined name, to avoid some precocious naming



64
65
66
# File 'lib/geoffrey/package.rb', line 64

def get_name
  @name
end

#installObject

Does the actual installing of the package For .pkg files it executes installer. For .app files it just moves it to /Applications – TODO maybe add more customization options TODO investigate other possible extensions.



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/geoffrey/package.rb', line 119

def install
  return unless should_install

  case File.extname file_to_install
  when ".pkg"
    echo "Installing package"
    execute "installer -pkg #{file_to_install} -target /"
  when ".app"
    echo "Moving file to /Applications"
    FileUtils.mv file_to_install, "/Applications"
  end
  echo "Install successful"
end

#name(val = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/geoffrey/package.rb', line 53

def name(val=nil)
  if val.nil? && @name.nil? && (!@url.nil? || !@guessed_name.nil?)
    @name = remove_extension(@guessed_name||get_filename)
  elsif !val.nil?
    @name = val
  end
  @name
end

#processObject

The idea is to put together everything that has to be done after you’ve initialized the proper attributes.



70
71
72
73
# File 'lib/geoffrey/package.rb', line 70

def process
  download_and_decompress
  install
end