Debian Codegraph @

bin/fill_placeholders

raw
1 #!/usr/bin/env bash
2
3
4 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
5 THIS_PROG="$0"
6 SUBPROGRAM="ls"
7
8 . "$DIR"/_utils.sh
9
10
11 function show_help {
12 cat <<-EOF
13 USAGE: $THIS_PROG VERSION_NUMBER
14
15 This script will fill all placeholder values in the lookatme me project.
16 Specifically:
17
18 * {{VERSION}} will be replaced with the version number (no 'v' prefix!)
19 * {{LOOKATME_HELP_OUTPUT}} will be replaced with output from "lookatme --help"
20 * {{LOOKATME_HELP_OUTPUT_INDENTED}} will be replaced with the indented
21 output of "lookatme --help"
22 EOF
23 }
24
25 function replace_in_files {
26 local placeholder="$1"
27 local replacement="$2"
28
29 log "==== Replacing $placeholder"
30 (
31 cd "$DIR"/..
32 files_to_replace=$(grep -rl "$placeholder" $(git ls-files) | grep -v bin/fill_placeholders)
33 python3 <(cat <<-EOF
34 import sys
35
36 placeholder = sys.argv[1]
37 replacement = sys.argv[2]
38 files_to_replace = sys.argv[3:]
39
40 for file in files_to_replace:
41 with open(file, "r") as f:
42 data = f.read()
43
44 new_data = data.replace(placeholder, replacement)
45 if new_data != data:
46 print(" replacing {}".format(file))
47 with open(file, "w") as f:
48 f.write(new_data)
49 EOF
50 ) "$placeholder" "$replacement" $files_to_replace
51 )
52 }
53
54 function set_version {
55 replace_in_files "{{VERSION}}" "$VERSION"
56 }
57
58 function set_help_output {
59 local help_output=$(cd "$DIR"/.. ; python3 -m lookatme --help)
60 replace_in_files "{{LOOKATME_HELP_OUTPUT}}" "$help_output"
61
62 local help_output_indented=$(sed 's/^/ /' <<<$help_output)
63 replace_in_files "{{LOOKATME_HELP_OUTPUT_INDENTED}}" "$help_output_indented"
64 }
65
66
67 function parse_args {
68 VERSION=""
69 while [ $# -ne 0 ] ; do
70 param="$1"
71 shift
72
73 case "$param" in
74 --help|-h)
75 show_help
76 exit 1
77 ;;
78 *)
79 VERSION="$param"
80 ;;
81 esac
82 done
83 }
84
85 parse_args "$@"
86 # needs to come first, can contain {{VERSION}} placeholders
87 set_help_output
88 set_version