Module: WebExtNativeAppPacker

Defined in:
lib/web-ext-native-app-packer.rb

Defined Under Namespace

Modules: Helper

Constant Summary collapse

TARGET_PATH_OSX =
{
  chrome: {
    system: '/Library/Google/Chrome/NativeMessagingHosts',
    user: '$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts'
  },
  chromium: {
    system: '/Library/Application Support/Chromium/NativeMessagingHosts',
    user: '$HOME/Library/Application Support/Chromium/NativeMessagingHosts'
  },
  firefox: {
    system: '/Library/Application Support/Mozilla/NativeMessagingHosts',
    user: '$HOME/Library/Application Support/Mozilla/NativeMessagingHosts'
  }
}
TARGET_PATH_LINUX =
{
  chrome: {
    system: '/etc/opt/chrome/native-messaging-hosts',
    user: '$HOME/.config/google-chrome/NativeMessagingHosts'
  },
  chromium: {
    system: '/etc/chromium/native-messaging-hosts',
    user: '$HOME/.config/chromium/NativeMessagingHosts'
  },
  firefox: {
    system: '/usr/lib64/mozilla/native-messaging-hosts',
    user: '$HOME/.mozilla/native-messaging-hosts'
  }
}
TARGET_PATH_WINDOWS =
{
  chrome: {
    system: 'HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts',
    user: 'HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts'
  },
  chromium: {
    system: 'HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts',
    user: 'HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts'
  },
  firefox: {
    system: 'HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\NativeMessagingHosts',
    user: 'HKEY_CURRENT_USER\SOFTWARE\Mozilla\NativeMessagingHosts'
  }
}

Class Method Summary collapse

Class Method Details

.copy_project_file(input) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/web-ext-native-app-packer.rb', line 62

def self.copy_project_file(input)
  input_path = File.join(input.project_dir, '*')
  each_item do |platform, browser_name|
    output_path = File.join(
      input.output_dir,
      [platform, browser_name].join('-'),
    )
    `cp -r #{input_path} #{output_path}`
  end
end

.each_itemObject



199
200
201
202
203
204
205
# File 'lib/web-ext-native-app-packer.rb', line 199

def self.each_item
  ['osx', 'linux', 'windows'].each do |platform|
    ['chrome', 'chromium', 'firefox'].each do |browser_name|
      yield platform, browser_name
    end
  end
end

.get_target_path(platform, browser_name) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/web-ext-native-app-packer.rb', line 140

def self.get_target_path(platform, browser_name)
  dict = \
    case platform
    when 'osx'    then TARGET_PATH_OSX
    when 'linux'  then TARGET_PATH_LINUX
    when 'windows'then TARGET_PATH_WINDOWS
    end
  dict[browser_name.to_sym]
end

.perform(input) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/web-ext-native-app-packer.rb', line 51

def self.perform(input)
  render_manifest(input)
  render_app_loader(input)
  render_unix_like_install_script(input)
  render_unix_like_uninstall_script(input)
  render_windows_install_script(input)
  render_windows_uninstall_script(input)
  copy_project_file(input)
  zip_folds(input)
end

.render_app_loader(input) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/web-ext-native-app-packer.rb', line 151

def self.render_app_loader(input)
  erb_path = File.expand_path('../template/app-loader.bat.erb', __FILE__)
  each_item do |platform, browser_name|
    if platform == 'windows'
      v = OpenStruct.new({
        execute_cmd: input.execute_cmd,
        app_path: input.app_path
      })
      out_path = File.join(
        input.output_dir,
        [platform, browser_name].join('-'),
        'app_loader.bat'
      )
      Helper.render(v, erb_path, out_path)
    end
  end
end

.render_manifest(input) ⇒ Object



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
# File 'lib/web-ext-native-app-packer.rb', line 169

def self.render_manifest(input)
  erb_path = File.expand_path('../template/manifest.json.erb', __FILE__)
  each_item do |platform, browser_name|
    v = OpenStruct.new({
      app_name: input.app_name,
      app_description: input.app_description
    })
    if browser_name == 'firefox'
      v.white_list_key = 'allowed_extensions'
      v.extension_identify = input['extension_id']
    else
      v.white_list_key = 'allowed_origins'
      v.extension_identify = input['extension_origin']
    end

    if platform == 'windows'
      v.app_path = 'app_loader.bat'
    else
      v.app_path = 'APP_PATH'
    end

    out_path = File.join(
      input.output_dir,
      [platform, browser_name].join('-'),
      'manifest.json'
    )
    Helper.render(v, erb_path, out_path)
  end
end

.render_script(input, erb_path:, platforms:, filename:) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/web-ext-native-app-packer.rb', line 117

def self.render_script(input, erb_path:, platforms:, filename:)
  each_item do |platform, browser_name|
    if platforms.include?(platform)
      target_path = get_target_path(platform, browser_name)
      v = OpenStruct.new({
        app_name: input.app_name,
        app_path: input.app_path,
        target_path_system: target_path[:system],
        target_path_user: target_path[:user]
      })
      out_path = File.join(
        input.output_dir,
        [platform, browser_name].join('-'),
        filename
      )
      Helper.render(v, erb_path, out_path)
      if out_path.end_with?('.sh')
        `chmod a+x #{out_path}`
      end
    end
  end
end

.render_unix_like_install_script(input) ⇒ Object



89
90
91
92
93
94
# File 'lib/web-ext-native-app-packer.rb', line 89

def self.render_unix_like_install_script(input)
  render_script(input,
    erb_path: File.expand_path('../template/install-unix-like.sh.erb', __FILE__),
    platforms: ['osx', 'linux'],
    filename: 'install.sh')
end

.render_unix_like_uninstall_script(input) ⇒ Object



96
97
98
99
100
101
# File 'lib/web-ext-native-app-packer.rb', line 96

def self.render_unix_like_uninstall_script(input)
  render_script(input,
    erb_path: File.expand_path('../template/uninstall-unix-like.sh.erb', __FILE__),
    platforms: ['osx', 'linux'],
    filename: 'uninstall.sh')
end

.render_windows_install_script(input) ⇒ Object



103
104
105
106
107
108
# File 'lib/web-ext-native-app-packer.rb', line 103

def self.render_windows_install_script(input)
  render_script(input,
    erb_path: File.expand_path('../template/install-windows.bat.erb', __FILE__),
    platforms: ['windows'],
    filename: 'install.bat')
end

.render_windows_uninstall_script(input) ⇒ Object



110
111
112
113
114
115
# File 'lib/web-ext-native-app-packer.rb', line 110

def self.render_windows_uninstall_script(input)
  render_script(input,
    erb_path: File.expand_path('../template/uninstall-windows.bat.erb', __FILE__),
    platforms: ['windows'],
    filename: 'uninstall.bat')
end

.zip_folds(input) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/web-ext-native-app-packer.rb', line 73

def self.zip_folds(input)
  each_item do |platform, browser_name|
    name = input.app_name.gsub('_', '-').gsub('.', '-')
    input_fold = File.join(
      input.output_dir,
      [platform, browser_name].join('-'),
    )
    archive_path = File.join(
      input.output_dir,
      [[name, platform, browser_name].join('-'), 'zip'].join('.')
    )
    pack_sh = File.expand_path('../pack.sh', __FILE__)
    `#{pack_sh} #{input_fold} #{archive_path}`
  end
end