class RestoreadyTheme::Cli
Constants
- DEFAULT_WHITELIST
- TIMEFORMAT
Public Instance Methods
bootstrap(api_key=nil, api_url=nil, site_url = nil, theme_name = 'starter')
click to toggle source
# File lib/restoready_theme/cli.rb, line 46 def bootstrap(api_key=nil, api_url=nil, site_url = nil, theme_name = 'starter') config = {:api_key => api_key, :api_url => api_url, :site_url => site_url} say("Creating directory named #{theme_name}", :green) if File.directory?(theme_name) say("Directory #{theme_name} existing, choose another theme name", :red) exit else empty_directory(theme_name) end say("Registering #{theme_name} theme on #{site_url}", :green) theme_info = RestoreadyTheme::HttpClient.new(api_url, api_key).install_starter(theme_name) say("Saving configuration to #{theme_name}", :green) config.merge!(theme_id: theme_info['id']) create_file("#{theme_name}/config.yml", config.to_yaml) say("Downloading #{theme_name} assets from RestoReady") Dir.chdir(theme_name) download() end
configure(api_key=nil, api_url=nil, site_url = nil, theme_id=nil)
click to toggle source
# File lib/restoready_theme/cli.rb, line 39 def configure(api_key=nil, api_url=nil, site_url = nil, theme_id=nil) config = {api_key: api_key, api_url: api_url, site_url: site_url, theme_id: theme_id} create_file('config.yml', config.to_yaml) check end
download(*keys)
click to toggle source
# File lib/restoready_theme/cli.rb, line 79 def download(*keys) assets = keys.empty? ? http_client.asset_list : keys if options['exclude'] assets = assets.delete_if { |asset| asset =~ Regexp.new(options['exclude']) } end assets.each do |asset| download_asset(asset) say("Downloaded: #{asset}", :green) unless options['quiet'] end say("Done.", :green) unless options['quiet'] end
open(*keys)
click to toggle source
# File lib/restoready_theme/cli.rb, line 70 def open(*keys) if Launchy.open restoready_theme_url say("Done.", :green) end end
remove(*keys)
click to toggle source
# File lib/restoready_theme/cli.rb, line 126 def remove(*keys) check keys.each do |key| delete_asset(key, options['quiet']) end say("Done.", :green) unless options['quiet'] end
replace(*keys)
click to toggle source
# File lib/restoready_theme/cli.rb, line 106 def replace(*keys) check say("Are you sure you want to completely replace your restoready theme assets? This is not undoable.", :yellow) if ask("Continue? (Y/N): ") == "Y" # only delete files on remote that are not present locally # files present on remote and present locally get overridden anyway remote_assets = keys.empty? ? (http_client.asset_list - local_assets_list) : keys remote_assets.each do |asset| delete_asset(asset, options['quiet']) unless http_client.ignore_files.any? { |regex| regex =~ asset } end local_assets = keys.empty? ? local_assets_list : keys local_assets.each do |asset| send_asset(asset, options['quiet']) end say("Done.", :green) unless options['quiet'] end end
systeminfo()
click to toggle source
# File lib/restoready_theme/cli.rb, line 157 def systeminfo ruby_version = "#{RUBY_VERSION}" ruby_version += "-p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL puts "Ruby: v#{ruby_version}" puts "Operating System: #{RUBY_PLATFORM}" %w(Thor Listen HTTParty Launchy).each do |lib| require "#{lib.downcase}/version" puts "#{lib}: v" + Kernel.const_get("#{lib}::VERSION") end end
upload(*keys)
click to toggle source
# File lib/restoready_theme/cli.rb, line 95 def upload(*keys) check assets = keys.empty? ? local_assets_list : keys assets.each do |asset| send_asset(asset, options['quiet']) end say("Done.", :green) unless options['quiet'] end
watch()
click to toggle source
# File lib/restoready_theme/cli.rb, line 137 def watch check puts "Watching current folder: #{Dir.pwd}" watcher do |filename, event| filename = filename.gsub("#{Dir.pwd}/", '') next if next_watch?(filename, event) action = if [:changed, :new].include?(event) :send_asset elsif event == :delete :delete_asset else raise NotImplementedError, "Unknown event -- #{event} -- #{filename}" end send(action, filename, options['quiet']) end end
Protected Instance Methods
config()
click to toggle source
# File lib/restoready_theme/cli.rb, line 170 def config @config ||= YAML.load_file 'config.yml' end
http_client()
click to toggle source
# File lib/restoready_theme/cli.rb, line 180 def http_client @http_client = RestoreadyTheme::HttpClient.new end
restoready_theme_url()
click to toggle source
# File lib/restoready_theme/cli.rb, line 174 def restoready_theme_url url = config[:site_url] ||= "" url += "/fr?preview_theme_id=#{config[:theme_id]}" if config[:theme_id] && config[:theme_id].to_i > 0 url end
Private Instance Methods
binary_file?(path)
click to toggle source
# File lib/restoready_theme/cli.rb, line 287 def binary_file?(path) mime = MimeMagic.by_path(path) say("'#{path}' is an unknown file-type, uploading asset as binary", :yellow) if mime.nil? && ENV['TEST'] != 'true' mime.nil? || (!mime.text? && mime.subtype != "svg+xml") end
check()
click to toggle source
# File lib/restoready_theme/cli.rb, line 335 def check response = show_during("[#{timestamp}] Configuration check") do http_client.check_theme end if !response.success? report_error(Time.now, "Configuration [FAIL]", response) exit elsif JSON.parse(response.body)['tenant']['theme_id'] == config[:theme_id].to_i say("Configuration [FAIL] : Can't edit an active theme", :red) exit end end
delete_asset(key, quiet=false)
click to toggle source
# File lib/restoready_theme/cli.rb, line 259 def delete_asset(key, quiet=false) return unless valid?(key) data = {key: key} asset_getting = http_client.get_asset(key) if asset_getting['response'].success? data.merge!(id: asset_getting['id']) else report_error(Time.now, "#{key} not found.", asset_getting['response']) end response = show_during("[#{timestamp}] Removing: #{key}", quiet) do http_client.delete_asset(data) end if response.success? say("[#{timestamp}] Removed: #{key}", :green) unless quiet else report_error(Time.now, "Could not deleted #{key}", response) end end
download_asset(key)
click to toggle source
# File lib/restoready_theme/cli.rb, line 205 def download_asset(key) return unless valid?(key) asset = http_client.get_asset(key) content = asset['value'] format = "w+b" content = Base64.decode64(content) if binary_file?(key) FileUtils.mkdir_p(File.dirname(key)) File.open(key, format) {|f| f.write content} if content end
errors_from_response(response)
click to toggle source
# File lib/restoready_theme/cli.rb, line 298 def errors_from_response(response) object = {status: response.status} errors = JSON.parse(response.body)['errors'] object[:errors] = case errors when NilClass '' when String errors.strip else errors.values.join(", ") end object.delete(:errors) if object[:errors].length <= 0 object end
local_assets_list()
click to toggle source
# File lib/restoready_theme/cli.rb, line 192 def local_assets_list local_files.reject do |p| @permitted_files ||= (DEFAULT_WHITELIST | http_client.whitelist_files).map{|pattern| Regexp.new(pattern)} @permitted_files.none? { |regex| regex =~ p } || http_client.ignore_files.any? { |regex| regex =~ p } end end
local_files()
click to toggle source
# File lib/restoready_theme/cli.rb, line 199 def local_files Dir.glob(File.join('**', '*')).reject do |f| File.directory?(f) end end
next_watch?(filename, event)
click to toggle source
# File lib/restoready_theme/cli.rb, line 326 def next_watch?(filename, event) pref = filename.split('/')[0] local_assets_list_tmp = local_assets_list local_assets_list_tmp.push(filename) if event == :delete return true unless local_assets_list_tmp.include?(filename) (pref != 'assets' && pref != 'snippets' && event == :new) || (pref != 'assets' && pref != 'snippets' && event == :delete) end
report_error(time, message, response)
click to toggle source
# File lib/restoready_theme/cli.rb, line 293 def report_error(time, message, response) say("[#{timestamp(time)}] Error: #{message}", :red) say("Error Details: #{errors_from_response(response)}", :yellow) end
send_asset(asset, quiet=false)
click to toggle source
# File lib/restoready_theme/cli.rb, line 216 def send_asset(asset, quiet=false) return unless valid?(asset) data = {key: asset} content = File.read(asset) asset_getting = http_client.get_asset(asset) if asset_getting['response'].success? data.merge!(id: asset_getting['id']) end if binary_file?(asset) || http_client.is_binary_data?(content) content = File.open(asset, "rb") { |io| io.read } data.merge!(value: Base64.encode64(content), content_type: MimeMagic.by_path(asset).type) else data.merge!(value: content, content_type: MimeMagic.by_path(asset).type) end update_response = show_during("[#{timestamp}] Uploading: #{asset}", quiet) do http_client.update_asset(data) end if update_response.success? say("[#{timestamp}] Uploaded: #{asset}", :green) unless quiet return end if !http_client.is_creatable?(asset) || update_response.status != 404 report_error(Time.now, "Could not upload #{asset}", update_response) return end create_response = show_during("[#{timestamp}] Creating: #{asset}", quiet) do http_client.create_asset(data) end if create_response.success? say("[#{timestamp}] #{asset} create", :green) unless quiet return end report_error(Time.now, "Could not created #{asset}", create_response) rescue Errno::ENOENT say("[#{timestamp}] #{asset} not found in the local repository", :red) exit end
show_during(message = '', quiet = false) { || ... }
click to toggle source
# File lib/restoready_theme/cli.rb, line 315 def show_during(message = '', quiet = false, &block) print(message) unless quiet result = yield print("\r#{' ' * message.length}\r") unless quiet result end
timestamp(time = Time.now)
click to toggle source
# File lib/restoready_theme/cli.rb, line 322 def timestamp(time = Time.now) time.strftime(TIMEFORMAT) end
valid?(key)
click to toggle source
# File lib/restoready_theme/cli.rb, line 280 def valid?(key) return true if DEFAULT_WHITELIST.include?(key.split('/').first + "/") say("'#{key}' is not in a valid file for theme uploads", :yellow) say("Files need to be in one of the following subdirectories: #{DEFAULT_WHITELIST.join(' ')}", :yellow) false end
watcher() { |filename, event| ... }
click to toggle source
# File lib/restoready_theme/cli.rb, line 186 def watcher FileWatcher.new(Dir.pwd).watch() do |filename, event| yield(filename, event) end end