Class: BuildOutput

Inherits:
Object show all
Defined in:
lib/build/BuildOutput.rb

Constant Summary collapse

NOTE =
0
WARNING =
1
ERROR =
2
FATAL =
3
MESSAGES =
['NOTE', 'WARNING', 'ERROR', 'FATAL']
LEVELS =
[NOTE, WARNING, ERROR, FATAL]
@@mqueue =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuildOutput

Returns a new instance of BuildOutput.



22
23
# File 'lib/build/BuildOutput.rb', line 22

def initialize()
end

Class Method Details

.col_message(level, message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/build/BuildOutput.rb', line 51

def col_message(level, message)
  out = case level
  when FATAL
    message.bold.red
  when ERROR
    message.red
  when WARNING
    message.cyan
  when NOTE
    message.blue
  end

  out
end

.decorate_message(message, title) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/build/BuildOutput.rb', line 39

def decorate_message( message, title )
  message_content = []

  message_content << ' **** ' + title + '*' * (105 - title.length)

  message_content.concat( prepare_message(message).map{|el| ' ' * 3 + el })

  message_content << ' ' + '*' * (110)

  message_content
end

.error(message, title = nil) ⇒ Object



154
155
156
# File 'lib/build/BuildOutput.rb', line 154

def error(message, title = nil)
  log(ERROR, message, title)
end

.fatal(message, title = nil) ⇒ Object



158
159
160
# File 'lib/build/BuildOutput.rb', line 158

def fatal(message, title = nil)
  log(ERROR, message, title)
end

.getLogTextObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/build/BuildOutput.rb', line 162

def getLogText
  txt = []
  @@mqueue.sort_by { |hsh| hsh[:level] }
  @@mqueue.each do |message|
    out = decorate_message( message[:content], message[:title] ).join($/)
    col = col_message(message[:level], out)
    message[:output] = out
    message[:color] = col
    txt << col
  end
  txt << '' unless txt.empty?
  txt.join($/)
end

.log(level, message, title) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/build/BuildOutput.rb', line 113

def log(level, message, title)
  if !LEVELS.include?(level)
    return
  end

  title = MESSAGES[level] + (title.nil? ? " " : (": " + title + " "))

  message = {
    :level => level,
    :title => title,
    :content => prepare_message(message)
  }

  last = @@mqueue.last

  if !last.nil? && (last[:level] == level) && (last[:title] == title)
    last[:content] =  merge_head_tail(last[:content], message[:content])
  else
    @@mqueue << message
  end
end

.merge_head_tail(src, msg) ⇒ Object



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
110
111
# File 'lib/build/BuildOutput.rb', line 80

def merge_head_tail(src, msg)  
  begining = []
  ending = []
  result = []

  # find same headers
  b_len = to_merge(src.each, msg.each)
  if (b_len > 0)
    begining.concat(src.take(b_len))
    src = src.drop(b_len)
    msg = msg.drop(b_len)
  end

  # find same footers
  e_len = to_merge(src.reverse_each, msg.reverse_each)
  if (e_len > 0)
    ending = src.drop(src.length - e_len)
    src = src.take(src.length - e_len)
    msg = msg.take(msg.length - e_len)
  end

  # special case about joining lines

  if src.length == 1 && msg.length == 1
    if msg.first.length > src.first.length && msg.first.start_with?(src.first)
      # warning(["Build message '#{msg} appended from ", " * " + caller.join($/ + " ^ "),'instead of being added to error string'], 'Error in build script')
      src = []
    end
  end

  begining.concat(src).concat(msg).concat(ending)
end

.note(message, title = nil) ⇒ Object



146
147
148
# File 'lib/build/BuildOutput.rb', line 146

def note(message, title = nil)
  log(NOTE, message, title)
end

.prepare_message(message) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/build/BuildOutput.rb', line 26

def prepare_message(message)
  if message.kind_of?(Array)
    out = []
    message.flatten(1).each do |line|
      out.concat( line.split($/) )
    end
  else
    out = message.split($/) 
  end

  out
end

.put_log(level, message, title = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/build/BuildOutput.rb', line 135

def put_log(level, message, title = nil)
  if !LEVELS.include?(level)
    return
  end

  msg = decorate_message(message, MESSAGES[level] + (title.nil? ? " " : (": " + title + " ")))
  col = col_message(level, msg.join($/))

  puts col
end

.to_merge(s1, s2) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/build/BuildOutput.rb', line 66

def to_merge(s1, s2)
  i = 0
  loop do
    e1 = s1.next rescue :eof
    e2 = s2.next rescue :eof
    if e1 == :eof || e2 == :eof || e1 != e2
      break
    else
      i += 1 
    end
  end
  return i
end

.warning(message, title = nil) ⇒ Object



150
151
152
# File 'lib/build/BuildOutput.rb', line 150

def warning(message, title = nil)
  log(WARNING, message, title)
end