Class: Furoshiki::Shoes::SwtApp

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/furoshiki/shoes/swt_app.rb

Constant Summary collapse

REMOTE_TEMPLATE_URL =
'https://s3.amazonaws.com/net.wasnotrice.shoes/wrappers/shoes-app-template-0.0.1.zip'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SwtApp

Returns a new instance of SwtApp.

Parameters:

  • config (Shoes::Package::Configuration)

    user configuration



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/furoshiki/shoes/swt_app.rb', line 18

def initialize(config)
  @config = config

  unless config.valid?
    raise Furoshiki::ConfigurationError, "Invalid configuration.\n#{config.error_message_list}"
  end

  home = ENV['FUROSHIKI_HOME'] || Dir.home
  @cache_dir = Pathname.new(home).join('.furoshiki', 'cache')
  @default_package_dir = working_dir.join('pkg')
  @package_dir = default_package_dir
  @default_template_path = cache_dir.join(template_filename)
  @template_path = default_template_path
  @tmp = @package_dir.join('tmp')
end

Instance Attribute Details

#cache_dirPathname (readonly)

Returns cache directory.

Returns:

  • (Pathname)

    cache directory



47
48
49
# File 'lib/furoshiki/shoes/swt_app.rb', line 47

def cache_dir
  @cache_dir
end

#configObject (readonly)

Returns the value of attribute config.



49
50
51
# File 'lib/furoshiki/shoes/swt_app.rb', line 49

def config
  @config
end

#default_package_dirPathname (readonly)

Returns default package directory: ./pkg.

Returns:

  • (Pathname)

    default package directory: ./pkg



35
36
37
# File 'lib/furoshiki/shoes/swt_app.rb', line 35

def default_package_dir
  @default_package_dir
end

#default_template_pathPathname (readonly)

Returns default path to .app template.

Returns:

  • (Pathname)

    default path to .app template



41
42
43
# File 'lib/furoshiki/shoes/swt_app.rb', line 41

def default_template_path
  @default_template_path
end

#package_dirPathname

Returns package directory.

Returns:

  • (Pathname)

    package directory



38
39
40
# File 'lib/furoshiki/shoes/swt_app.rb', line 38

def package_dir
  @package_dir
end

#template_pathPathname

Returns path to .app template.

Returns:

  • (Pathname)

    path to .app template



44
45
46
# File 'lib/furoshiki/shoes/swt_app.rb', line 44

def template_path
  @template_path
end

#tmpObject (readonly)

Returns the value of attribute tmp.



51
52
53
# File 'lib/furoshiki/shoes/swt_app.rb', line 51

def tmp
  @tmp
end

Instance Method Details

#app_nameObject



207
208
209
# File 'lib/furoshiki/shoes/swt_app.rb', line 207

def app_name
  "#{config.name}.app"
end

#app_pathObject



215
216
217
# File 'lib/furoshiki/shoes/swt_app.rb', line 215

def app_path
  package_dir.join app_name
end

#cache_templateObject



78
79
80
81
# File 'lib/furoshiki/shoes/swt_app.rb', line 78

def cache_template
  cache_dir.mkpath unless cache_dir.exist?
  download_template unless template_path.size?
end

#create_tmpObject



70
71
72
# File 'lib/furoshiki/shoes/swt_app.rb', line 70

def create_tmp
  tmp.mkpath
end

#download(remote_url, local_path) ⇒ Object



103
104
105
# File 'lib/furoshiki/shoes/swt_app.rb', line 103

def download(remote_url, local_path)
  download_following_redirects remote_url, local_path
end

#download_following_redirects(remote_url, local_path, redirect_limit = 5) ⇒ Object



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
# File 'lib/furoshiki/shoes/swt_app.rb', line 107

def download_following_redirects(remote_url, local_path, redirect_limit = 5)
  if redirect_limit == 0
    raise Furoshiki::DownloadError,
          "Too many redirects trying to reach #{remote_url}"
  end

  uri = URI(remote_url)
  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(uri.request_uri)
    http.request request do |response|
      case response
      when Net::HTTPSuccess then
        warn "Downloading #{remote_url} to #{local_path}"
        open(local_path, 'wb') do |file|
          response.read_body do |chunk|
            file.write chunk
          end
        end
      when Net::HTTPRedirection then
        location = response['location']
        warn "Redirected to #{location}"
        download_following_redirects(location, local_path, redirect_limit - 1)
      else
        raise Furoshiki::DownloadError, "Couldn't download app template at #{remote_url}. #{response.value}"
      end
    end
  end
end

#download_templateObject



99
100
101
# File 'lib/furoshiki/shoes/swt_app.rb', line 99

def download_template
  download remote_template_url, template_path
end

#downloads_urlObject



136
137
138
# File 'lib/furoshiki/shoes/swt_app.rb', line 136

def downloads_url
  "http://shoesrb.com/downloads"
end

#ensure_jar_existsObject



151
152
153
154
155
156
# File 'lib/furoshiki/shoes/swt_app.rb', line 151

def ensure_jar_exists
  jar = SwtJar.new(@config)
  path = tmp.join(jar.filename)
  jar.package(tmp) unless File.exist?(path)
  path
end

#executable_pathObject



219
220
221
# File 'lib/furoshiki/shoes/swt_app.rb', line 219

def executable_path
  app_path.join('Contents/MacOS/JavaAppLauncher')
end

#extract_templateObject

Raises:

  • (IOError)


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/furoshiki/shoes/swt_app.rb', line 170

def extract_template
  raise IOError, "Couldn't find app template at #{template_path}." unless template_path.size?
  extracted_app = nil
  ::Zip::ZipFile.new(template_path).each do |entry|
    # Fragile hack
    extracted_app = template_path.join(entry.name) if Pathname.new(entry.name).extname == '.app'
    p = tmp.join(entry.name)
    p.dirname.mkpath
    entry.extract(p)
  end
  mv tmp.join(extracted_app.basename.to_s), tmp_app_path
end

#inject_configObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/furoshiki/shoes/swt_app.rb', line 183

def inject_config
  plist = tmp_app_path.join 'Contents/Info.plist'
  template = Plist.parse_xml(plist)
  template['CFBundleIdentifier'] = "com.hackety.shoes.#{config.shortname}"
  template['CFBundleDisplayName'] = config.name
  template['CFBundleName'] = config.name
  template['CFBundleVersion'] = config.version
  template['CFBundleIconFile'] = Pathname.new(config.icons[:osx]).basename.to_s if config.icons[:osx]
  File.open(plist, 'w') { |f| f.write template.to_plist }
end

#inject_iconObject



194
195
196
197
198
199
200
201
# File 'lib/furoshiki/shoes/swt_app.rb', line 194

def inject_icon
  if config.icons[:osx]
    icon_path = working_dir.join(config.icons[:osx])
    raise IOError, "Couldn't find app icon at #{icon_path}" unless icon_path.exist?
    resources_dir = tmp_app_path.join('Contents/Resources')
    cp icon_path, resources_dir.join(icon_path.basename)
  end
end

#inject_jar(jar_path) ⇒ Object

Injects JAR into APP. The JAR should be the only item in the Contents/Java directory. If this directory contains more than one JAR, the “first” one gets run, which may not be what we want.

Parameters:

  • jar_path (Pathname, String)

    the location of the JAR to inject



163
164
165
166
167
168
# File 'lib/furoshiki/shoes/swt_app.rb', line 163

def inject_jar(jar_path)
  jar_dir = tmp_app_path.join('Contents/Java')
  jar_dir.rmtree
  jar_dir.mkdir
  cp Pathname.new(jar_path), jar_dir
end

#latest_template_versionObject



95
96
97
# File 'lib/furoshiki/shoes/swt_app.rb', line 95

def latest_template_version
  '0.0.1'
end

#move_to_package_dir(path) ⇒ Object



145
146
147
148
149
# File 'lib/furoshiki/shoes/swt_app.rb', line 145

def move_to_package_dir(path)
  dest = package_dir.join(path.basename)
  dest.rmtree if dest.exist?
  mv path.to_s, dest
end

#packageObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/furoshiki/shoes/swt_app.rb', line 53

def package
  remove_tmp
  create_tmp
  cache_template
  extract_template
  inject_icon
  inject_config
  jar_path = ensure_jar_exists
  inject_jar jar_path
  move_to_package_dir tmp_app_path
  tweak_permissions
rescue => e
  raise e
ensure
  remove_tmp
end

#remote_template_urlObject



140
141
142
143
# File 'lib/furoshiki/shoes/swt_app.rb', line 140

def remote_template_url
  #"#{downloads_url}/#{template_basename}-#{latest_template_version}#{template_extension}"
  REMOTE_TEMPLATE_URL
end

#remove_tmpObject



74
75
76
# File 'lib/furoshiki/shoes/swt_app.rb', line 74

def remove_tmp
  tmp.rmtree if tmp.exist?
end

#template_basenameObject



83
84
85
# File 'lib/furoshiki/shoes/swt_app.rb', line 83

def template_basename
  'shoes-app-template'
end

#template_extensionObject



87
88
89
# File 'lib/furoshiki/shoes/swt_app.rb', line 87

def template_extension
  '.zip'
end

#template_filenameObject



91
92
93
# File 'lib/furoshiki/shoes/swt_app.rb', line 91

def template_filename
  "#{template_basename}#{template_extension}"
end

#tmp_app_pathObject



211
212
213
# File 'lib/furoshiki/shoes/swt_app.rb', line 211

def tmp_app_path
  tmp.join app_name
end

#tweak_permissionsObject



203
204
205
# File 'lib/furoshiki/shoes/swt_app.rb', line 203

def tweak_permissions
  executable_path.chmod 0755
end

#working_dirObject



223
224
225
# File 'lib/furoshiki/shoes/swt_app.rb', line 223

def working_dir
  config.working_dir
end