Module: Shoes::Pack

Defined in:
lib/shoes/pack.rb

Class Method Summary collapse

Class Method Details

.dmg(script, opt, &blk) ⇒ Object


117
118
119
120
121
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/shoes/pack.rb', line 117

def self.dmg(script, opt, &blk)
  name = File.basename(script).gsub(/\.\w+$/, '')
  app_name = name.capitalize.gsub(/[-_](\w)/) { $1.capitalize }
  vol_name = name.capitalize.gsub(/[-_](\w)/) { " " + $1.capitalize }
  app_app = "#{app_name}.app"
  vers = [1, 0]

  tmp_dir = File.join(LIB_DIR, "+dmg")
  FileUtils.rm_rf(tmp_dir)
  FileUtils.mkdir_p(tmp_dir)
  FileUtils.cp(File.join(DIR, "static", "stubs", "blank.hfz"),
               File.join(tmp_dir, "blank.hfz"))
  app_dir = File.join(tmp_dir, app_app)
  res_dir = File.join(tmp_dir, app_app, "Contents", "Resources")
  mac_dir = File.join(tmp_dir, app_app, "Contents", "MacOS")
  [res_dir, mac_dir].map { |x| FileUtils.mkdir_p(x) }
  FileUtils.cp(File.join(DIR, "static", "Shoes.icns"), app_dir)
  FileUtils.cp(File.join(DIR, "static", "Shoes.icns"), res_dir)
  File.open(File.join(app_dir, "Contents", "PkgInfo"), 'w') do |f|
    f << "APPL????"
  end
  File.open(File.join(app_dir, "Contents", "Info.plist"), 'w') do |f|
    f << <<END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleGetInfoString</key>
  <string>#{app_name} #{vers.join(".")}</string>
  <key>CFBundleExecutable</key>
  <string>#{name}-launch</string>
  <key>CFBundleIdentifier</key>
  <string>org.hackety.#{name}</string>
  <key>CFBundleName</key>
  <string>#{app_name}</string>
  <key>CFBundleIconFile</key>
  <string>Shoes.icns</string>
  <key>CFBundleShortVersionString</key>
  <string>#{vers.join(".")}</string>
  <key>CFBundleInfoDictionaryVersion</key>
  <string>6.0</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>IFMajorVersion</key>
  <integer>#{vers[0]}</integer>
  <key>IFMinorVersion</key>
  <integer>#{vers[1]}</integer>
</dict>
</plist>
END
  end
  File.open(File.join(app_dir, "Contents", "version.plist"), 'w') do |f|
    f << <<END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>BuildVersion</key>
  <string>1</string>
  <key>CFBundleVersion</key>
  <string>#{vers.join(".")}</string>
  <key>ProjectName</key>
  <string>#{app_name}</string>
  <key>SourceVersion</key>
  <string>#{Time.now.strftime("%Y%m%d")}</string>
</dict>
</plist>
END
  end
  File.open(File.join(mac_dir, "#{name}-launch"), 'w') do |f|
    f << <<END
#!/bin/bash
SHOESPATH=/Applications/Shoes.app/Contents/MacOS
APPPATH="${0%/*}"
unset DYLD_LIBRARY_PATH
cd "$APPPATH"
echo "[Pango]" > /tmp/pangorc
echo "ModuleFiles=$SHOESPATH/pango.modules" >> /tmp/pangorc
if [ ! -d /Applications/Shoes.app ]
  then ./cocoa-install
fi
open -a /Applications/Shoes.app "#{File.basename(script)}"
# DYLD_LIBRARY_PATH=$SHOESPATH PANGO_RC_FILE="$APPPATH/pangorc" $SHOESPATH/shoes-bin "#{File.basename(script)}"
END
  end
  FileUtils.cp(script, File.join(mac_dir, File.basename(script)))
  FileUtils.cp(File.join(DIR, "static", "stubs", "cocoa-install"),
    File.join(mac_dir, "cocoa-install"))

  dmg = Binject::DMG.new(File.join(tmp_dir, "blank.hfz"), vol_name)
  f2 = pkg("osx", opt)
  if f2
    dmg.grow(10)
    dmg.inject_file("setup.dmg", f2.path)
  end
  dmg.inject_dir(app_app, app_dir)
  dmg.chmod_file(0755, "#{app_app}/Contents/MacOS/#{name}-launch")
  dmg.chmod_file(0755, "#{app_app}/Contents/MacOS/cocoa-install")
  dmg.save(script.gsub(/\.\w+$/, '') + ".dmg") do |perc|
    blk[perc * 0.01] if blk
  end
  FileUtils.rm_rf(tmp_dir)
  blk[1.0] if blk
end

.exe(script, opt, &blk) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/shoes/pack.rb', line 84

def self.exe(script, opt, &blk)
  size = File.size(script)
  f = File.open(script, 'rb')
  exe = Binject::EXE.new(File.join(DIR, "static", "stubs", "blank.exe"))
  size += script.length
  exe.inject("SHOES_FILENAME", File.basename(script))
  size += File.size(script)
  exe.inject("SHOES_PAYLOAD", f)
  f2 = pkg("win32", opt)
  if f2
    size += File.size(f2.path)
    f3 = File.open(f2.path, 'rb')
    exe.inject("SHOES_SETUP", f3)

    count, last = 0, 0.0
    exe.save(script.gsub(/\.\w+$/, '') + ".exe") do |len|
      count += len
      prg = count.to_f / size.to_f
      blk[last = prg] if blk and prg - last > 0.02 and prg < 1.0
    end
    
    f.close
    f2.close
    f3.close
  else
    # doesn't work on Linux or OSX
    Dir.chdir DIR + '/static/stubs' do
      `.\\shoes-stub-inject.exe #{script.gsub('/', "\\")}`
    end
  end
  blk[1.0] if blk
end

.linux(script, opt, &blk) ⇒ Object


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/shoes/pack.rb', line 222

def self.linux(script, opt, &blk)
  name = File.basename(script).gsub(/\.\w+$/, '')
  app_name = name.capitalize.gsub(/[-_](\w)/) { $1.capitalize }
  run_path = script.gsub(/\.\w+$/, '') + ".run"
  tgz_path = script.gsub(/\.\w+$/, '') + ".tgz"
  tmp_dir = File.join(LIB_DIR, "+run")
  FileUtils.mkdir_p(tmp_dir)
  pkgf = pkg("linux", opt)
  prog = 1.0
  if pkgf
    size = Shy.hrun(pkgf)
    pblk = Shy.progress(size) do |name, perc, left|
      blk[perc * 0.5]
    end if blk
    Shy.xzf(pkgf, tmp_dir, &pblk)
    prog -= 0.5
  end

  FileUtils.cp(script, File.join(tmp_dir, File.basename(script)))
  File.open(File.join(tmp_dir, "sh-install"), 'wb') do |a|
    rewrite a, File.join(DIR, "static", "stubs", "sh-install"),
      'SCRIPT' => "./#{File.basename(script)}"
  end
  FileUtils.chmod 0755, File.join(tmp_dir, "sh-install")

  raw = Shy.du(tmp_dir)
  File.open(tgz_path, 'wb') do |f|
    pblk = Shy.progress(raw) do |name, perc, left|
      blk[prog + (perc * prog)]
    end if blk
    Shy.czf(f, tmp_dir, &pblk)
  end
   
  md5, fsize = Shy.md5sum(tgz_path), File.size(tgz_path)
  File.open(run_path, 'wb') do |f|
    rewrite f, File.join(DIR, "static", "stubs", "blank.run"),
      'CRC' => '0000000000', 'MD5' => md5, 'LABEL' => app_name, 'NAME' => name,
      'SIZE' => fsize, 'RAWSIZE' => (raw / 1024) + 1, 'TIME' => Time.now, 'FULLSIZE' => raw
    File.open(tgz_path, 'rb') do |f2|
      f.write f2.read(8192) until f2.eof
    end
  end
  FileUtils.chmod 0755, run_path
  FileUtils.rm_rf(tgz_path)
  FileUtils.rm_rf(tmp_dir)
  blk[1.0] if blk
end

.pkg(platform, opt) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
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
74
75
76
77
78
79
80
81
82
# File 'lib/shoes/pack.rb', line 19

def self.pkg(platform, opt)
  extension = case platform
  when "win32" then
   "exe"
  when "linux" then
   "run"
  when "osx" then
   "dmg"
  else
   raise "Unknown platform"
  end
  
  case opt
  when Shoes::I_YES then
    url = "http://shoes.heroku.com/pkg/#{Shoes::RELEASE_NAME.downcase}/#{platform}/shoes"
    local_file_path = File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform, "latest_shoes.#{extension}")
  when Shoes::I_NOV then
    url = "http://shoes.heroku.com/pkg/#{Shoes::RELEASE_NAME.downcase}/#{platform}/shoes-novideo"
    local_file_path = File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform, "latest_shoes-novideo.#{extension}")  
  when I_NET then
    url = false
  else
    raise "missing download option #{opt}"
  end
  
  FileUtils.makedirs File.join(LIB_DIR, Shoes::RELEASE_NAME.downcase, platform)
  
  if url then
    begin
      url = open(url).read.strip
      debug url
      internet_ok = true
    rescue Exception => e
      error e
      internet_ok = false
    end

    if File.exists? local_file_path
      return  open(local_file_path)
    elsif internet_ok then
      begin
        debug "Downloading #{url}..."
        downloaded = open(url)
        debug "Download of #{url} finished"
      rescue Exception => e
        error "Could not download from the internet at #{url}\n" + e
        internet_ok = false
      end
      if internet_ok then
        begin
          File.open(local_file_path, "wb") do |f|
            f.write(downloaded.read)
          end
          return  open(local_file_path)
        rescue Exception => e
            raise "The download failed from\n#{url}\nor could not write to local files" + e
        end
      end
    else
      noHopeMsg = "Failed to find an existing Shoes at:\n#{local_file_path}\nor download from\n#{url} to include with your script."
     raise noHopeMsg 
    end
  end
end

.rewrite(a, before, hsh) ⇒ Object


11
12
13
14
15
16
17
# File 'lib/shoes/pack.rb', line 11

def self.rewrite a, before, hsh
  File.open(before) do |b|
    b.each do |line|
      a << line.gsub(/\#\{(\w+)\}/) { hsh[$1] }
    end
  end
end