Class: PodBuilder::Command::Switch

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

Class Method Summary collapse

Class Method Details

.call(options) ⇒ Object



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

def self.call(options)
  Configuration.check_inited
  PodBuilder::prepare_basepath
  
  argument_pods = ARGV.dup
  
  unless argument_pods.count > 0 
    return -1
  end
  unless argument_pods.count == 1
    raise "\n\nSpecify a single pod to switch\n\n".red 
  end
  
  pod_name_to_switch = argument_pods.first
  
  check_not_building_subspec(pod_name_to_switch)

  installer, analyzer = Analyze.installer_at(PodBuilder::basepath, false)
  all_buildable_items = Analyze.podfile_items(installer, analyzer)

  raise "\n\nPod `#{pod_name_to_switch}` wasn't found in Podfile" unless all_buildable_items.map(&:root_name).include?(pod_name_to_switch)
  
  unless options.has_key?(:switch_mode)
    podfile_item = all_buildable_items.detect { |x| x.root_name == pod_name_to_switch }
    options[:switch_mode] = request_switch_mode(pod_name_to_switch, podfile_item)

    if options[:switch_mode].nil?
      return 0
    end
  end

  if options[:switch_mode] == "prebuilt"
    check_prebuilded(pod_name_to_switch)
  end

  podfile_path = PodBuilder::project_path("Podfile")
  podfile_content = File.read(podfile_path)

  pod_lines = []
  podfile_content.each_line do |line|
    if pod_name = Podfile.pod_definition_in(line, false)
      if pod_name.start_with?("PodBuilder/")
        pod_name = pod_name.split("PodBuilder/").last.gsub("_", "/")
      end

      unless pod_name.split("/").first == pod_name_to_switch
        pod_lines.push(line)
        next
      end

      if pod_name.include?("/")
        podfile_items = all_buildable_items.select { |x| x.name == pod_name }
      else
        podfile_items = all_buildable_items.select { |x| x.root_name == pod_name }
      end

      unless podfile_items.count > 0
        raise "\n\nPod `#{pod_name_to_switch}` wasn't found in Podfile\n".red
      end

      matches = line.match(/(#\s*pb<)(.*?)(>)/)
      if matches&.size == 4
        default_pod_name = matches[2]
      else
        puts "⚠️ Did not found pb<> entry, assigning default pod name #{pod_name}"
        default_pod_name = pod_name
      end

      unless podfile_item = all_buildable_items.detect { |x| x.name == default_pod_name }
        raise "\n\nPod `#{default_pod_name}` wasn't found in Podfile\n".red
      end
      podfile_item = podfile_item.dup

      indentation = line.detect_indentation

      case options[:switch_mode]
      when "prebuilt"
        line = indentation + podfile_item.prebuilt_entry + "\n"
      when "development"
        podfile_item.path = find_podspec(podfile_item)
        podfile_item.is_external = true

        line = indentation + podfile_item.entry + "\n"
      when "default"
        line = indentation + podfile_item.entry + "\n"
      else
        break
      end
    end

    pod_lines.push(line)
  end
  
  File.write(podfile_path, pod_lines.join)
  
  Dir.chdir(PodBuilder::project_path)
  system("pod install")

  return 0
end