Class: DeployGate::CommandBuilder

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/deploygate/command_builder.rb

Defined Under Namespace

Classes: NotInternetConnectionError

Constant Summary collapse

PING_URL =
'https://deploygate.com'
LOGIN =
'login'
LOGOUT =
'logout'
DEPLOY =
'deploy'
ADD_DEVICES =
'add-devices'
CONFIG =
'config'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



4
5
6
# File 'lib/deploygate/command_builder.rb', line 4

def arguments
  @arguments
end

Instance Method Details

#check_updatevoid

This method returns an undefined value.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/deploygate/command_builder.rb', line 190

def check_update
  current_version = DeployGate::VERSION

  # check cache
  if DeployGate::Config::CacheVersion.exist?
    data = DeployGate::Config::CacheVersion.read
    if Time.parse(data['check_date']) > 1.day.ago
      # cache available
      latest_version = data['latest_version']
      if Gem::Version.new(latest_version) > Gem::Version.new(current_version)
        show_update_message(latest_version)
      end
    else
      request_gem_update_checker
    end
  else
    request_gem_update_checker
  end
end

#create_error_issue_body(error) ⇒ String

Parameters:

  • error (Exception)

Returns:

  • (String)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/deploygate/command_builder.rb', line 133

def create_error_issue_body(error)
  return <<EOF

# Status
deploygate-cli ver #{DeployGate::VERSION}

# Error message
#{error.message}

# Backtrace
```
#{error.backtrace.join("\n")}
```
EOF
end

#create_issue_url(command, error) ⇒ String

Parameters:

  • command (Symbol)
  • error (Exception)

Returns:

  • (String)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/deploygate/command_builder.rb', line 152

def create_issue_url(command, error)
  title = case command
               when LOGIN
                 I18n.t('command_builder.login.error', e: error.class)
               when LOGOUT
                 I18n.t('command_builder.logout.error', e: error.class)
               when DEPLOY
                 I18n.t('command_builder.deploy.error', e: error.class)
               when ADD_DEVICES
                 I18n.t('command_builder.add_devices.error', e: error.class)
               when CONFIG
                 I18n.t('command_builder.config.error', e: error.class)
             end

  options = {
      :title => title,
      :body  => create_error_issue_body(error),
  }
  GithubIssueRequest::Url.new(options).to_s
end

#error_handling(command, error) ⇒ Object

Parameters:

  • command (Symbol)
  • error (Exception)


175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/deploygate/command_builder.rb', line 175

def error_handling(command, error)
  STDERR.puts HighLine.color(I18n.t('command_builder.error_handling.message', message: error.message), HighLine::RED)

  return if ENV['CI'] # When run ci server
  return if error.kind_of?(DeployGate::NotIssueError)
  puts ''
  if HighLine.agree(I18n.t('command_builder.error_handling.agree')) {|q| q.default = "n"}
    url = create_issue_url(command, error)
    puts I18n.t('command_builder.error_handling.please_open', url: url)
    system('open', url) if Commands::Deploy::Push.openable?
  end
  puts ''
end

#internet_connection?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/deploygate/command_builder.rb', line 237

def internet_connection?
  Net::Ping::HTTP.new(PING_URL).ping?
end

#request_gem_update_checkervoid

This method returns an undefined value.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/deploygate/command_builder.rb', line 211

def request_gem_update_checker
  gem_name = DeployGate.name.downcase
  current_version = DeployGate::VERSION

  checker = GemUpdateChecker::Client.new(gem_name, current_version)
  if checker.update_available
    show_update_message(checker.latest_version)
  end
  cache_data = {
      :latest_version => checker.latest_version,
      :check_date => Time.now
  }
  DeployGate::Config::CacheVersion.write(cache_data)
end

#runObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/deploygate/command_builder.rb', line 35

def run
  setup()

  program :name, I18n.t('command_builder.name')
  program :version,  VERSION
  program :description, I18n.t('command_builder.description')

  command LOGIN do |c|
    c.syntax = 'dg login'
    c.description = I18n.t('command_builder.login.description')
    c.option '--terminal', I18n.t('command_builder.login.terminal')
    c.action do |args, options|
      options.default :terminal => false
      begin
        Commands::Login.run(args, options)
      rescue => e
        error_handling(LOGIN, e)
        raise e
      end
    end
  end

  command DEPLOY do |c|
    c.syntax = 'dg deploy /path/to/app'
    c.description = I18n.t('command_builder.deploy.description')
    c.option '--message STRING', String, I18n.t('command_builder.deploy.message')
    c.option '--user STRING', String, I18n.t('command_builder.deploy.user')
    c.option '--distribution-key STRING', String, I18n.t('command_builder.deploy.distribution_key')
    c.option '--configuration STRING', String, I18n.t('command_builder.deploy.configuration')
    c.option '--scheme STRING', String, I18n.t('command_builder.deploy.scheme')
    c.option '--open', I18n.t('command_builder.deploy.open')
    c.option '--disable_notify', I18n.t('command_builder.deploy.disable_notify')
    c.action do |args, options|
      options.default :message => '', :user => nil, :open => false, 'disable_notify' => false, :command => nil
      begin
        Commands::Deploy.run(args, options)
      rescue => e
        error_handling(DEPLOY, e)
        raise e
      end
    end
  end
  alias_command :'push', :deploy

  command ADD_DEVICES do |c|
    c.syntax = 'dg add-devices'
    c.description = I18n.t('command_builder.add_devices.description')
    c.option '--user STRING', String, I18n.t('command_builder.add_devices.user')
    c.option '--udid STRING', String, I18n.t('command_builder.add_devices.udid')
    c.option '--device-name STRING', String, I18n.t('command_builder.add_devices.device_name')
    c.option '--distribution-key STRING', String, I18n.t('command_builder.add_devices.distribution_key')
    c.option '--configuration STRING', String, I18n.t('command_builder.deploy.configuration')
    c.option '--server', I18n.t('command_builder.add_devices.server.description')
    c.action do |args, options|
      options.default :user => nil, :server => false, :command => 'add_devices'
      begin
        Commands::AddDevices.run(args, options)
      rescue => e
        error_handling(ADD_DEVICES, e)
        raise e
      end
    end
  end

  command LOGOUT do |c|
    c.syntax = 'dg logout'
    c.description = I18n.t('command_builder.logout.description')
    c.action do |args, options|
      begin
        Commands::Logout.run
      rescue => e
        error_handling(LOGOUT, e)
        raise e
      end
    end
  end

  command CONFIG do |c|
    c.syntax = 'dg config'
    c.description = I18n.t('command_builder.config.description')
    c.option '--json', I18n.t('command_builder.config.json')
    c.option '--name STRING', String, I18n.t('command_builder.config.name')
    c.option '--token STRING', String, I18n.t('command_builder.config.token')
    c.action do |args, options|
      begin
        Commands::Config.run(args, options)
      rescue => e
        error_handling(CONFIG, e)
        raise e
      end
    end
  end

  run!
end

#setupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deploygate/command_builder.rb', line 17

def setup
  # set Ctrl-C trap
  Signal.trap(:INT){
    puts ''
    exit 0
  }

  # check internet connection
  unless internet_connection?
    STDERR.puts HighLine.color(I18n.t('command_builder.not_internet_connection_error'), HighLine::RED)
    exit
  end

  # check update
  GithubIssueRequest::Url.config('deploygate', 'deploygate-cli')
  check_update()
end

#show_update_message(latest_version) ⇒ void

This method returns an undefined value.

Parameters:

  • latest_version (String)


228
229
230
231
232
233
234
# File 'lib/deploygate/command_builder.rb', line 228

def show_update_message(latest_version)
  gem_name = DeployGate.name.downcase
  current_version = DeployGate::VERSION
  STDERR.puts ''
  STDERR.puts HighLine.color(I18n.t('command_builder.show_update_message', gem_name: gem_name, latest_version: latest_version, current_version: current_version), HighLine::YELLOW)
  STDERR.puts ''
end