Class: PodfilePatch

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-podfile_patch/podfile_patch.rb

Instance Method Summary collapse

Instance Method Details

#get_raw_dependencies(target_defination) ⇒ Object



78
79
80
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 78

def get_raw_dependencies(target_defination)
    (target_defination.send :get_hash_value, 'dependencies') || []
end

#main(podfile, patchfile_path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 10

def main(podfile, patchfile_path)

    patch_file_path = Pathname.new(patchfile_path)
    if not patch_file_path.expand_path.file? 
        podfile_root_path = Pathname.new(podfile.defined_in_file).dirname
        patch_file_path = (podfile_root_path + patch_file_path).expand_path
    end
    if not patch_file_path.file?
        Pod::UI.info "Podfile patch feature is enabled. (No patch file now.)"
        return  
    end

    patchfile = self.patchfile(patch_file_path)
    self.patch(podfile: podfile, with_podfile: patchfile)

    Pod::UI.titled_section "Podfile patch feature is enabled" do
        next unless Pod::UI.config.verbose?
        Pod::UI.section "Patchfile Info" do 
            self.print_podfile_raw_dependencies patchfile
        end
        Pod::UI.section "Modified Podfile Info" do 
            self.print_podfile_raw_dependencies podfile
        end
    end
end

#merge_target_definitions_dependency(targetdef, patch_targetdef) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 52

def merge_target_definitions_dependency(targetdef, patch_targetdef)
    # Demo Data
    # Original Target
    # ```
    # TargetDefination 'Pods':
    # [{"BDWebImage"=>["1.0.1.1-binary"]}]
    #
    #     TargetDefination 'Pods-FrameworkDemos_Tests':
    #     ["BDWebImage/SDAdapter", "BDWebImage/Download", {"TTNetworkManager"=>["2.2.8.46-rc.18"]}, {"AFNetworking"=>[{:path=>"/Users/lea/Downloads/AFNetworking"}]}]
    #   
    #     TargetDefination 'Pods-FrameworkDemos_Example':
    #     ["BDWebImage", {"TTNetworkManager"=>["2.2.8.46-rc.18"]}]
    # ```
    #
    # Patch Target
    # ```
    # TargetDefination 'Pods':
    # [{"AFNetworking"=>["2.3"]}]
    # ```

    # 1. find all dependency patch_targetdef: patched_dependencies
    # 2. modify targetdef's depedencies recursively:
    #       - replace the requirement to the one in patched_dependencies 
    #       - add to target if not contained


    def get_raw_dependencies(target_defination)
        (target_defination.send :get_hash_value, 'dependencies') || []
    end

    all_dependencies_in_patch = begin
        def all_dependencies(target_defination)
            d = get_raw_dependencies(target_defination) 
            d += (target_defination.children.map do |subtarget|
                all_dependencies(subtarget)
            end.flatten || [])
            d
        end
        all_dependencies(patch_targetdef)
    end
    
    def transform_target_dependency_to_dict(dependencies)
        dict = {}
        dependencies.each do |name_or_hash|
            if name_or_hash.is_a?(Hash)
                name = name_or_hash.keys.first
                requirements = name_or_hash.values.first
                dict[name] = requirements
            else
                dict[name_or_hash] = {}
            end
        end
        dict
    end

    dependencies_dict_in_patch = transform_target_dependency_to_dict(all_dependencies_in_patch)

    def modify_target_defination_recursively(target_defination, &block)
        block.call(target_defination)
        target_defination.children.each { |t| modify_target_defination_recursively(t, &block) }
    end

    patch_file_all_targets_dict = begin
        targets = {}
        modify_target_defination_recursively(patch_targetdef) do |t|
            targets[t.name] = t
        end
        targets
    end

    patch_file_target_to_dependency_dict = begin
        dict = {}
        modify_target_defination_recursively(patch_targetdef) do |t|
            deps = get_raw_dependencies(t)
            dict[t.name] = transform_target_dependency_to_dict(deps)
        end
        dict
    end

    modified_dependencies_names = {} # pod name => target name

    modify_target_defination_recursively(targetdef) do |t|
        deps = get_raw_dependencies(t)
        new_deps = []
        deps_set = Set.new
        
        # modify origianl dependency
        deps.each do |name_or_hash|
            name = nil
            if name_or_hash.is_a?(Hash)
                name = name_or_hash.keys.first
            else
                name = name_or_hash
            end
            if dependencies_dict_in_patch.key? name
                new_deps << {name => dependencies_dict_in_patch[name]}
                modified_dependencies_names[name] = t
            else
                new_deps << name_or_hash
            end
            deps_set.add(name)
        end
        # add new dependency
        patching_deps = patch_file_target_to_dependency_dict[t.name]
        if not patching_deps.blank?
            patching_deps.each do |name, requirements|
                if not deps_set.include? name
                    new_deps << (requirements.empty? ? name : {name => requirements})
                    modified_dependencies_names[name] = t
                end
            end
        end

        t.send :set_hash_value, "dependencies", new_deps
    end

    # handle MODULAR_HEADERS & INHIBIT_WARNING
    [ 'inhibit_warnings',
      'use_modular_headers'].each do |method|
        modify_target_defination_recursively(targetdef) do |t|
            original_inhibit_warning_dict = t.send(:get_hash_value,method, {})
            original_inhibit_warning_dict.each do |key, value|
                # the key is "for_pods" or "not_for_pods" or "all"
                # Clear if patchfile have modified the pod
                next unless value.kind_of? Array
                value.reject! { |name| modified_dependencies_names.include? name}
            end

            patch_target = patch_file_all_targets_dict[t.name]
            if not patch_target.nil?
                patch_inhibit_warning_dict = patch_target.send(:get_hash_value,method, {})
                patch_inhibit_warning_dict.each do |key, value|
                    if value.kind_of? Array
                        original = original_inhibit_warning_dict[key] || []
                        original.concat(value).uniq! 
                        original_inhibit_warning_dict[key] = original
                    elsif [true, false].include? value # "all"
                        original_inhibit_warning_dict[key] = value
                    end
                end
            end
            # delete key if blank
            if original_inhibit_warning_dict.empty?
                t.send(:internal_hash).delete(method)
            end
        end
    end

    # handle CONFIGURATION_WHITELIST
    modify_target_defination_recursively(targetdef) do |t|
        key = "configuration_pod_whitelist"
        original = t.send :get_hash_value, key, {}
        original.each do |config_name, pods|
            # Clear if patchfile have modified the pod
            next unless pods.kind_of? Array
            pods.reject! { |name| modified_dependencies_names.include? name}
        end
        
        patch_target = patch_file_all_targets_dict[t.name]
        if not patch_target.nil?
            patched = patch_target.send :raw_configuration_pod_whitelist
            patched.each do |config_name, pods|
                next unless pods.kind_of? Array
                original_pods = original[config_name] || []
                original_pods.concat(pods).uniq! 
                original[config_name] = original_pods
            end
        end
        # delete key if blank
        if original.empty?
            t.send(:internal_hash).delete(key)
        end
    end
end

#modify_target_defination_recursively(target_defination, &block) ⇒ Object



109
110
111
112
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 109

def modify_target_defination_recursively(target_defination, &block)
    block.call(target_defination)
    target_defination.children.each { |t| modify_target_defination_recursively(t, &block) }
end

#patch(podfile:, with_podfile:) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 42

def patch(podfile:, with_podfile: )
    original = podfile
    patch = with_podfile

    merge_target_definitions_dependency(
        podfile.root_target_definitions[0], 
        patch.root_target_definitions[0]
        )
end

#patchfile(patch_file_path) ⇒ Object

Podfile


38
39
40
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 38

def patchfile(patch_file_path) # [Podfile]
    Pod::Podfile.from_ruby(patch_file_path)
end


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 227

def print_podfile_raw_dependencies(podfile)

    def print_target_def_and_its_dependencies(target_def, intend = "")
        puts "|\n"
        puts "| #{intend}TARGET_DEFINITION: #{target_def}"
        puts "| #{intend}INHIBIT_WARNING: #{target_def.send :raw_inhibit_warnings_hash}"
        puts "| #{intend}DEPS: #{target_def.send :get_hash_value, 'dependencies'}"
        target_def.children.each do |target_def|
            print_target_def_and_its_dependencies(target_def, intend + "    ")
        end
    end
    puts "#{podfile.defined_in_file}"
    podfile.root_target_definitions.each do |target_def|
        print_target_def_and_its_dependencies(target_def)
    end
end


229
230
231
232
233
234
235
236
237
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 229

def print_target_def_and_its_dependencies(target_def, intend = "")
    puts "|\n"
    puts "| #{intend}TARGET_DEFINITION: #{target_def}"
    puts "| #{intend}INHIBIT_WARNING: #{target_def.send :raw_inhibit_warnings_hash}"
    puts "| #{intend}DEPS: #{target_def.send :get_hash_value, 'dependencies'}"
    target_def.children.each do |target_def|
        print_target_def_and_its_dependencies(target_def, intend + "    ")
    end
end

#transform_target_dependency_to_dict(dependencies) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cocoapods-podfile_patch/podfile_patch.rb', line 93

def transform_target_dependency_to_dict(dependencies)
    dict = {}
    dependencies.each do |name_or_hash|
        if name_or_hash.is_a?(Hash)
            name = name_or_hash.keys.first
            requirements = name_or_hash.values.first
            dict[name] = requirements
        else
            dict[name_or_hash] = {}
        end
    end
    dict
end