geoffrey 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/geoffrey.rb CHANGED
@@ -7,12 +7,13 @@ module Geoffrey
7
7
 
8
8
  # Define a package installation from the URL specified. It will download
9
9
  # the file, and if it's compressed, it will decompress it.It currently
10
- # supports zip, tar.gz, .gz and .dmg files. Once extracted it will look for a .pkg
11
- # file and try to install it.
10
+ # supports zip, tar.gz, .gz and .dmg files. Once extracted it will look for
11
+ # a .pkg or a .app file and try to install it.
12
12
  #
13
13
  # Some options can be defined:
14
14
  #
15
- # * +:sudo+ if it has to be executed as sudo.
15
+ # * +:sudo+ if it has to be executed as sudo. Generally .pkg files that
16
+ # ask for a password will require this.
16
17
  # * +:unless+ a Proc defining if it doesn't have to try to install it.
17
18
  # * +:if+ inverse of inverse
18
19
  # * +:file+ what file has to be installed from all of the ones that came in
@@ -9,7 +9,7 @@ module Geoffrey
9
9
  #--
10
10
  # TODO add support for .dmg files would be great
11
11
  class Package
12
- attr_accessor :url, :options, :format, :filename, :dir
12
+ attr_accessor :url, :options, :format, :filename, :dir, :file
13
13
 
14
14
  def initialize
15
15
  @options = {}
@@ -32,23 +32,21 @@ module Geoffrey
32
32
  # TODO raise some error if the file can't be found or something
33
33
  # TODO add some option to define the file name for weird url's
34
34
  def download_and_decompress
35
- return nil if shouldnt_install
35
+ return unless should_install
36
36
 
37
37
  @filename = @url.split("/").last
38
38
  unless @url.nil?
39
39
  get_format
40
40
 
41
- file = Tempfile.new("geoffrey")
42
- file.write open(@url).read
43
- file.close
41
+ @file = Tempfile.new("geoffrey")
42
+ @file.write open(@url).read
43
+ @file.close
44
44
 
45
- @dir = "#{Dir.tmpdir}/#{filename}"
46
- FileUtils.rm_rf @dir if File.exist?(@dir)
47
- FileUtils.mkdir_p @dir
45
+ @dir = "#{Dir.tmpdir}/#{filename}"
46
+ FileUtils.rm_rf dir if File.exist?(dir)
47
+ FileUtils.mkdir_p dir
48
48
 
49
- decompress file, @dir
50
-
51
- @dir
49
+ decompress
52
50
  end
53
51
  end
54
52
 
@@ -56,13 +54,14 @@ module Geoffrey
56
54
  #--
57
55
  # TODO add support for .app files
58
56
  def install
59
- return nil if shouldnt_install
57
+ return unless should_install
60
58
 
61
- command = case File.extname file_to_install
59
+ case File.extname file_to_install
62
60
  when ".pkg"
63
- "installer -pkg #{file_to_install} -target /"
61
+ execute "installer -pkg #{file_to_install} -target /"
62
+ when ".app"
63
+ FileUtils.mv file_to_install, "/Appliactions"
64
64
  end
65
- execute command
66
65
  end
67
66
 
68
67
  # If it was specified through options, then get that, otherwise
@@ -72,32 +71,36 @@ module Geoffrey
72
71
  def file_to_install
73
72
  return @file_to_install if defined?(@file_to_install)
74
73
  @file_to_install = if @options[:file]
75
- "#{@dir}/#{@options[:file]}"
74
+ "#{dir}/#{@options[:file]}"
76
75
  else
77
- files = Dir.glob("#{@dir}/**/*.pkg").first
78
- files ||= Dir.glob("#{@dir}/**/*").first
76
+ candidate = Dir.glob("#{dir}/**/*.pkg").first
77
+ candidate ||= Dir.glob("#{dir}/**/*.app").first
78
+ candidate ||= Dir.glob("#{dir}/**/*").first
79
+ candidate
79
80
  end
80
81
  end
81
82
 
82
83
  private
83
84
 
84
- def shouldnt_install
85
+ def should_install
85
86
  if @options[:unless] && @options[:unless].respond_to?(:call)
86
- @options[:unless].call
87
+ !@options[:unless].call
87
88
  elsif @options[:if] && @options[:if].respond_to?(:call)
88
- !@options[:if].call
89
+ @options[:if].call
89
90
  else
90
- false
91
+ true
91
92
  end
92
93
  end
93
94
 
94
95
  # Simple wrapper to execute things, using sudo or not as per defiend in the
95
96
  # options. Useful for debugging too :)
96
- def execute(command, options = {})
97
- options = @options.merge(options)
98
- command = "sudo #{command}" if options[:sudo]
99
- puts "Executing: #{command}" if $verbose
100
- `#{command}`
97
+ def execute(command, opts = {})
98
+ if String === command
99
+ opts = @options.merge(opts)
100
+ command = "sudo #{command}" if opts[:sudo]
101
+ puts "Executing: #{command}" if $verbose
102
+ `#{command}`
103
+ end
101
104
  end
102
105
 
103
106
  def get_format
@@ -105,11 +108,9 @@ module Geoffrey
105
108
  when '.zip' then :zip
106
109
  when '.tar' then :tar
107
110
  when '.tgz' then :tgz
108
- when '.gz'
109
- @filename =~ /\.tar\.gz/ ? :tgz : :gz
111
+ when '.gz' then filename =~ /\.tar\.gz/ ? :tgz : :gz
110
112
  when '.dmg' then :dmg
111
- else
112
- :unknown
113
+ else :unknown
113
114
  end
114
115
  end
115
116
 
@@ -117,36 +118,35 @@ module Geoffrey
117
118
  # +#{Dir.tmpdir}/#{filename}+
118
119
  #--
119
120
  # TODO better isolate each download inside the tmpdir?
120
- def decompress(file,target)
121
- command = case @format
121
+ def decompress
122
+ command = case format
122
123
  when :zip
123
- "unzip -o #{file.path} -d #{target} 1> /dev/null"
124
+ execute "unzip -o #{file.path} -d #{dir} 1> /dev/null", :sudo => false
124
125
  when :tar
125
- "tar xf #{file.path} -C #{target}"
126
+ execute "tar xf #{file.path} -C #{dir}", :sudo => false
126
127
  when :tgz
127
- "tar xzf #{file.path} -C #{target}"
128
+ execute "tar xzf #{file.path} -C #{dir}", :sudo => false
128
129
  when :gz
129
- "cp #{file.path} #{target}/#{filename}.gz; gunzip #{target}/*"
130
+ execute "cp #{file.path} #{dir}/#{filename}.gz; gunzip #{dir}/*", :sudo => false
130
131
  when :unknown
131
- "cp #{file.path} #{target}/#{filename}"
132
+ execute "cp #{file.path} #{dir}/#{filename}", :sudo => false
132
133
  when :dmg
133
- extract_from_dmg(file,target)
134
- nil
134
+ extract_from_dmg
135
135
  end
136
- execute command, :sudo => false if command
137
136
  end
138
137
 
139
138
 
140
- def extract_from_dmg(file,target)
141
- execute("mv #{file.path} #{target}.dmg")
142
- str = execute("hdiutil attach #{target}.dmg")
139
+ # Mount the dmg file, copy contents to tmpdir, unmount, remove dmg
140
+ def extract_from_dmg
141
+ execute("mv #{file.path} #{dir}.dmg")
142
+ str = execute("hdiutil attach #{dir}.dmg")
143
143
  unless $?.exitstatus > 0
144
144
  info = str.split("\t")
145
145
  dmg_dir = info.last.chomp
146
146
  device = info.first.strip.split("/").last
147
- execute("cp -r #{dmg_dir}/** #{target}")
147
+ execute("cp -r #{dmg_dir}/** #{dir}")
148
148
  execute("hdiutil detach #{device}")
149
- execute("rm -f #{target}.dmg")
149
+ execute("rm -f #{dir}.dmg")
150
150
  end
151
151
  end
152
152
 
@@ -1,3 +1,3 @@
1
1
  module Geoffrey
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ -f s
2
+ -c
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Albert Llop
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-29 00:00:00 +02:00
17
+ date: 2010-08-30 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -129,6 +129,7 @@ files:
129
129
  - lib/geoffrey/version.rb
130
130
  - spec/package_spec.rb
131
131
  - spec/plist_spec.rb
132
+ - spec/spec.opts
132
133
  - spec/spec_helper.rb
133
134
  - spec/static/example.plist
134
135
  - spec/static/test.dmg