iditacards/configure.py
2024-10-14 17:38:03 -04:00

99 lines
2.5 KiB
Python

#!/usr/bin/env python3
""" Generates build.ninja for iditacards build in output/. """
import glob
import subprocess
import json
import os
cue_sources = glob.glob(f'*.cue')
yaml_sources = glob.glob(f'*.yaml')
sources = cue_sources + yaml_sources
everything = json.loads(subprocess.run(['cue', 'export', '--'] + sources, stdout=subprocess.PIPE).stdout)
assets = everything['assets']
pseudos = everything['pseudoassets']
all_assets = { **assets, **pseudos }
print('# Generated from configure.sh, do not edit this file!\n')
with open(f'{script_dir}/rules.ninja', 'r') as f:
print(f.read())
print(f'''
base = .
rule configure
description = recreate $out using $in
command = $in > $out
generator = 1
restat = 1
build build.ninja: configure ./configure.py | {' '.join(sources)}
build output/.everything.json: cuegen {' '.join(sources)}
filter = .
''')
for name, asset in all_assets.items():
assetdir = os.path
if asset['kind'] == 'template':
print(f'''
build output/.{name}.update | .{name}.json: extract .everything.json
filter = --arg asset '{name}' '.assets[$$asset].data'
target = .{name}.json
build tex/{name}.tex: template2tex .{name}.json | ../templates/{asset["template"]}
template = ../templates/{asset["template"]}
build {name}.pdf: tex2pdf tex/{name}.tex || .{name}.pdf.dd
dyndep = .{name}.pdf.dd
build .{name}.pdf.dd: scantex tex/{name}.tex
target = {name}.pdf
build {name}.{'raw.' if 'print' in asset else ''}png: pdf2png {name}.pdf
w = {asset['size'].split('x')[0]}
h = {asset['size'].split('x')[1]}
''')
if asset['kind'] == 'image':
print(f'''
build {name}.{'raw.' if 'print' in asset else ''}png: copy ../{asset['source']}
''')
if asset['kind'] == 'tex':
print(f'''
build {name}.pdf: tex2pdf ../{asset['source']} || .{name}.pdf.dd
dyndep = .{name}.pdf.dd
build .{name}.pdf.dd: scantex ../{asset['source']}
target = {name}.pdf
build {name}.{'raw.' if 'print' in asset else ''}png: pdf2png {name}.pdf
w = {asset['size'].split('x')[0]}
h = {asset['size'].split('x')[1]}
''')
if asset['kind'] == 'texdoc':
print(f'''
build {name}.pdf: tex2pdf2x ../{asset['source']} || .{name}.pdf.dd
dyndep = .{name}.pdf.dd
build .{name}.pdf.dd: scantex ../{asset['source']}
target = {name}.pdf
''')
if asset['kind'] == 'cat':
print(f'''
build {name}.pdf: pdfunite {' '.join(content + '.pdf' for content in asset['contents'])}
''')
if 'print' in asset:
print(f'''
build {name}.png: convert {name}.raw.png
args = {asset['print']}
''')