summaryrefslogtreecommitdiff
path: root/src/lib/generation-metadata.source.sh
blob: 20f5d095122003adce822d2c5af026684ed5002c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Generation metadata: collect a snapshot of the run (generator version, source
# and generated file counts, effective settings) and serialise it to the
# dist/shuriken.json sidecar. Moved out of album-metadata.source.sh (task 6r0):
# describing the generation run is its own concern, distinct from the EXIF
# presentation that stays in the album module. The collector depends on the file
# counters (count_files / count_incoming_images / count_tree_files, now in
# image.source.sh), current_timestamp_iso (template.source.sh) and the JSON
# helpers (json_string / json_bool, template.source.sh); all are sourced before
# this module, and these are runtime calls anyway, so source order documents the
# dependency without affecting availability. Behaviour and signatures are
# unchanged by the move.

_collect_generation_metadata() {
    local -r tarball_file="$1"; shift

    declare -gA _GENERATION_METADATA=()
    _GENERATION_METADATA["generator_name"]='shuriken'
    _GENERATION_METADATA["generator_version"]="$VERSION"
    _GENERATION_METADATA["generated_at"]=$(current_timestamp_iso)
    _GENERATION_METADATA["config_source"]="$SHURIKEN_CONFIG_SOURCE"
    _GENERATION_METADATA["template_name"]=$(basename "$TEMPLATE_DIR")
    _GENERATION_METADATA["template_directory"]="$TEMPLATE_DIR"
    _GENERATION_METADATA["source_incoming_dir"]="$INCOMING_DIR"
    _GENERATION_METADATA["source_image_count"]=$(count_incoming_images)
    _GENERATION_METADATA["generated_photo_count"]=$(count_files "$DIST_DIR/photos")
    _GENERATION_METADATA["generated_thumb_count"]=$(count_files "$DIST_DIR/thumbs")
    _GENERATION_METADATA["generated_html_count"]=$(
        count_tree_files "$DIST_DIR" '*.html'
    )
    _GENERATION_METADATA["tarball_included"]="$TARBALL_INCLUDE"
    _GENERATION_METADATA["tarball_file"]="$tarball_file"
    # SC2153: a lowercase "title" local in the --check-sourced stats modules
    # makes shellcheck suspect $TITLE is a misspelling. The config global is
    # correct here; the suppression used to ride on a literal ${TITLE:-} in
    # template.source.sh, which is now an indirect expansion, so disable it here.
    # shellcheck disable=SC2153
    _GENERATION_METADATA["settings_title"]="$TITLE"
    _GENERATION_METADATA["settings_height"]="$HEIGHT"
    _GENERATION_METADATA["settings_thumbheight"]="$THUMBHEIGHT"
    _GENERATION_METADATA["settings_maxpreviews"]="$MAXPREVIEWS"
    _GENERATION_METADATA["settings_subdivide_percent"]="$THUMB_SUBDIVIDE_PERCENT"
    _GENERATION_METADATA["settings_feature_percent"]="$THUMB_FEATURE_PERCENT"
    _GENERATION_METADATA["settings_image_jobs"]="$IMAGE_JOBS"
    _GENERATION_METADATA["settings_random_seed"]="$RANDOM_SEED"
    _GENERATION_METADATA["settings_shuffle"]="$SHUFFLE"
    _GENERATION_METADATA["settings_splash_page"]="$SPLASH_PAGE"
    _GENERATION_METADATA["settings_details_page"]="$DETAILS_PAGE"
    _GENERATION_METADATA["settings_stats_page"]="$STATS_PAGE"
    _GENERATION_METADATA["settings_original_basepath"]="$ORIGINAL_BASEPATH"
}

# Emit the generator/generated_at/config_source/template/source/generated/tarball
# JSON sections (everything before "settings"). All read the _GENERATION_METADATA
# global the collector populated; split out only to keep the serialiser short.
_generation_metadata_json_head() {
    printf '{\n'
    printf '  "generator": {\n'
    printf '    "name": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["generator_name"]}")"
    printf '    "version": %s\n' \
        "$(json_string "${_GENERATION_METADATA["generator_version"]}")"
    printf '  },\n'
    printf '  "generated_at": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["generated_at"]}")"
    printf '  "config_source": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["config_source"]}")"
    printf '  "template": {\n'
    printf '    "name": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["template_name"]}")"
    printf '    "directory": %s\n' \
        "$(json_string "${_GENERATION_METADATA["template_directory"]}")"
    printf '  },\n'
    printf '  "source": {\n'
    printf '    "incoming_dir": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["source_incoming_dir"]}")"
    printf '    "image_count": %s\n' \
        "${_GENERATION_METADATA["source_image_count"]}"
    printf '  },\n'
    printf '  "generated": {\n'
    printf '    "photo_count": %s,\n' \
        "${_GENERATION_METADATA["generated_photo_count"]}"
    printf '    "thumb_count": %s,\n' \
        "${_GENERATION_METADATA["generated_thumb_count"]}"
    printf '    "html_count": %s\n' \
        "${_GENERATION_METADATA["generated_html_count"]}"
    printf '  },\n'
    printf '  "tarball": {\n'
    printf '    "included": %s,\n' \
        "$(json_bool "${_GENERATION_METADATA["tarball_included"]}")"
    printf '    "file": %s\n' \
        "$(json_string "${_GENERATION_METADATA["tarball_file"]}")"
    printf '  },\n'
}

# Emit the "settings" object and the closing brace. Split from the head so each
# half stays around 30 lines; identical byte output to the original one-shot.
_generation_metadata_json_settings() {
    printf '  "settings": {\n'
    printf '    "title": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_title"]}")"
    printf '    "height": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_height"]}")"
    printf '    "thumbheight": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_thumbheight"]}")"
    printf '    "maxpreviews": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_maxpreviews"]}")"
    printf '    "subdivide_percent": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_subdivide_percent"]}")"
    printf '    "feature_percent": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_feature_percent"]}")"
    printf '    "image_jobs": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_image_jobs"]}")"
    printf '    "random_seed": %s,\n' \
        "$(json_string "${_GENERATION_METADATA["settings_random_seed"]}")"
    printf '    "shuffle": %s,\n' \
        "$(json_bool "${_GENERATION_METADATA["settings_shuffle"]}")"
    printf '    "splash_page": %s,\n' \
        "$(json_bool "${_GENERATION_METADATA["settings_splash_page"]}")"
    printf '    "details_page": %s,\n' \
        "$(json_bool "${_GENERATION_METADATA["settings_details_page"]}")"
    printf '    "stats_page": %s,\n' \
        "$(json_bool "${_GENERATION_METADATA["settings_stats_page"]}")"
    printf '    "original_basepath": %s\n' \
        "$(json_string "${_GENERATION_METADATA["settings_original_basepath"]}")"
    printf '  }\n'
    printf '}\n'
}

# Serialise _GENERATION_METADATA to the dist/shuriken.json layout. Thin
# orchestrator over the head + settings emitters above.
_generation_metadata_json() {
    _generation_metadata_json_head
    _generation_metadata_json_settings
}

write_generation_metadata() {
    local -r tarball_file="$1"; shift

    _collect_generation_metadata "$tarball_file"
    {
        _generation_metadata_json
    } > "$DIST_DIR/shuriken.json"
}