xref: /aosp_15_r20/external/skia/tools/fiddle/make_all_examples_cpp.py (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#!/usr/bin/env python3
2# Copyright 2019 Google LLC.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Run this script to re-generate the `all_examples.cpp` file after adding or
6deleting example fiddles.
7"""
8import argparse
9import difflib
10import glob
11import os
12
13ALL_EXAMPLES = 'all_examples.cpp'
14
15
16parser = argparse.ArgumentParser()
17parser.add_argument(
18    '--print-diff',
19    action='store_true',
20    help='Print the diff between the old and new `all_examples.cpp` file.',
21)
22args = parser.parse_args()
23
24
25os.chdir(os.path.dirname(__file__))
26
27with open(ALL_EXAMPLES, 'r') as o:
28    prev_lines = o.readlines()
29
30with open(ALL_EXAMPLES, 'w+') as o:
31    o.write(
32        '// Copyright 2019 Google LLC.\n// Use of this source code is '
33        'governed by a BSD-style license that can be found in the '
34        'LICENSE file.\n'
35    )
36    for path in sorted(glob.glob('../../docs/examples/*.cpp')):
37        # strip ../../
38        path = path[6:]
39        o.write('#include "%s"\n' % path)
40
41if args.print_diff:
42    with open(ALL_EXAMPLES, 'r') as o:
43        diff = difflib.unified_diff(prev_lines, o.readlines())
44        print(''.join(diff))
45