module DealUtils

Public Instance Methods

logC(text) click to toggle source
# File lib/deal/utils/utils.rb, line 102
def logC(text)
   logInner '34',"[COMMAND] #{text}"
end
logE(text) click to toggle source

打印不同级别的 log。根据级别不同,样式(颜色)不同

# File lib/deal/utils/utils.rb, line 90
def logE(text)
   logInner '31',"[ERROR] !!#{text}"
end
logInner(color_code,text) click to toggle source
# File lib/deal/utils/utils.rb, line 83
def logInner(color_code,text)
   clolr="\033[#{color_code}m"
   nc="\033[0m"
   puts "#{clolr}#{text}#{nc}"
end
logN(text) click to toggle source
# File lib/deal/utils/utils.rb, line 98
def logN(text)
   logInner '32',"[NOTE] #{text}"
end
logW(text) click to toggle source
# File lib/deal/utils/utils.rb, line 94
def logW(text)
   logInner '33',"[WARNING] #{text}"
end
print_console(str) click to toggle source
process() { || ... } click to toggle source
# File lib/deal/utils/utils.rb, line 4
def process
    startTime = Time.new
    if block_given?
        yield
    end
    endTime = Time.new
    logN "执行耗时:#{format("%.2f",(endTime-startTime)*1000).to_f}ms"

end
run_shell(command,force = false,slient = false,retryCount = 0 ) { |out_lines, error_lines,exitstatus| ... } click to toggle source
# File lib/deal/utils/utils.rb, line 18
 def run_shell(command,force = false,slient = false,retryCount = 0 )
     # if not slient
     #     logC command
     # end
     if slient<2
         logC command
     end
     stdin, stdout, stderr,wait_thr = Open3.popen3(command)
    pid = wait_thr[:pid]
    out_lines = []
    error_lines = []
    stdout.sync = true
    out = Thread.new do
        # while !stdout.eof?
        #     c = stdout.getc
        #     putc c
        # end
        stdout.each do |line|
            out_lines.push line
            if not slient
                logN line
            end
        end
    end
    err = Thread.new do
        stderr.each do |line|
            error_lines.push line
            if not slient
                logW line
            end
        end
    end
    out.join
    err.join

    stderr.close
    stdin.close
    stdout.close
    status = wait_thr.value
    if status.exitstatus != 0
        if !force
            if retryCount > 0
                sleep 1
                run_shell command,force,slient,retryCount-1
                return
            else
                if slient
                    logE error_lines
                end
                exit 1
            end
        end
    end
    unless block_given?

        return  [out_lines,error_lines,status.exitstatus]
    else
        yield out_lines, error_lines,status.exitstatus
    end
end
set_progress(index, char = '*') click to toggle source
# File lib/deal/utils/utils.rb, line 14
def set_progress(index, char = '*')
    print (char * (index / 2.5).floor).ljust(40, " "), " #{index}%\r"
end