如何从 Applescript/Automator/Shell Script 自动创建新的 Xcode 目标 [英] How to automate creation of new Xcode targets from Applescript/Automator/Shell Script

查看:17
本文介绍了如何从 Applescript/Automator/Shell Script 自动创建新的 Xcode 目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

复制现有目标并重命名

编辑新目标的构建设置

向源和资源部分添加一个组,然后重命名它们

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

关闭 Xcode

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

解决方案

让我从这个脚本开始(适用于 Xcode 4.2.1),

  • 在/this/path/project.xcodeproj 上打开一个 XCode 项目(完成)
  • 复制现有目标并重命名(未实现)
  • 编辑新目标的构建设置(完成)
  • 向源和资源"部分添加一个组,然后重命名它们(完成)
  • 将源文件添加到组,并将文件添加到新的目标(部分)
  • 关闭 XCode(完成)
<块引用>

!/usr/bin/osascript

# 参考:AppleScript 编辑器 ->文件 ->打开目录... ->Xcode 4.2.1# http://vgable.com/blog/2009/04/24/how-to-check-if-an-application-is-running-with-applescript/在 ApplicationIsRunning(appName)告诉应用程序系统事件"将 appNameIsRunning 设置为存在(名称为 appName 的进程)返回 appNameIsRunning结束应用程序正在运行如果不是 ApplicationIsRunning("Xcode") 那么日志(启动Xcode ...")启动应用程序 IDcom.apple.dt.Xcode"延迟 5别的log("Xcode 已经在运行...")万一将 project_path 设置为/PATH/TO/YOUR_PROJECT.xcodeproj"log ("在 " & project_path 处打开一个 XCode 项目)将 _project_name 设置为 (do shell script "echo $(basename " & project_path & ") | sed -e 's/.xcodeproj//'")日志(project_name 设置为" & _project_name)将 _target_name 设置为YOUR_TARGET"告诉应用程序 IDcom.apple.dt.Xcode"将 _project_file 设置为(POSIX 文件 project_path 作为别名)打开_project_file# 加载带有 path_to_project 路径的文档集 display yes将 _workspace 设置为活动工作区文档将 _project 设置为(名称 = _project_name 的 _workspace 的项目)的第一项将 _target 设置为(名称 = _target_name 的 _project 的目标)的第一项# 因为这行不通,所以事件不能求助于系统事件# 使用属性 {name:_target_name & 复制 _target_克隆"}# 使用具有属性 {name:_target_name & 的 _target 的数据构建配置创建新的构建配置_克隆"}# 启用日志(编辑新目标的构建设置")将 _build_configuration 设置为(名称 =Debug"的 _target 的构建配置)的第一项将 _build_configuration 的构建设置PRODUCT_NAME"的值设置为KeySING"# http://lists.apple.com/archives/xcode-users//2008/Feb/msg00497.html日志(将组添加到源和资源部分,然后重命名它们")将 _new_group_name 设置为groupX"告诉_project的根组将 _groups 设置为(获取名称 = _new_group_name 的组)如果(_groups 的长度)= 0 那么设置 _new_group 以创建具有属性的新组 {name:_new_group_name, path:_new_group_name, path type:group relative}别的将 _new_group 设置为 _groups 的第一项万一日志(将源文件添加到组,并将文件添加到新目标")告诉_new_group将 _new_file_name 设置为whatever.h"设置 _new_files 以获取(名称 = _new_file_name 的文件引用)如果(_new_files 的长度)= 0 那么# Xcode 崩溃# 使用属性创建新文件引用 ¬# {name:_new_file_name, 文件编码:utf8, 路径类型:group relative,¬# 路径:_new_file_name}万一结束告诉结束告诉日志(关闭 XCode")退出结束告诉

I am currently working on a way to automate the process of adding new targets to my Xcode projects. One target has to be added to multiple Xcode projects and each target in the different project needs the same source files to be added, same groups to store the source files in the Xcode project, and the same build settings. Doing this manually can take a while and is very prone to human error, and I have to do this task fairly often. I have already written a script to generate new source files, copy them to system folders, edit source files with new information etc, but now I need to automate the Xcode part.

This sums up what I want my automation to achieve:

Open an Xcode project at /this/path/project.xcodeproj

Duplicate an existing target and rename it

Edit the Build Settings of the new target

Add a group to the Source and Resources section, then rename them

Add source files to the groups, and add the file to the new Target

Close Xcode

Ideally I want this to run from my Bourne Shell script, I know you can launch automator workflows from there. What is the best approach to achieve this though?

解决方案

let me start with this script (for Xcode 4.2.1),

  • Open an XCode project at /this/path/project.xcodeproj (done)
  • Duplicate an existing target and rename it (not implemented)
  • Edit the Build Settings of the new target (done)
  • Add a group to the Source and Resources section, then rename them (done)
  • Add source files to the groups, and add the file to the new Target(partially)
  • Close XCode (done)

!/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

这篇关于如何从 Applescript/Automator/Shell Script 自动创建新的 Xcode 目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆