cocoapods自动清理小脚本

公司的项目使用cocoaPods管理插件包的,随着业务的发展,已经搞出250左右的pod了,多更新几次,小固态就扛不住了。。想着手动删也比较累,毕竟有些pod还是要留着用个把天的,一波全删了还得装回来,还是搞个小脚本吧如下:

#!/usr/bin/env ruby

require 'fileutils'
require 'open3'

KReleasePath = '/Library/Caches/CocoaPods/Pods/Release'
KExternalPath = '/Library/Caches/CocoaPods/Pods/External'

KUser = ENV['USER']
KleftCount = 2
KlastVisitTime = 2

def deletePodWithPath(path)
    if File.directory?(path)
      #~/Library/Caches/CocoaPods/Pods/Release/..
      podsDirArray = Dir.entries(path).select { |item| item[0]!='.' }
      if podsDirArray.size > KleftCount
        podsDirArray.sort! do |dir1, dir2|
          dir1 <=> dir2
        end
        toDeleteCount = podsDirArray.size - KleftCount
        for dir in podsDirArray do
          subPath = path + '/' + dir
          if File.mtime(subPath)
            interval = (Time.now.to_i - File.mtime(subPath).to_i)/(60*60*24)
            if interval > KlastVisitTime
              toDeleteCount -= 1
              Open3.popen3("rm -r #{subPath}") do |stdin, stdout, stderr, wait_thr|
                while err = stderr.gets do 
                  puts err
                end
              end
            end
            if toDeleteCount <= 0
              break
            end
          end
        end
      end
    end
end

releasePath = '/Users/'+KUser+'/Library/Caches/CocoaPods/Pods/Release'
dirArray = Dir.entries(releasePath).select { |item| item[0]!='.' }
dirArray.each do |path|
  subPath = releasePath +'/' + path
  deletePodWithPath(subPath)
end
puts "Total checked #{releasePath} pods: #{dirArray.size}"

externalPath = '/Users/'+KUser+'/Library/Caches/CocoaPods/Pods/External'
dirArray = Dir.entries(externalPath).select { |item| item[0]!='.' }
dirArray.each do |path|
  subPath = externalPath +'/' + path
  deletePodWithPath(subPath)
end
puts "Total checked #{externalPath} pods: #{dirArray.size}"

exit 1 unless $?.success?

没考虑太多,适用于我司,大概是留下一个pod中最近的2个版本,老版本再见。老哥们可以试试,不保证没bug,记得先搞下chmod 777

你可能感兴趣的:(cocoapods自动清理小脚本)