Class: CreateBundle::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/create_bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
20
21
# File 'lib/create_bundle.rb', line 14

def initialize(options)
  @logger = Logger.new(STDOUT)
  @app_path = Pathname(options[:source])
  @custom_icon = options[:icon]
  @custom_script = options[:script]
  options[:target] = options[:source] if options[:bare]
  @target_path = options[:target] ? Pathname(options[:target]) : Pathname(@app_path.basename.to_s)
end

Instance Attribute Details

#app_pathObject

Returns the value of attribute app_path.



12
13
14
# File 'lib/create_bundle.rb', line 12

def app_path
  @app_path
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/create_bundle.rb', line 12

def logger
  @logger
end

#target_pathObject

Returns the value of attribute target_path.



12
13
14
# File 'lib/create_bundle.rb', line 12

def target_path
  @target_path
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/create_bundle.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#contents_dirObject



54
55
56
# File 'lib/create_bundle.rb', line 54

def contents_dir
  target_path + 'Contents'
end

#copy_iconObject



86
87
88
89
# File 'lib/create_bundle.rb', line 86

def copy_icon
  File.link(icon_path, resources_dir + 'applet.icns')
  logger.debug "Copied icon to: #{(resources_dir + 'applet.icns')}" if verbose
end

#copy_scriptObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/create_bundle.rb', line 98

def copy_script
  return false unless @custom_script
  if Pathname(@custom_script).exist?
    File.link(Pathname(@custom_script), macos_dir + 'applet')
    true
  else
    puts "Script file doesn't exist"
    exit_and_cleanup
  end
end

#createObject



114
115
116
117
118
119
120
121
# File 'lib/create_bundle.rb', line 114

def create
  create_dirs
  copy_icon
  create_exec
  create_plist
rescue Errno::EEXIST => e
  logger.error e.message
end

#create_dir(path) ⇒ Object



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

def create_dir(path)
  Dir.mkdir path
  logger.debug "Created dir: #{path}" if verbose
end

#create_dirsObject



66
67
68
# File 'lib/create_bundle.rb', line 66

def create_dirs
  [target_path, contents_dir, resources_dir, macos_dir].each { |dir| create_dir(dir) }
end

#create_execObject



109
110
111
112
# File 'lib/create_bundle.rb', line 109

def create_exec
  copy_script || write_script
  FileUtils.chmod 0o755, macos_dir + 'applet'
end

#create_plistObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/create_bundle.rb', line 75

def create_plist
  target_plist_hash = {
    'CFBundleExecutable' => 'applet',
    'CFBundleIconFile' => 'applet'
  }
  f = File.new(contents_dir + 'Info.plist', 'w')
  f.puts target_plist_hash.to_plist
  f.close
  logger.debug "Created plist file: #{(contents_dir + 'Info.plist')}" if verbose
end

#custom_icon_pathObject



23
24
25
26
27
28
29
30
31
# File 'lib/create_bundle.rb', line 23

def custom_icon_path
  return false unless @custom_icon
  if Pathname(@custom_icon).exist?
    Pathname(@custom_icon)
  else
    puts "Icon file doesn't exist"
    exit_and_cleanup
  end
end

#exit_and_cleanupObject



123
124
125
126
# File 'lib/create_bundle.rb', line 123

def exit_and_cleanup
  FileUtils.rm_rf(target_path)
  exit
end

#icon_fileObject



50
51
52
# File 'lib/create_bundle.rb', line 50

def icon_file
  !/icns$/.match?(plist['CFBundleIconFile']) ? plist['CFBundleIconFile'] + '.icns' : plist['CFBundleIconFile']
end

#icon_pathObject



33
34
35
36
37
38
39
# File 'lib/create_bundle.rb', line 33

def icon_path
  custom_icon_path || app_path + 'Contents' + 'Resources' + icon_file
rescue ArgumentError
  logger.warn 'Problem reading source plist file, probably binary format, falling back to default icon name'
  icon = app_path + 'Contents' + 'Resources' + 'AppIcon.icns'
  icon || exit_and_cleanup
end

#macos_dirObject



62
63
64
# File 'lib/create_bundle.rb', line 62

def macos_dir
  contents_dir + 'MacOS'
end

#plistObject



46
47
48
# File 'lib/create_bundle.rb', line 46

def plist
  Plist.parse_xml(plist_path)
end

#plist_pathObject



41
42
43
44
# File 'lib/create_bundle.rb', line 41

def plist_path
  path = app_path + 'Contents' + 'Info.plist'
  path.exist? ? path : (logger.info("Source doesn't look like an app bundle") && exit)
end

#resources_dirObject



58
59
60
# File 'lib/create_bundle.rb', line 58

def resources_dir
  contents_dir + 'Resources'
end

#write_scriptObject



91
92
93
94
95
96
# File 'lib/create_bundle.rb', line 91

def write_script
  f = File.new(macos_dir + 'applet', 'w')
  f.puts "#!/bin/sh\nopen -a \"#{ARGV[0]}\""
  f.close
  logger.debug "Created exec: #{(macos_dir + 'applet')}" if verbose
end