comparison ScriptingBridge_Xcode.lua @ 2:5c54957c04c5

Added ScriptingBridge/LuaCocoa example.
author Eric Wing <ewing@anscamobile.com>
date Fri, 29 Jul 2011 18:53:54 -0700
parents
children d63043d62684
comparison
equal deleted inserted replaced
1:143ac8f6fe35 2:5c54957c04c5
1 #!/Library/Frameworks/LuaCocoa.framework/Versions/Current/Tools/luacocoa
2 --[[
3 Simple Scripting Bridge example using Xcode 3 to demonstrate automated building and running.
4 --]]
5
6 LuaCocoa.import("ScriptingBridge")
7 LuaCocoa.import("Foundation")
8
9 function GetURLFromFileAndPath(file_name)
10 local current_working_directory = NSFileManager:defaultManager():currentDirectoryPath()
11
12 local file_string = NSString:stringWithUTF8String_(file_name):stringByExpandingTildeInPath():stringByStandardizingPath()
13 if not file_string:isAbsolutePath() then
14 file_string = current_working_directory:stringByAppendingPathComponent_(file_string):stringByStandardizingPath()
15 end
16 --print("file_string", file_string)
17
18 local ns_url = NSURL:URLWithString_(file_string:stringByAddingPercentEscapesUsingEncoding_(NSUTF8StringEncoding))
19 --print("ns_url", ns_url)
20
21 return ns_url
22 end
23
24 local xcode_application = SBApplication:applicationWithBundleIdentifier_("com.apple.Xcode")
25
26 if xcode_application ~= nil then
27 -- arg[0] is the script name
28 -- arg[1] is either 'os' or 'simulator'
29 -- negative index values represent arguments that come before the script
30 -- we only want the arguments after the script name
31 -- so use a numeric for
32 local file_to_open = arg[1] or "MySampleProject.xcodeproj"
33 local url_to_open = GetURLFromFileAndPath(file_to_open)
34 local xcode_document = xcode_application:open(url_to_open)
35
36 local simulator_or_device = arg[2] or "os"
37
38 -- The sample project has "Debug" and "Release" configurations to choose from.
39 local build_configuration_type = "Release"
40
41 --[[
42 local all_documents = xcode_application:projects()
43
44 print(all_documents, #all_documents)
45 for i=1, #all_documents do
46 local a_doc = all_documents[i]
47 print("doc", a_doc)
48 end
49 --]]
50
51 -- I'm assuming that the document we just opened becomes the activeProjectDocument
52 local active_project_document = xcode_application:activeProjectDocument()
53 print("active_project", active_project_document, active_project_document:name(), active_project_document:path())
54
55
56 local xcode_project = active_project_document:project()
57 print(xcode_project, xcode_project:name())
58
59
60 -- This could be a problem: activeSDK() is nil. Looks like an Apple bug.
61 -- Hmmm, Sometimes it works. I don't understand this.
62 -- iphoneos4.2
63 -- iphonesimulator4.2
64 print("activeSDK", xcode_project:activeSDK())
65 local active_sdk = tostring(xcode_project:activeSDK())
66 print("active_sdk", active_sdk)
67
68 if not string.match(active_sdk, simulator_or_device) then
69 if "simulator" == simulator_or_device then
70 local sdk = string.gsub(active_sdk, "os", "simulator")
71 print("sdk", sdk)
72 xcode_project:setActiveSDK(sdk)
73 else
74 local sdk = string.gsub(active_sdk, "simulator", "os")
75 print("sdk", sdk)
76 xcode_project:setActiveSDK(sdk)
77 end
78 print("changed activeSDK", xcode_project:activeSDK())
79 end
80
81
82 -- I don't think we need to worry about setting the architexture since we build Universal (fat)
83 -- However, have seen Xcode get into weird states when switching between simulator and device.
84 -- Sometimes you need to set this and verify you actually changed it.
85 -- xcode_project:setActiveArchitecture("armv7")
86 -- xcode_project:setActiveArchitecture("i386")
87 -- print("activeArchitecture", xcode_project:activeArchitecture())
88
89
90 -- I'm unclear on buildConfigurations vs buildConfigurationTypes
91 -- I don't think we need to use buildConfigurations
92 --[[
93 local array_of_build_configurations = xcode_project:buildConfigurations()
94 for i=1, #array_of_build_configurations do
95 print("buildConfigurations[:".. tostring(i) .. "]" .. array_of_build_configurations[i]:name())
96 end
97 --]]
98
99 local array_of_build_configuration_types = xcode_project:buildConfigurationTypes()
100 local desired_build_configuration_type = nil
101 for i=1, #array_of_build_configuration_types do
102 print("buildConfigurationType[".. tostring(i) .. "]" .. array_of_build_configuration_types[i]:name())
103 -- Xcode is acting weird. It seems to overwrite entries and replace them, but I don't think it is actually
104 -- replacing them. We need to pick the right element.
105 -- For us, Release is the 2nd position, not the first.
106 if tostring(array_of_build_configuration_types[i]:name()) == tostring(build_configuration_type) then
107 desired_build_configuration_type = array_of_build_configuration_types[i]
108 end
109 end
110
111 local array_of_executables = xcode_project:executables()
112 for i=1, #array_of_executables do
113 print("executables[".. tostring(i) .. "]" .. array_of_executables[i]:name())
114 end
115
116 local array_of_targets = xcode_project:targets()
117 local active_target = xcode_project:activeTarget()
118 for i=1, #array_of_targets do
119 print("targets[".. tostring(i) .. "]" .. array_of_targets[i]:name())
120 -- Watch out: __eq (==) always returns false for different types.
121 local the_name = array_of_targets[i]:name()
122 if tostring(the_name) == "OpenGLES2" then
123 active_target = array_of_targets[i]
124 end
125 end
126 xcode_project:setActiveTarget_(active_target)
127
128 --print("config type", xcode_project:activeBuildConfigurationType():name())
129
130 -- You are not allowed to create a new instance of XcodeBuildConfigurationType
131 -- Instead, you must use an object returned from the system.
132 -- It appears you cannot use copy or mutableCopy as they throw exceptions.
133 -- XcodeBuildConfigurationType = NSClassFromString("XcodeBuildConfigurationType")
134 -- local build_configuration_type = XcodeBuildConfigurationType:alloc():init()
135 -- copy and mutableCopy throw exceptions
136 local active_build_configuration_type = xcode_project:activeBuildConfigurationType()
137 if active_build_configuration_type ~= desired_build_configuration_type then
138 print("desired_build_configuration_type", desired_build_configuration_type)
139 xcode_project:setActiveBuildConfigurationType_(desired_build_configuration_type)
140 end
141 -- Calling name() sometimes fails and crashes Xcode 3.2.6
142 -- print("config type", xcode_project:activeBuildConfigurationType():name())
143
144
145 local active_executable = xcode_project:activeExecutable()
146 print(active_executable, active_executable:name())
147 active_executable:setName_("OpenGLES2")
148
149 print("ActiveTarget:", xcode_project:activeTarget():name())
150 print("ActiveExecutable:", xcode_project:activeExecutable():name())
151 ret_string = xcode_project:buildStaticAnalysis_transcript_using_(false, false, build_configuration_type)
152
153 -- local build_configuration_type =
154
155 print(ret_string)
156 print("launching...")
157 -- Refetch the object just in case since we tried changing it
158 active_executable = xcode_project:activeExecutable()
159
160 -- launch() doesn't seem to actually work. We must use debug()
161 -- local ret_string = active_executable:launch()
162 local ret_string = active_executable:debug()
163 print(ret_string)
164
165
166 else
167 print("Xcode not available")
168 end
169
170