2010-04-22 57 views
30

我目前正在開發一種自動化向我的XCode項目添加新目標的過程。必須將一個目標添加到多個XCode項目中,並且不同項目中的每個目標需要添加相同的源文件,將相同的組存儲在XCode項目中的源文件以及相同的生成設置。手動執行此操作可能需要一段時間,而且很容易出現人爲錯誤,我必須經常執行此任務。我已經編寫了一個腳本來生成新的源文件,將它們複製到系統文件夾,用新信息編輯源文件等,但現在我需要自動化XCode部分。如何從Applescript/Automator/Shell腳本自動創建新的XCode目標

此總結了我想我的自動化程度達到什麼:

公開賽/this/path/project.xcodeproj

的XCode項目複製現有的目標,並重新命名

編輯生成新的目標

的設置添加一個組到源和資源部分,然後重命名這些

添加源˚F到爾斯組,並將該文件添加到新的目標

關閉了XCode

理想我想這是從我的Bourne shell腳本運行的,我知道你可以從那裏發射的Automator工作流。我不確定實現這個目標的最佳途徑是什麼,有什麼想法?在此先感謝:-)

+1

如果是相同的源文件等不能不要將它們構建到框架或捆綁包中並在所有項目中使用編譯版本 - 那麼問題就是爲每個項目添加一個框架 – Mark 2011-12-07 15:46:21

回答

2

讓我先用這個腳本(的Xcode 4.2.1),

  • 公開賽/this/path/project.xcodeproj的XCode項目(完成)
  • 複製現有的目標並將其重命名(未實現)
  • 編輯的新目標(完成)
  • 添加組到源和資源部分,然後將其重命名的構建設置(完成)
  • 添加源文件該組,並添加噸他文件複製到新目標(部分)
  • 關閉的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 
相關問題