[macplist]: interpolate file

Rather than using an inline interpolated heredoc string, or generating
the file in a pre-build step, allow the macplist task itself to
perform string interpolation on the contents of a plist file.
This commit is contained in:
Caitlin Potter 2015-06-14 15:44:58 -04:00
parent 65150769cd
commit 9d763fc74f
3 changed files with 139 additions and 3 deletions

View File

@ -0,0 +1,3 @@
int main(int argc, const char** argv) {
return 0;
}

124
tests/macplist/wscript Normal file
View File

@ -0,0 +1,124 @@
#! /usr/bin/env python3
import platform, sys
top = '.'
out = 'bin'
plist_string = '''
<?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>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>{app_name}</string>
<key>CFBundleIdentifier</key>
<string>{bundle_domain}</string>
<key>CFBundleVersion</key>
<string>{bundle_version}</string>
<key>MiscKey</key>
<string>{env[PLATFORM_NAME]}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>{bundle_name}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
</dict>
</plist>
'''
plist_context = {
'bundle_domain': 'com.foo.bar.baz',
'app_name': 'macplist_test',
'bundle_version': '1.6.7'
}
expected_dict = {
'env': {
'PLATFORM_NAME': 'darwin'
}
}
def options(opt):
opt.load('compiler_c')
def configure(conf):
conf.load('compiler_c')
def build(bld):
# Only testing feature on Darwin
if (platform.system() != 'Darwin'):
return
bld.env.PLATFORM_NAME = 'darwin'
plist = {'bundle_name': 'InterpolatedPlistFileTest'}
plist.update(plist_context)
bld.path.make_node('Info.plist').write(plist_string)
bld.program(
features="c cprogram",
target="InterpolatedPlistFileTest",
source="src/main.c",
mac_app=True,
mac_plist="Info.plist",
plist_context=plist)
bld.add_post_fun(test1)
plist = {'bundle_name': 'InterpolatedPlistStringTest'}
plist.update(plist_context)
bld.program(
features="c cprogram",
target="InterpolatedPlistStringTest",
source="src/main.c",
mac_app=True,
mac_plist=plist_string,
plist_context=plist)
bld.add_post_fun(test2)
bld.program(
features="c cprogram",
target="DefaultPlistTest",
source="src/main.c",
mac_app=True)
bld.add_post_fun(test3)
def assert_eq(expected, actual):
exception_string = '''
Expected `{expected}`
but instead got `{actual}`
'''
if (expected != actual):
raise Exception(exception_string.format(expected=expected,actual=actual))
def test1(ctx):
expected_plist = {'bundle_name': 'InterpolatedPlistFileTest'}
expected_plist.update(plist_context)
expected_plist.update(expected_dict)
expected_plist = plist_string.format(**expected_plist)
plist = ctx.path.make_node('bin/InterpolatedPlistFileTest.app/Contents/Info.plist').read()
assert_eq(expected_plist, plist)
def test2(ctx):
expected_plist = {'bundle_name': 'InterpolatedPlistStringTest'}
expected_plist.update(plist_context)
expected_plist.update(expected_dict)
expected_plist = plist_string.format(**expected_plist)
plist = ctx.path.make_node('bin/InterpolatedPlistStringTest.app/Contents/Info.plist').read()
assert_eq(expected_plist, plist)
def test3(ctx):
from waflib.Tools import c_osx
expected_plist = {'app_name': 'DefaultPlistTest'}
expected_plist = c_osx.app_info.format(**expected_plist)
plist = ctx.path.make_node('bin/DefaultPlistTest.app/Contents/Info.plist').read()
assert_eq(expected_plist, plist)

View File

@ -24,7 +24,7 @@ app_info = '''
<key>NOTE</key>
<string>THIS IS A GENERATED FILE, DO NOT MODIFY</string>
<key>CFBundleExecutable</key>
<string>%s</string>
<string>{app_name}</string>
</dict>
</plist>
'''
@ -127,6 +127,14 @@ def create_task_macplist(self):
dir = self.create_bundle_dirs(name, out)
n1 = dir.find_or_declare(['Contents', 'Info.plist'])
self.plisttask = plisttask = self.create_task('macplist', [], n1)
plisttask.context = {
'app_name': self.link_task.outputs[0].name,
'env': self.env
}
plist_ctx = getattr(self, 'plist_context', None)
if (plist_ctx):
plisttask.context.update(plist_ctx)
if getattr(self, 'mac_plist', False):
node = self.path.find_resource(self.mac_plist)
@ -135,7 +143,7 @@ def create_task_macplist(self):
else:
plisttask.code = self.mac_plist
else:
plisttask.code = app_info % self.link_task.outputs[0].name
plisttask.code = app_info
inst_to = getattr(self, 'install_path', '/Applications') + '/%s/Contents/' % name
self.bld.install_files(inst_to, n1)
@ -184,5 +192,6 @@ class macplist(Task.Task):
txt = self.code
else:
txt = self.inputs[0].read()
context = getattr(self, 'context', {})
txt = txt.format(**context)
self.outputs[0].write(txt)