自动将生成的源文件添加到 xcode 项目中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4965077/
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
Automatically adding generated source files to an xcode project
提问by HowsItStack
I am using an IDL which automatically generates source files for my xcode project. Does anyone know how I can automatically have the generated files added to the project? Currently I have to delete the current files from the project and add the new ones. This gets really annoying.
我正在使用 IDL,它会自动为我的 xcode 项目生成源文件。有谁知道我如何自动将生成的文件添加到项目中?目前我必须从项目中删除当前文件并添加新文件。这真的很烦人。
Using a folder reference works for the header files but xcode doesn't want to recognize any files in a folder reference as source files. Has anyone ever found a solution to this problem?
使用文件夹引用适用于头文件,但 xcode 不想将文件夹引用中的任何文件识别为源文件。有没有人找到解决这个问题的方法?
回答by Adrian
I also spent a couple of days writing a solution for this problem. Here is a ruby script that you can add to your project's target as a run script build phase. This was tested with XCode 3.2.4 and ruby 1.8.7.
我还花了几天时间为这个问题编写了一个解决方案。这是一个 ruby 脚本,您可以将其作为运行脚本构建阶段添加到项目的目标中。这是用 XCode 3.2.4 和 ruby 1.8.7 测试的。
For this to work you will need to install rb-appscript ruby gem. ( eg: sudo gem install rb-appscript )
为此,您需要安装 rb-appscript ruby gem。(例如: sudo gem install rb-appscript )
There is little setup to do:
几乎没有什么设置要做:
- First it needs a list of compile targets for added files.
- Second it expects a project group name which it will synchronize with it's associated disk folder ('objc' in my case) . Obviously this group needs to exist and to point to a real folder.
- 首先,它需要一个添加文件的编译目标列表。
- 其次,它需要一个项目组名称,它将与其关联的磁盘文件夹(在我的情况下为“objc”)同步。显然,这个组需要存在并指向一个真正的文件夹。
Here is the script:
这是脚本:
require 'rubygems'
require 'appscript'
target_names = ['MinitSample'] # Put your target names here
group_name = 'objc' # Name of Xcode project group where to add the generated files
project_name = ENV["PROJECT_NAME"]
project_dir = ENV["PROJECT_DIR"]
xcode = Appscript.app('Xcode')
project = xcode.projects[project_name]
group = project.groups[group_name]
group_path = group.real_path.get
generated_files = Dir.glob(group_path+"/*.m")
missing_files = Array.new(generated_files)
group.item_references.get.each {|item|
item_path = item.real_path.get
missing_files.delete(item_path)
if ! generated_files.include?(item_path) then
group.file_references[item.name.get].delete
puts "Deleting #{File.basename(item_path)} from group #{group_name}, as it is not in generated files list"
end
}
if missing_files.empty? then
puts "There are no new files to add. "
exit
end
# holds the compile targets for generated files
targets = []
project.targets.get.each{ |target|
targets << target if target_names.include?(target.name.get)
}
if targets.empty? then
puts "Unable to find #{target_names.inspect} in project targets ! Aborting"
exit
end
missing_files.each{ |path|
file_name = File.basename(path)
msg = "Adding #{file_name} to group #{group_name} and to targets: "
item = xcode.make(:new => :file_reference,
:at => group.item_references.after,
:with_properties => {:full_path => path,
:name => file_name})
targets.each {|target|
xcode.add(item,{:to=>target})
msg += target.name.get
}
puts msg
}
回答by fabb
A good idea can be found here: https://stackoverflow.com/a/17894337/354018
一个好主意可以在这里找到:https: //stackoverflow.com/a/17894337/354018
Basically, you import the generated .m
files into a known source file that is added to the compile phase.
基本上,您将生成的.m
文件导入到添加到编译阶段的已知源文件中。
回答by Jon
I've added a radar (details at http://www.openradar.me/radar?id=4885314376040448) asking for support for this.
我添加了一个雷达(详情见http://www.openradar.me/radar?id=4885314376040448)请求对此提供支持。
回答by Guillaume
Look at xcode-editorproject, an API for manipulating Xcode project files.
查看xcode-editor项目,这是一个用于操作 Xcode 项目文件的 API。
You can add files to a project, specify which target it belongs, add xib files and frameworks.
您可以向项目添加文件,指定它属于哪个目标,添加 xib 文件和框架。