lib/leap_cli/util.rb in leap_cli-1.8.1 vs lib/leap_cli/util.rb in leap_cli-1.9

- old
+ new

@@ -8,10 +8,14 @@ module Util extend self @@exit_status = nil + def log(*args, &block) + LeapCli.log(*args, &block) + end + ## ## QUITTING ## def exit_status(code=nil) @@ -34,27 +38,26 @@ end # # exit with error code and with a message that we are bailing out. # - def bail!(*message) - if block_given? - LeapCli.set_log_level(3) - yield - elsif message - log 0, *message + def bail!(*message, &block) + LeapCli.logger.log_level = 3 if LeapCli.logger.log_level < 3 + if message.any? + log(0, *message, &block) + else + log(0, :bailing, "out", :color => :red, :style => :bold, &block) end - log 0, :bail, "" - raise SystemExit.new(@exit_status || 1) + raise SystemExit.new(exit_status || 1) end # # quit with message, but no additional error or warning about bailing. # def quit!(message='') puts(message) - raise SystemExit.new(@exit_status || 0) + raise SystemExit.new(exit_status || 0) end # # bails out with message if assertion is false. # @@ -117,11 +120,11 @@ def assert_files_missing!(*files) options = files.last.is_a?(Hash) ? files.pop : {} base = options[:base] || Path.provider file_list = files.collect { |file_path| file_path = Path.named_path(file_path, base) - File.exists?(file_path) ? Path.relative_path(file_path, base) : nil + File.exist?(file_path) ? Path.relative_path(file_path, base) : nil }.compact if file_list.length > 1 bail! do log :error, "Sorry, we can't continue because these files already exist: #{file_list.join(', ')}." log options[:msg] if options[:msg] @@ -136,11 +139,11 @@ def assert_files_exist!(*files) options = files.last.is_a?(Hash) ? files.pop : {} file_list = files.collect { |file_path| file_path = Path.named_path(file_path) - !File.exists?(file_path) ? Path.relative_path(file_path) : nil + !File.exist?(file_path) ? Path.relative_path(file_path) : nil }.compact if file_list.length > 1 bail! do log :missing, "these files: #{file_list.join(', ')}" log options[:msg] if options[:msg] @@ -155,11 +158,11 @@ # takes a list of symbolic paths. returns true if all files exist or are directories. def file_exists?(*files) files.each do |file_path| file_path = Path.named_path(file_path) - if !File.exists?(file_path) + if !File.exist?(file_path) return false end end return true end @@ -231,11 +234,11 @@ # 3. yields contents # 4. replaces file with return value of the block # def replace_file!(filepath, &block) filepath = Path.named_path(filepath) - if !File.exists?(filepath) + if !File.exist?(filepath) content = yield(nil) unless content.nil? write_file!(filepath, content) end else @@ -256,11 +259,11 @@ end end def remove_file!(filepath) filepath = Path.named_path(filepath) - if File.exists?(filepath) + if File.exist?(filepath) if File.directory?(filepath) remove_directory!(filepath) else begin File.unlink(filepath) @@ -296,11 +299,11 @@ end def write_file!(filepath, contents) filepath = Path.named_path(filepath) ensure_dir File.dirname(filepath) - existed = File.exists?(filepath) + existed = File.exist?(filepath) if existed if file_content_equals?(filepath, contents) log :nochange, filepath, 2 return end @@ -318,15 +321,15 @@ end def rename_file!(oldpath, newpath) oldpath = Path.named_path(oldpath) newpath = Path.named_path(newpath) - if File.exists? newpath + if File.exist? newpath log :skipping, "#{Path.relative_path(newpath)}, file already exists" return end - if !File.exists? oldpath + if !File.exist? oldpath log :skipping, "#{Path.relative_path(oldpath)}, file is missing" return end FileUtils.mv oldpath, newpath log :moved, "#{Path.relative_path(oldpath)} to #{Path.relative_path(newpath)}" @@ -423,14 +426,21 @@ `which git && git rev-parse 2>/dev/null` return $? == 0 end end + def is_git_subrepo?(dir) + Dir.chdir(dir) do + `ls .gitrepo 2>/dev/null` + return $? == 0 + end + end + def current_git_branch(dir) Dir.chdir(dir) do branch = `git symbolic-ref HEAD 2>/dev/null`.strip if branch.chars.any? - branch.sub /^refs\/heads\//, '' + branch.sub(/^refs\/heads\//, '') else nil end end end