gpt4 book ai didi

xcode - 如何从 Applescript/Automator/Shell 脚本自动创建新的 Xcode 目标

转载 作者:行者123 更新时间:2023-12-02 17:33:11 25 4
gpt4 key购买 nike

我目前正在研究一种方法来自动化向我的 Xcode 项目添加新目标的过程。必须将一个目标添加到多个 Xcode 项目中,并且不同项目中的每个目标都需要添加相同的源文件、在 Xcode 项目中存储源文件的相同组以及相同的build设置。手动执行此操作可能需要一段时间,并且很容易出现人为错误,而且我必须经常执行此任务。我已经编写了一个脚本来生成新的源文件,将它们复制到系统文件夹,使用新信息编辑源文件等,但现在我需要自动化 Xcode 部分。

这总结了我希望自动化实现的目标:

在/this/path/project.xcodeproj 打开 Xcode 项目

复制现有目标并重命名

编辑新目标的build设置

将组添加到源和资源部分,然后重命名它们

将源文件添加到组中,并将文件添加到新的目标

关闭 Xcode

理想情况下,我希望它从我的 Bourne Shell 脚本运行,我知道您可以从那里启动自动化工作流程。实现这一目标的最佳方法是什么?

最佳答案

让我从这个脚本开始(针对 Xcode 4.2.1),

  • 在/this/path/project.xcodeproj 打开一个 XCode 项目(完成)
  • 复制现有目标并重命名(未实现)
  • 编辑新目标的build设置(完成)
  • 将组添加到“源和资源”部分,然后重命名(完成)
  • 将源文件添加到组中,并将文件添加到新的目标(部分)
  • 关闭 XCode(完成)

!/usr/bin/osascript

# Reference: AppleScript Editor -> File -> Open Directory ... -> Xcode 4.2.1

# http://vgable.com/blog/2009/04/24/how-to-check-if-an-application-is-running-with-applescript/
on ApplicationIsRunning(appName)
tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
return appNameIsRunning
end ApplicationIsRunning

if not ApplicationIsRunning("Xcode") then
log ("Launching Xcode ...")
launch application id "com.apple.dt.Xcode"
delay 5
else
log("Xcode is already running ...")
end if

set project_path to "/PATH/TO/YOUR_PROJECT.xcodeproj"
log ("Open an XCode project at " & project_path)

set _project_name to (do shell script "echo $(basename " & project_path & ") | sed -e 's/.xcodeproj//'")
log ("project_name set to " & _project_name)

set _target_name to "YOUR_TARGET"

tell application id "com.apple.dt.Xcode"
set _project_file to (POSIX file project_path as alias)
open _project_file
# load documentation set with path path_to_project display yes
set _workspace to active workspace document
set _project to first item of (projects of _workspace whose name = _project_name)
set _target to first item of (targets of _project whose name = _target_name)

# as this won't work, cannot event resort to system event
# duplicate _target with properties {name:_target_name & "_clone"}
# make new build configuration with data build configurations of _target with properties {name:_target_name & "_clone"}
# activate

log ("Edit the Build Settings of the new target")
set _build_configuration to first item of (build configurations of _target whose name = "Debug")
set value of build setting "PRODUCT_NAME" of _build_configuration to "KeySING"

# http://lists.apple.com/archives/xcode-users//2008/Feb/msg00497.html
log ("Add a group to the Source and Resources section, then rename them")
set _new_group_name to "groupX"
tell root group of _project
set _groups to (get groups whose name = _new_group_name)
if (length of _groups) = 0 then
set _new_group to make new group with properties {name:_new_group_name, path:_new_group_name, path type:group relative}
else
set _new_group to first item of _groups
end if

log ("Add source files to the groups, and add the file to the new Target")
tell _new_group
set _new_file_name to "whatever.h"
set _new_files to get (file references whose name = _new_file_name)
if (length of _new_files) = 0 then
# Xcode crashes
# make new file reference with properties ¬
# {name:_new_file_name, file encoding:utf8, path type:group relative,¬
# path:_new_file_name}
end if
end tell
end tell

log ("Close XCode")
quit

end tell

关于xcode - 如何从 Applescript/Automator/Shell 脚本自动创建新的 Xcode 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2694750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com