Skip to content

Commit 15b5c3f

Browse files
committed
Added a module to create a new parser
Include in your Rakefile like this: require 'git_version' include GitVersion task :default do puts git_version.sha end
1 parent c310eb1 commit 15b5c3f

File tree

4 files changed

+185
-167
lines changed

4 files changed

+185
-167
lines changed
Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,7 @@
1-
require 'json'
2-
require 'open3'
1+
require 'git_version/parser'
32

4-
class GitVersion
5-
attr_reader :args
6-
attr_accessor :gitversion_exe
7-
8-
def initialize(args = [])
9-
@args = args
10-
end
11-
12-
def method_missing(symbol, *args)
13-
keys = [symbol.to_s, pascal_case(symbol.to_s)]
14-
15-
found_key = keys.find { |key| json.has_key?(key) }
16-
return json[found_key] if found_key
17-
18-
super
19-
end
20-
21-
def json
22-
@json ||= run_gitversion
23-
end
24-
25-
def gitversion_exe
26-
@gitversion_exe ||= File.expand_path(File.join(File.dirname(__FILE__), '../bin/GitVersion.exe'))
27-
end
28-
29-
def inspect
30-
unless @json
31-
32-
return <<EOF
33-
#{to_s}
34-
Will invoke #{cmd_string} when first used.
35-
EOF
36-
37-
else
38-
39-
return <<EOF
40-
#{to_s}
41-
Invoked #{cmd_string} and parsed its output:
42-
#{json.inspect}
43-
EOF
44-
45-
end
46-
end
47-
48-
private
49-
def run_gitversion
50-
stdout_and_stderr, status = Open3.capture2e(*cmd)
51-
52-
raise StandardError.new("Failed running #{cmd_string}, #{status}. We received the following output:\n#{stdout_and_stderr}") unless status.success?
53-
54-
JSON.parse(stdout_and_stderr)
55-
end
56-
57-
def cmd
58-
cmd = [gitversion_exe]
59-
cmd << args
60-
cmd.flatten.reject(&:nil?)
61-
end
62-
63-
def cmd_string
64-
cmd.join(' ')
65-
end
66-
67-
def pascal_case(str)
68-
str
69-
.to_s
70-
.split('_')
71-
.inject([]) { |buffer, e| buffer.push(e.capitalize) }
72-
.join
3+
module GitVersion
4+
def git_version(args = nil)
5+
Parser.new(args)
736
end
747
end
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
require 'json'
2+
require 'open3'
3+
4+
module GitVersion
5+
class Parser
6+
attr_reader :args
7+
attr_accessor :gitversion_exe
8+
9+
def initialize(args = [])
10+
@args = args
11+
end
12+
13+
def method_missing(symbol, *args)
14+
keys = [symbol.to_s, pascal_case(symbol.to_s)]
15+
16+
found_key = keys.find { |key| json.has_key?(key) }
17+
return json[found_key] if found_key
18+
19+
super
20+
end
21+
22+
def json
23+
@json ||= run_gitversion
24+
end
25+
26+
def gitversion_exe
27+
@gitversion_exe ||= File.expand_path(File.join(File.dirname(__FILE__), '../../bin/GitVersion.exe'))
28+
end
29+
30+
def inspect
31+
unless @json
32+
33+
return <<EOF
34+
#{to_s}
35+
Will invoke #{cmd_string} when first used.
36+
EOF
37+
38+
else
39+
40+
return <<EOF
41+
#{to_s}
42+
Invoked #{cmd_string} and parsed its output:
43+
#{json.inspect}
44+
EOF
45+
46+
end
47+
end
48+
49+
private
50+
def run_gitversion
51+
stdout_and_stderr, status = Open3.capture2e(*cmd)
52+
53+
raise StandardError.new("Failed running #{cmd_string}, #{status}. We received the following output:\n#{stdout_and_stderr}") unless status.success?
54+
55+
JSON.parse(stdout_and_stderr)
56+
end
57+
58+
def cmd
59+
cmd = [gitversion_exe]
60+
cmd << args
61+
cmd.flatten.reject(&:nil?)
62+
end
63+
64+
def cmd_string
65+
cmd.join(' ')
66+
end
67+
68+
def pascal_case(str)
69+
str
70+
.to_s
71+
.split('_')
72+
.inject([]) { |buffer, e| buffer.push(e.capitalize) }
73+
.join
74+
end
75+
end
76+
end
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
require 'git_version'
2+
3+
describe GitVersion::Parser do
4+
describe 'defaults' do
5+
its(:gitversion_exe) { should match(%r|/bin/GitVersion.exe$|) }
6+
its(:args) { should be_empty }
7+
end
8+
9+
describe 'GitVersion.exe invocation' do
10+
before {
11+
allow(Open3).to receive(:capture2e).and_return(gitversion_exe_return)
12+
}
13+
14+
let(:gitversion_exe_return) { ['{ "Sha": 1234 }', OpenStruct.new(success?: true)] }
15+
16+
describe 'cached results' do
17+
context 'accessing multiple properties' do
18+
it 'should run GitVersion.exe only once' do
19+
subject.sha
20+
subject.sha
21+
22+
expect(Open3).to have_received(:capture2e).once
23+
end
24+
end
25+
end
26+
27+
describe 'arguments' do
28+
subject { described_class.new(args) }
29+
30+
let(:args) { %w(some additional args) }
31+
32+
it 'should pass args to the executable' do
33+
subject.sha
34+
35+
expect(Open3).to have_received(:capture2e).with(*([subject.gitversion_exe] + args))
36+
end
37+
end
38+
39+
context 'GitVersion.exe fails' do
40+
let(:gitversion_exe_return) { ['some error output', OpenStruct.new(success?: false)] }
41+
42+
it 'should fail' do
43+
expect { subject.it_does_not_matter }.to raise_error(StandardError, /Failed running.*some error output/m)
44+
end
45+
end
46+
end
47+
48+
describe 'after the executable has been built' do
49+
before {
50+
subject.gitversion_exe = File.expand_path('../../GemBuild/bin/GitVersion.exe')
51+
}
52+
53+
its(:json) { should_not be_nil }
54+
its(:sha) { should match(/[0-9 a-f]{40}/) }
55+
end
56+
57+
describe 'accessing properties' do
58+
before {
59+
allow(subject).to receive(:json).and_return(json)
60+
}
61+
62+
let(:json) { { 'InformationalVersion' => 'blah' } }
63+
64+
it 'should translate snake case to pascal case' do
65+
expect(subject.informational_version).to eq('blah')
66+
end
67+
68+
it 'should support pascal case' do
69+
pascal = subject.InformationalVersion
70+
snake = subject.informational_version
71+
72+
expect(pascal).to eq(snake)
73+
end
74+
75+
context 'with nil value' do
76+
let(:json) { { 'is_nil' => nil } }
77+
78+
it 'should yield nil' do
79+
expect(subject.is_nil).to be_nil
80+
end
81+
end
82+
end
83+
84+
describe '#inspect' do
85+
context 'no properties accessed yet' do
86+
it 'writes what will happen' do
87+
expect(subject.inspect).to match(/.+GitVersion.+\nWill invoke .+GitVersion.exe when first used./)
88+
end
89+
end
90+
91+
context 'properties accessed' do
92+
before {
93+
allow(Open3).to receive(:capture2e).and_return(['{ "Sha": 1234 }', OpenStruct.new(success?: true)])
94+
}
95+
96+
it 'writes what happened' do
97+
subject.sha
98+
expect(subject.inspect).to match(/.+GitVersion.+\nInvoked .+GitVersion.exe and parsed its output:\n.+/)
99+
end
100+
end
101+
end
102+
end
Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,9 @@
11
require 'git_version'
22

33
describe GitVersion do
4-
describe 'defaults' do
5-
its(:gitversion_exe) { should match(%r|/bin/GitVersion.exe$|) }
6-
its(:args) { should be_empty }
7-
end
8-
9-
describe 'GitVersion.exe invocation' do
10-
before {
11-
allow(Open3).to receive(:capture2e).and_return(gitversion_exe_return)
12-
}
13-
14-
let(:gitversion_exe_return) { ['{ "Sha": 1234 }', OpenStruct.new(success?: true)] }
15-
16-
describe 'cached results' do
17-
context 'accessing multiple properties' do
18-
it 'should run GitVersion.exe only once' do
19-
subject.sha
20-
subject.sha
21-
22-
expect(Open3).to have_received(:capture2e).once
23-
end
24-
end
25-
end
26-
27-
describe 'arguments' do
28-
subject { described_class.new(args) }
29-
30-
let(:args) { %w(some additional args) }
31-
32-
it 'should pass args to the executable' do
33-
subject.sha
34-
35-
expect(Open3).to have_received(:capture2e).with(*([subject.gitversion_exe] + args))
36-
end
37-
end
38-
39-
context 'GitVersion.exe fails' do
40-
let(:gitversion_exe_return) { ['some error output', OpenStruct.new(success?: false)] }
41-
42-
it 'should fail' do
43-
expect { subject.it_does_not_matter }.to raise_error(StandardError, /Failed running.*some error output/m)
44-
end
45-
end
46-
end
47-
48-
describe 'after the executable has been built' do
49-
before {
50-
subject.gitversion_exe = File.expand_path('../../GemBuild/bin/GitVersion.exe')
51-
}
52-
53-
its(:json) { should_not be_nil }
54-
its(:sha) { should match(/[0-9 a-f]{40}/) }
55-
end
56-
57-
describe 'accessing properties' do
58-
before {
59-
allow(subject).to receive(:json).and_return(json)
60-
}
61-
62-
let(:json) { { 'InformationalVersion' => 'blah' } }
63-
64-
it 'should translate snake case to pascal case' do
65-
expect(subject.informational_version).to eq('blah')
66-
end
67-
68-
it 'should support pascal case' do
69-
pascal = subject.InformationalVersion
70-
snake = subject.informational_version
71-
72-
expect(pascal).to eq(snake)
73-
end
74-
75-
context 'with nil value' do
76-
let(:json) { { 'is_nil' => nil } }
77-
78-
it 'should yield nil' do
79-
expect(subject.is_nil).to be_nil
80-
end
81-
end
82-
end
83-
84-
describe '#inspect' do
85-
context 'no properties accessed yet' do
86-
it 'writes what will happen' do
87-
expect(subject.inspect).to match(/.+GitVersion.+\nWill invoke .+GitVersion.exe when first used./)
88-
end
89-
end
90-
91-
context 'properties accessed' do
92-
before {
93-
allow(Open3).to receive(:capture2e).and_return(['{ "Sha": 1234 }', OpenStruct.new(success?: true)])
94-
}
4+
include described_class
955

96-
it 'writes what happened' do
97-
subject.sha
98-
expect(subject.inspect).to match(/.+GitVersion.+\nInvoked .+GitVersion.exe and parsed its output:\n.+/)
99-
end
100-
end
6+
it 'creates a GitVersionParser' do
7+
expect(git_version).to be_an_instance_of(GitVersion::Parser)
1018
end
1029
end

0 commit comments

Comments
 (0)