Skip to content

Commit 223c0fd

Browse files
committed
Merge pull request #45 from Swinject/rubocop
Rubocop
2 parents 9382691 + 9beca50 commit 223c0fd

File tree

12 files changed

+99
-81
lines changed

12 files changed

+99
-81
lines changed

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AllCops:
2+
TargetRubyVersion: 2.0
3+
Exclude:
4+
- '*.podspec'
5+
- 'ExampleProject/**/**'
6+
7+
Style/StringLiterals:
8+
EnforcedStyle: double_quotes
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Uncomment this line to define a global platform for your project
2-
platform :ios, '8.0'
2+
platform :ios, "8.0"
33
# Uncomment this line if you're using Swift
44
use_frameworks!
55

6-
target 'CodeGenerationExample' do
6+
target "CodeGenerationExample" do
77
pod "Swinject"
8-
pod 'Swinject-CodeGen', :git => 'https://github.com/Swinject/Swinject-CodeGen.git'
8+
pod "Swinject-CodeGen", git: "https://github.com/Swinject/Swinject-CodeGen.git"
99
end

Gemfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source "https://rubygems.org"
44
gem "cocoapods", "1.0.0.rc.1"
55
gem "fastlane"
66

7-
require 'rubygems'
8-
gem 'minitest' # ensures you're using the gem, and not the built in MT
9-
gem 'minitest-reporters'
7+
require "rubygems"
8+
gem "minitest" # ensures you're using the gem, and not the built in MT
9+
gem "minitest-reporters"
10+
gem "rubocop"

Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GEM
99
tzinfo (~> 1.1)
1010
addressable (2.4.0)
1111
ansi (1.5.0)
12+
ast (2.2.0)
1213
babosa (1.0.2)
1314
builder (3.2.2)
1415
cert (1.4.1)
@@ -196,6 +197,8 @@ GEM
196197
net-ssh (3.1.1)
197198
netrc (0.7.8)
198199
os (0.9.6)
200+
parser (2.3.1.0)
201+
ast (~> 2.2)
199202
pem (1.3.0)
200203
fastlane_core (>= 0.36.1, < 1.0.0)
201204
spaceship (>= 0.22.0, < 1.0.0)
@@ -205,13 +208,21 @@ GEM
205208
spaceship (>= 0.24.1, < 1.0.0)
206209
terminal-table (~> 1.4.5)
207210
plist (3.1.0)
211+
powerpack (0.1.1)
208212
produce (1.1.2)
209213
fastlane_core (>= 0.30.0, < 1.0.0)
210214
spaceship (>= 0.16.0, < 1.0.0)
215+
rainbow (2.1.0)
211216
representable (2.3.0)
212217
uber (~> 0.0.7)
213218
retriable (2.1.0)
214219
rouge (1.10.1)
220+
rubocop (0.39.0)
221+
parser (>= 2.3.0.7, < 3.0)
222+
powerpack (~> 0.1)
223+
rainbow (>= 1.99.1, < 3.0)
224+
ruby-progressbar (~> 1.7)
225+
unicode-display_width (~> 1.0, >= 1.0.1)
215226
ruby-progressbar (1.8.0)
216227
rubyzip (1.1.7)
217228
scan (0.6.0)
@@ -263,6 +274,7 @@ GEM
263274
unf (0.1.4)
264275
unf_ext
265276
unf_ext (0.0.7.2)
277+
unicode-display_width (1.0.5)
266278
xcodeproj (1.0.0.rc.1)
267279
activesupport (>= 3)
268280
claide (= 1.0.0.rc.1)
@@ -280,6 +292,7 @@ DEPENDENCIES
280292
fastlane
281293
minitest
282294
minitest-reporters
295+
rubocop
283296

284297
BUNDLED WITH
285298
1.12.3

Tests/test.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
1-
require 'minitest/autorun'
2-
require 'minitest/reporters'
1+
require "minitest/autorun"
2+
require "minitest/reporters"
33

44
reporter_options = { color: true }
5-
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(reporter_options)]
5+
default_reporter = Minitest::Reporters::DefaultReporter.new(reporter_options)
6+
Minitest::Reporters.use! [default_reporter]
67

8+
# code generation tests
79
class TestCodegeneration < Minitest::Test
8-
def buildCSV(fileName)
9-
%x(./../bin/swinject_codegen -i #{fileName}).chomp
10+
def build_csv(file_name)
11+
`./../bin/swinject_codegen -i #{file_name}`.chomp
1012
end
1113

12-
def readFile(fileName)
13-
File.read(fileName).chomp
14+
def read_file(file_name)
15+
File.read(file_name).chomp
1416
end
1517

16-
def runTest(baseFileName)
17-
assert_equal buildCSV("Examples/#{baseFileName}.csv"), readFile("ExpectedCode/#{baseFileName}.swift")
18+
def run_test(base_file_name)
19+
output = build_csv("Examples/#{base_file_name}.csv")
20+
example = read_file("ExpectedCode/#{base_file_name}.swift")
21+
assert_equal output, example
1822
end
1923

2024
def test_example_a
21-
runTest("ExampleA")
25+
run_test("ExampleA")
2226
end
2327

2428
def test_example_b
25-
runTest("ExampleB")
29+
run_test("ExampleB")
2630
end
2731

2832
def test_example_c
29-
runTest("ExampleC")
33+
run_test("ExampleC")
3034
end
3135

3236
def test_example_d
33-
runTest("ExampleD")
37+
run_test("ExampleD")
3438
end
3539

3640
def test_example_e
37-
runTest("ExampleE")
41+
run_test("ExampleE")
3842
end
3943

4044
def test_example_f
41-
runTest("ExampleF")
45+
run_test("ExampleF")
4246
end
4347
end

bin/swinject_codegen

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
require "erb"
33
require "Shellwords"
44
require "optparse"
5-
require_relative "../source/CSVParser"
6-
require_relative "../source/YMLParser"
7-
require_relative "../source/YMLSerializer"
5+
require_relative "../source/csv_parser"
6+
require_relative "../source/yml_parser"
7+
require_relative "../source/yml_serializer"
88

99
@options = {
1010
MIGRATION: false,
@@ -50,7 +50,7 @@ def main
5050
output_filename = @options[:OUTPUT_FILE]
5151

5252
if input_filename.end_with?("csv")
53-
YMLSerializer.new.serialize_hash_to_YML(definition_hash, output_filename)
53+
YMLSerializer.new.serialize_hash_to_yml(definition_hash, output_filename)
5454
elsif input_filename.end_with?("yml")
5555
serialize_hash_to_CSV(definition_hash, output_filename)
5656
end
@@ -75,9 +75,9 @@ end
7575

7676
def parse_input_file(input_filename)
7777
if input_filename.end_with?("csv")
78-
return CSVParser.new.parse_CSV(input_filename)
78+
return CSVParser.new.parse_csv(input_filename)
7979
elsif input_filename.end_with?("yml")
80-
return YMLParser.new.parse_YML(input_filename)
80+
return YMLParser.new.parse_yml(input_filename)
8181
else
8282
puts "unknown input format for file #{input_filename}"
8383
exit
@@ -92,17 +92,16 @@ def write_container_file(input_hash, output_filename)
9292
@content_array = input_hash[:DEFINITIONS]
9393
@dependencies = input_hash[:DEPENDENCIES]
9494

95-
code = ERB.new(File.read("#{templates_folder()}/container.erb"), nil, "-").result
95+
code = ERB.new(File.read("#{templates_folder}/container.erb"), nil, "-").result
9696

9797
if output_filename.nil?
9898
puts code
9999
else
100-
File.open(output_filename, 'w') do |file_to_write_to|
100+
File.open(output_filename, "w") do |file_to_write_to|
101101
file_to_write_to.puts code
102102
puts "Generated code in #{output_filename}"
103103
end
104104
end
105-
106105
end
107106

108107
def write_migration_file(input_hash, output_filename)
@@ -112,27 +111,26 @@ def write_migration_file(input_hash, output_filename)
112111
register_function_call_without_last_comma = definition[:register_function_call].reverse.sub(",", "").reverse
113112

114113
migration_hash = {
115-
resolve_function_signature_regex: Shellwords.escape(".#{definition[:resolve_function_signature].split("->").first.strip}"),
114+
resolve_function_signature_regex: Shellwords.escape(".#{definition[:resolve_function_signature].split('->').first.strip}"),
116115
resolve_function_call: Shellwords.escape("#{definition[:resolve_function_call]})!"),
117-
register_function_signature_regex: Shellwords.escape(".#{definition[:register_function_signature].split("(").first}"),
116+
register_function_signature_regex: Shellwords.escape(".#{definition[:register_function_signature].split('(').first}"),
118117
register_function_call: Shellwords.escape("#{register_function_call_without_last_comma})")
119118
}
120119

121120
@migration_array.push migration_hash
122121
end
123122

124-
migration_code = ERB.new(File.read("#{templates_folder()}/migration.erb"), nil, "-").result
123+
migration_code = ERB.new(File.read("#{templates_folder}/migration.erb"), nil, "-").result
125124

126125
if output_filename.nil?
127126
puts migration_code
128127
else
129128
migration_file_name = "#{output_filename}.migration.sh"
130-
File.open(migration_file_name, 'w') do |file_to_write_to|
129+
File.open(migration_file_name, "w") do |file_to_write_to|
131130
file_to_write_to.puts migration_code
132131
puts "Generated migration code in #{migration_file_name}"
133132
end
134133
end
135-
136134
end
137135

138136
def prepare_definitions(hash)
@@ -145,22 +143,22 @@ def prepare_definitions(hash)
145143
argument_hashes = definition[:arguments]
146144

147145
register_function_signature = "register"
148-
register_function_signature << definition[:component].gsub("<", "").gsub(">", "").gsub(".", "")
146+
register_function_signature << definition[:component].delete("<").delete(">").delete(".")
149147
register_function_signature << "_#{name}" unless has_no_name
150148
register_function_signature << "(registerClosure: (resolver: ResolverType"
151-
register_function_signature << ", " unless (argument_hashes.nil? || argument_hashes.empty?)
152-
register_function_signature << argument_hashes.map {|a| "#{a[:argument_name]}: #{a[:argument_type]}"}.join(", ") unless (argument_hashes.nil? || argument_hashes.empty?)
149+
register_function_signature << ", " unless argument_hashes.nil? || argument_hashes.empty?
150+
register_function_signature << argument_hashes.map { |a| "#{a[:argument_name]}: #{a[:argument_type]}" }.join(", ") unless argument_hashes.nil? || argument_hashes.empty?
153151
register_function_signature << ") -> (#{definition[:component]})) -> ServiceEntry<#{definition[:service]}>"
154152

155153
register_function_call = ".register("
156154
register_function_call << "#{definition[:service]}.self,"
157155
register_function_call << " name: \"#{name}\"," unless has_no_name
158156

159157
resolve_function_signature = "resolve"
160-
resolve_function_signature << definition[:component].gsub("<", "").gsub(">", "").gsub(".", "")
158+
resolve_function_signature << definition[:component].delete("<").delete(">").delete(".")
161159
resolve_function_signature << "_#{name}" unless has_no_name
162160
resolve_function_signature << "("
163-
resolve_function_signature << argument_hashes.each_with_index.map {|a,i| "#{a[:argument_name] + ' ' if i==0}#{a[:argument_name]}: #{a[:argument_type]}"}.join(", ") unless (argument_hashes.nil? || argument_hashes.empty?)
161+
resolve_function_signature << argument_hashes.each_with_index.map { |a, i| "#{a[:argument_name] + ' ' if i == 0}#{a[:argument_name]}: #{a[:argument_type]}" }.join(", ") unless argument_hashes.nil? || argument_hashes.empty?
164162
resolve_function_signature << ") -> #{definition[:component]}"
165163

166164
resolve_function_call = ".resolve("
@@ -183,13 +181,10 @@ def serialize_hash_to_CSV(hash, output_filename)
183181
if output_filename.nil?
184182
puts code
185183
else
186-
File.open(output_filename, 'w') do |file_to_write_to|
184+
File.open(output_filename, "w") do |file_to_write_to|
187185
file_to_write_to.puts code
188186
end
189187
end
190-
191188
end
192189

193-
if __FILE__ == $0
194-
main()
195-
end
190+
main if __FILE__ == $PROGRAM_NAME

erb/container.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import <%= dependency %>
99
extension Resolvable {
1010
<% @content_array.each do |hash| %>
1111
func <%= hash[:resolve_function_signature] %> {
12-
return self<%= hash[:resolve_function_call] %><%= ", argument: #{hash[:arguments][0][:argument_name]}" if !hash[:arguments].nil? && hash[:arguments].count == 1 -%><%= ", arguments: (#{hash[:arguments].map {|a| a[:argument_name]}.join(', ')})" if !hash[:arguments].nil? && hash[:arguments].count > 1 -%>)<%=hash[:service] != hash[:component] ? " as! #{hash[:component]}" : "!"%>
12+
return self<%= hash[:resolve_function_call] %><%= ", argument: #{hash[:arguments][0][:argument_name]}" if !hash[:arguments].nil? && hash[:arguments].count == 1 -%><%= ", arguments: (#{hash[:arguments].map {|a| a[:argument_name]}.join(", ")})" if !hash[:arguments].nil? && hash[:arguments].count > 1 -%>)<%=hash[:service] != hash[:component] ? " as! #{hash[:component]}" : "!"%>
1313
}
1414
<% end -%>
1515
}

source/YMLParser.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

source/YMLSerializer.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
# responsible for parsing the csv files
12
class CSVParser
2-
3-
def parse_CSV(input_filename)
3+
def parse_csv(input_filename)
44
result_hash = {
55
DEPENDENCIES: [],
66
DEFINITIONS: []
@@ -13,22 +13,22 @@ def parse_CSV(input_filename)
1313
# ignores empty lines
1414
elsif line.start_with?("#")
1515
# detect command
16-
if(line.start_with?("#ADD_DEPENDENCY "))
16+
if line.start_with?("#ADD_DEPENDENCY ")
1717
result_hash[:DEPENDENCIES].push(line.split(" ")[1])
18-
elsif(line.start_with?("# ADD_DEPENDENCY "))
18+
elsif line.start_with?("# ADD_DEPENDENCY ")
1919
result_hash[:DEPENDENCIES].push(line.split(" ")[2])
2020
end
2121
elsif line.start_with?("//")
2222
# ignores comments
2323
else
24-
array = line.split(";").map { |a| a.strip }
24+
array = line.split(";").map(&:strip)
2525
arguments = array[3..-1]
2626
argument_hashes = nil
2727
unless arguments.nil?
28-
arguments.reject { |a| a.empty? }
28+
arguments.reject(&:empty?)
2929
argument_hashes = arguments.map do |a|
3030
hash = nil
31-
if(a.include?(":"))
31+
if a.include?(":")
3232
hash = {
3333
argument_name: a.split(":").first.strip,
3434
argument_type: a.split(":").last.strip
@@ -57,7 +57,6 @@ def parse_CSV(input_filename)
5757
result_hash[:DEFINITIONS].push hash
5858
end
5959
end
60-
return result_hash
60+
result_hash
6161
end
62-
6362
end

0 commit comments

Comments
 (0)