Skip to content

Commit

Permalink
render only modified template files
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuther committed Dec 14, 2022
1 parent 8daef81 commit f035273
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.last_run
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ _Automatic code generation from Jinja2 templates_
Generate your code by running the following command:

```bash
python3 ./src/StempelWerk.py PATH_TO_SETTINGS_FILE
python3 ./src/StempelWerk.py [--only-modified] PATH_TO_SETTINGS_FILE
```

## Performance

By default, StempelWerk renders all template files located in the
specified template directory.

When you use the command line argument `--only-modified`, however,
StempelWerk tries to process only the template files that have changed
since the last successful run. Use of this command line argument is
highly discouraged in CI/CD pipelines!

_This logic is not infallible: some file systems update modification
times in a weird manner, and changes to master templates (called
"stencils" in StempelWerk) are currently not handled. However, in such
a case you can simply use StempelWerk without the `--only-modified`
argument._

## Settings

Settings for StempelWerk are provided in the form of a JSON file. The
Expand All @@ -32,10 +48,9 @@ directory.

### `stencil_dir_name`

Name of directory that contains master templates (called "stencils" in
StempelWerk). All files in `template_dir/stencil_dir_name` will be
loaded into Jinja2 and can be referenced from other templates at
runtime.
Name of directory that contains stencils. All files in
`template_dir/stencil_dir_name` will be loaded into Jinja2 and can be
referenced from other templates at runtime.

In addition, files in any directory matching this name will not be
rendered.
Expand Down
2 changes: 2 additions & 0 deletions process_templates.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#! /usr/bin/bash

# process all templates
python3 "./src/StempelWerk.py" ../settings_example.json
45 changes: 39 additions & 6 deletions src/StempelWerk.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
#
# ----------------------------------------------------------------------------

import datetime
import json
import math
import os
import pathlib
import platform
Expand All @@ -54,12 +56,14 @@
from DirWalk.DirWalk import dirwalk


VERSION = '0.3.2'

# ensure that this script can be called from anywhere
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)

VERSION = '0.4.0'
LAST_RUN_FILE = os.path.normpath(
os.path.join(script_dir, '../.last_run'))


# Auto-create settings class to write leaner code
#
Expand Down Expand Up @@ -190,7 +194,7 @@ def render_template(settings, cached_templates, template_filename):
print()


def process_templates(settings_path):
def process_templates(settings_path, only_modified=False):
settings = load_settings(settings_path)

# do not end entries with path separators ("/" or "\")!
Expand All @@ -206,13 +210,31 @@ def process_templates(settings_path):
# load and cache stencils
cached_templates = cache_templates(settings, list_templates=False)

modified_after = None
if only_modified:
try:
# get time of last run
with open(LAST_RUN_FILE) as f:
modified_after = f.read().strip()
except IOError:
modified_after = None

# find all Jinja2 files in template directory
for template_filename in dirwalk(
settings.template_dir,
included=inclusions,
modified_after=None):
modified_after=modified_after):
render_template(settings, cached_templates, template_filename)

# save time of current run
with open(LAST_RUN_FILE, mode='w') as f:
# round down to ensure that files with inaccurate timestamps and
# other edge cases are included
current_timestamp = math.floor(
datetime.datetime.now().timestamp())

f.write(str(current_timestamp))


def display_version():
print()
Expand All @@ -231,7 +253,18 @@ def display_version():

display_version()

command_line_arguments = list(sys.argv)
only_modified = False

# extremely primitive command line parsing
if '--only-modified' in command_line_arguments:
only_modified = True
command_line_arguments.remove('--only-modified')

# highly sophisticated command line parsing
settings_path = command_line_arguments.pop()

# settings path is relative to the path of this script
settings_path = os.path.join(script_dir, sys.argv[1])
settings_path = os.path.join(script_dir, settings_path)

process_templates(settings_path)
process_templates(settings_path, only_modified)
4 changes: 4 additions & 0 deletions update_templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/bash

# process only templates modified since the last run
python3 "./src/StempelWerk.py" --only-modified ../settings_example.json

0 comments on commit f035273

Please sign in to comment.