简体   繁体   English

在 2 个 iOS 应用程序之间共享通用代码:Swift Package Manager? 可可豆? 莫诺波?

[英]Share common code between 2 iOS apps: Swift Package Manager? Cocoapods? Monorepo?

I am about to start working on two new iOS apps, that will share a bunch of business logic, like the whole API layer plus the models.我即将开始开发两个新的 iOS 应用程序,它们将共享一堆业务逻辑,例如整个 API 层和模型。 Instead of copying the shared code in both projects and trying to keep that in sync, I want to use some way to share the code.我不想在两个项目中复制共享代码并试图保持同步,而是想使用某种方式来共享代码。

My first though was that I'd use the Swift Package Manager.我的第一个想法是我会使用 Swift Package Manager。 It's really easy to create a new package in Xcode, but I don't really know how to use the local version of the shared package in both apps while I am working on it, while also making it work for other devs (or CI!) checking out the apps.在 Xcode 中创建一个新包真的很容易,但我真的不知道如何在我工作时在这两个应用程序中使用共享包的本地版本,同时也让它适用于其他开发人员(或 CI! ) 检查应用程序。 Can't really commit the apps having a local path dependency, because that would only work for me, right?不能真正提交具有本地路径依赖性的应用程序,因为那只对我有用,对吗?

So, can I use a local version of that core package while working on the 2 apps and that core package, but still have it all pushed to GitHub and that it compiles on CI and for other devs too?那么,我可以在处理 2 个应用程序和该核心包时使用该核心包的本地版本,但仍将其全部推送到 GitHub 并在 CI 和其他开发人员上进行编译吗? Or would it only work on my machine since it would reference a local path?或者它只能在我的机器上工作,因为它会引用本地路径?

Would using Cocoapods with a private pod make things easier or would I run into the exact same problem of working on a local path dependency on my computer, but wanting to have it work for other devs too?将 Cocoapods 与私有 Pod 一起使用会使事情变得更容易,还是会遇到完全相同的问题,即在我的计算机上处​​理本地路径依赖项,但希望它也适用于其他开发人员?

Or should I just go with a monorepo containing both apps and the shared code, and just have everything use relative paths?或者我应该使用包含应用程序和共享代码的 monorepo,并且让所有内容都使用相对路径? Would a SPM package inside of the monorepo be helpful in this situation, or just use relative paths in both apps?在这种情况下,monorepo 内的 SPM 包会有所帮助,还是只在两个应用程序中使用相对路径?

I would recommend creating a (private) cocoapod for your business logic.我建议为您的业务逻辑创建一个(私有)cocoapod。 Both your apps can refer to that cocoapod either via the release, somewhere in the repo or as a development pod, as needed.您的两个应用程序都可以根据需要通过发行版、repo 中的某个位置或作为开发 pod 来引用该 cocoapod。

In order to avoid editing Podfile all the time, you can control your pod's source via external files and/or environment variables.为了避免一直编辑Podfile ,您可以通过外部文件和/或环境变量来控制 Pod 的源。 Here's what I do in a couple of my projects:这是我在我的几个项目中所做的:

def library_pod
  src = ENV["POD_SOURCE"]
  if src == nil && File.file?(".pod_source")
    src = File.foreach(".pod_source").first.chomp
  end
  src = (src || "").split(":")
  case src[0]
  when "dev"
    # local dev pod, editable
    pod 'MyLibrary', :path => '../mylibrary', :inhibit_warnings => false
  when "head"
    pod 'MyLibrary', :git => 'https://github.com/mycompany/MyLibrary'
  when "branch"
    pod 'MyLibrary', :git => 'https://github.com/mycompany/MyLibrary', :branch=> src[1]
  else
    # default: use the release version 
    pod 'MyLibrary'
  end
end

target 'MyApp' do
  pod 'Pod1'
  pod 'Pod2'
  library_pod
end

where usually I have POD_SOURCE=dev in the environment on my dev machine.通常我的开发机器上的环境中有POD_SOURCE=dev .pod_source contains release on master, and whatever branch name is appropriate in feature branches, so that my CI can to the right thing. .pod_source包含 master 上的release ,以及特性分支中适合的任何分支名称,以便我的 CI 可以正确地进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM