Class: PodBuilder::Command::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/pod_builder/command/clean.rb

Class Method Summary collapse

Class Method Details

.call(options) ⇒ Object



7
8
9
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
35
36
37
38
39
40
# File 'lib/pod_builder/command/clean.rb', line 7

def self.call(options)
  Configuration.check_inited
  PodBuilder::prepare_basepath

  update_repo = options[:update_repos] || false
  installer, analyzer = Analyze.installer_at(PodBuilder::basepath)
  all_buildable_items = Analyze.podfile_items(installer, analyzer)

  podspec_names = all_buildable_items.map(&:podspec_name)
  rel_paths = all_buildable_items.map(&:prebuilt_rel_path)

  base_path = PodBuilder::basepath("Rome")
  framework_files = Dir.glob("#{base_path}/**/*.framework")
  puts "Looking for unused frameworks".yellow
  clean(framework_files, base_path, rel_paths)

  rel_paths.map! { |x| "#{x}.dSYM"}

  base_path = PodBuilder::basepath("dSYM/iphoneos")
  dSYM_files_iphone = Dir.glob("#{base_path}/**/*.dSYM")
  puts "Looking for iPhoneOS unused dSYMs".yellow    
  clean(dSYM_files_iphone, base_path, rel_paths)

  base_path = PodBuilder::basepath("dSYM/iphonesimulator")
  dSYM_files_sim = Dir.glob("#{base_path}/**/*.dSYM")
  puts "Looking for iPhone Simulator unused dSYMs".yellow
  clean(dSYM_files_sim, base_path, rel_paths)

  puts "Looking for unused sources".yellow
  clean_sources(podspec_names)

  puts "\n\nšŸŽ‰ done!\n".green
  return true
end

.clean(files, base_path, rel_paths) ⇒ Object



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
# File 'lib/pod_builder/command/clean.rb', line 68

def self.clean(files, base_path, rel_paths)
  files = files.map { |x| [Pathname.new(x).relative_path_from(Pathname.new(base_path)).to_s, x] }.to_h

  paths_to_delete = []
  files.each do |rel_path, path|
    unless !rel_paths.include?(rel_path)
      next
    end

    paths_to_delete.push(path)
  end

  paths_to_delete.each do |path|
    confirm = ask("\n#{path} unused.\nDelete it? [Y/N] ") { |yn| yn.limit = 1, yn.validate = /[yn]/i }
    if confirm.downcase == 'y'
      PodBuilder::safe_rm_rf(path)
    end
  end

  current_dir = Dir.pwd
  Dir.chdir(base_path)
  # Before deleting anything be sure we're in a git repo
  h = `git rev-parse --show-toplevel`.strip()
  raise "\n\nNo git repository found in current folder `#{Dir.pwd}`!\n".red if h.empty?    
  system("find . -type d -empty -delete") # delete empty folders
  Dir.chdir(current_dir)
end

.clean_sources(podspec_names) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pod_builder/command/clean.rb', line 42

def self.clean_sources(podspec_names)        
  base_path = PodBuilder::basepath("Sources")

  repo_paths = Dir.glob("#{base_path}/*")

  paths_to_delete = []
  repo_paths.each do |path|
    podspec_name = File.basename(path)

    unless !podspec_names.include?(podspec_name)
      next
    end

    paths_to_delete.push(path)
  end

  paths_to_delete.flatten.each do |path|
    confirm = ask("#{path} unused.\nDelete it? [Y/N] ") { |yn| yn.limit = 1, yn.validate = /[yn]/i }
    if confirm.downcase == 'y'
      PodBuilder::safe_rm_rf(path)
    end
  end
end