Skip to content

Commit 27052cc

Browse files
committedApr 8, 2018
More restructuring and documentation.
- Moved concrete patterns into TexLogParser class. - Logger moved into LogParser module. - Circle steps now output Ruby and Gem versions.
1 parent 06df071 commit 27052cc

19 files changed

+536
-389
lines changed
 

‎.circleci/config.yml

+96-85
Original file line numberDiff line numberDiff line change
@@ -9,104 +9,115 @@ jobs:
99
- run:
1010
name: Prepare Ruby environment
1111
command: |
12+
ruby --version
13+
echo "gem `gem --version`"
1214
gem update --system
1315
gem install bundler
1416
bundle install
1517
- run: rake test
1618
test-2.4:
17-
docker:
18-
- image: circleci/ruby:2.4
19-
steps:
20-
- checkout
21-
- run:
22-
name: Prepare Ruby environment
23-
command: |
24-
gem update --system
25-
gem install bundler
26-
bundle install
27-
- run: rake test
19+
docker:
20+
- image: circleci/ruby:2.4
21+
steps:
22+
- checkout
23+
- run:
24+
name: Prepare Ruby environment
25+
command: |
26+
ruby --version
27+
echo "gem `gem --version`"
28+
gem update --system
29+
gem install bundler
30+
bundle install
31+
- run: rake test
2832
test-2.5:
29-
docker:
30-
- image: circleci/ruby:2.5
31-
steps:
32-
- checkout
33-
- run:
34-
name: Prepare Ruby environment
35-
command: |
36-
gem update --system
37-
gem install bundler
38-
bundle install
39-
- run: rake test
33+
docker:
34+
- image: circleci/ruby:2.5
35+
steps:
36+
- checkout
37+
- run:
38+
name: Prepare Ruby environment
39+
command: |
40+
ruby --version
41+
echo "gem `gem --version`"
42+
gem update --system
43+
gem install bundler
44+
bundle install
45+
- run: rake test
4046
build:
41-
docker:
42-
- image: circleci/ruby:2.5
43-
steps:
44-
- checkout
45-
- run:
46-
name: Prepare Ruby environment
47-
command: |
48-
gem update --system
49-
gem install bundler
50-
bundle install
51-
- run:
52-
name: Build Gem
53-
command: gem build texlogparser.gemspec
54-
- persist_to_workspace:
55-
root: .
56-
paths:
57-
- .circleci/gem_credentials
58-
- tex_log_parser*.gem
59-
- test/texlogs/*.log
47+
docker:
48+
- image: circleci/ruby:2.5
49+
steps:
50+
- checkout
51+
- run:
52+
name: Prepare Ruby environment
53+
command: |
54+
ruby --version
55+
echo "gem `gem --version`"
56+
gem update --system
57+
gem install bundler
58+
bundle install
59+
- run:
60+
name: Build Gem
61+
command: gem build texlogparser.gemspec
62+
- persist_to_workspace:
63+
root: .
64+
paths:
65+
- .circleci/gem_credentials
66+
- tex_log_parser*.gem
67+
- test/texlogs/*.log
6068
doc:
61-
docker:
62-
- image: circleci/ruby:2.5
63-
steps:
64-
- checkout
65-
- run:
66-
name: Prepare Ruby environment
67-
command: |
68-
gem update --system
69-
gem install bundler
70-
bundle install
71-
- run:
72-
name: Build Documentation
73-
command: bundle exec rake doc
69+
docker:
70+
- image: circleci/ruby:2.5
71+
steps:
72+
- checkout
73+
- run:
74+
name: Prepare Ruby environment
75+
command: |
76+
ruby --version
77+
echo "gem `gem --version`"
78+
gem update --system
79+
gem install bundler
80+
bundle install
81+
- run:
82+
name: Build Documentation
83+
command: rake doc
7484
test-cli:
75-
docker:
76-
- image: circleci/ruby:2.5
77-
steps:
78-
- attach_workspace:
79-
at: .
80-
- run:
81-
name: Install Gem
82-
command: gem install tex_log_parser*.gem
83-
- run:
84-
name: Test version output
85-
command: texlogparser -v
86-
- run:
87-
name: Test an actual log
88-
command: texlogparser -i `ls -d1 test/texlogs/*.log | head -1`
85+
docker:
86+
- image: circleci/ruby:2.5
87+
steps:
88+
- attach_workspace:
89+
at: .
90+
- run:
91+
name: Install Gem
92+
command: gem install tex_log_parser*.gem
93+
- run:
94+
name: Test version output
95+
command: texlogparser -v
96+
- run:
97+
name: Test an actual log
98+
command: texlogparser -i `ls -d1 test/texlogs/*.log | head -1`
8999
deploy:
90-
docker:
91-
- image: circleci/ruby:2.5
92-
steps:
93-
- attach_workspace:
94-
at: .
95-
- run:
96-
name: Deploy Gem
97-
command: |
98-
cat .circleci/gem_credentials | sed -e "s/__RUBYGEMS_API_KEY__/${RUBYGEMS_API_KEY}/" > ~/.gem/credentials
99-
gem push tex_log_parser*.gem
100-
shred -u ~/.gem/credentials
101-
filters:
102-
branches:
103-
ignore: /.*/
104-
tags:
105-
only: /^v.*/
100+
docker:
101+
- image: circleci/ruby:2.5
102+
steps:
103+
- attach_workspace:
104+
at: .
105+
- run:
106+
name: Deploy Gem
107+
command: |
108+
echo "gem `gem --version`"
109+
cat .circleci/gem_credentials | sed -e "s/__RUBYGEMS_API_KEY__/${RUBYGEMS_API_KEY}/" > ~/.gem/credentials
110+
gem push tex_log_parser*.gem
111+
shred -u ~/.gem/credentials
112+
filters:
113+
branches:
114+
ignore: /.*/
115+
tags:
116+
only: /^v.*/
106117

107118
workflows:
108119
version: 2
109-
test:
120+
test-and-deploy:
110121
jobs:
111122
- test-2.3
112123
- test-2.4

‎Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ desc 'Build documentation'
1414
YARD::Rake::YardocTask.new do |t|
1515
t.name = 'doc'
1616
# t.files = ['lib/**/*.rb', OTHER_PATHS] # optional
17-
t.options = %w(--markup-provider=redcarpet --markup=markdown --main=README.md)
17+
t.options = %w[--markup-provider=redcarpet --markup=markdown --main=README.md]
1818
end
1919

2020
task default: :test

‎bin/texlogparser

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ formats = %i[file_line json]
1717
OptionParser.new do |opts|
1818
opts.banner = 'Usage: texlogparser [options]'
1919
opts.on('-d', '--debug', 'Output debug information') do
20-
Logger.debugging = true
20+
LogParser::Logger.debug = true
2121
end
2222
opts.on('-f ENUM', '--format ENUM', formats,
2323
'Output format', "One of: #{formats.map(&:to_s).join(', ')}") do |format|
@@ -39,7 +39,7 @@ OptionParser.new do |opts|
3939
exit
4040
end
4141
opts.on_tail('-v', '--version', 'Show version') do
42-
puts TexLogParser::VERSION
42+
puts "#{File.basename(__FILE__)} #{TexLogParser::VERSION}"
4343
exit
4444
end
4545

@@ -58,7 +58,7 @@ end
5858

5959
input = STDIN
6060
input = File.open(options[:input], 'r') if options[:input].is_a?(String)
61-
parser = TexLogParser.new(input, options)
61+
parser = TexLogParser.new(input)
6262
messages = parser.parse
6363

6464
output = STDOUT

‎lib/log_parser/buffer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,15 @@ def [](offset, length = nil)
103103
#
104104
# @param [Integer] offset
105105
# The number of lines to drop.
106+
# @return [void]
106107
def forward(offset = 1)
107108
self[offset]
108109
@buffer.slice!(0, offset)
109110
end
110111

111112
# Closes the `IO` this buffer reads from, if any.
113+
#
114+
# @return [void]
112115
def close
113116
@stream.close unless @stream.nil? || @stream.closed?
114117
end

‎lib/log_parser/log_parser.rb

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require 'log_parser/logger'
34
require 'log_parser/buffer'
45
require 'log_parser/message'
56
require 'log_parser/pattern'
@@ -10,9 +11,28 @@ module LogParser
1011
attr_reader :scope_changes_by_line if Logger.debug?
1112

1213
# TODO: document
14+
# @return [Array<Message>]
15+
def parse
16+
skip_empty_lines
17+
until empty?
18+
parse_next_lines
19+
skip_empty_lines
20+
end
21+
22+
# TODO: Remove duplicates?
23+
@messages
24+
end
25+
26+
protected
27+
28+
# Creates a new instance.
29+
#
30+
# This parser will read lines one by one from the given `log`.
31+
# If it is an `IO` or `StringIO`, only those lines currently under investigation will be kept in memory.
32+
#
1333
# @param [Array<String>,IO,StringIO] log
14-
# @param [Hash] _options
15-
def initialize(log, _options = {})
34+
# A set of log lines that will be parsed.
35+
def initialize(log)
1636
@files = []
1737

1838
@messages = []
@@ -23,11 +43,13 @@ def initialize(log, _options = {})
2343
@scope_changes_by_line = {} if Logger.debug?
2444
end
2545

46+
# @abstract
2647
# @return [Array<Pattern>]
2748
def patterns
2849
raise NotImplementedError
2950
end
3051

52+
# @abstract
3153
# @param [String] _line
3254
# @return [Array<String,:pop>] A list of new scopes this line enters (strings)
3355
# and leaves (`:pop`).
@@ -40,19 +62,6 @@ def empty?
4062
@lines.empty?
4163
end
4264

43-
# TODO: document
44-
# @return [Array<Message>]
45-
def parse
46-
skip_empty_lines
47-
until empty?
48-
parse_next_lines
49-
skip_empty_lines
50-
end
51-
52-
# TODO: Remove duplicates?
53-
@messages
54-
end
55-
5665
private
5766

5867
def skip_empty_lines

‎lib/log_parser/logger.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module LogParser
4+
# A simple helper, probably to be replaced by a proper logging library
5+
# at some point.
6+
class Logger
7+
class << self
8+
# Switches debugging mode on and off.
9+
#
10+
# @param [true,false] flag
11+
# @return [void]
12+
def debug=(flag)
13+
@debugging = flag
14+
end
15+
16+
# Indicates whether we are debugging.
17+
#
18+
# @return [true,false]
19+
# `true` if we are in debugging mode, `false` otherwise.
20+
def debug?
21+
@debugging || !ENV['DEBUG'].nil?
22+
end
23+
24+
# Logs the given message to STDOUT if `debug?` is true.
25+
#
26+
# @param [String] message
27+
# @return [void]
28+
def debug(message)
29+
puts message if debug?
30+
end
31+
end
32+
end
33+
end

‎lib/log_parser/message.rb

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def initialize(message:, source_file: nil, source_lines: nil, log_lines: nil,
3838
attr_accessor :message, :source_file, :source_lines, :log_lines,
3939
:preformatted, :level
4040

41+
# Convert this message to a file-line string representation.
42+
#
43+
# @return [String]
4144
def to_s
4245
lines = if @source_lines.nil?
4346
''
@@ -56,6 +59,10 @@ def to_s
5659
MSG
5760
end
5861

62+
# Convert this message to JSON.
63+
#
64+
# @return [String]
65+
# The JSON string representing this message.
5966
def to_json(_options = {})
6067
hash = {
6168
level: @level,

0 commit comments

Comments
 (0)
Please sign in to comment.