lib/ib/generator.rb in ib-0.3.4 vs lib/ib/generator.rb in ib-0.3.5
- old
+ new
@@ -1,76 +1,35 @@
+require 'erb'
+require 'tilt/erb'
+require 'ib/generator/rendering_helper'
+
class IB::Generator
+ def initialize motion_template_type
+ # NOTE: motion_template_type equal to Motion::Project::App.template
+ # but, this class use its value for judging build platform.
+ @build_platform = motion_template_type
+ end
+
+ def absolute_template_path path
+ File.expand_path(File.join(File.dirname(__FILE__), path))
+ end
+
+ def render_stub_file path, files
+ template = Tilt::ERBTemplate.new(absolute_template_path(path), { :trim => '<>' })
+ template.render(RenderingHelper.new(@build_platform, files))
+ end
+
def write files, dest
files = IB::Parser.new.find_all(files)
-
+
FileUtils.mkpath dest
File.open("#{dest}/Stubs.h", 'w') do |f|
- f.write <<-OBJC
-// Generated by IB v#{IB::VERSION} gem. Do not edit it manually
-// Run `rake ib:open` to refresh
-
-#import <Foundation/Foundation.h>
-#import <CoreData/CoreData.h>
-#import <UIKit/UIKit.h>
-
-#{generate_objc(files)}
-OBJC
+ f.write render_stub_file('generator/templates/Stubs.h.erb', files)
end
File.open("#{dest}/Stubs.m", 'w') do |f|
- f.write <<-OBJC
-// Generated by IB v#{IB::VERSION} gem. Do not edit it manually
-// Run `rake ib:open` to refresh
-
-#import "Stubs.h"
-
-#{generate_objc_impl(files)}
-OBJC
+ f.write render_stub_file('generator/templates/Stubs.m.erb', files)
end
-
end
- def generate_objc files
- output = ""
- files.map do |path, infos|
- infos.each do |info|
- output << <<-OBJC
-@interface #{info[:class][0][0]}#{info[:class][0][1] ? ": #{info[:class][0][1]}" : ""}
-
-#{info[:outlets].map {|name, type| "@property IBOutlet #{generate_type(type)} #{name};" }.join("\n")}
-
-#{info[:outlet_collections].map {|name, type| "@property IBOutletCollection(#{type}) NSArray * #{name};" }.join("\n")}
-
-#{info[:actions].map {|action| "-(IBAction) #{generate_action(action)};" }.join("\n")}
-
-@end
-OBJC
- output << "\n\n"
- end
- end
- output
- end
-
- def generate_objc_impl files
- output = ""
- files.map do |path, infos|
- infos.each do |info|
- output << <<-OBJC
-@implementation #{info[:class][0][0]}
-
-@end
-OBJC
- output << "\n\n"
- end
- end
- output
- end
-
- def generate_type type
- type == "id" ? type : "#{type} *"
- end
-
- def generate_action action
- action[1] ? "#{action[0]}:(#{action[2] ? "#{action[2]}*" : 'id'}) #{action[1]}" : "#{action[0]}"
- end
end