Skip to content

Commit

Permalink
Meson the manual
Browse files Browse the repository at this point in the history
Co-Authored-By: Qyriad <qyriad@qyriad.me>
Co-Authored-By: eldritch horrors <pennae@lix.systems>
  • Loading branch information
3 people committed Aug 19, 2024
1 parent ffbb5a8 commit 535f68a
Show file tree
Hide file tree
Showing 17 changed files with 683 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/manual/.version
22 changes: 22 additions & 0 deletions doc/manual/generate-deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

import glob
import sys

# meson expects makefile-style dependency declarations, i.e.
#
# target: dependency...
#
# meson seems to pass depfiles straight on to ninja even though
# it also parses the file itself (or at least has code to do so
# in its tree), so we must live by ninja's rules: only slashes,
# spaces and octothorpes can be escaped, anything else is taken
# literally. since the rules for these aren't even the same for
# all three we will just fail when we encounter any of them (if
# asserts are off for some reason the depfile will likely point
# to nonexistant paths, making everything phony and thus fine.)
for path in glob.glob(sys.argv[1] + '/**', recursive=True):
assert '\\' not in path
assert ' ' not in path
assert '#' not in path
print("ignored:", path)
61 changes: 61 additions & 0 deletions doc/manual/json-to-tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3

"""
This script is a helper for this project's Meson buildsystem, to replace its
usage of `nix eval --write-to`. Writing a JSON object as a nested directory
tree is more generic, easier to maintain, and far, far less cursed. Nix
has 'good' support for JSON output. Let's just use it.
"""

import argparse
from pathlib import Path
import json
import sys

name = 'json-to-tree.py'

def log(*args, **kwargs):
kwargs['file'] = sys.stderr
return print(f'{name}:', *args, **kwargs)

def write_dict_to_directory(current_directory: Path, data: dict, files_written=0):
current_directory.mkdir(parents=True, exist_ok=True)
for key, value in data.items():
nested_path = current_directory / key
match value:
case dict(nested_data):
files_written += write_dict_to_directory(nested_path, nested_data)

case str(content):
nested_path.write_text(content)
files_written += 1

case rest:
assert False, \
f'should have been called on a dict or string, not {type(rest)=}\n\t{rest=}'

return files_written

def main():
parser = argparse.ArgumentParser(name)
parser.add_argument('-i', '--input', type=argparse.FileType('r'), default='-',
help='The JSON input to operate on and output as a directory tree',
)
parser.add_argument('-o', '--output', type=Path, required=True,
help='The place to put the directory tree',
)
args = parser.parse_args()

json_string = args.input.read()

try:
data = json.loads(json_string)
except json.JSONDecodeError:
log(f'could not decode JSON from input: {json_string}')
raise


files_written = write_dict_to_directory(args.output, data)
log(f'wrote {files_written} files')

sys.exit(main())
Loading

0 comments on commit 535f68a

Please sign in to comment.