|
1 | | -import os |
2 | 1 | import json |
3 | | - |
| 2 | +import os |
| 3 | +import random |
| 4 | +from pathlib import Path |
4 | 5 |
|
5 | 6 | output_folders = [ |
6 | | - "source-output", # dev output |
| 7 | + "source-output", # dev output |
7 | 8 | "build-output", # default production output |
8 | | - "build-client" # I believe we create the production outputs here |
| 9 | + "build-client", # I believe we create the production outputs here |
9 | 10 | ] |
10 | 11 |
|
11 | 12 |
|
12 | | -def read_json_file(filename): |
| 13 | +def _get_applications_from_metadata(): |
13 | 14 | dirname = os.path.dirname(__file__) |
14 | | - meta_filename = os.path.join(dirname, filename) |
15 | | - with open(meta_filename, "r") as file: |
| 15 | + meta_filename = os.path.join(dirname, "apps_metadata.json") |
| 16 | + with open(meta_filename) as file: |
16 | 17 | metadata = json.load(file) |
17 | 18 | return metadata["applications"] |
18 | 19 |
|
19 | 20 |
|
20 | 21 | def update_apps_metadata(): |
21 | 22 | dirname = os.path.dirname(__file__) |
22 | | - applications = read_json_file("apps_metadata.json") |
| 23 | + applications = _get_applications_from_metadata() |
23 | 24 | for i in applications: |
24 | 25 | application = i.get("application") |
25 | 26 | for output_folder in output_folders: |
26 | | - filename = os.path.join(dirname, '..', output_folder, application, "index.html") |
27 | | - if not os.path.isfile(filename): |
28 | | - continue |
29 | | - with open(filename, "r") as file: |
30 | | - data = file.read() |
| 27 | + index_file_path = Path(dirname).joinpath( |
| 28 | + "..", output_folder, application, "index.html" |
| 29 | + ) |
| 30 | + if os.path.isfile(index_file_path): |
| 31 | + print(f"Updating app metadata: {index_file_path.resolve()}") |
31 | 32 | replacements = i.get("replacements") |
32 | 33 | for key in replacements: |
33 | 34 | replace_text = replacements[key] |
34 | | - data = data.replace("${"+key+"}", replace_text) |
35 | | - with open(filename, "w") as file: |
36 | | - print(f"Updating app metadata: {filename}") |
37 | | - file.write(data) |
| 35 | + index_file_path.write_text( |
| 36 | + index_file_path.read_text().replace( |
| 37 | + "${" + key + "}", |
| 38 | + replace_text, |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _get_output_file_paths(filename): |
| 44 | + output_file_paths: list[Path] = [] |
| 45 | + dirname = os.path.dirname(__file__) |
| 46 | + applications = _get_applications_from_metadata() |
| 47 | + for i in applications: |
| 48 | + application = i.get("application") |
| 49 | + for output_folder in output_folders: |
| 50 | + output_file_path = Path(dirname).joinpath( |
| 51 | + "..", output_folder, application, filename |
| 52 | + ) |
| 53 | + if output_file_path.is_file(): |
| 54 | + output_file_paths.append(output_file_path) |
| 55 | + return output_file_paths |
| 56 | + |
| 57 | + |
| 58 | +def add_no_cache_param(vcs_ref_client): |
| 59 | + index_file_paths = _get_output_file_paths("index.html") |
| 60 | + for index_file_path in index_file_paths: |
| 61 | + print(f"Updating vcs_ref_client: {index_file_path.resolve()}") |
| 62 | + index_file_path.write_text( |
| 63 | + index_file_path.read_text().replace( |
| 64 | + "${boot_params}", |
| 65 | + "nocache=" + vcs_ref_client, |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + boot_file_paths = _get_output_file_paths("boot.js") |
| 70 | + for boot_file_path in boot_file_paths: |
| 71 | + print(f"Updating addNoCacheParam URL_PARAMETERS: {boot_file_path.resolve()}") |
| 72 | + boot_file_path.write_text( |
| 73 | + boot_file_path.read_text().replace( |
| 74 | + "addNoCacheParam : false", |
| 75 | + "addNoCacheParam : true", |
| 76 | + ) |
| 77 | + ) |
38 | 78 |
|
39 | 79 |
|
40 | 80 | if __name__ == "__main__": |
41 | 81 | update_apps_metadata() |
| 82 | + vcs_ref_client = os.getenv("VCS_REF_CLIENT", str(random.random())) |
| 83 | + add_no_cache_param(vcs_ref_client) |
0 commit comments