在xcode中使用本地swift软件包而不使其成为git repos

情况 (The situation)

At the time of writing, Xcode forces you to have a separate git repository for each Swift package if you want to add it as a dependency. It can reside on your hard drive, and you can certainly use a file:// URL, but it still has to be a git repository:

在撰写本文时,如果您想将其添加为依赖项,则Xcode会 强制您为每个Swift软件包拥有一个单独的git存储库 。 它可以驻留在您的硬盘上,当然可以使用file:// URL,但是它仍然必须是git存储库:

Image for post

One possible way to get around this is to forget about .xcodeproj altogether and use the Swift Package Manager for everything, including your final executable. Xcode 11 added support for SwiftPM projects.

解决此问题的一种可能方法是完全忘记.xcodeproj,并使用Swift Package Manager处理所有内容,包括最终的可执行文件。 Xcode 11添加了对SwiftPM项目的支持。

Furthermore, you can specify relative dependencies between packages using the file system. No need to mess with specific versions or git tags:

此外,您可以使用文件系统指定软件包之间的相对依赖性。 无需弄乱特定版本或git标签:

let package = Package(
name: "MyProduct",
products: [
.library(
name: "MyProduct",
targets: ["MyProduct"]),
],
dependencies: [
.package(path: "../MySoundLibrary"),
.package(path: "../MyGraphicsLibrary")
],
targets: [
.target(
name: "Trade",
dependencies: ["MySoundLibrary","MyGraphicsLibrary"])
]
)

But the Swift Package Manager only has two types of projects: library and executable, whereas Xcode supports more than 20 different types of projects across four different platforms (iOS, watchOS, tvOS, and macOS). Even if you use SwiftPM projects for all your modules, odds are you’ll want to use an Xcode project for your final deliverable.

但是Swift Package Manager只有两种类型的项目:库和可执行文件,而Xcode在四个不同平台(iOS,watchOS,tvOS和macOS)上支持20多种不同类型的项目。 即使您对所有模块都使用SwiftPM项目,您还是很可能希望为最终交付使用Xcode项目。

解决方案 (The solution)

It just so happens that Xcode has a feature that allows you to use a local folder to override an existing dependency. In other words, if you previously imported a Swift package from a git repository, you can specify a local folder as a temporary replacement. Even better, your local folder doesn’t have to be a git repository!

它只是恰巧,Xcode中一项功能 ,允许您使用本地文件夹来覆盖现有的依赖。 换句话说,如果您以前是从git信息库导入的Swift包,则可以将本地文件夹指定为临时文件夹。 更好的是,您的本地文件夹不必是git存储库!

All we need is a way to quickly generate a fake git repository to make Xcode happy. The following bash/zsh script will do the job:

我们需要的是一种快速生成伪造的git存储库以使Xcode满意的方法。 以下bash / zsh 脚本将完成这项工作:

temppack() {
if [ -z "$1" ]
then
echo "Usage: temppack PackageName"
else
mkdir temppack
cd temppack
mkdir $1
cd $1
swift package init --type library
git init
git add .
git commit -m "first"
echo ""
echo "file://$(pwd)" | pbcopy
echo "The repository path for $1 is now in your clipboard."
echo ""
[[ $BASH_VERSION ]] && read -p "Delete temp files?[Y/N]" -n 1 -r
[[ $ZSH_VERSION ]] && read -q "REPLY?Delete temp files?[Y/N]"
echo ""
cd ../..
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Deleting temporary folder..."
rm -rf temppack
echo "Done."
fi
fi
}

You can add this script to your bash/zsh startup file. I only tested it on zsh (macOS 10.15) but I did my best to keep it backwards compatible with bash. The script expects a single argument which should obviously match the name of your actual Swift package:

您可以将此脚本添加到bash / zsh启动文件中。 我只在zsh (macOS 10.15)上进行了测试,但我尽了最大努力使其与bash向后兼容。 该脚本需要一个参数,该参数显然应与您实际的Swift软件包的名称匹配:

> temppack MySwiftPackageCreating library package: MySwiftPackage
Creating Package.swift
Creating README.md
...
...
...The repository path for MySwiftPackage is in your clipboard.Delete temp files [Y/N]?

Don’t answer to the [Y/N] prompt just yet. Switch back to Xcode and use the URL in your clipboard to import the Swift package:

暂时不回答[是/否]提示。 切换回Xcode并使用剪贴板中的URL导入Swift包:

Image for post

Make sure you select this option in the following screen:

确保在以下屏幕中选择此选项:

Image for post

After you’re done, the newly imported dependency should appear on the left-hand side, as well as on the Packages grid on the right.

完成后,新导入的依赖项应出现在左侧以及右侧的Packages网格中。

Image for post

Now, using Finder navigate to the folder with your real Swift Package and drag-n-drop it under your project. Make sure you drop it directly below your project, not inside other package or folder.

现在,使用Finder导航到带有实际 Swift包的文件夹,然后将其拖放到您的项目下。 确保将其直接放在项目下方,而不是放在其他包或文件夹中。

Image for post

As soon as you do that, you’ll see how the git repository dependency disappears:

这样做之后,您将看到git仓库依赖关系如何消失:

Image for post

Don’t let that fool you, Xcode still has a reference to the repository, and it still shows up on the Packages grid.

不要让这个傻瓜欺骗您, Xcode仍然具有对存储库的引用,它仍然显示在Packages网格中。

If you have more packages to add, to back to your terminal window and answer No to the [Y/N] prompt. Repeat this process for all your Swift packages and answer Yes for the last one of them, in order to remove the temporary files.

如果要添加更多软件包,请返回到终端窗口,并在[Y / N]提示下回答No。 对所有Swift软件包重复此过程,并为其中的最后一个软件包回答Yes ,以删除临时文件。

最后一件事 (One last thing)

Since the git repository reference is not getting used, it’s tempting to remove it. Don’t. This reference is the only thing making this whole process work. Xcode needs to believe your folder is overriding something, if you remove the reference, it will stop recognizing your folder as a dependency.

由于git仓库引用没有被使用,因此很想删除它。 别。 该参考是使整个过程正常工作的唯一方法。 Xcode需要相信您的文件夹正在覆盖某些内容,如果删除引用,它将停止将您的文件夹识别为依赖项。

翻译自: https://medium.com/@royalstream/using-local-swift-packages-in-xcode-without-making-them-git-repos-3aa046cc222c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值