module Fanforce::CLI::Utils extend self def self.included(base) base.extend(self) end def divider(template) line_width = 150 line_width = line_width - $1.size if template =~ /(\s+)[=-]+/ template.gsub('+', "\n").gsub(/[-]+/, '-' * line_width).gsub(/[=]+/, '=' * line_width) end def log(msg='') puts msg end def error(msg, command=nil) puts divider '+-+' puts 'ERROR '.format(:red,:bold) + msg puts divider '-++' exit end def fmt(text, *args) "#{fmt_start(*args)}#{text}#{fmt_end}" end def fmt_start(*args) effect = if args.include?(:bold) then 1 elsif args.include?(:underline) then 4 else 0 end color = if args.include?(:red) then 31 elsif args.include?(:green) then 32 elsif args.include?(:magenta) then 35 else 39 end "\033[#{effect};#{color}m" end def fmt_end "\033[0m" end def confirm(msg, exit_unless_confirmed=true) print "#{msg} [y/n]: " input = $stdin.gets.strip confirmed = !input.downcase.include?('n') (!confirmed && exit_unless_confirmed) ? exit(0) : confirmed end def prompt(msg, required=false) print "#{msg}" response = $stdin.gets.strip (required && response.blank?) ? prompt(msg, required) : response end def require_gem(gem, file_to_require, retries=0) require file_to_require rescue LoadError => e raise if retries > 0 puts `gem install #{gem}` Gem.clear_paths require_gem(gem, file_to_require, retries+1) end def find_cli_type(home_dir) dir_names = home_dir.split('/') types = { directory_of_internals: false, single_internal: false, directory_of_apps: false, single_app: false } types[:single_app] = true if File.exists?("#{home_dir}/config.json") and File.exists?("#{home_dir}/../.fanforce-app-factory") types[:directory_of_apps] = true if File.exists?("#{home_dir}/.fanforce-app-factory") if [dir_names[-1],dir_names[-2]].include?('Fanforce') && !File.exists?("#{home_dir}/.git") && !File.exists?("#{home_dir}/.fanforce-app-factory") types[:directory_of_internals] = true end if [dir_names[-1],dir_names[-2],dir_names[-2]].include?('Fanforce') and File.exists?("#{home_dir}/.git") and !File.exists?("#{home_dir}/../.fanforce-app-factory") types[:single_internal] = true end found_types = types.inject([]) {|found_types, (key,bool)| bool ? found_types << key : found_types } if found_types.size != 1 divider '+-' (found_types.size == 0) ? log('COULD NOT DEDUCE CLI TYPE') : log("CONFLICTING CLI TYPES: #{found_types.to_s}") divider '+-' exit end return found_types.first end end class String def format(*args) Fanforce::CLI::Utils.fmt(self, *args) end end