lib/key_master.rb in cocoapods-keys-1.0.2 vs lib/key_master.rb in cocoapods-keys-1.1.0
- old
+ new
@@ -1,13 +1,16 @@
require 'set'
+require 'erb'
+require 'digest'
module CocoaPodsKeys
class KeyMaster
attr_accessor :name, :interface, :implementation
- def initialize(keyring)
+ def initialize(keyring, time=Time.now)
+ @time = time
@keys = Hash[keyring.keychain_data.map { |(key, value)| [key[0].downcase + key[1..-1], value] }]
@name = keyring.code_name + 'Keys'
@used_indexes = Set.new
@indexed_keys = {}
@data = generate_data
@@ -54,106 +57,20 @@
data
end
def generate_interface
- erb = <<-SOURCE
-//
-// Generated by CocoaPods-Keys
-// on #{Time.now.strftime("%d/%m/%Y")}
-// For more information see https://github.com/cocoapods/cocoapods-keys
-//
-
-#import <Foundation/NSString.h>
-
-@interface <%= @name %> : NSObject
-<% @keys.each do |key, value| %>
-- (NSString *)<%= key %>;<% end %>
-
-@end
-SOURCE
-
- render_erb(erb)
+ render_erb("Keys.h.erb")
end
def generate_implementation
- require 'digest'
-
- erb = <<-SOURCE
-//
-// Generated by CocoaPods-Keys
-// on #{Time.now.strftime("%d/%m/%Y")}
-// For more information see https://github.com/cocoapods/cocoapods-keys
-//
-
-#import <objc/runtime.h>
-#import <Foundation/NSDictionary.h>
-#import "<%= @name %>.h"
-
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wincomplete-implementation"
-
-@implementation <%= @name %>
-
-#pragma clang diagnostic pop
-
-+ (BOOL)resolveInstanceMethod:(SEL)name
-{
- NSString *key = NSStringFromSelector(name);
- NSString * (*implementation)(<%= name %> *, SEL) = NULL;
-<% @keys.each do |key, value| %>
- if ([key isEqualToString:@"<%= key %>"]) {
- implementation = _podKeys<%= Digest::MD5.hexdigest(key) %>;
- }
-<% end %>
- if (!implementation) {
- return [super resolveInstanceMethod:name];
- }
-
- return class_addMethod([self class], name, (IMP)implementation, "@@:");
-}
-<% @keys.each do |key, value| %>
-static NSString *_podKeys<%= Digest::MD5.hexdigest(key) %>(<%= name %> *self, SEL _cmd)
-{
- <% if @indexed_keys.length > 0 %>
- <% if @indexed_keys[key].length > 0 %>
- char cString[<%= @indexed_keys[key].length + 1 %>] = { <%= key_data_arrays[key] %>, '\\0' };
- <% else %>
- char cString[1] = { '\\0' };
- <% end %>
- return [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
- <% else %>
- return @"";
- <% end %>
-}
-<% end %>
-
-static char <%= name %>Data[<%= @data_length %>] = "<%= @data %>";
-
-- (NSString *)description
-{
- return [@{
-<%- @keys.each do |key, value| -%>
- @"<%= key %>": self.<%= key %>,
-<%- end -%>
- } description];
-}
-
-- (id)debugQuickLookObject
-{
- return [self description];
-}
-
-@end
-SOURCE
-
- render_erb(erb)
+ render_erb("Keys.m.erb")
end
:private
-
- def render_erb(erb)
- require 'erb'
+
+ def render_erb(erb_template)
+ erb = IO.read(File.join(__dir__, "../templates", erb_template))
ERB.new(erb, nil, '-').result(binding)
end
def key_data_arrays
Hash[@indexed_keys.map {|key, value| [key, value.map { |i| name + "Data[#{i}]" }.join(', ')]}]