Class: Roasted::Provider::App

Inherits:
Object
  • Object
show all
Defined in:
lib/roasted/provider/app.rb

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, app, options) ⇒ App

Returns a new instance of App.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roasted/provider/app.rb', line 21

def initialize(action, app, options)
  @app = app
  @action = action
  @options = options
  @installs = { :app => @app }
  @hooks = {}
  
  path = File.expand_path(File.join(File.dirname(__FILE__), "app", "#{app.downcase}.rb"))
  @parser = Parser.new(self)
  @parser.instance_eval(File.read(path), path)
end

Instance Attribute Details

#accept_eulaObject

Returns the value of attribute accept_eula.



15
16
17
# File 'lib/roasted/provider/app.rb', line 15

def accept_eula
  @accept_eula
end

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/roasted/provider/app.rb', line 9

def action
  @action
end

#appObject

Returns the value of attribute app.



8
9
10
# File 'lib/roasted/provider/app.rb', line 8

def app
  @app
end

#checksumObject

Returns the value of attribute checksum.



14
15
16
# File 'lib/roasted/provider/app.rb', line 14

def checksum
  @checksum
end

#domainObject

Returns the value of attribute domain.



18
19
20
# File 'lib/roasted/provider/app.rb', line 18

def domain
  @domain
end

#hooksObject

Returns the value of attribute hooks.



19
20
21
# File 'lib/roasted/provider/app.rb', line 19

def hooks
  @hooks
end

#installsObject

Returns the value of attribute installs.



16
17
18
# File 'lib/roasted/provider/app.rb', line 16

def installs
  @installs
end

#licenseObject

Returns the value of attribute license.



17
18
19
# File 'lib/roasted/provider/app.rb', line 17

def license
  @license
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/roasted/provider/app.rb', line 10

def options
  @options
end

#sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/roasted/provider/app.rb', line 12

def source
  @source
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/roasted/provider/app.rb', line 13

def type
  @type
end

Class Method Details

.create(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/roasted/provider/app.rb', line 33

def self.create(options)
  action = options.delete(:action)
  app = options.delete(:app)

  # Replace ourself with custom class
  path = File.expand_path(File.join(File.dirname(__FILE__), "app", "#{app.downcase}.rb"))
  raise "Cannot find app provider #{app}" unless File.exists?(path)
  
  new(action, app, options)
end

Instance Method Details

#installObject

Actions, at this point only install



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
74
75
76
77
78
79
80
# File 'lib/roasted/provider/app.rb', line 45

def install
  if File.exists?("/Applications/#{@installs[:app]}.app")
    if @installs[:version]
      # Check version
      info = Plist.parse_xml(File.read("/Applications/#{@installs[:app]}.app/Contents/Info.plist"))
      return if info["CFBundleShortVersionString"] >= @installs[:version]
    else
      # No version requested, installs app exists, skipping installation
      return
    end
  end

  puts "Installing #{@app}"
  
  self.send("install_#{self.type}")
  
  # Handle license
  if @options[:license] and @license
    puts "Setting up license"
    if @license[:block]
      @license[:block].call(@options[:license])
    else
      self.send("license_#{@license[:type]}", @options[:license])
    end
  end
  
  # Handle custom functionality
  if @options[:block]
    @parser.instance_eval &@options[:block]
  end
  
  # Hooks
  if @hooks[:after_install]
    @hooks[:after_install].call
  end
end

#install_dmgObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/roasted/provider/app.rb', line 102

def install_dmg
  tmp = download_source

  # Attach it
  needs_eula = system("hdiutil imageinfo #{tmp.path} | grep -q 'Software License Agreement: true'")
  raise "Requires EULA Acceptance; add 'accept_eula' to application resource" if needs_eula and !@accept_eula
  accept_eula_cmd = @accept_eula ? "yes |" : ""
  # require 'pry';binding.pry if needs_eula
  system "#{accept_eula_cmd} hdiutil attach '#{tmp.path}' > /dev/null"
  
  # Get volume path
  hdi = Plist.parse_xml(`hdiutil info -plist`)
  image = hdi["images"].select{|i| i["image-path"] == tmp.path}.first
  mntinfo = image["system-entities"].select{|i| i.has_key?("mount-point")}.first
  mount_point = mntinfo["mount-point"]
  disk = mntinfo["dev-entry"]
  
  # Find app
  Dir["#{mount_point}/*#{@app}*.{app,pkg,mpkg}"].each do |entry|
    type = @apptype || File.extname(entry)[1..-1].to_sym
    case type
    when :app
      system "rsync -aH '#{entry}' '/Applications/'"
    when :pkg
    when :mpkg
      system "sudo installer -pkg '#{entry}' -target /"
    else
      puts "Don't know how to handle entry #{entry}, type #{type}!"
    end
  end

  # Detach
  system("hdiutil detach -quiet '#{mount_point}'")
  
  # Remove image
  tmp.unlink        
end

#install_tbzObject



92
93
94
95
96
97
98
99
100
# File 'lib/roasted/provider/app.rb', line 92

def install_tbz
  tmp = download_source
  
  # Simple and easy, just extract bzip tar
  system "tar -C '/Applications' -jxf '#{tmp.path}'"
  
  # Cleanup
  tmp.unlink        
end

#install_zipObject



82
83
84
85
86
87
88
89
90
# File 'lib/roasted/provider/app.rb', line 82

def install_zip
  tmp = download_source
  
  # Simple and easy, just extract zip
  system "ditto -x -k '#{tmp.path}' '/Applications/'"
  
  # Cleanup
  tmp.unlink
end

#license_preferences(license) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/roasted/provider/app.rb', line 140

def license_preferences(license)
  domain_exists = system("defaults domains | grep #{@domain} >/dev/null")
  
  if domain_exists
    @license[:options].each do |key, name|
      system "defaults write #{@domain} '#{name}' '#{license[key]}'"
    end
  else
    plist = @license[:options].collect {|key, name| "\"#{name}\" = \"#{license[key]}\";"}.join(" ")
    system "defaults write #{@domain} '{#{plist}}'"
  end
end

#to_sObject



164
165
166
# File 'lib/roasted/provider/app.rb', line 164

def to_s
  "#{self.class}: #{self.options.inspect}"
end