forked from fadingred/Greenwich
/
create
executable file
·178 lines (160 loc) · 8.13 KB
/
create
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
#!/usr/bin/env python
#
# Copyright (c) 2013 FadingRed LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
from scriptlib.config import OptionParser
from scriptlib.i18n import Strings, Xib, Storyboard
import subprocess
import codecs
import shutil
import errno
import sys
import os
# global defines
# ----------------------------------------------------------------------
LOCALIZE_SYMBOL = os.environ.get('GREENWICH_LOCALIZATION_SYMBOL', 'NSLocalizedString') # localize symbol used in code
IOS_APPLICATION_TARGET = os.environ.get('EFFECTIVE_PLATFORM_NAME', '').find('iphone') != -1 and \
os.environ.get('PACKAGE_TYPE', '') == 'com.apple.package-type.wrapper.application'
# main create strings function
# ----------------------------------------------------------------------
def copy_strings(strings, config):
source = strings.path()
if 'inactive' in strings.lang: return
if not os.path.exists(source): return
destination = os.path.join(config.app_resources,
'%s.lproj' % strings.lang,
'%s.strings' % strings.name)
try: mtime = os.path.getmtime(destination)
except OSError: mtime = 0
if strings.modification_time() >= mtime:
sys.stdout.write('Copying %s.strings (%s) to %s\n' % (strings.name, strings.lang, destination))
try: os.makedirs(os.path.dirname(destination))
except OSError: pass
shutil.copyfile(source, destination)
def create_strings(config):
for resources in config.resources.split(':'):
if os.path.exists(resources):
# if copying strings, make sure that this is taking place after the copying of bundle resources since
# we will later check for the existence of the compiled nib file when copying over strings files.
if config.copy_strings:
try: resources_len = len(os.listdir(resources))
except: resources_len = 0
try: app_resources_len = len(os.listdir(config.app_resources))
except: app_resources_len = 0
if resources_len and not app_resources_len:
sys.stderr.write('warning: Creating strings through Greenwich should take place after the copy bundle resources build phase\n')
for docname in os.listdir(resources):
docclass = None
if docname.endswith('.xib'): docclass = Xib
elif docname.endswith('.storyboard'): docclass = Storyboard
if docclass:
doc = docclass(docname, config, resources=resources)
strings = Strings(docname, config.lang, config, resources=resources)
if doc.modification_time() > doc.generation_time():
sys.stdout.write('Generating %s strings for %s\n' % (config.lang, doc.name))
doc.generate(strings)
# handle all languages other than the base language of the file
# by integrating (or creating) the same strings files in that lproj
# directory
for langname in os.listdir(resources):
if langname.endswith('.lproj'):
lang = Strings(docname, langname, config, resources=resources)
if doc.generation_time() > lang.modification_time() and lang.lang != config.lang:
lang.integrate_changes(strings)
if config.copy_strings and (
os.path.exists(os.path.join(config.app_resources, '%s.nib' % doc.name)) or
os.path.exists(os.path.join(config.app_resources, '%s.storyboardc' % doc.name))):
copy_strings(lang, config)
# handle localizable.strings file
latest = None # get the latest modification time of any source .m file
sources = []
for source in config.source.split(':'):
if os.path.exists(source):
sources.append(source)
for file in os.listdir(source):
if file.endswith('.m') or file.endswith('.h'):
mod = os.path.getmtime(os.path.join(source, file))
if not latest or mod > latest: latest = mod
# run genstrings on all sources at the same time & put the
# localizable strings file in the first resources directory
resources = config.resources.split(':')[0]
localizable = Strings('Localizable', config.lang, config, resources=resources)
if latest > localizable.modification_time():
sys.stdout.write('Generating %s strings for source files\n' % (config.lang,))
if not os.path.exists(resources): os.mkdir(resources)
localizable.ensure_dir()
find_args = [ 'find' ] + sources + [ '-name', '*.m', '-or', '-name', '*.h', '-print0', ]
find_results = subprocess.check_output(find_args).split("\0")
genstrings_args = [ 'genstrings', '-s', LOCALIZE_SYMBOL, '-o', os.path.dirname(localizable.path()) ] + find_results
subprocess.check_output(genstrings_args)
file = codecs.open(localizable.path(), encoding='utf-16')
data = file.read()
file.close()
file = codecs.open(localizable.path(), 'w', encoding='utf-8')
file.write(data)
# handle all languages other than the base language of the file
# by integrating (or creating) the same strings files in that lproj
# directory
for langname in os.listdir(resources):
if langname.endswith('.lproj'):
lang = Strings(localizable.name, langname, config, resources=resources)
if latest > lang.modification_time() and lang.lang != config.lang:
lang.integrate_changes(localizable)
if config.copy_strings:
copy_strings(lang, config)
def copy_ios_framework(config):
search_paths = [
os.path.dirname(config.app),
os.environ.get('BUILT_PRODUCTS_DIR', ''),
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
os.path.dirname(os.path.dirname(os.path.dirname(sys.argv[0]))),
]
framework_path = None
handled = set()
for path in search_paths:
if not path in handled:
handled.add(path)
framework_path = os.path.join(path, 'Greenwich.framework')
if os.path.exists(framework_path): break
else: framework_path = None
if framework_path:
frameworks = os.path.join(config.app, 'Bundles')
destination = os.path.join(frameworks, 'Greenwich.framework')
if not os.path.exists(destination):
sys.stdout.write('Copying Greenwich resources into application bundle\n')
headers = os.path.join(destination, 'Headers')
executable = os.path.join(destination, 'Greenwich')
if not os.path.exists(frameworks):
os.makedirs(frameworks)
shutil.copytree(framework_path, destination, symlinks=True)
shutil.rmtree(headers)
os.unlink(executable)
else:
sys.stdout.write('warning: could not locate Greenwich framework to copy resources to application\n')
# main function
# ----------------------------------------------------------------------
if __name__ == '__main__':
usage = """usage: %%prog [options]
Used to update strings files for all xibs, storyboards and source files in a target."""
parser = OptionParser(usage=usage)
c = 'Copy created strings files to application resources folder [default]'
n = 'Disable copy of created strings files to application resources folder'
parser.add_option('-c', '--copy', action="store_true", dest='copy_strings', default=True, help=c)
parser.add_option('--no-copy', action="store_false", dest='copy_strings', help=n)
config = parser.parse_config()[0]
create_strings(config)
if IOS_APPLICATION_TARGET and config.copy_strings:
copy_ios_framework(config)