-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
builder.py
executable file
Β·1628 lines (1401 loc) Β· 57.7 KB
/
builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Build and Launch iPhone Application in Simulator or install
# the application on the device via iTunes
#
import os, sys, uuid, subprocess, shutil, signal, string, traceback, imp, filecmp, inspect
import platform, time, re, run, glob, codecs, hashlib, datetime, plistlib
from compiler import Compiler, softlink_for_simulator
from projector import Projector
from xml.dom.minidom import parseString
from xml.etree.ElementTree import ElementTree
from os.path import join, splitext, split, exists
from tools import ensure_dev_path
# the template_dir is the path where this file lives on disk
template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
# add the parent and the common directory so we can load libraries from those paths too
sys.path.append(os.path.join(template_dir,'../'))
sys.path.append(os.path.join(template_dir,'../common'))
sys.path.append(os.path.join(template_dir, '../module'))
script_ok = False
from tiapp import *
from css import csscompiler
import localecompiler
from module import ModuleDetector
from tools import *
ignoreFiles = ['.gitignore', '.cvsignore']
ignoreDirs = ['.git','.svn', 'CVS']
# need this so unicode works
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
def version_sort(a,b):
x = float(a[0:3]) # ignore more than 2 places
y = float(b[0:3]) # ignore more than 2 places
if x > y:
return -1
if x < y:
return 1
return 0
# this will return the version of the iOS SDK that we have installed
def check_iphone_sdk(s):
found = []
output = run.run(["xcodebuild","-showsdks"],True,False)
#print output
if output:
for line in output.split("\n"):
if line[0:1] == '\t':
line = line.strip()
i = line.find('-sdk')
if i < 0: continue
type = line[0:i]
cmd = line[i+5:]
if cmd.find("iphoneos")==0:
ver = cmd[8:]
found.append(ver)
# The sanity check doesn't have to be as thorough as prereq.
if s in found:
return s
# Sanity check failed. Let's find something close.
return sorted(found,version_sort)[0]
def dequote(s):
if s[0:1] == '"':
return s[1:-1]
return s
# force kill the simulator if running
def kill_simulator():
run.run(['/usr/bin/killall',"ios-sim"],True)
run.run(['/usr/bin/killall',"iPhone Simulator"],True)
def write_project_property(f,prop,val):
existing_val = read_project_property(f,prop)
if existing_val!=val:
fx = open(f,'w')
fx.write("%s=%s\n"%(prop,val))
fx.close()
def read_project_property(f,prop):
if os.path.exists(f):
contents = open(f).read()
for line in contents.splitlines(False):
(k,v) = line.split("=")
if k == prop:
return v
return None
def read_project_appid(f):
return read_project_property(f,'TI_APPID')
def read_project_version(f):
return read_project_property(f,'TI_VERSION')
def infoplist_has_appid(f,appid):
if os.path.exists(f):
contents = codecs.open(f,encoding='utf-8').read()
return contents.find(appid)>0
return False
def copy_module_resources(source, target, copy_all=False, force=False):
if not os.path.exists(os.path.expanduser(target)):
os.makedirs(os.path.expanduser(target))
for root, dirs, files in os.walk(source, True, None, True):
for name in ignoreDirs:
if name in dirs:
dirs.remove(name) # don't visit ignored directories
for file in files:
if copy_all==False and splitext(file)[-1] in ('.html', '.js', '.css', '.a', '.m', '.c', '.cpp', '.h', '.mm'):
continue
if file in ignoreFiles:
continue
from_ = os.path.join(root, file)
to_ = os.path.expanduser(from_.replace(source, target, 1))
to_directory = os.path.expanduser(split(to_)[0])
if not exists(to_directory):
os.makedirs(to_directory)
# only copy if different filesize or doesn't exist
if not os.path.exists(to_) or os.path.getsize(from_)!=os.path.getsize(to_) or force:
if os.path.exists(to_): os.remove(to_)
shutil.copyfile(from_, to_)
# WARNING: This could be a time bomb waiting to happen, because it mangles
# the app bundle name for NO REASON. Or... does it?
def make_app_name(s):
r = re.compile('[0-9a-zA-Z_]')
buf = ''
for i in s:
if i=='-':
buf+='_'
continue
if r.match(i)!=None:
buf+=i
# if name starts with number, we simply append a k to it
if re.match('^[0-9]+',buf):
buf = 'k%s' % buf
return buf
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc+=node.data
elif node.nodeType == node.ELEMENT_NODE:
rc+=getText(node.childNodes)
return rc
def make_map(dict):
props = {}
curkey = None
for i in dict.childNodes:
if i.nodeType == 1:
if i.nodeName == 'key':
curkey = str(getText(i.childNodes)).strip()
elif i.nodeName == 'dict':
props[curkey] = make_map(i)
curkey = None
elif i.nodeName == 'array':
s = i.getElementsByTagName('string')
if len(s):
txt = ''
for t in s:
txt+=getText(t.childNodes)
props[curkey]=txt
else:
props[curkey]=None
curkey = None
else:
if i.childNodes.length > 0:
props[curkey] = getText(i.childNodes)
else:
props[curkey] = i.nodeName
curkey = None
return props
def dump_resources_listing(rootdir,out):
out.write("\nFile listing for %s\n\n" % rootdir)
total = 0
for root, subFolders, files in os.walk(rootdir, True, None, True):
for file in files:
p = os.path.join(root,file)
s = os.path.getsize(p)
total+=s
s = "[%.0f]" % s
p = p[len(rootdir)+1:]
if p.startswith('build/android') or p.startswith('build/mobileweb'): continue
out.write(" %s %s\n" % (string.ljust(p,120),string.ljust(s,8)))
out.write("-" * 130)
out.write("\nTotal files: %.1f MB\n" % ((total/1024)/1024))
out.write("\n")
def dump_infoplist(infoplist,out):
plist = codecs.open(infoplist, encoding='utf-8').read()
out.write("Contents of Info.plist\n\n")
out.write(plist)
out.write("\n")
out.write("=" * 130)
out.write("\n\n")
def read_provisioning_profile(f,o):
f = open(f,'rb').read()
b = f.index('<?xml')
e = f.index('</plist>')
xml_content = f[b:e+8]
o.write("Reading provisioning profile:\n\n%s\n" % xml_content)
dom = parseString(xml_content)
dict = dom.getElementsByTagName('dict')[0]
props = make_map(dict)
return props
def get_aps_env(provisioning_profile):
entitlements = provisioning_profile['Entitlements']
if entitlements.has_key('aps-environment'):
return entitlements['aps-environment']
return None
def get_task_allow(provisioning_profile):
entitlements = provisioning_profile['Entitlements']
return entitlements['get-task-allow']
def get_app_prefix(provisioning_profile):
appid_prefix = provisioning_profile['ApplicationIdentifierPrefix']
return appid_prefix
def get_profile_uuid(provisioning_profile):
return provisioning_profile['UUID']
def generate_customized_entitlements(provisioning_profile,appid,uuid,command,out):
get_task_value = get_task_allow(provisioning_profile)
aps_env = get_aps_env(provisioning_profile)
buffer = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
"""
app_prefix = None
if command=='distribute':
app_prefix = get_app_prefix(provisioning_profile)
out.write("Using app_prefix = %s\n\n" % (app_prefix))
buffer+="""
<key>application-identifier</key>
<string>%s.%s</string>
""" % (app_prefix,appid)
buffer+="<key>get-task-allow</key>\n <%s/>" % get_task_value
if aps_env!=None:
buffer+="\n<key>aps-environment</key>\n <string>%s</string>" % aps_env
if command=='distribute':
buffer+="""
<key>keychain-access-groups</key>
<array>
<string>%s.%s</string>
</array>
""" % (app_prefix,appid)
buffer+="""
</dict>
</plist>"""
return buffer
def xcode_version():
output = run.run(['xcodebuild','-version'],True,False)
if output:
versionLine = output.split('\n')[0]
return float(versionLine.split(' ')[1].rpartition('.')[0])
def distribute_xc4(name, icon, log):
# Locations of bundle, app binary, dsym info
log.write("Creating distribution for xcode4...\n");
timestamp = datetime.datetime.now()
date = timestamp.date().isoformat()
time = timestamp.time().strftime('%H-%M-%S')
archive_name = os.path.join(date,'%s_%s' % (name, time))
archive_bundle = os.path.join(os.path.expanduser("~/Library/Developer/Xcode/Archives"),"%s.xcarchive" % archive_name)
archive_app = os.path.join(archive_bundle,"Products","Applications","%s.app" % name)
archive_dsym = os.path.join(archive_bundle,"dSYM")
# create directories
if not os.access(archive_bundle, os.F_OK): os.makedirs(archive_bundle)
if not os.access(archive_app, os.F_OK): os.makedirs(archive_app)
if not os.access(archive_dsym, os.F_OK): os.makedirs(archive_dsym)
# copy app bundles into the approps. places
os.system('ditto "%s.app" "%s"' % (name,archive_app))
os.system('ditto "%s.app.dSYM" "%s"' % (name,archive_dsym))
# plist processing time - this is the biggest difference from XC3.
archive_info_plist = os.path.join(archive_bundle,'Info.plist')
log.write("Writing archive plist to: %s\n\n" % archive_info_plist)
# load existing plist values so that we can use them in generating the archive
# plist
os.system('/usr/bin/plutil -convert xml1 -o "%s" "%s"' % (os.path.join(archive_bundle,'Info.xml.plist'),os.path.join(archive_app,'Info.plist')))
project_info_plist = plistlib.readPlist(os.path.join(archive_bundle,'Info.xml.plist'))
appbundle = "Applications/%s.app" % name
# NOTE: We chop off the end '.' of 'CFBundleVersion' to provide the 'short' version
version = project_info_plist['CFBundleVersion']
app_version_ = version.split('.')
if(len(app_version_) > 3):
version = app_version_[0]+'.'+app_version_[1]+'.'+app_version_[2]
archive_info = {
'ApplicationProperties' : {
'ApplicationPath' : appbundle,
'CFBundleIdentifier' : project_info_plist['CFBundleIdentifier'],
'CFBundleShortVersionString' : version,
'IconPaths' : [os.path.join(appbundle,icon), os.path.join(appbundle,icon)]
},
'ArchiveVersion' : float(1),
'CreationDate' : datetime.datetime.utcnow(),
'Name' : name,
'SchemeName' : name
}
# write out the archive plist and clean up
log.write("%s\n\n" % archive_info)
plistlib.writePlist(archive_info,archive_info_plist)
os.remove(os.path.join(archive_bundle,'Info.xml.plist'))
# Workaround for dumb xcode4 bug that doesn't update the organizer unless
# files are touched in a very specific manner
temp = os.path.join(os.path.expanduser("~/Library/Developer/Xcode/Archives"),"temp")
os.rename(archive_bundle,temp)
os.rename(temp,archive_bundle)
def is_indexing_enabled(tiapp, simulator_dir, **kwargs):
# darwin versions:
# - 9.x: Leopard (10.5)
# - 10.x: Snow Leopard (10.6)
# - 11.x: Lion (10.7)
# for testing purposes
platform_release = kwargs.get("platform_release", platform.release())
darwin_version = [int(n) for n in platform_release.split(".")]
enable_mdfind = True
if tiapp.has_app_property('ti.ios.enablemdfind'):
enable_mdfind = tiapp.to_bool(tiapp.get_app_property('ti.ios.enablemdfind'))
# mdfind is specifically disabled, so don't use it
if not enable_mdfind:
return False
# pre-Leopard, mdfind / mdutil don't exist
if darwin_version[0] < 10:
return False
# for testing purposes
indexer_status = kwargs.get("indexer_status")
if indexer_status == None:
indexer_status = run.run(['mdutil', '-a', '-s'], True)
# An error occurred running mdutil, play it safe
if indexer_status == None:
return False
lines = indexer_status.splitlines()
mount_point_status = {}
for i in range(0, len(lines), 2):
mount_point = lines[i].rstrip(':')
if len(lines) > (i+1):
status = lines[i+1].strip('\t.')
# Only add mount points that the simulator_dir starts with
if simulator_dir.startswith(mount_point):
mount_point_status[mount_point] = status
# mdutil must be disabled if we don't get the right amount of output
else:
return False
if len(mount_point_status) > 0:
# There may be multiple volumes that have a mount point that the
# simulator_dir matches, so the one with the longest length
# *should* be the most specific / correct mount point.
mount_points = mount_point_status.keys()
mount_points.sort(lambda a, b: cmp(len(b), len(a)))
status = mount_point_status[mount_points[0]]
if 'Indexing enabled' in status:
return True
return False
HEADER = """/**
* Appcelerator Titanium Mobile
* This is generated code. Do not modify. Your changes *will* be lost.
* Generated code is Copyright (c) 2009-2011 by Appcelerator, Inc.
* All Rights Reserved.
*/
#import <Foundation/Foundation.h>
"""
DEFAULTS_IMPL_HEADER= """#import "TiUtils.h"
#import "ApplicationDefaults.h"
@implementation ApplicationDefaults
+ (NSMutableDictionary*) copyDefaults
{
NSMutableDictionary * _property = [[NSMutableDictionary alloc] init];\n
"""
FOOTER ="""
@end
"""
def copy_tiapp_properties(project_dir):
tiapp = ElementTree()
src_root = os.path.dirname(sys.argv[0])
assets_tiappxml = os.path.join(project_dir,'tiapp.xml')
if not os.path.exists(assets_tiappxml):
shutil.copy(os.path.join(project_dir, 'tiapp.xml'), assets_tiappxml)
tiapp.parse(open(assets_tiappxml, 'r'))
impf = open("ApplicationDefaults.m",'w+')
appl_default = os.path.join(project_dir,'build','iphone','Classes','ApplicationDefaults.m')
impf.write(HEADER)
impf.write(DEFAULTS_IMPL_HEADER)
for property_el in tiapp.findall("property"):
name = property_el.get("name")
type = property_el.get("type")
value = property_el.text
if name == None: continue
if value == None: value = ""
if type == "string":
impf.write(""" [_property setObject:[TiUtils stringValue:@"%s"] forKey:@"%s"];\n"""%(value,name))
elif type == "bool":
impf.write(""" [_property setObject:[NSNumber numberWithBool:[TiUtils boolValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
elif type == "int":
impf.write(""" [_property setObject:[NSNumber numberWithInt:[TiUtils intValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
elif type == "double":
impf.write(""" [_property setObject:[NSNumber numberWithDouble:[TiUtils doubleValue:@"%s"]] forKey:@"%s"];\n"""%(value,name))
elif type == None:
impf.write(""" [_property setObject:[TiUtils stringValue:@"%s"] forKey:@"%s"];\n"""%(value,name))
else:
print """[WARN] Cannot set property "%s" , type "%s" not supported""" % (name,type)
if (len(tiapp.findall("property")) > 0) :
impf.write("\n return _property;\n}")
else:
impf.write("\n [_property release];")
impf.write("\n return nil;\n}")
impf.write(FOOTER)
impf.close()
if open(appl_default,'r').read() == open('ApplicationDefaults.m','r').read():
os.remove('ApplicationDefaults.m')
return False
else:
shutil.copyfile('ApplicationDefaults.m',appl_default)
os.remove('ApplicationDefaults.m')
return True
def cleanup_app_logfiles(tiapp, log_id, iphone_version):
print "[DEBUG] finding old log files"
sys.stdout.flush()
simulator_dir = os.path.expanduser('~/Library/Application\ Support/iPhone\ Simulator/%s' % iphone_version)
# No need to clean if the directory doesn't exist
if not os.path.exists(simulator_dir):
return
results = None
# If the indexer is enabled, we can use spotlight for faster searching
if is_indexing_enabled(tiapp, simulator_dir):
print "[DEBUG] Searching for old log files with mdfind..."
sys.stdout.flush()
results = run.run(['mdfind',
'-onlyin', simulator_dir,
'-name', '%s.log' % log_id
], True)
# Indexer is disabled, revert to manual crawling
if results == None:
print "[DEBUG] Searching for log files without mdfind..."
sys.stdout.flush()
def find_all_log_files(folder, fname):
results = []
for root, dirs, files in os.walk(os.path.expanduser(folder)):
for file in files:
if fname==file:
fullpath = os.path.join(root, file)
results.append(fullpath)
return results
for f in find_all_log_files(simulator_dir, '%s.log' % log_id):
print "[DEBUG] removing old log file: %s" % f
sys.stdout.flush()
os.remove(f)
else:
for i in results.splitlines(False):
print "[DEBUG] removing old log file: %s" % i
os.remove(i)
def find_name_conflicts(project_dir, project_name):
for dir in ['Resources', 'Resources/iphone']:
for name in os.listdir(os.path.join(project_dir, dir)):
if name.lower() == project_name.lower():
print "[ERROR] Project name %s conflicts with resource named %s: Cannot build. Please change one." % (project_name, os.path.join(project_dir, dir, name))
sys.exit(1)
pass
#
# this script is invoked from our tooling but you can run from command line too if
# you know the arguments
#
# the current pattern is <command> [arguments]
#
# where the arguments are dependent on the command being passed
#
def main(args):
global script_ok
argc = len(args)
if argc < 2 or argc==2 and (args[1]=='--help' or args[1]=='-h'):
print "%s <command> <version> <project_dir> <appid> <name> [options]" % os.path.basename(args[0])
print
print "available commands: "
print
print " install install the app to itunes for testing on iphone"
print " simulator build and run on the iphone simulator"
print " adhoc build for adhoc distribution"
print " distribute build final distribution bundle"
print " xcode build from within xcode"
print " run build and run app from project folder"
sys.exit(1)
print "[INFO] One moment, building ..."
sys.stdout.flush()
start_time = time.time()
command = args[1].decode("utf-8")
ensure_dev_path()
target = 'Debug'
deploytype = 'development'
devicefamily = 'iphone'
debug = False
build_only = False
simulator = False
xcode_build = False
force_xcode = False
simtype = devicefamily
# when you run from xcode, we'll pass xcode as the command and the
# xcode script will simply pass some additional args as well as xcode
# will add some additional useful stuff to the ENVIRONMENT and we pull
# those values out here
if command == 'xcode':
xcode_build = True
src_root = os.environ['SOURCE_ROOT']
project_dir = os.path.abspath(os.path.join(src_root,'../','../'))
name = os.environ['PROJECT_NAME']
target = os.environ['CONFIGURATION']
appid = os.environ['TI_APPID']
arch = os.environ['CURRENT_ARCH']
sdk_name = os.environ['SDK_NAME']
iphone_version = sdk_name.replace('iphoneos','').replace('iphonesimulator','')
# SUPPORTED_DEVICE_FAMILIES 1 or 2 or both
# TARGETED_DEVICE_FAMILY 1 or 2
target_device = os.environ['TARGETED_DEVICE_FAMILY']
if target_device == '1':
devicefamily = 'iphone'
elif target_device == '2':
devicefamily = 'ipad'
elif target_device == '1,2':
devicefamily = 'universal'
if arch == 'i386':
# simulator always indicates simulator
deploytype = 'development'
else:
# if arch!=i386 indicates a build for device
if target=='Debug':
# non-simulator + debug build indicates test on device
deploytype = 'test'
else:
# non-simulator + release build indicates package for distribution
deploytype = 'production'
#Ensure the localization files are copied in the application directory
out_dir = os.path.join(os.environ['TARGET_BUILD_DIR'],os.environ['CONTENTS_FOLDER_PATH'])
localecompiler.LocaleCompiler(name,project_dir,devicefamily,deploytype,out_dir).compile()
compiler = Compiler(project_dir,appid,name,deploytype)
compiler.compileProject(xcode_build,devicefamily,iphone_version)
script_ok = True
sys.exit(0)
else:
# the run command is when you run from titanium using the run command
# and it will run the project in the current directory immediately in the simulator
# from the command line
if command == 'run':
if argc < 3:
print "Usage: %s run <project_dir> [ios_version]" % os.path.basename(args[0])
sys.exit(1)
if argc == 3:
iphone_version = check_iphone_sdk('4.0')
else:
iphone_version = dequote(args[3].decode("utf-8"))
project_dir = os.path.expanduser(dequote(args[2].decode("utf-8")))
iphonesim = os.path.abspath(os.path.join(template_dir,'ios-sim'))
iphone_dir = os.path.abspath(os.path.join(project_dir,'build','iphone'))
tiapp_xml = os.path.join(project_dir,'tiapp.xml')
ti = TiAppXML(tiapp_xml)
appid = ti.properties['id']
name = ti.properties['name']
command = 'simulator' # switch it so that the rest of the stuff works
else:
iphone_version = dequote(args[2].decode("utf-8"))
iphonesim = os.path.abspath(os.path.join(template_dir,'ios-sim'))
project_dir = os.path.expanduser(dequote(args[3].decode("utf-8")))
appid = dequote(args[4].decode("utf-8"))
name = dequote(args[5].decode("utf-8"))
tiapp_xml = os.path.join(project_dir,'tiapp.xml')
ti = TiAppXML(tiapp_xml)
app_name = make_app_name(name)
iphone_dir = os.path.abspath(os.path.join(project_dir,'build','iphone'))
# We need to create the iphone dir if necessary, now that
# the tiapp.xml allows build target selection
if not os.path.isdir(iphone_dir):
if os.path.exists(iphone_dir):
os.remove(iphone_dir)
os.makedirs(iphone_dir)
project_xcconfig = os.path.join(iphone_dir,'project.xcconfig')
target = 'Release'
ostype = 'os'
version_file = None
log_id = None
provisioning_profile = None
debughost = None
debugport = None
debugairkey = None
postbuild_modules = []
finalize_modules = []
def run_finalize():
try:
if finalize_modules:
for p in finalize_modules:
print "[INFO] Running finalize %s..." % p[0]
o.write("Running finalize %s" % p[0])
p[1].finalize()
except Exception,e:
print "[ERROR] Error in finalize: %s" % e
o.write("Error in finalize: %s" % e)
# starting in 1.4, you don't need to actually keep the build/iphone directory
# if we don't find it, we'll just simply re-generate it
if not os.path.exists(iphone_dir):
from iphone import IPhone
print "[INFO] Detected missing project but that's OK. re-creating it..."
iphone_creator = IPhone(name,appid)
iphone_creator.create(iphone_dir,True)
sys.stdout.flush()
# we use different arguments dependent on the command
# pluck those out here
if command == 'distribute':
iphone_version = check_iphone_sdk(iphone_version)
link_version = iphone_version
dist_keychain = None
appuuid = dequote(args[6].decode("utf-8"))
dist_name = dequote(args[7].decode("utf-8"))
output_dir = os.path.expanduser(dequote(args[8].decode("utf-8")))
if argc > 9:
devicefamily = dequote(args[9].decode("utf-8"))
if argc > 10:
dist_keychain = dequote(args[10].decode("utf-8"))
print "[INFO] Switching to production mode for distribution"
deploytype = 'production'
elif command in ['simulator', 'build']:
link_version = check_iphone_sdk(iphone_version)
deploytype = 'development'
debug = True
simulator = command == 'simulator'
build_only = command == 'build'
target = 'Debug'
ostype = 'simulator'
if argc > 6:
devicefamily = dequote(args[6].decode("utf-8"))
if argc > 7:
simtype = dequote(args[7].decode("utf-8"))
else:
# 'universal' helpfully translates into iPhone here... just in case.
simtype = devicefamily
if argc > 8:
# this is host:port from the debugger
debughost = dequote(args[8].decode("utf-8"))
if debughost=='':
debughost = None
debugport = None
else:
debughost,debugport = debughost.split(":")
elif command in ['install', 'adhoc']:
iphone_version = check_iphone_sdk(iphone_version)
link_version = iphone_version
dist_keychain = None
appuuid = dequote(args[6].decode("utf-8"))
dist_name = dequote(args[7].decode("utf-8"))
if argc > 8:
devicefamily = dequote(args[8].decode("utf-8"))
if argc > 9:
dist_keychain = dequote(args[9].decode("utf-8"))
if dist_keychain=='':
dist_keychain = None
if argc > 10:
# this is host:port:airkey from the debugger
debughost = dequote(args[10].decode("utf-8"))
if debughost=='':
debughost = None
debugport = None
debugairkey = None
else:
debughost,debugport,debugairkey = debughost.split(":")
if command == 'install':
target = 'Debug'
deploytype = 'test'
elif command == 'adhoc':
target = 'Release'
deploytype = 'production'
# setup up the useful directories we need in the script
build_out_dir = os.path.abspath(os.path.join(iphone_dir,'build'))
build_dir = os.path.abspath(os.path.join(build_out_dir,'%s-iphone%s'%(target,ostype)))
app_dir = os.path.abspath(os.path.join(build_dir,name+'.app'))
binary = os.path.join(app_dir,name)
sdk_version = os.path.basename(os.path.abspath(os.path.join(template_dir,'../')))
iphone_resources_dir = os.path.join(iphone_dir,'Resources')
version_file = os.path.join(iphone_resources_dir,'.version')
force_rebuild = read_project_version(project_xcconfig)!=sdk_version or not os.path.exists(version_file)
infoplist = os.path.join(iphone_dir,'Info.plist')
githash = None
custom_fonts = []
# Before doing a single thing, we want to check for conflicts and bail out if necessary.
find_name_conflicts(project_dir, app_name)
# if we're not running in the simulator we want to clean out the build directory
if command!='simulator' and os.path.exists(build_out_dir):
shutil.rmtree(build_out_dir)
if not os.path.exists(build_out_dir):
os.makedirs(build_out_dir)
# write out the build log, useful for debugging
o = codecs.open(os.path.join(build_out_dir,'build.log'),'w',encoding='utf-8')
def log(msg):
print msg
o.write(msg)
try:
buildtime = datetime.datetime.now()
o.write("%s\n" % ("="*80))
o.write("Appcelerator Titanium Diagnostics Build Log\n")
o.write("The contents of this file are useful to send to Appcelerator Support if\n")
o.write("reporting an issue to help us understand your environment, build settings\n")
o.write("and aid in debugging. Please attach this log to any issue that you report.\n")
o.write("%s\n\n" % ("="*80))
o.write("Starting build at %s\n\n" % buildtime.strftime("%m/%d/%y %H:%M"))
# write out the build versions info
versions_txt = read_config(os.path.join(template_dir,'..','version.txt'))
o.write("Build details:\n\n")
for key in versions_txt:
o.write(" %s=%s\n" % (key,versions_txt[key]))
o.write("\n\n")
if versions_txt.has_key('githash'):
githash = versions_txt['githash']
o.write("Script arguments:\n")
for arg in args:
o.write(unicode(" %s\n" % arg, 'utf-8'))
o.write("\n")
o.write("Building from: %s\n" % template_dir)
o.write("Platform: %s\n\n" % platform.version())
# print out path to debug
xcode_path=run.run(["/usr/bin/xcode-select","-print-path"],True,False)
if xcode_path:
o.write("Xcode path is: %s\n" % xcode_path)
else:
o.write("Xcode path undetermined\n")
# find the module directory relative to the root of the SDK
titanium_dir = os.path.abspath(os.path.join(template_dir,'..','..','..','..'))
tp_module_dir = os.path.abspath(os.path.join(titanium_dir,'modules','iphone'))
force_destroy_build = command!='simulator'
detector = ModuleDetector(project_dir)
missing_modules, modules = detector.find_app_modules(ti, 'iphone')
module_lib_search_path, module_asset_dirs = locate_modules(modules, project_dir, app_dir, log)
common_js_modules = []
if len(missing_modules) != 0:
print '[ERROR] Could not find the following required iOS modules:'
for module in missing_modules:
print "[ERROR]\tid: %s\tversion: %s" % (module['id'], module['version'])
sys.exit(1)
# search for modules that the project is using
# and make sure we add them to the compile
for module in modules:
if module.js:
common_js_modules.append(module)
continue
module_id = module.manifest.moduleid.lower()
module_version = module.manifest.version
module_lib_name = ('lib%s.a' % module_id).lower()
# check first in the local project
local_module_lib = os.path.join(project_dir, 'modules', 'iphone', module_lib_name)
local = False
if os.path.exists(local_module_lib):
module_lib_search_path.append([module_lib_name, local_module_lib])
local = True
log("[INFO] Detected third-party module: %s" % (local_module_lib))
else:
if module.lib is None:
module_lib_path = module.get_resource(module_lib_name)
log("[ERROR] Third-party module: %s/%s missing library at %s" % (module_id, module_version, module_lib_path))
sys.exit(1)
module_lib_search_path.append([module_lib_name, os.path.abspath(module.lib).rsplit('/',1)[0]])
log("[INFO] Detected third-party module: %s/%s" % (module_id, module_version))
force_xcode = True
if not local:
# copy module resources
img_dir = module.get_resource('assets', 'images')
if os.path.exists(img_dir):
dest_img_dir = os.path.join(app_dir, 'modules', module_id, 'images')
if not os.path.exists(dest_img_dir):
os.makedirs(dest_img_dir)
module_asset_dirs.append([img_dir, dest_img_dir])
# copy in any module assets
module_assets_dir = module.get_resource('assets')
if os.path.exists(module_assets_dir):
module_dir = os.path.join(app_dir, 'modules', module_id)
module_asset_dirs.append([module_assets_dir, module_dir])
full_version = sdk_version
if 'version' in versions_txt:
full_version = versions_txt['version']
if 'timestamp' in versions_txt or 'githash' in versions_txt:
full_version += ' ('
if 'timestamp' in versions_txt:
full_version += '%s' % versions_txt['timestamp']
if 'githash' in versions_txt:
full_version += ' %s' % versions_txt['githash']
full_version += ')'
print "[INFO] Titanium SDK version: %s" % full_version
print "[INFO] iPhone Device family: %s" % devicefamily
print "[INFO] iPhone SDK version: %s" % iphone_version
if simulator or build_only:
print "[INFO] iPhone simulated device: %s" % simtype
# during simulator we need to copy in standard built-in module files
# since we might not run the compiler on subsequent launches
for module_name in ('facebook','ui'):
img_dir = os.path.join(template_dir,'modules',module_name,'images')
dest_img_dir = os.path.join(app_dir,'modules',module_name,'images')
if not os.path.exists(dest_img_dir):
os.makedirs(dest_img_dir)
module_asset_dirs.append([img_dir,dest_img_dir])
# when in simulator since we point to the resources directory, we need
# to explicitly copy over any files
ird = os.path.join(project_dir,'Resources','iphone')
if os.path.exists(ird):
module_asset_dirs.append([ird,app_dir])
# We also need to copy over the contents of 'platform/iphone'
platform_iphone = os.path.join(project_dir,'platform','iphone')
if os.path.exists(platform_iphone):
module_asset_dirs.append([platform_iphone,app_dir])
for ext in ('ttf','otf'):
for f in glob.glob('%s/*.%s' % (os.path.join(project_dir,'Resources'),ext)):
custom_fonts.append(f)
if not (simulator or build_only):
version = ti.properties['version']
# we want to make sure in debug mode the version always changes
version = "%s.%d" % (version,time.time())
if (deploytype != 'production'):
ti.properties['version']=version
pp = os.path.expanduser("~/Library/MobileDevice/Provisioning Profiles/%s.mobileprovision" % appuuid)
provisioning_profile = read_provisioning_profile(pp,o)
create_info_plist(ti, template_dir, project_dir, infoplist)
applogo = None
clean_build = False
# check to see if the appid is different (or not specified) - we need to re-generate
if read_project_appid(project_xcconfig)!=appid or not infoplist_has_appid(infoplist,appid):
clean_build = True
force_xcode = True
new_lib_hash = None
lib_hash = None
existing_git_hash = None
# this code simply tries and detect if we're building a different
# version of the project (or same version but built from different git hash)
# and if so, make sure we force rebuild so to propagate any code changes in
# source code (either upgrade or downgrade)
if os.path.exists(app_dir):
if os.path.exists(version_file):
line = open(version_file).read().strip()
lines = line.split(",")
v = lines[0]
log_id = lines[1]
if len(lines) > 2:
lib_hash = lines[2]
existing_git_hash = lines[3]
if lib_hash==None:
force_rebuild = True
else:
if template_dir==v and force_rebuild==False:
force_rebuild = False
else:
log_id = None
else:
force_rebuild = True
else:
force_rebuild = True
o.write("\ngithash=%s, existing_git_hash=%s\n" %(githash,existing_git_hash))
if githash!=existing_git_hash:
force_rebuild = True
# we want to read the md5 of the libTiCore.a library since it must match
# the current one we're building and if not, we need to force a rebuild since
# that means we've copied in a different version of the library and we need
# to rebuild clean to avoid linking errors
source_lib=os.path.join(template_dir,'libTiCore.a')
fd = open(source_lib,'rb')
m = hashlib.md5()
m.update(fd.read(1024)) # just read 1K, it's binary
new_lib_hash = m.hexdigest()
fd.close()
if new_lib_hash!=lib_hash:
force_rebuild=True
o.write("forcing rebuild since libhash (%s) not matching (%s)\n" % (lib_hash,new_lib_hash))
lib_hash=new_lib_hash
# when we force rebuild, we need to re-compile and re-copy source, libs etc
if force_rebuild:
o.write("Performing full rebuild\n")
print "[INFO] Performing full rebuild. This will take a little bit. Hold tight..."
sys.stdout.flush()
# In order to avoid dual-mangling, we need to make sure that if we're re-projecting,
# there is NOT an existing xcodeproj file.
if not os.path.exists(os.path.join(iphone_dir, "%s.xcodeproj" % name)):
project = Projector(name,sdk_version,template_dir,project_dir,appid)
project.create(template_dir,iphone_dir)
force_xcode = True
if os.path.exists(app_dir): shutil.rmtree(app_dir)
# we have to re-copy if we have a custom version
create_info_plist(ti, template_dir, project_dir, infoplist)
# since compiler will generate the module dependencies, we need to
# attempt to compile to get it correct for the first time.
compiler = Compiler(project_dir,appid,name,deploytype)
compiler.compileProject(xcode_build,devicefamily,iphone_version,True)
else:
if simulator:
softlink_for_simulator(project_dir,app_dir)
contents="TI_VERSION=%s\n"% sdk_version
contents+="TI_SDK_DIR=%s\n" % template_dir.replace(sdk_version,'$(TI_VERSION)')
contents+="TI_APPID=%s\n" % appid
contents+="OTHER_LDFLAGS[sdk=iphoneos*]=$(inherited) -weak_framework iAd\n"
contents+="OTHER_LDFLAGS[sdk=iphonesimulator*]=$(inherited) -weak_framework iAd\n"
contents+="#include \"module\"\n"
xcconfig = open(project_xcconfig,'w+')
xccontents = xcconfig.read()
if contents!=xccontents:
o.write("writing contents of %s:\n\n%s\n" % (project_xcconfig,contents))
o.write("old contents\n\n%s\n" % (xccontents))
xcconfig.write(contents)