将文件从脚本添加到 Xcode 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1371351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add files to an Xcode project from a script?
提问by Andrew
Right now I'm using a few scripts to generate files that I'm including as resources in Xcode. The thing is I'm running the script, then deleting from the project, then adding back into the project. There must be a way to automate this last step, so that the script can generate the files and automatically add them into the xcode project for me.
现在我正在使用一些脚本来生成我作为资源包含在 Xcode 中的文件。问题是我正在运行脚本,然后从项目中删除,然后重新添加到项目中。必须有一种方法可以自动化这最后一步,以便脚本可以生成文件并自动将它们添加到我的 xcode 项目中。
I'm using bash but any language examples would help.
我正在使用 bash,但任何语言示例都会有所帮助。
Thanks, Andrew
谢谢,安德鲁
采纳答案by e.James
This can be done by adding a new build phase to your application.
这可以通过向您的应用程序添加一个新的构建阶段来完成。
In your Xcode project browser, find the target for your application, and expand it to show all of the build phases.
Add a new "run script" build phaseto your target. The easiest way is to right-click on the target and choose "Add/New Build Phase/New Run Script Build Phase"
Adding the new build phase should bring up an inspector window. In this window, you can enter the entire shell script, or simply a command line to run the script.
Here's the gold: At the bottom of the inspector window you can specify input files and output files. Specifying input files sets up dependencies automatically (the shell script will only be executed if some of the input files have been modified). Specifying output files automatically propagates the dependencies to those files. When your shell script is run, Xcode knows that it needs to deal with those files that the shell script has modified.
Be sure to drag your new build phase up to the top of the list of phases as shown in the screenshot below. The order will be important if you need those resource files to be included in the bundle.
Save, build, commit to the repository, ask for a raise, get some fresh air and have a nice day! :)
在您的 Xcode 项目浏览器中,找到您的应用程序的目标,并将其展开以显示所有构建阶段。
向您的目标添加一个新的“运行脚本”构建阶段。最简单的方法是右键单击目标并选择“添加/新建构建阶段/新建运行脚本构建阶段”
添加新的构建阶段应该会打开一个检查器窗口。在这个窗口中,您可以输入整个 shell 脚本,或者只是一个命令行来运行脚本。
这是黄金:在检查器窗口的底部,您可以指定输入文件和输出文件。指定输入文件会自动设置依赖项(只有在修改了某些输入文件时才会执行 shell 脚本)。指定输出文件会自动将依赖关系传播到这些文件。当你的 shell 脚本运行时,Xcode 知道它需要处理那些 shell 脚本修改过的文件。
请务必将您的新构建阶段拖到阶段列表的顶部,如下面的屏幕截图所示。如果您需要将这些资源文件包含在包中,那么顺序将很重要。
保存、构建、提交到存储库、要求加薪、呼吸新鲜空气并度过美好的一天!:)
回答by Ben Cochran
I had a similar need as Andrew. I needed to be able to include resources from a script without knowing those resources ahead of time. Here's the solutions I came up with:
我和安德鲁有类似的需求。我需要能够在不提前知道这些资源的情况下包含脚本中的资源。这是我想出的解决方案:
Add a new Run Script build phase after “Copy Bundle Resource” that contains the following command:
在“复制捆绑资源”之后添加一个新的运行脚本构建阶段,其中包含以下命令:
find -L ${SRCROOT}/SomeDerivedResources \
-type f -not -name ".*" \
-not -name "`basename ${INFOPLIST_FILE}`" \
| xargs -t -I {} \
cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
Looks scary, but let's break it down:
看起来很可怕,但让我们分解一下:
find -L ${SRCROOT}/SomeDerivedResources
This crawls the directory SomeDerivedResources in our source root (-L
tells it to follow symbolic links)
这会抓取我们源根目录中的 SomeDerivedResources 目录(-L
告诉它遵循符号链接)
-type f
Only include regular files
仅包含常规文件
-not -name ".*"
Ignore files starting with a dot
忽略以点开头的文件
-not -name "`basename ${INFOPLIST_FILE}`"
In my case, my Info plists live in my SomeDerivedResources directory so we need to exclude that file from being copied to our product
就我而言,我的 Info plists 位于我的 SomeDerivedResources 目录中,因此我们需要排除该文件被复制到我们的产品
| xargs -t -I {}
Pipe the results of find
into xargs
with -t
(echo resulting commands to stderr so they show up in our build log), -I
(run the command once for each input file) and use {}
as our argument placeholder
将结果通过管道find
输送到xargs
with -t
(将结果命令回显到 stderr,以便它们显示在我们的构建日志中),-I
(为每个输入文件运行一次命令)并{}
用作我们的参数占位符
cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
Lastly, copy each found file (denoted by {}
) to our product's resource directory.
最后,将找到的每个文件(由 表示{}
)复制到我们产品的资源目录。
I realized when typing this that using an rsync setup instead of cp could prevent us from copying resources each time you build. If your resources are very large it might be worth looking in to.
我在输入时意识到使用 rsync 设置而不是 cp 可能会阻止我们在每次构建时复制资源。如果您的资源非常大,则可能值得研究。
(Also, a folder reference wouldn't work for my need for a few reasons. One, my icons are in my DerivedResources directory and having them in a subdirectory in the bundle seems not to work. Also, I ideally wanted to be able to use [UIImage imageNamed:@"MyAwesomeHappyImage.png"]
and -pathForResource:ofType:
(and some of my files are nested further inside my DerivedResources directory). If your needs don't contain those restraints, I highlysuggest you go the folder reference route.)
(此外,由于一些原因,文件夹引用无法满足我的需要。第一,我的图标位于我的 DerivedResources 目录中,并且将它们放在包中的子目录中似乎不起作用。此外,我理想地希望能够使用[UIImage imageNamed:@"MyAwesomeHappyImage.png"]
和-pathForResource:ofType:
(我的一些文件进一步嵌套在我的 DerivedResources 目录中)。如果您的需求不包含这些限制,我强烈建议您使用文件夹引用路径。)
回答by Adriano Lucas
For those with large number of files, to avoid having to recopy (or recheck) each file, as suggested by @Ben Cochran (thanks a lot for the great script), this is how to do it with rsync:
对于那些拥有大量文件的人,为了避免重新复制(或重新检查)每个文件,正如@Ben Cochran 所建议的那样(非常感谢伟大的脚本),这是使用 rsync 的方法:
回答by geowar
Basically, the files just need to be copied into the main bundle
基本上,文件只需要复制到主包中
In that case just add a folder reference to the project (Create a folder in your project folder and then drag it into your projects "Resources" group (in the "Files & Groups" list; then in the sheet that appears select the "Create Folder References for any added Folder" radio button) and Xcode will copy the folder and all of its contents into the target bundle at build time.
在这种情况下,只需添加对项目的文件夹引用(在项目文件夹中创建一个文件夹,然后将其拖到项目“资源”组中(在“文件和组”列表中;然后在出现的工作表中选择“创建任何添加的文件夹的文件夹引用”单选按钮),Xcode 将在构建时将该文件夹及其所有内容复制到目标包中。
Just an additional note: If you use this method to add image subfolders you'll have to prefix the image name with the subfolder name to use '[UIImage imageNamed:]'. For example if you have an image named "Rendezvous.png" in a subfolder named "MyImages":
再补充一点:如果您使用此方法添加图像子文件夹,则必须在图像名称前加上子文件夹名称才能使用“[UIImage imageNamed:]”。例如,如果您在名为“MyImages”的子文件夹中有一个名为“Rendezvous.png”的图像:
`
`
// this won't work
UIImage * image = [UIImage imageNamed:@"Rendezvous"];
if (image) {
NSLog(@"Found Rendezvous!");
} else {
NSLog(@"Didn't find Rendezvous.");
}
// but this will!
image = [UIImage imageNamed:@"MyImages/Rendezvous"];
if (image) {
NSLog(@"Found MyImages/Rendezvous!");
} else {
NSLog(@"Didn't find MyImages/Rendezvous.");
}
`
`
回答by Michael Nachbaur
If you already have the files somewhere on your system, relative to your source root, you can always add a "Copy Files" phase. This will allow you to specify a directory where your resources should be copied from.
如果您的系统上某个地方已经有了这些文件,相对于您的源根目录,您可以随时添加“复制文件”阶段。这将允许您指定应从中复制资源的目录。
You can combine this with the Build Script phase answer provided to you already. For instance, run a script to check out your assets from Subversion into a subdirectory of your project, and then follow that up with a Copy Files phase that copies from "$(SRCROOT)/Assets".
您可以将此与已经提供给您的构建脚本阶段答案结合起来。例如,运行一个脚本将您的资产从 Subversion 检出到您项目的子目录中,然后执行从“$(SRCROOT)/Assets”复制的 Copy Files 阶段。
回答by Richard
I know it's a bit late, but I just came across this articleexplaining how to do something that sounds like what you're looking for.
我知道这有点晚了,但我刚刚看到这篇文章,解释了如何做一些听起来像你正在寻找的东西。
回答by Igor
I found myself with a similar situation using Ionic Capacitor. What I was expecting was to include files on the "Copy Bundle Resources" bundle phase. What I found is that Ionic already packs you some inclusions and if you slip your files along this folders you get it included as well.
我发现自己在使用离子电容器时遇到了类似的情况。我期待的是在“复制捆绑资源”捆绑阶段包含文件。我发现 Ionic 已经为您打包了一些内容,如果您将文件放在此文件夹中,您也会将其包含在内。
Do you see the App folder inclusion? It our entry point.
您看到包含的 App 文件夹了吗?这是我们的切入点。
To include on it I add a script that do something like this:
为了包含在它上面,我添加了一个脚本来做这样的事情:
cp -Rf ./includes/yourfolder/ ./ios/App/App/