Last active
April 23, 2016 10:20
-
-
Save Lutzifer/e76b91218cb4eeeaef53d6230691cc46 to your computer and use it in GitHub Desktop.
Code for swinject code generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "erb" | |
require "Shellwords" | |
require "set" | |
@dependencies = Set.new | |
@contentArray = [] | |
@migrationArray = [] | |
ARGV[0..-2].each do |filename| | |
f = File.open(filename) | |
f.each_line {|line| | |
if line.nil? || line.chomp.empty? | |
# ignores empty lines | |
elsif line.start_with?("#") | |
# detect command | |
if(line.start_with?("#ADD_DEPENDENCY ")) | |
@dependencies.add(line.split(" ")[1]) | |
elsif(line.start_with?("# ADD_DEPENDENCY ")) | |
@dependencies.add(line.split(" ")[2]) | |
end | |
elsif line.start_with?("//") | |
# ignores comments | |
else | |
array = line.split(";").map { |a| a.strip } | |
arguments = array[3..-1] | |
argumentHashes = nil | |
unless arguments.nil? | |
arguments.reject { |a| a.empty? } | |
argumentHashes = arguments.map do |a| | |
hash = nil | |
if(a.include?(":")) | |
hash = { | |
:argumentName => a.split(":").first, | |
:argumentType => a.split(":").last | |
} | |
else | |
hash = { | |
:argumentName => a.downcase, | |
:argumentType => a | |
} | |
end | |
hash | |
end | |
end | |
targetClass = array[1] | |
targetClassName = targetClass.gsub("<", "").gsub(">", "") | |
baseClass = array[0] | |
name = array[2] | |
hasNoName = (name.nil? || name.empty?) | |
registerFunctionSignature = "register" | |
registerFunctionSignature = registerFunctionSignature + targetClassName | |
registerFunctionSignature = registerFunctionSignature + "_#{name}" unless hasNoName | |
registerFunctionSignature = registerFunctionSignature + "(registerClosure: (resolver: ResolverType" | |
registerFunctionSignature = registerFunctionSignature + ", " unless (argumentHashes.nil? || argumentHashes.empty?) | |
registerFunctionSignature = registerFunctionSignature + argumentHashes.map {|a| "#{a[:argumentName]}: #{a[:argumentType]}"}.join(", ") unless (argumentHashes.nil? || argumentHashes.empty?) | |
registerFunctionSignature = registerFunctionSignature + ") -> (#{targetClass})) -> ServiceEntry<#{baseClass}>" | |
registerFunctionCall = ".register(" | |
registerFunctionCall = registerFunctionCall + "#{baseClass}.self," | |
registerFunctionCall = registerFunctionCall + " name: \"#{name}\"," unless hasNoName | |
resolveFunctionSignature = "resolve" | |
resolveFunctionSignature = resolveFunctionSignature + targetClassName | |
resolveFunctionSignature = resolveFunctionSignature + "_#{name}" unless hasNoName | |
resolveFunctionSignature = resolveFunctionSignature + "(" | |
resolveFunctionSignature = resolveFunctionSignature + argumentHashes.map {|a| "#{a[:argumentName]}: #{a[:argumentType]}"}.join(", ") unless (argumentHashes.nil? || argumentHashes.empty?) | |
resolveFunctionSignature = resolveFunctionSignature + ") -> #{targetClass}" | |
resolveFunctionCall = ".resolve(" | |
resolveFunctionCall = resolveFunctionCall + "#{baseClass}.self" | |
resolveFunctionCall = resolveFunctionCall + ", name: \"#{name}\"" unless hasNoName | |
hash = { | |
:baseClass => baseClass, | |
:targetClass => targetClass, | |
:targetClassName => targetClassName, | |
:name => name, | |
:arguments => argumentHashes, | |
:resolveFunctionSignature => resolveFunctionSignature, | |
:resolveFunctionCall => resolveFunctionCall, | |
:registerFunctionSignature => registerFunctionSignature, | |
:registerFunctionCall => registerFunctionCall, | |
} | |
@contentArray.push hash | |
registerFunctionCallWithoutLastComma = registerFunctionCall.reverse.sub(",", "").reverse | |
migrationHash = { | |
:resolveFunctionSignatureRegex => Shellwords.escape(".#{resolveFunctionSignature.split("->").first.strip}"), | |
:resolveFunctionCall => Shellwords.escape("#{resolveFunctionCall})!"), | |
:registerFunctionSignatureRegex => Shellwords.escape(".#{registerFunctionSignature.split("(").first}"), | |
:registerFunctionCall => Shellwords.escape("#{registerFunctionCallWithoutLastComma})") | |
} | |
@migrationArray.push migrationHash | |
end | |
} | |
end | |
fileToWriteTo = File.open(ARGV[-1], 'w') | |
fileToWriteTo.puts ERB.new(File.read('template.erb'), nil, "-").result | |
fileToWriteTo.close | |
migrationFileToWriteTo = File.open("#{ARGV[-1]}.migration.sh", 'w') | |
migrationFileToWriteTo.puts ERB.new(File.read('migration.erb'), nil, "-").result | |
migrationFileToWriteTo.close |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PersonType; InjectablePerson; initializer | |
PersonType; InjectablePerson | |
PersonType; PersonType | |
PersonType; InjectablePerson; ; argumentName:ArgumentType | |
PersonType; InjectablePerson; ; argumentName:ArgumentType; ArgumentTypeWithoutSpecificName; title:String; String | |
PersonType; InjectablePerson; initializer; argumentName:ArgumentType; ArgumentTypeWithoutSpecificName; title:String; String |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find . -type f | grep ".swift" > swiftindex.temp | |
while IFS= read -r filename | |
do | |
LC_ALL=C sed -i "" \ | |
<% @migrationArray.each do |hash| -%> | |
-e s/<%=hash[:resolveFunctionCall]%>/<%=hash[:resolveFunctionSignatureRegex]%>/g \ | |
-e s/<%=hash[:registerFunctionCall]%>/<%=hash[:registerFunctionSignatureRegex]%>/g \ | |
<% end -%> "$filename" | |
done < swiftindex.temp | |
rm swiftindex.temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# Ignore the following line in the erb, the erb is not autogenerated and can be modified %> | |
// this code is autogenerated, do not modify! | |
import Swinject | |
<% @dependencies.each do |dependency| %> | |
import <%= dependency %> | |
<% end -%> | |
extension Resolvable { | |
<% @contentArray.each do |hash| %> | |
func <%= hash[:resolveFunctionSignature] %> { | |
return self<%= hash[:resolveFunctionCall] %><%= ", argument: #{hash[:arguments][0][:argumentName]}" if !hash[:arguments].nil? && hash[:arguments].count == 1 -%><%= ", arguments: (#{hash[:arguments].map {|a| a[:argumentName]}.join(', ')})" if !hash[:arguments].nil? && hash[:arguments].count > 1 -%>)!<%= " as! #{hash[:targetClass]}" if hash[:baseClass] != hash[:targetClass]%> | |
} | |
<% end -%> | |
<% @contentArray.each do |hash| %> | |
func <%= hash[:registerFunctionSignature] %> { | |
return (self as! Container)<%= hash[:registerFunctionCall] %> factory: registerClosure) | |
} | |
<% end -%> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment