module Flappy::UpdatePod

Public Instance Methods

check_and_find_podfile() click to toggle source

check

# File lib/flappy/util/update_pod.rb, line 202
def check_and_find_podfile
  podfile_path = ''

  Dir.glob("#{@work_dir}/**/[Pp]odfile").each do |name|
    unless File.directory?(name)
      podfile_path = name
    end
  end

  if podfile_path.blank?
    log_iOS('No Podfile Found')
  else
    log_iOS("Found Podfile: #{podfile_path}")
  end

  podfile_path
end
check_and_find_podfile_lock() click to toggle source
# File lib/flappy/util/update_pod.rb, line 220
def check_and_find_podfile_lock
  podfile_lock_path = ''

  Dir.glob("#{@work_dir}/**/[Pp]odfile.lock").each do |name|
    unless File.directory?(name)
      podfile_lock_path = name
    end
  end

  if podfile_lock_path.blank?
    log_iOS('No Podfile.lock Found')
  else
    log_iOS("Found Podfile.lock: #{podfile_lock_path}")
  end

  podfile_lock_path
end
initialize_pod_cmd(args, options) click to toggle source
# File lib/flappy/util/update_pod.rb, line 64
def initialize_pod_cmd(args, options)
  pod_cmd = ''

  unless @podfile_path.blank?
    pod_work_dir = File.dirname(@podfile_path)

    if options[:'update-pod'] || options[:'only-update-souche-pod']
      pod_cmd = "cd #{pod_work_dir}; "

      if options[:'only-update-souche-pod']
        repo_cmd = update_pod_repos(args, options)
        pod_cmd += repo_cmd unless repo_cmd.blank?

        # pod repo update之后pod update之前
        if options[:'pod-env-test']
          pod_cmd += "pod_env_test=1 "
        end

        pod_cmd += "pod update --no-repo-update --verbose"
      else
        # pod repo update之后pod update之前
        if options[:'pod-env-test']
          pod_cmd += "pod_env_test=1 "
        end

        if options[:'no-repo-update']
          pod_cmd += "pod update --no-repo-update --verbose"
        else
          pod_cmd += "pod update --verbose"
        end
      end
    end


  else
    log_iOS('podfile path is empty')
  end

  pod_cmd
end
merge_spec_hash(spec_hash_podfile, spec_hash_podfile_lock) click to toggle source
# File lib/flappy/util/update_pod.rb, line 46
def merge_spec_hash(spec_hash_podfile, spec_hash_podfile_lock)
  if spec_hash_podfile.blank? || spec_hash_podfile_lock.blank?
    return
  end
  spec_hash_podfile.each do |key_podfile, value_podfile|
    spec_hash_podfile_lock.each do |key_podfile_lock, value_podfile_lock|
      if key_podfile == key_podfile_lock
        spec_hash_merged = value_podfile.merge(value_podfile_lock)
        spec_hash_podfile.store(key_podfile, spec_hash_merged)
        break
      end
    end
  end

  log_iOS("spec_hash: \n#{spec_hash_podfile}")
  @spec_hash = spec_hash_podfile
end
read_podfile() click to toggle source

read

# File lib/flappy/util/update_pod.rb, line 106
def read_podfile
  if @podfile_path.blank?
    return
  end

  spec_hash = {}
  File.foreach(@podfile_path).with_index do |line, line_num|
    # 突出显示打印匹配的部分
    # puts line.gsub(/^pod .*'$/) {  |str|  "<<#{str}>>"   }
    # 打印匹配的部分
    # line.gsub(/^pod .*'$/) {  |str|   puts "#{str}"   }
    # puts line.scan(/^pod .*'$/)

    line_trim = line.gsub(/\s+/, "") unless line.blank?
    # matched_string = line_trim.scan(/^pod.*'$/)[0] unless line_trim.blank?

    matched_list = line_trim.scan(/^pod.*'$/) unless line_trim.blank?
    unless matched_list.nil?
      matched_string = matched_list[0] if matched_list.count > 0
    end

    unless matched_string.blank?
      name_version = matched_string.split(/'(.*?)'/)
      spec_name = name_version[1]
      podfile_spec_version = name_version[3]

      unless spec_name.blank?
        version_hash = {}
        unless podfile_spec_version.blank?
          version_hash.store('Podfile', podfile_spec_version)
        else
          version_hash.store('Podfile', '')
        end
        spec_hash.store(spec_name, version_hash)
      end
    end
  end

  spec_hash
end
read_podfile_lock() click to toggle source
# File lib/flappy/util/update_pod.rb, line 147
def read_podfile_lock
  if @podfile_lock_path.blank?
    return
  end

  textfile = "#{Dir.tmpdir}/podfile_lock_#{Time.now.strftime('%Y%m%d%H%M%S')}.txt"
  podfile_lock = File.read(@podfile_lock_path)
  File.open(textfile, "w") do |f|
    in_header = true
    podfile_lock.each_line do |line|
      if in_header && /PODS:/ !~ line
        next
      else
        in_header = false
      end

      break if /DEPENDENCIES:/ =~ line

      f.write line
    end
  end

  spec_hash = {}
  if File.exists?(textfile)
    File.foreach(textfile).with_index do |line|
      unless line.blank?
        matched_list = line.scan(/^  - .*$/) unless line.blank?
        unless matched_list.nil?
          matched_string = matched_list[0] if matched_list.count > 0

          unless matched_string.blank?
            spec_name = matched_string.split(/  - (.*?) \(/)[1]
            podfile_lock_spec_version = matched_string.split(/ \((.*?)\)/)[1]

            unless spec_name.blank?
              version_hash = {}
              unless podfile_lock_spec_version.blank?
                version_hash.store('Podfile.lock', podfile_lock_spec_version)
              else
                version_hash.store('Podfile.lock', '')
              end
              spec_hash.store(spec_name, version_hash)
            end
          end
        end
      end
    end
  end

  FileUtils.rm_rf(textfile)

  spec_hash
end
save_pod_file_and_lock() click to toggle source
# File lib/flappy/util/update_pod.rb, line 37
def save_pod_file_and_lock
  @dest_pod_file_path = File.join(@app_output_archive_info_path, "#{@start_time}_#{@app_dir_name}_Podfile")
  FileUtils.cp_r(@podfile_path, @dest_pod_file_path) if File.exists?(@podfile_path)

  @dest_pod_file_lock_path = File.join(@app_output_archive_info_path, "#{@start_time}_#{@app_dir_name}_Podfile_lock")
  FileUtils.cp_r(@podfile_lock_path, @dest_pod_file_lock_path) if File.exists?(@podfile_lock_path)
end
save_spec_hash_to_file() click to toggle source
# File lib/flappy/util/update_pod.rb, line 32
def save_spec_hash_to_file
  @spec_hash_file_path = File.join(@app_output_archive_info_path, "#{@start_time}_#{@app_dir_name}_Pod_json")
  File.write(@spec_hash_file_path, @spec_hash) unless @spec_hash.blank?
end
update_pod_if_needed(args, options) click to toggle source
# File lib/flappy/util/update_pod.rb, line 6
def update_pod_if_needed(args, options)
  @podfile_path = check_and_find_podfile
  spec_hash_podfile = read_podfile

  pod_cmd = initialize_pod_cmd(args, options)
  unless pod_cmd.blank?
    log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Updating pods......')
    log_iOS("pod cmd: #{pod_cmd}")

    log_iOS_pod(pod_cmd)
    if $?.to_i != 0
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Pod update failed')
      exit 1
    else
      log_iOS('>>>>>>>>>>>>>>>>>>>>>>>>>>>> Pod update succeed')
    end
  end

  @podfile_lock_path = check_and_find_podfile_lock
  spec_hash_podfile_lock = read_podfile_lock

  merge_spec_hash(spec_hash_podfile, spec_hash_podfile_lock)
  save_spec_hash_to_file
  save_pod_file_and_lock
end
update_pod_repos(args, options) click to toggle source
# File lib/flappy/util/update_pod.rb, line 239
def update_pod_repos(args, options)
  if options[:'only-update-souche-pod']
    repos_to_update = []
    repo_path = File.expand_path("~/.cocoapods/repos")
    Dir.glob(repo_path + "/*").each do |name|   # 一层
      if File.directory?(name)
        basename = File.basename(name)
        if basename != "master" && basename != ".DS_Store"
          repos_to_update << basename
        end
      end
    end
    log_iOS("repos_to_update: #{repos_to_update}")

    repo_cmd = ''
    repos_to_update.each { |repo|
      repo_cmd += "pod repo update --verbose #{repo}; "  # 只更新souche repo
    }
  else
    repo_cmd = "pod repo update --verbose"
  end

  repo_cmd
end