Class: TRMNLP::Commands::Push

Inherits:
Base
  • Object
show all
Defined in:
lib/trmnlp/commands/push.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

ZIP_PATH =

Build the archive under the system temp dir, not the working directory — a relative path would litter the user’s project (or the repo root, in specs) if a run is interrupted before the ensure-cleanup.

File.join(Dir.tmpdir, "trmnlp-upload-#{Process.pid}.zip").freeze

Instance Method Summary collapse

Methods inherited from Base

#initialize, options_from, run

Constructor Details

This class inherits a constructor from TRMNLP::Commands::Base

Instance Method Details

#callObject



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
# File 'lib/trmnlp/commands/push.rb', line 20

def call
  context.validate!
  authenticate!

  is_new = false

  api = APIClient.new(config)

  plugin_settings_id = options.id || config.plugin.id
  if plugin_settings_id.nil?
    reporter.info 'Creating a new plugin on the server...'
    response = api.post_plugin_setting(name: 'New TRMNLP Plugin', plugin_id: 37) # hardcoded id for private_plugin
    plugin_settings_id = response.dig('data', 'id')
    is_new = true
  end

  unless is_new || options.force
    answer = prompt('Plugin settings on the server will be overwritten. Are you sure? (y/n) ').downcase
    raise Aborted, 'aborting' unless %w[y yes].include?(answer)
  end

  Zip::File.open(ZIP_PATH, create: true) do |zip_file|
    paths.src_files.each do |file|
      zip_file.add(File.basename(file), file)
    end
  end

  response = api.post_plugin_setting_archive(plugin_settings_id, ZIP_PATH)
  paths.plugin_config.write(response.dig('data', 'settings_yaml'))

  size = File.size(ZIP_PATH)

  reporter.info <<~HEREDOC
    Uploaded plugin (#{size} bytes)
    Dashboard: #{config.app.edit_plugin_settings_uri(plugin_settings_id)}
  HEREDOC

  if is_new
    reporter.info <<~HEREDOC

      IMPORTANT! Don't forget to add it to your device playlist!

          #{config.app.playlists_uri}
    HEREDOC
  end
rescue StandardError
  if is_new && plugin_settings_id
    reporter.info 'Error during creation, cleaning up...'
    api.delete_plugin_setting(plugin_settings_id)
  end

  raise
ensure
  FileUtils.rm_f(ZIP_PATH)
end