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(path, target_path = nil, custom_script = nil, custom_icon = nil, bare = false) ⇒ Base



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

def initialize(path, target_path = nil, custom_script = nil, custom_icon = nil, bare = false)
  @logger = Logger.new(STDOUT)
  @app_path = Pathname(path)
  @custom_icon = custom_icon
  @custom_script = custom_script
  if bare
    target_path = path
  else
    (logger.info("Source doesn't look like an app bundle") && exit) unless plist_path.exist?
  end
  @target_path = target_path ? Pathname(target_path) : 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



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

def contents_dir
  target_path + 'Contents'
end

#copy_iconObject



89
90
91
92
# File 'lib/create_bundle.rb', line 89

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

#copy_scriptObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/create_bundle.rb', line 102

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



117
118
119
120
121
122
123
124
# File 'lib/create_bundle.rb', line 117

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

#create_dir(path) ⇒ Object



73
74
75
76
# File 'lib/create_bundle.rb', line 73

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

#create_dirsObject



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

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

#create_execObject



113
114
115
# File 'lib/create_bundle.rb', line 113

def create_exec
  copy_script || write_script
end

#create_plistObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/create_bundle.rb', line 78

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



27
28
29
30
31
32
33
34
35
# File 'lib/create_bundle.rb', line 27

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



126
127
128
129
# File 'lib/create_bundle.rb', line 126

def exit_and_cleanup
  FileUtils.rm_rf(target_path)
  exit
end

#icon_fileObject



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

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

#icon_pathObject



37
38
39
40
41
42
43
# File 'lib/create_bundle.rb', line 37

def 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



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

def macos_dir
  contents_dir + 'MacOS'
end

#plistObject



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

def plist
  Plist.parse_xml(plist_path)
end

#plist_pathObject



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

def plist_path
  app_path + 'Contents' + 'Info.plist'
end

#resources_dirObject



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

def resources_dir
  contents_dir + 'Resources'
end

#write_scriptObject



94
95
96
97
98
99
100
# File 'lib/create_bundle.rb', line 94

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