Class: CocoapodsPruneLocalizations::Pruner

Inherits:
Object
  • Object
show all
Defined in:
lib/pruner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, user_options) ⇒ Pruner

Returns a new instance of Pruner.



5
6
7
8
9
10
11
# File 'lib/pruner.rb', line 5

def initialize(context, user_options)
  @sandbox_root = Pathname.new context.sandbox_root
  @pod_project = Xcodeproj::Project.open File.join(context.sandbox_root, 'Pods.xcodeproj')
  @user_options = self.class.user_options(context, user_options)
  @pruned_bundles_path = File.join(context.sandbox_root, "Pruned Localized Bundles")
  FileUtils.mkdir @pruned_bundles_path unless Dir.exist? @pruned_bundles_path
end

Class Method Details

.user_options(context, orig_user_opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pruner.rb', line 13

def self.user_options(context, orig_user_opts = {})
  user_options = {}
  if orig_user_opts["localizations"]
    user_options["localizations"] = orig_user_opts["localizations"].map do |loc|
      if loc.end_with? ".lproj"
        loc
      else
        loc + ".lproj"
      end
    end
  else
    user_options["localizations"] = Utils.user_project_localizations(context.umbrella_targets)
  end
  user_options
end

Instance Method Details

#prune!Object



41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/pruner.rb', line 41

def prune!
  rsrc_scripts_files = Hash.new
  self.resources_scripts(@pod_project["Targets Support Files"]).each do |file|
    rsrc_scripts_files[file] = File.readlines(file.real_path)
  end
  
  langs_to_keep = @user_options["localizations"] || []
  Pod::UI.title 'Pruning unused localizations' do

    #Group all the Pods
    pod_groups = @pod_project["Pods"].children.objects
    dev_pod_group = @pod_project["Development Pods"]
    pod_groups += dev_pod_group.children.objects if dev_pod_group

    pod_groups.each do |group|

      #Gather all Resources groups
      resGroups = group.recursive_children_groups.select do |group|
        group.name == "Resources"
      end
      next unless resGroups.length > 0
      
      markForRemoval = []
      trimmedBundlesToAdd = Hash.new

      resGroups.each do |resGroup|
        subTrimmedBundlesToAdd = Hash.new
        resGroup.files.each do |file|
          keep = true
          if file.path.end_with? ".lproj"
            keep = langs_to_keep.include?(File.basename(file.path))
          elsif file.path.end_with? ".bundle"
            trimmed_bundle = self.trimmed_bundle(file.real_path)
            if trimmed_bundle 
              subTrimmedBundlesToAdd[file.real_path] = trimmed_bundle
              keep = false
            end
          end
          if !keep
            markForRemoval << file
          end
        end
        trimmedBundlesToAdd[resGroup] = subTrimmedBundlesToAdd unless subTrimmedBundlesToAdd.length == 0              
      end

      #Remove file references indentified for removal
      if markForRemoval.length > 0
        Pod::UI.section "Pruning in #{group.path}" do
          markForRemoval.each do |file|
            Pod::UI.message "- Pruning #{file}"
            
            unless file.path.end_with? ".bundle"
              relative_path = file.real_path.relative_path_from @sandbox_root
              rsrc_scripts_files.each_value do |lines|
                for i in 0...lines.length
                  line = lines[i]
                  if line.include?(relative_path.to_s)
                    lines[i] = ""
                  end
                end
              end
            end
            file.remove_from_project
          end
        end
      end

      if trimmedBundlesToAdd.length > 0
        Pod::UI.section "Adding trimmed bundles to #{group.path}" do
          group_path = File.join(@pruned_bundles_path, group.path)
          FileUtils.mkdir group_path unless Dir.exist? group_path
          trimmedBundlesToAdd.each_pair do |resGroup, bundleArray|
            bundleArray.each_pair do |original_bundle_path, bundle_path|
              bundle_name = File.basename(original_bundle_path)
              new_bundle_path = File.join(group_path, bundle_name)
              FileUtils.rm_r(new_bundle_path) if File.exist? new_bundle_path
              FileUtils.mv(bundle_path, new_bundle_path)
              resGroup.new_reference(new_bundle_path)
              
              relative_path = original_bundle_path.relative_path_from(@sandbox_root).to_s
              new_relative_path = Pathname.new(new_bundle_path).relative_path_from(@sandbox_root).to_s
              rsrc_scripts_files.each_value do |lines|
                for i in 0...lines.length
                  lines[i] = lines[i].gsub(relative_path, new_relative_path)
                end
              end
            end
          end
        end
      end
      
    end
    
    rsrc_scripts_files.each_pair do |file, lines|
      fd = File.open(file.real_path, "w")
      lines.each do |line|
        fd.puts line unless (line == "") 
      end
      fd.close
    end
    @pod_project.save
  end
end

#resources_scripts(group) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pruner.rb', line 29

def resources_scripts(group)
  file_references = []
  group.children.objects.each do |children|
    if children.class == Xcodeproj::Project::Object::PBXFileReference && children.path.end_with?("resources.sh")
        file_references << children
    elsif children.class == Xcodeproj::Project::Object::PBXGroup
        file_references.concat(self.resources_scripts(children))
    end
  end
  file_references
end

#trimmed_bundle(bundle_path) ⇒ Object



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
# File 'lib/pruner.rb', line 145

def trimmed_bundle(bundle_path)
  langs_to_keep = @user_options["localizations"] || []
  return unless Dir.exist? bundle_path
  tmp_dir = Dir.mktmpdir
  changed_bundle = false
  Dir.foreach(bundle_path) do |file_name|
    if (file_name == "." || file_name == "..") 
      next
    end
    
    absolute_file_path = File.join(bundle_path, file_name)
    if file_name.end_with? ".lproj"
      if langs_to_keep.include?(file_name)
        FileUtils.cp_r(absolute_file_path, tmp_dir)
      else
        changed_bundle = true
      end
    elsif file_name.end_with? ".bundle"
      sub_trimmed_bundle = self.trimmed_bundle(absolute_file_path)
      if sub_trimmed_bundle
        sub_bundle_path = File.join(tmp_dir, file_name)
        FileUtils.mv(sub_trimmed_bundle, sub_bundle_path)
        changed_bundle = true
      else
        FileUtils.cp_r(absolute_file_path, tmp_dir)
      end
    else
      FileUtils.cp_r(absolute_file_path, tmp_dir)
    end
  end
  
  tmp_dir if changed_bundle
end