I've been using Cocoapods to manage dependencies for my Swift project. I came across this package which is not listed at Cocoapods. Instead it suggests using Swift Package Manager. However, whenever I try to use Swift Package Manager to do basically anything, it ends up completely destroying my entire project.
我一直在使用Cocoapods来管理我的Swift项目的依赖关系。我偶然发现了这个不在可可豆中列出的包裹。相反,它建议使用Swift包管理器。然而,每当我尝试使用Swift包管理器做任何事情时,它最终会完全破坏我的整个项目。
So, in order to figure out how to actually use Swift Package Manager, I'm playing with it in a test project.
因此,为了弄清楚如何实际使用Swift包管理器,我在一个测试项目中使用它。
Here's what I've tried:
这是我试过:
- Create a new project from Xcode
- 从Xcode创建一个新项目
File -> New -> Project -> Cocoa App
文件->新->项目->可可应用
Product Name: basic-ssh-test
产品名称:basic-ssh-test
This creates a basic application which loads a blank window when I hit "Run". Just for fun I added print("test")
to the applicationDidFinishLaunching
function in my AppDelegate
so the debug window says "test" when I run the program.
这将创建一个基本的应用程序,当我点击“Run”时,它将加载一个空白窗口。为了好玩,我在AppDelegate中为applicationDidFinishLaunching函数添加了print(“test”),这样在运行程序时,调试窗口会显示“test”。
Now I want to add the Shout
package as a dependency.
现在,我想添加一个“呼叫包”作为一个依赖项。
swift package init --type executable
- swift包init——输入可执行文件
This creates the following files:
这将创建以下文件:
Creating executable package: basic-ssh-test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/basic-ssh-test/main.swift
Creating Tests/
Now I'm going to add the "Shout" dependency to my new Package.swift
file:
现在我要将“呼喊”依赖项添加到我的新包中。快速文件:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "basic-ssh-test",
dependencies: [
.package(url: "https://github.com/jakeheis/Shout", from: "0.2.0")
],
targets: [
.target(
name: "basic-ssh-test",
dependencies: ["Shout"]),
]
)
And I'll pull in the dependencies:
我将引入依赖项:
swift package resolve
- 斯威夫特包解决
This results in the dependencies being pulled into the .build
directory:
这导致依赖项被拖到.build目录中:
Fetching https://github.com/jakeheis/Shout
Fetching https://github.com/jakeheis/CSSH
Fetching https://github.com/IBM-Swift/BlueSocket
Cloning https://github.com/IBM-Swift/BlueSocket
Resolving https://github.com/IBM-Swift/BlueSocket at 0.12.91
Cloning https://github.com/jakeheis/CSSH
Resolving https://github.com/jakeheis/CSSH at 1.0.3
Cloning https://github.com/jakeheis/Shout
Resolving https://github.com/jakeheis/Shout at 0.3.0
Now I regenerate the xcodeproj
file:
现在我重新生成xcodeproj文件:
swift package generate-xcodeproj
- 斯威夫特包generate-xcodeproj
Now when I open the xcodeproj
file there is a new group called Sources
that has a single directory basic-ssh-test
with a single swift file in it main.swift
with print("Hello, world!")
.
现在,当我打开xcodeproj文件时,有一个名为source的新组,它有一个简单的目录basic-ssh-test,其中有一个简单的swift文件。迅速与打印(“Hello,world !”)。
The code that I'm actually interested in running is now in a blue folder called basic-ssh-test
. All of the necessary files are still in there, but instead of running my application, Xcode is running the main.swift
file. I can tell because the debug output is "Hello, world!" instead of "test".
我实际上对运行感兴趣的代码现在位于一个名为basic-ssh-test的蓝色文件夹中。所有必需的文件仍然在其中,但是Xcode没有运行我的应用程序,而是运行main。快速文件。我可以看到,因为调试输出是“Hello, world!”而不是“test”。
I've read a couple of tutorials that claim that Swift Package Manager will move my source files and continue to build the same as before, but that's clearly not the case.
我读过一些教程,声称Swift包管理器将移动我的源文件,并继续构建与以前相同的文件,但事实并非如此。
There's also no longer a "Main Interface" option in my Build Settings, so I can't select "MainMenu.xib" as my application starting point.
在我的构建设置中也不再有“主界面”选项,所以我不能选择“MainMenu”。xib作为我的申请起点。
This is essentially the same thing that happens when I try to use Swift Project Manager with my existing project. It pulls in the dependencies, but it basically ignores my entire existing project and just runs the "hello world" code.
这基本上与我在现有项目中使用Swift项目管理器时发生的情况相同。它会拉入依赖项,但它基本上忽略了我整个现有的项目,只运行“hello world”代码。
How do I use Swift Package Manager to add a dependency to an existing project without ignoring the existing codebase?
如何使用Swift包管理器向现有项目添加依赖项,而不忽略现有的代码基?
3 个解决方案
#1
8
I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
我认为SPM只兼容Mac命令行可执行文件或库。这明确表明SPM根本不支持iOS、watchOS或tvOS平台。但是,由于macOS AppKit/Cocoa应用程序目标与iOS或tvOS Xcode目标非常相似,我认为这一声明意味着SPM也不能直接用于macOS Cocoa应用程序,我认为这正是您所希望的。
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.
这里似乎有一些关于如何在iOS应用程序目标中使用它的工作,这些目标在很大程度上应该转换为macOS Cocoa目标。
#2
3
Until the time of this writing answer, Swift Package Manager do not support the iOS, tvOS, and watchOS. Instead, you will have to add the files from the package directly to your project.
在撰写本文之前,Swift包管理器不支持iOS、tvOS和watchOS。相反,您必须将包中的文件直接添加到项目中。
I would suggest you creating a Dependencies group in your project and a group below that with the package name, like this answer:
我建议您在您的项目中创建一个依赖项组,并在其下面创建一个带有包名的组,如下所示:
So, first you will add the files dependency that you want to import into your project to the name of the package name that included in Dependencies group that we make before.
因此,首先,您将把要导入到项目中的文件依赖项添加到我们以前创建的依赖项组中包含的包名的名称中。
After you add the files then you can access the code as you usually would when you write it yourself like the image below. No need imports. You can see more details in here. hope it helps.
在您添加文件之后,您就可以像您自己编写代码时那样访问代码了,如下图所示。不需要进口。你可以在这里看到更多的细节。希望它可以帮助。
#3
2
You can create a windowed application using SPM (I have a whole project sitting waiting for ABI that does this) you just need some boiler plate in swift.main. I’ll dig it out.
您可以使用SPM创建一个窗口应用程序(我有一个完整的项目等待ABI来完成这一操作),您只需要在quickt.main中使用一些锅炉盘。我要挖出来。
However the method others are suggesting is never going to work (with current versions of SPM).
然而,其他人提出的方法永远不会奏效(使用当前版本的SPM)。
However, what I am seeing is that you really want to use SPM to manage dependencies (keeping up to date etc). So I think you could do that much more easily.
然而,我所看到的是,您确实希望使用SPM来管理依赖关系(保持更新等等)。所以我认为你可以更容易地做到这一点。
Rather than creating a executable as your default target, instead create a library (perhaps call it basic-ssh-dependencies). Generate your Xcode project and drag THAT into your primary Xcode project and configure your target to to depend on it. When you update the library from SPM for changes in dependencies you should re-gen your Xcode-proj.
与其创建一个可执行文件作为默认目标,不如创建一个库(也许可以称之为basic-ssh-dependencies)。生成Xcode项目并将其拖到主Xcode项目中,并将目标配置为依赖于它。当您从SPM更新库以获得依赖项的更改时,您应该重新生成Xcode-proj。
Let me know your mileage and any wrinkles, or if I’ve misunderstood what you are trying to achieve.
让我知道你的里程和任何皱纹,或者如果我误解了你的目标。
#1
8
I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
我认为SPM只兼容Mac命令行可执行文件或库。这明确表明SPM根本不支持iOS、watchOS或tvOS平台。但是,由于macOS AppKit/Cocoa应用程序目标与iOS或tvOS Xcode目标非常相似,我认为这一声明意味着SPM也不能直接用于macOS Cocoa应用程序,我认为这正是您所希望的。
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.
这里似乎有一些关于如何在iOS应用程序目标中使用它的工作,这些目标在很大程度上应该转换为macOS Cocoa目标。
#2
3
Until the time of this writing answer, Swift Package Manager do not support the iOS, tvOS, and watchOS. Instead, you will have to add the files from the package directly to your project.
在撰写本文之前,Swift包管理器不支持iOS、tvOS和watchOS。相反,您必须将包中的文件直接添加到项目中。
I would suggest you creating a Dependencies group in your project and a group below that with the package name, like this answer:
我建议您在您的项目中创建一个依赖项组,并在其下面创建一个带有包名的组,如下所示:
So, first you will add the files dependency that you want to import into your project to the name of the package name that included in Dependencies group that we make before.
因此,首先,您将把要导入到项目中的文件依赖项添加到我们以前创建的依赖项组中包含的包名的名称中。
After you add the files then you can access the code as you usually would when you write it yourself like the image below. No need imports. You can see more details in here. hope it helps.
在您添加文件之后,您就可以像您自己编写代码时那样访问代码了,如下图所示。不需要进口。你可以在这里看到更多的细节。希望它可以帮助。
#3
2
You can create a windowed application using SPM (I have a whole project sitting waiting for ABI that does this) you just need some boiler plate in swift.main. I’ll dig it out.
您可以使用SPM创建一个窗口应用程序(我有一个完整的项目等待ABI来完成这一操作),您只需要在quickt.main中使用一些锅炉盘。我要挖出来。
However the method others are suggesting is never going to work (with current versions of SPM).
然而,其他人提出的方法永远不会奏效(使用当前版本的SPM)。
However, what I am seeing is that you really want to use SPM to manage dependencies (keeping up to date etc). So I think you could do that much more easily.
然而,我所看到的是,您确实希望使用SPM来管理依赖关系(保持更新等等)。所以我认为你可以更容易地做到这一点。
Rather than creating a executable as your default target, instead create a library (perhaps call it basic-ssh-dependencies). Generate your Xcode project and drag THAT into your primary Xcode project and configure your target to to depend on it. When you update the library from SPM for changes in dependencies you should re-gen your Xcode-proj.
与其创建一个可执行文件作为默认目标,不如创建一个库(也许可以称之为basic-ssh-dependencies)。生成Xcode项目并将其拖到主Xcode项目中,并将目标配置为依赖于它。当您从SPM更新库以获得依赖项的更改时,您应该重新生成Xcode-proj。
Let me know your mileage and any wrinkles, or if I’ve misunderstood what you are trying to achieve.
让我知道你的里程和任何皱纹,或者如果我误解了你的目标。