处理Podfile的预安装挂钩时发生错误

时间:2015-07-23 10:58:58

标签: install cocoapods podfile

我运行pod install时遇到错误。

[!] An error occurred while processing the pre-install hook of the Podfile.

undefined method `pods' for #<Pod::Installer:0x007f9f93a66b90>

/Users/XieYunjia/warmupApp/Podfile:49:in `block (2 levels) in from_ruby'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-core-0.38.0/lib/cocoapods-core/podfile.rb:153:in `call'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-core-0.38.0/lib/cocoapods-core/podfile.rb:153:in `pre_install!'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:731:in `run_podfile_pre_install_hook'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:719:in `block in run_podfile_pre_install_hooks'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/user_interface.rb:140:in `message'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:718:in `run_podfile_pre_install_hooks'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:142:in `block in download_dependencies'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/user_interface.rb:59:in `section'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:139:in `download_dependencies'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/installer.rb:104:in `install!'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/command/project.rb:71:in `run_install_with_update'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/command/project.rb:101:in `run'
/Library/Ruby/Gems/2.0.0/gems/claide-0.9.1/lib/claide/command.rb:312:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/lib/cocoapods/command.rb:48:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.38.0/bin/pod:44:in `<top (required)>'
/usr/bin/pod:23:in `load'
/usr/bin/pod:23:in `<main>'

这是我的Podfile的内容。

platform :ios, "7.0"

pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => '2.5.4'
pod 'MagicalRecord', :git => 'https://github.com/magicalpanda/MagicalRecord.git', :tag => 'v2.3.0-beta.5',:inhibit_warnings => true
pod 'ReactiveCocoa', '~> 2.4.7'

......

pod 'LTNavigationBar', '~> 2.0.1'
pod 'TSMessages' ,:head

# DEBUG
pod 'SocketRocket', '~> 0.2.0' ,:inhibit_warnings => true,     :configurations => ['Debug']
pod 'PonyDebugger', '~> 0.4.3' ,:inhibit_warnings => true, :configurations => ['Debug']
pod 'Reveal-iOS-SDK', :configurations => ['Debug']

target :warmupTests, :exclusive => true do
  pod 'Kiwi'
end

#remove all unsupported localization files
pre_install do |installer|
  supported_locales = ['base' , 'zh-hans', 'en' , 'english']

  installer.pods.each do |pod|
    %x[ find "#{pod.root}" -name '*.lproj' ].split.each do |bundle|
      if (!supported_locales.include?(File.basename(bundle,         ".lproj").downcase))
        puts "Removing #{bundle}"
        FileUtils.rm_rf(bundle)
      end
    end
  end
end

如果我删除了下面显示的最后几行,则此错误将消失。

#remove all unsupported localization files
pre_install do |installer|
  supported_locales = ['base' , 'zh-hans', 'en' , 'english']

  installer.pods.each do |pod|
    %x[ find "#{pod.root}" -name '*.lproj' ].split.each do |bundle|
      if (!supported_locales.include?(File.basename(bundle,         ".lproj").downcase))
        puts "Removing #{bundle}"
        FileUtils.rm_rf(bundle)
      end
    end
  end
end

导致此错误的原因是什么,我该如何解决? 抱歉我的英语不好。

4 个答案:

答案 0 :(得分:6)

您在CocoaPods 0.38+上收到此错误消息,只需将installer.project更改为installer.pods_project即可解决此问题。

有关详细信息,请参阅https://github.com/CocoaPods/CocoaPods/issues/3918

  

是的,这是由于您在Podfile中使用的hooks API的更改,有关详细信息,请参阅http://blog.cocoapods.org/CocoaPods-0.38/

答案 1 :(得分:2)

Cocoapods最近改为installer。你会想做这样的事情:

pre_install do |installer|
    supported_locales = ['base', 'zh-hans', 'en', 'english']

    Dir.glob(File.join(installer.sandbox.pod_dir('FormatterKit'), '**', '*.lproj')).each do |bundle|
        if (!supported_locales.include?(File.basename(bundle, ".lproj").downcase))
            puts "Removing #{bundle}"
            FileUtils.rm_rf(bundle)
        end
    end
end

答案 2 :(得分:1)

尝试使用此类内容以实现向后兼容性

pre_install do |installer_or_rep|
    # backwards compatibility info http://blog.cocoapods.org/CocoaPods-0.38/
    supported_locales = ['base' , 'zh-hans', 'en' , 'english']

    if installer_or_rep.respond_to?(:installer)
        # pre 0.38.0
        installer_or_rep.pods.each do |pod|
            delete_unsupported_locales(pod.root, supported_locales)
        end
    else
        # post 0.38.0
       delete_unsupported_locales(installer_or_rep.sandbox.root, supported_locales)
    end
end

def delete_unsupported_locales(root, supported_locales)
    Dir.glob(File.join(root, '**', '*.lproj')).each do |bundle|
        if (!supported_locales.include?(File.basename(bundle, ".lproj").downcase))
            puts "Removing #{bundle}"
            FileUtils.rm_rf(bundle)
        end
    end
end

答案 3 :(得分:0)

更新了适用于pod v1.3.1的脚本(截至4/10/2017)

platform :ios, '9.0'

target 'MyApp-iOS' do

    use_frameworks!

    # Pods for MyApp-iOS

    # Remove unused languages from Pods
    pre_install do |installer|
        supported_locales = ['base', 'en', 'english']
        delete_unsupported_locales(installer.sandbox.root, supported_locales)
    end
end

def delete_unsupported_locales(root, supported_locales)
    Dir.glob(File.join(root, '**', '*.lproj')).each do |bundle|
        if (!supported_locales.include?(File.basename(bundle, ".lproj").downcase))
            puts "Removing #{bundle}"
            FileUtils.rm_rf(bundle)
        end
    end
end