【问题标题】:Preserve folder structure Cocoa Pods保留文件夹结构 Cocoa Pods
【发布时间】:2014-01-16 17:33:23
【问题描述】:

我已经使用本教程创建了一个简单且私有的 pod:http://pablin.org/2013/05/18/cocoapods-for-internal-libraries/

实际上我的仓库只有一组类

一切正常,我可以完美地安装我的 pod。唯一的问题是所有文件都安装在主文件夹中,因此它不会保留文件夹结构。

我有这个文件夹结构,存储库名为:myRepository

 Classes 
 |
 ------ foo.h and foo.m
 ------ Controller Layer
        |
        ----------- foo2.h and foo2.m
 ------ ViewLayer
        |
        ----------- foo3.h and foo3.m

所有文件都复制到名为 myRepository 的文件夹中。

这是我的 podspec:

Pod::Spec.new do |s|

  s.name         = "fooClasses"
  s.version      = "0.0.1"
  s.summary      = "Common clases of foo"

  s.homepage     = "http://foo.com"

  s.license      = 'BSD'
  s.license      = { :type => 'Foo License', :file => 'LICENSE.txt' }

  s.author       = { "me" => "me@me.com" }

  s.platform     = :ios, '7.0'

  s.source       = { :git => "https://github.com/myRepository.git", :tag => "0.0.1" }

  s.source_files  = "**/*.{h,m}"

  s.requires_arc = true
end

我已尝试使用 s.preserve_path = "*" 和 s.preserve_path = "Classes"

有什么想法吗?

谢谢!!!!!!!!!

【问题讨论】:

标签: ios objective-c cocoapods


【解决方案1】:

我已经实现了使用 subspec 创建自己的文件夹。

您可以使用此行创建子规范:

s.subspec 'folder name' do |ss|
    ss.source_files = 'files'
    ss.frameworks = 'frameworks'
end

ss.frameworks 不是强制性的。

您可以从 CocoaPods 邮件列表中看到完整的答案:

https://groups.google.com/forum/#!topic/cocoapods/0kV8r2xnqA8

感谢 mcitrrus

我更喜欢遵循 ​​AFNetworking pod 规范:

https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking.podspec

【讨论】:

  • 我认为这不是 OP 想要实现的目标。子规范旨在模块化您的 Pod - 即。你可以有可选的组件。我认为他只是想在 Pods 项目中维护某种文件夹层次结构。 preserve_path 似乎也没有这样做,因为根据文档 By default, CocoaPods removes all files that are not matched by any of the other file pattern.
  • @itsthejb 好吧,回答的人是 OP :]
  • 我个人认为这不是你想要的,也不应该这样做。子规范不引用文件夹,因为 iOS 不使用名称间距。我相信子规格适用于吊舱内的吊舱。以AFNetworking为例。
【解决方案2】:

如果您经常使用开发 pod,那么 cocoapods 创建的默认项目结构有很多问题:

  • 如果您的source_files / resources 等的根目录中没有至少一个文件,则根目录在项目中没有对应的组。

  • 组的位置 指向您的 pod 目录的根目录(不一定是您的源文件所在的位置)。这意味着您不能只右键单击组和“添加文件”,因为它会将文件放在错误的位置。

  • 组和文件的顺序在模块之间有所不同,因为事物是按名称排序的,并且您的 pod 显然具有不同的名称,这些名称可能出现在 Frameworks / Pods / Dependencies / Support Files 之前。


我一直在寻找解决上述所有问题的解决方案,并最终提出以下解决方案:

先决条件

我使用如下目录结构设置我的 pod(示例显示名为 SomeLib 的 pod)

Example/
   SomeLib-Example/...
   SomeLib-Example.xcodeproject

fix_project_structure (this is an empty text file)

SomeLib.podspec

Sources/
   FolderA/
      ClassA.swift
   FolderB/
      ClassA.swift

Tests/
   fix_project_structure (another empty text file)
   FolderA/
      ClassATests.swift
   FolderB/
      ClassBTests.swift

第 1 步 - 让组镜像目录层次结构

在您的podspec 文件中,我们利用位于根目录中的空fix_project_structure 来修复组结构...

  s.source_files = 'Sources/**/*.{swift}', 'fix_project_structure'
  s.resource_bundles = {
    s.name => ['Sources/**/*.{xcassets,json,storyboard,xib,xcdatamodeld}', 'fix_project_structure']
  }

  s.test_spec 'Tests' do |test_spec|
    test_spec.source_files = 'Tests/**/*', 'Tests/fix_project_structure'
  end  

第 2 步 - 重新排序和修复组位置

这实际上是一个可选步骤(见评论)

将以下post_integrate 步骤添加到您的Podfile...

post_integrate do |installer|

  # Fix up the Development Pods so they are easier to work with.
  # The following changes are made to any pod that contains a `fix_project_structure` file
  #  - Removes the (now redundant) `fix_project_structure` file
  #  - Moves the root {project_name} group to the top
  #  - Moves the {project_name}/Sources group to the top
  #  - Moves the {project_name}/Tests group below {project_name}/Sources
  #  - Repoints the {project_name}/Tests group so it reflects its location on disc
  # Note this script makes no functional changes to the pod so you should be able to comment out 
  #   all of this script and have things continue to work just fine
  installer.generated_projects.each do |project|

    fix_files = project.files.select { |file| File.basename(file.path) == "fix_project_structure" }
    project_name = "#{File.basename(project.path, '.xcodeproj')}"
    next if fix_files.nil? || fix_files.empty? || project_name.nil?

    puts "#{project_name} -> #{project.path}"
    puts "  `fix_project_structure` file(s) FOUND"
    puts "    Removing `fix_project_structure` file(s) from project entirely"
    fix_files.each { |fix_file| fix_file.remove_from_project }

    project_group = project.groups.find.find { |group| group.to_s == project_name }
    next if project_group.nil?

    puts "  `#{project_name}` group FOUND"
    puts "    Moving `#{project_name}` group to top"
    project.main_group.children.delete(project_group)
    project.main_group.children.insert(0, project_group)

    sources_group = project_group.groups.find.find { |subgroup| subgroup.to_s == 'Sources' }
    if sources_group.nil? != true
      puts "    `#{project_name}/Sources` group FOUND"
      puts "      Moving `#{project_name}/Sources` group to top"
      project_group.children.delete(sources_group)
      project_group.children.insert(0, sources_group)

      tests_group = project_group.groups.find.find { |subgroup| subgroup.to_s == 'Tests' }
      if tests_group.nil? != true
        puts "    `#{project_name}/Test` group FOUND"

        puts "      Moving `#{project_name}/Test` group below `#{project_name}/Sources`"
        project_group.children.delete(tests_group)
        project_group.children.insert(1, tests_group)

        puts "      Adjusting `#{project_name}/Test` group and immediate children location"
        tests_group.set_path("#{tests_group.real_path}/Tests")
        tests_group.children.each do |tests_child|
          tests_child_path_before = tests_child.path
          tests_child.set_path("#{tests_child.real_path.to_s.gsub('/Tests/Tests/', '/Tests/')}")
          #puts "      '#{tests_child.path.to_s}' (previously '#{tests_child_path_before}')"
        end
      end
    end

    project.save

  end

end

【讨论】:

    【解决方案3】:

    由于 0.36 版本的 cocoapods 中所做的更改,此答案/问题已过时。

    PR 2647 添加了默认分组行为,只要您使用的是 ":path" pod。从 1.3.1 开始,行为是它将遍历 /Classes/... 结构,直到它看到一个文件或第二个子文件夹并从那里开始在 .xcodeproj 文件中进行分组。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2022-06-29
    • 2013-06-18
    • 2020-11-04
    • 2019-01-23
    相关资源
    最近更新 更多