class Pod::Patcher

Attributes

components_details[R]
hasInjectedSource[RW]
hasInjectedVemarsPods[RW]
podfile_dir[R]
template[R]

Public Class Methods

new(podfile_dir, baseline, components_details, source, git_url) click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 16
def initialize(podfile_dir, baseline, components_details, source, git_url)
  @podfile_dir = podfile_dir
  @template = PodfileTemplate.new(baseline, components_details, source)
  @components_details = components_details
  @git_url = git_url
  puts git_url
  @hasInjectedSource = false
  @hasInjectedVemarsPods = false
end

Public Instance Methods

execute() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 26
def execute
  insert_plist
  plugin_demos
  patch_podfile
end

Private Instance Methods

cleanup() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 89
def cleanup
  system("rm -rf ./.vemars")
end
clone_project() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 79
def clone_project
  system("git clone #{@git_url} ./.vemars")
  system("mv ./.vemars/DevPods ./DevPods")
end
find_podfile() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 93
def find_podfile
  locations = []

  Dir.entries(Dir.pwd).select { |e| e.end_with?(".xcworkspace") }.each do |xcworkspace|
    full_path = File.join(@podfile_dir, xcworkspace, "contents.xcworkspacedata")
    if File.file?(full_path)
      Document.new(File.new(full_path)).elements.each { |e|
        e.elements.each ("FileRef"){ |ref|
          locations << ref.attributes["location"]
        }
      }
    end
  end

  project = locations.reject { |e| e.include?("Pods/Pods.xcodeproj")  }.map { |location|
    File.expand_path("Podfile",File.dirname(File.join(@podfile_dir,location[6..-1])))
  }.select { |full_path|  File.file?(full_path) }

  if project.empty?

    full_path = File.join(@podfile_dir, "Podfile")
    return full_path if File.file?(full_path)

    full_path = File.join(@podfile_dir, "Project", "Podfile")
    return full_path if File.file?(full_path)
  end

  if project.empty?
    UI.puts "can not find Podfile in #{@podfile_dir}"
  end

  return project.first
end
inject_releasePodInTarget(line) click to toggle source

return post_inject

# File lib/cocoapods-vemars/services/patcher.rb, line 190
def inject_releasePodInTarget(line)
  if line.include?('target "') | line.include?("target '")
    return "\trelease_pod \n\tpod 'App/Debug', :path => './DevPods/', :inhibitat_warnings => false"
  end
  return ''
end
inject_source(anchor, line) click to toggle source

return (pre-inject, post-inject)

# File lib/cocoapods-vemars/services/patcher.rb, line 161
def inject_source(anchor, line)
  return '','' if @hasInjectedSource

  pre_inject = ''
  post_inject = ''
  if anchor.empty?
    pre_inject = @template.source_template
    @hasInjectedSource = true

  elsif line.include? anchor[anchor.length - 1]
    post_inject = @template.source_template
    @hasInjectedSource = true
  end
  return pre_inject,post_inject
end
inject_vemarsPodsDef(anchor, line) click to toggle source

return pre_inject

# File lib/cocoapods-vemars/services/patcher.rb, line 178
def inject_vemarsPodsDef(anchor, line)
  return '' if @hasInjectedVemarsPods

  if !anchor.empty? && line.include?(anchor[0])
    @hasInjectedVemarsPods = true
    return @template.releasePod, ''
  end
  return ''
end
insert_plist() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 129
def insert_plist
  puts "insert plist"
  plist_dir = Dir.pwd + "/onekit-config.plist"
  Dir.entries(Dir.pwd).select { |e| e.end_with?(".xcodeproj") }.each do |xcodeproj|
    full_path = File.join(@podfile_dir, xcodeproj)
    puts "insert plist in :#{full_path}"
    project = Xcodeproj::Project.open(full_path)
    file = project.new_file(plist_dir)
    main_target = project.targets.first
    main_target.add_resources([file])
    project.save
  end
end
patch_podfile() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 37
def patch_podfile
  podfile_path = find_podfile
  if podfile_path.nil? || podfile_path.empty?
    UI.puts "can not file project Podfile "
    return
  end
  UI.puts "patching project with podfile path #{podfile_path}"

  tempfile = Tempfile.new('podfile_temp')
  sourceAnchors = source_anchors(podfile_path)
  targetAnchors = target_anchor(podfile_path)

  File.open(podfile_path, 'r') do |f|
    f.each_line{ |line|
      pre_inject, post_inject = inject_source(sourceAnchors, line)
      if pre_inject.empty?
        pre_inject = inject_vemarsPodsDef(targetAnchors, line)
      end

      if post_inject.empty?
        post_inject = inject_releasePodInTarget(line)
      end

      if !pre_inject.empty?
        tempfile.puts pre_inject
      end
      tempfile.puts line
      if !post_inject.empty?
        tempfile.puts post_inject
      end
    }
  end
  tempfile.close
  FileUtils.mv(tempfile.path, podfile_path)
end
plugin_demos() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 73
def plugin_demos
  clone_project
  tailor_demos
  cleanup
end
source_anchors(podfile) click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 144
def source_anchors(podfile)
  result = []
  File.open podfile do |file|
    result = file.find_all { |line| line.include?('source') }
  end
  return result
end
tailor_demos() click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 84
def tailor_demos
  tailor = DemoTailor.new(@components_details)
  tailor.execute
end
target_anchor(podfile) click to toggle source
# File lib/cocoapods-vemars/services/patcher.rb, line 152
def target_anchor(podfile)
  result = []
  File.open podfile do |file|
    result = file.find_all { |line| line.include?('target "') | line.include?("target '")  }
  end
  return result
end