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
# 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 false
  end
  unless argument_pods.count == 1
    raise "\n\nSpecify a single pod to switch\n\n".red 
    return false
  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 true
    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)

        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")
end

.check_not_building_subspec(pod_to_switch) ⇒ Object



143
144
145
146
147
# File 'lib/pod_builder/command/switch.rb', line 143

def self.check_not_building_subspec(pod_to_switch)
  if pod_to_switch.include?("/")
    raise "\n\nCan't switch subspec #{pod_to_switch} refer to podspec name.\n\nUse `pod_builder switch #{pod_to_switch.split("/").first}` instead\n\n".red
  end
end

.check_prebuilded(pod_name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pod_builder/command/switch.rb', line 151

def self.check_prebuilded(pod_name)
  podspec_path = PodBuilder::basepath("PodBuilder.podspec")
  unless File.exist?(podspec_path)
    raise "Prebuilt podspec not found!".red
  end

  prebuilt_podspec = File.read(podspec_path)

  if !prebuilt_podspec.include?("s.subspec '#{pod_name}' do |p|")
    raise "\n\n#{pod_name} is not prebuilt.\n\nRun 'pod_builder build #{pod_name}'\n".red
  end
end

.find_podspec(podfile_item) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pod_builder/command/switch.rb', line 107

def self.find_podspec(podfile_item)
  unless Configuration.development_pods_paths.count > 0
    raise "\n\nPlease add the `development_pods_paths` in #{Configuration.dev_pods_configuration_filename} as per documentation\n".red
  end

  podspec_path = nil
  Configuration.development_pods_paths.each do |path|
    podspec = Dir.glob(File.expand_path("#{path}/**/#{podfile_item.root_name}*.podspec*"))
    podspec.select! { |x| !x.include?("/Local Podspecs/") }
    if podspec.count > 0
      podspec_path = Pathname.new(podspec.first).dirname.to_s
      break
    end
  end

  if podspec_path.nil?
    raise "\n\nCouln't find `#{pod_name}` sources in the following specified development pod paths: #{Configuration.development_pods_paths.join("\n")}\n".red
  end

  return podspec_path
end

.request_switch_mode(pod_name, podfile_item) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pod_builder/command/switch.rb', line 129

def self.request_switch_mode(pod_name, podfile_item)
  matches = podfile_item.entry.match(/(pod '.*?',)(.*)/)
  unless matches&.size == 3 
    raise "\n\nFailed matching pod name\n".red
  end

  default_entry = matches[2].strip

  modes = ["prebuilt", "development", "default"]
  mode_indx = ask("\n\nSwitch #{pod_name} to:\n1) Prebuilt\n2) Development pod\n3) Default (#{default_entry})\n\n") { |x| x.limit = 1, x.validate = /[1-3]/ }
  
  return modes[mode_indx.to_i - 1]
end