Class: ZendeskAppsTools::Command

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, ZendeskAppsSupport, CommandHelpers
Defined in:
lib/zendesk_apps_tools/command.rb

Constant Summary collapse

SHARED_OPTIONS =
{
  ['path', '-p'] => './',
  clean: false
}
DEFAULT_SERVER_PATH =
'./'
DEFAULT_CONFIG_PATH =
'./settings.yml'
DEFAULT_SERVER_PORT =
4567

Constants included from APIConnection

APIConnection::FULL_URL, APIConnection::URL_TEMPLATE

Constants included from Cache

ZendeskAppsTools::Cache::CACHE_FILE_NAME

Instance Method Summary collapse

Methods included from PackageHelper

#app_package, #zip

Methods included from Directory

#app_dir, #prompt_new_app_dir, #tmp_dir

Methods included from Deploy

#check_job, #check_status, #deploy_app, #find_app_id, #upload

Methods included from APIConnection

#get_connection, #prepare_api_auth

Methods included from Common

#api_request, #get_password_from_stdin, #get_value_from_stdin, #say_error_and_exit

Methods included from Cache

#cache_path, #clear_cache, #fetch_cache, #save_cache

Instance Method Details

#cleanObject



107
108
109
110
111
112
113
# File 'lib/zendesk_apps_tools/command.rb', line 107

def clean
  setup_path(options[:path])

  return unless File.exist?(Pathname.new(File.join(app_dir, 'tmp')).to_s)

  FileUtils.rm(Dir["#{tmp_dir}/app-*.zip"])
end

#createObject



166
167
168
169
170
171
172
173
174
175
# File 'lib/zendesk_apps_tools/command.rb', line 166

def create
  clear_cache
  @command = 'Create'

  unless options[:zipfile]
    app_name = JSON.parse(File.read(File.join options[:path], 'manifest.json'))['name']
  end
  app_name ||= get_value_from_stdin('Enter app name:')
  deploy_app(:post, '/api/v2/apps.json',  name: app_name)
end

#newObject



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
# File 'lib/zendesk_apps_tools/command.rb', line 36

def new
  enter = ->(variable) { "Enter this app author's #{variable}:\n" }
  invalid = ->(variable) { "Invalid #{variable}, try again:" }
  @author_name  = get_value_from_stdin(enter.call('name'),
                                       error_msg: invalid.call('name'))
  @author_email = get_value_from_stdin(enter.call('email'),
                                       valid_regex: /^.+@.+\..+$/,
                                       error_msg: invalid.call('email'))
  @author_url   = get_value_from_stdin(enter.call('url'),
                                       valid_regex: %r{^https?://.+$},
                                       error_msg: invalid.call('url'),
                                       allow_empty: true)
  @app_name     = get_value_from_stdin("Enter a name for this new app:\n",
                                       error_msg: invalid.call('app name'))

  @iframe_location = if options[:'iframe-only']
                       iframe_uri_text = 'Enter your iFrame URI or leave it blank to use'\
                                         " a default local template page:\n"
                       value = get_value_from_stdin(iframe_uri_text, allow_empty: true)
                       value == '' ? 'assets/iframe.html' : value
                     else
                       '_legacy'
                     end

  prompt_new_app_dir

  skeleton = options[:'iframe-only'] ? 'app_template_iframe' : 'app_template'
  is_custom_iframe = options[:'iframe-only'] && @iframe_location != 'assets/iframe.html'
  directory_options = is_custom_iframe ? { exclude_pattern: /iframe.html/ } : {}
  directory(skeleton, @app_dir, directory_options)
end

#packageObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zendesk_apps_tools/command.rb', line 91

def package
  return false unless invoke(:validate, [])

  setup_path(options[:path])
  archive_path = File.join(tmp_dir, "app-#{Time.now.strftime('%Y%m%d%H%M%S')}.zip")

  archive_rel_path = relative_to_original_destination_root(archive_path)

  zip archive_path

  say_status 'package', "created at #{archive_rel_path}"
  true
end

#server(*app_paths) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/zendesk_apps_tools/command.rb', line 124

def server(*app_paths)
  if !app_paths.empty?
    if options[:path] != DEFAULT_SERVER_PATH
      say_error_and_exit "please either use -p or list the directory structure directly"
    end

    if options[:config] != DEFAULT_CONFIG_PATH
      say_error_and_exit "cannot use -c in combination with multiple apps"
    end

    if !options[:app_id].nil?
      say_error_and_exit "cannot set app_id in combination with multiple apps"
    end
  else
    app_paths << options[:path]
  end

  apps = app_paths.map do | path |
    package = ZendeskAppsSupport::Package.new(path)
    settings_helper = ZendeskAppsTools::Settings.new

    settings_file_path = settings_helper.find_settings_file(path)
    settings = settings_file_path ? {} : settings_helper.get_settings_from_user_input(self, package.manifest_json['parameters'])

    {
      package: package,
      settings_file_path: settings_file_path,
      settings: settings
    }
  end

  require 'zendesk_apps_tools/server'
  ZendeskAppsTools::Server.tap do |server|
    server.set :port, options[:port]
    server.set :apps, apps
    server.run!
  end
end

#updateObject



180
181
182
183
184
185
186
187
188
189
# File 'lib/zendesk_apps_tools/command.rb', line 180

def update
  clear_cache
  @command = 'Update'

  app_id = fetch_cache('app_id') || find_app_id
  unless /\d+/ =~ app_id.to_s
    say_error_and_exit "App id not found\nPlease try running command with --clean or check your internet connection"
  end
  deploy_app(:put, "/api/v2/apps/#{app_id}.json", {})
end

#validateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zendesk_apps_tools/command.rb', line 70

def validate
  setup_path(options[:path])
  errors = app_package.validate(marketplace: false)
  valid = errors.none?

  if valid
    app_package.warnings.each { |w| say w.to_s, :yellow }
    say_status 'validate', 'OK'
  else
    errors.each do |e|
      say_status 'validate', e.to_s
    end
  end

  @destination_stack.pop if options[:path]
  exit 1 unless valid
  true
end