summaryrefslogtreecommitdiff
path: root/lib/bundler/ui.rb
blob: 3f5b97383fb24b3bb03d65483c6ac0c036288d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rubygems/user_interaction'

module Bundler
  class UI
    def warn(message, newline = nil)
    end

    def debug(message, newline = nil)
    end

    def trace(message, newline = nil)
    end

    def error(message, newline = nil)
    end

    def info(message, newline = nil)
    end

    def confirm(message, newline = nil)
    end

    def debug?
      false
    end

    class Shell < UI
      attr_reader :quiet
      attr_writer :shell

      def initialize(options = {})
        if options["no-color"] || !STDOUT.tty?
          Thor::Base.shell = Thor::Shell::Basic
        end
        @shell = Thor::Base.shell.new
        @quiet = false
        @debug = ENV['DEBUG']
        @trace = ENV['TRACE']
      end

      def info(msg, newline = nil)
        tell_me(msg, nil, newline) if !@quiet
      end

      def confirm(msg, newline = nil)
        tell_me(msg, :green, newline) if !@quiet
      end

      def warn(msg, newline = nil)
        tell_me(msg, :yellow, newline)
      end

      def error(msg, newline = nil)
        tell_me(msg, :red, newline)
      end

      def quiet=(value)
        @quiet = value
      end

      def quiet?
        @quiet
      end

      def debug?
        # needs to be false instead of nil to be newline param to other methods
        !!@debug && !@quiet
      end

      def debug!
        @debug = true
      end

      def debug(msg, newline = nil)
        tell_me(msg, nil, newline) if debug?
      end

      def trace(e, newline = nil)
        msg = ["#{e.class}: #{e.message}", *e.backtrace].join("\n")
        if debug?
          tell_me(msg, nil, newline)
        elsif @trace
          STDERR.puts "#{msg}#{newline}"
        end
      end

      private
      # valimism
      def tell_me(msg, color = nil, newline = nil)
        newline.nil? ? @shell.say(msg, color) : @shell.say(msg, color, newline)
      end
    end

    class RGProxy < ::Gem::SilentUI
      def initialize(ui)
        @ui = ui
        super()
      end

      def say(message)
        if message =~ /native extensions/
          @ui.info "with native extensions "
        else
          @ui.debug(message)
        end
      end
    end
  end
end