Class: OpencodeTheme::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/opencode_theme/cli.rb

Constant Summary collapse

IGNORE =
%w(config.yml).freeze
DEFAULT_WHITELIST =
%w(configs/ css/ elements/ img/ layouts/ pages/ js/).freeze
TIMEFORMAT =
'%H:%M:%S'.freeze

Instance Method Summary collapse

Instance Method Details

#bootstrap(api_key = nil, password = nil, theme_name = 'default', theme_base = 'default') ⇒ Object



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
# File 'lib/opencode_theme/cli.rb', line 96

def bootstrap(api_key = nil, password = nil, theme_name = 'default', theme_base = 'default')
  OpencodeTheme.config = { api_key: api_key, password: password }
  check_config = OpencodeTheme.check_config
  if check_config[:success]
    say('Configuration [OK]', :green)
  else
    report_error(Time.now, 'Configuration [FAIL]', check_config[:response])
    return
  end
  response = OpencodeTheme.theme_new(theme_base, theme_name)
  if response[:success]
    say("Create #{theme_name} theme on store", :green)
  else
    report_error(Time.now, 'Could not create a new theme', response[:response])
    return
  end
  say("Criando diretório do tema:#{theme_name}", :green)
  empty_directory(theme_name)
  say("Salvando configuração para o tema: #{theme_name}", :green)
  OpencodeTheme.config.merge!(theme_id: response[:response]['theme_id'], preview_url: response[:response]['preview'])
  create_file("#{theme_name}/config.yml", OpencodeTheme.config.to_yaml)
  say("Downloading arquivos do tema #{theme_name}")
  Dir.chdir(theme_name)
  download
end

#cleanObject



84
85
86
87
88
89
90
91
92
# File 'lib/opencode_theme/cli.rb', line 84

def clean
  OpencodeTheme.config
  response = OpencodeTheme.clean
  if response[:success]
    say('Clean cache [OK]\n', :green)
  else
    report_error(Time.now, 'Clean cache [FAIL]', response[:response])
  end
end

#components(action = 'list', name = nil, user = 'tray-desenvolvedores', origin = 'github') ⇒ Object



33
34
35
36
37
# File 'lib/opencode_theme/cli.rb', line 33

def components(action = 'list', name = nil, user = 'tray-desenvolvedores', origin = 'github')
  return show_list_components if action == 'list'
  return install_component(name, user, origin) if action == 'install' && !name.nil?
  return components_install if action == 'install' && name.nil?
end

#configure(api_key = nil, password = nil, theme_id = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opencode_theme/cli.rb', line 40

def configure(api_key = nil, password = nil, theme_id = nil)
  config = { api_key: api_key, password: password, theme_id: theme_id }
  if api_key.nil? || password.nil? || theme_id.nil?
    response = {}
    response['message'] = 'necessário informar api_key e password e theme_id.'
    return report_error(Time.now, 'Configuração [FALHOU]', response)
  end
  OpencodeTheme.config = config
  response = OpencodeTheme.check_config
  if response[:success]
    config[:preview_url] = response[:response]['preview']
    create_file('config.yml', config.to_yaml, force: true)
    say('Configuration [OK]', :green)
  else
    report_error(Time.now, 'Configuração [FALHOU]', response[:response])
  end
end

#download(*keys) ⇒ Object



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
# File 'lib/opencode_theme/cli.rb', line 132

def download(*keys)
  assets = if keys.empty?
             say('Aguarde enquanto listamos os arquivos...', :green)
             start_time = Time.now
             assets_list = OpencodeTheme.asset_list 
             total_assets = assets_list.count
             assets_list
           else
             [{ asset: keys.first, public_url: nil }]
           end
  say("encontrado: #{total_assets} arquivos" , :green)
  say("o tempo de download depende da quantidade de arquivos, por favor aguarde:" , :green)
  start_time = Time.now
  if assets.count > 1
    assets.to_enum.with_index(1).map do |file, accumator| 
      download_asset_uniq(file[:asset], file[:public_url])
      say("[#{total_assets}/#{accumator}] Downloaded: #{file[:asset]}", :green)
    end
    finish_time = Time.now 
    elapsed_seconds = ((finish_time.to_f - start_time.to_f)).to_i
    say("tempo total de download: #{elapsed_seconds.to_i}s")
  else
    download_asset_uniq(assets.first[:asset], assets.first[:public_url])
    say("Downloaded: #{assets.first[:asset]}", :green)
  end
end

#listObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/opencode_theme/cli.rb', line 59

def list
  config = OpencodeTheme.config
  response = OpencodeTheme.list
  if response[:response]['authentication'] == false
    response = {}
    response['message'] = 'necessário autenticação'
    return report_error(Time.now, 'Configuração [FALHOU]', response)
  end
  if response[:success]
    say("\n")
    response[:response]['themes'].each do |theme|
      color = theme['published'] == '1' ? :green : :red
      say('Theme name:   ', color)
      say("#{theme['name']}\n", color)
      say('Theme ID:     ', color)
      say("#{theme['id']}\n", color)
      say('Theme status: ', color)
      say("#{(theme['published'])}\n\n", color)
    end
  else
    report_error(Time.now, 'Could not list now', response[:response])
  end
end

#open(*keys) ⇒ Object



123
124
125
126
127
# File 'lib/opencode_theme/cli.rb', line 123

def open(*keys)
  if Launchy.open opencode_theme_url
    say('Done.', :green)
  end
end

#remove(*keys) ⇒ Object



171
172
173
174
175
176
# File 'lib/opencode_theme/cli.rb', line 171

def remove(*keys)
  keys.each do |key|
    delete_asset(key, options['quiet'])
  end
  say('Done.', :green) unless options['quiet']
end

#systeminfoObject



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/opencode_theme/cli.rb', line 201

def systeminfo
  ruby_version = RUBY_VERSION.to_s
  ruby_version += "-p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL
  say( 'Ruby: v' + ruby_version.to_s)
  say('OpencodeTheme: v' + OpencodeTheme::VERSION.to_s)
  say('Operating System: ' + RUBY_PLATFORM)
  %w(HTTParty Launchy).each do |lib|
    require "#{lib.downcase}/version"
    say("#{lib}: v" + Kernel.const_get("#{lib}::VERSION"))
  end
end

#upload(*keys) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/opencode_theme/cli.rb', line 161

def upload(*keys)
  assets = keys.empty? ? local_assets_list : keys
  assets.each do |asset|
    send_asset(asset.to_s, options['quiet'])
  end
  say('Done.', :green) unless options['quiet']
end

#watchObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/opencode_theme/cli.rb', line 181

def watch
  watcher do |filename, event|

    filename = filename.sub(Dir.pwd.to_s + '/', '')
    next if components_temporary_file?(filename)
    next unless file?(filename)
    action =
      if [:updated, :created].include?(event)
        :send_asset
      elsif event == :deleted
        :delete_asset
      else
        say("This is not file [#{filename}]", :blue)
        raise NotImplementedError, "Evento Desconhecido -- #{event} -- #{filename}"
      end
    send(action, filename, options['quiet'])
  end
end