Class: Lhj::Command::Init

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/init.rb

Overview

sync config

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #initialize, #run, #stop

Constructor Details

This class inherits a constructor from Lhj::Command

Instance Method Details

#down_load_urlsObject



19
20
21
22
23
24
# File 'lib/lhj/command/init.rb', line 19

def down_load_urls
  %w[http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/localizable_config.yml
     http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/oss_config.yml
     http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/yapi.yml
     http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/zh2hant.yml]
end

#download_fileObject



42
43
44
45
46
47
48
49
# File 'lib/lhj/command/init.rb', line 42

def download_file
  down_load_urls.each do |url|
    file_name = file_name_with_url(url)
    file_path = File.join(target_folder, file_name)
    save_file(url, file_path) unless File.exist?(file_path)
  end
  puts "工具初始化完成 \n"
end

#file_name_with_url(url) ⇒ Object



38
39
40
# File 'lib/lhj/command/init.rb', line 38

def file_name_with_url(url)
  url.scan(%r{/([^/]+)}).flatten.last
end

#handleObject



14
15
16
17
# File 'lib/lhj/command/init.rb', line 14

def handle
  FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
  download_file
end

#save_file(url, target) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/lhj/command/init.rb', line 30

def save_file(url, target)
  http_client = Faraday.new do |c|
    c.adapter Faraday.default_adapter
  end
  response = http_client.get(url)
  File.open(target, 'wb') { |fp| fp.write(response.body) }
end

#target_folderObject



26
27
28
# File 'lib/lhj/command/init.rb', line 26

def target_folder
  Lhj::Config.instance.home_dir
end