Skip to content

Commit 0539fd4

Browse files
adigienordicjm
authored andcommitted
[nrf noup] Add --keep-previous argument to zap-generate
* Remove previous ZAP generated files. * Add `-k`/`--keep-previous` argument to skip removing old files. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent a06ea5a commit 0539fd4

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

scripts/west/zap_generate.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88

99
from textwrap import dedent
10+
from pathlib import Path
1011

1112
from west import log
1213
from west.commands import CommandError, WestCommand
@@ -37,6 +38,7 @@ def do_add_parser(self, parser_adder):
3738
parser.add_argument('-m', '--matter-path', type=existing_dir_path,
3839
default=DEFAULT_MATTER_PATH, help='Path to Matter SDK')
3940
parser.add_argument('-f', '--full', action='store_true', help='Generate full data model files')
41+
parser.add_argument('-k', '--keep-previous', action='store_true', help='Keep previously generated files')
4042
return parser
4143

4244
def build_command(self, zap_file_path, output_path, templates_path):
@@ -68,10 +70,12 @@ def do_run(self, args, unknown_args):
6870
# make sure that the generate.py script uses the proper zap_cli binary (handled by west)
6971
os.environ["ZAP_INSTALL_PATH"] = str(zap_installer.get_zap_cli_path().parent.absolute())
7072

71-
7273
# Make sure that output directory exists
7374
output_path.mkdir(exist_ok=True)
7475

76+
if not args.keep_previous:
77+
self.clear_generated_files(output_path)
78+
7579
self.check_call(self.build_command(zap_file_path, output_path, app_templates_path))
7680

7781
if args.full:
@@ -81,3 +85,15 @@ def do_run(self, args, unknown_args):
8185
self.check_call(self.build_command(zap_file_path, output_path, templates_path))
8286

8387
log.inf(f"Done. Files generated in {output_path}")
88+
89+
def clear_generated_files(self, path: Path):
90+
log.inf("Clearing previously generated files:")
91+
for file in path.iterdir():
92+
if file.is_file():
93+
with open(file, 'r') as f:
94+
for line in f.readlines():
95+
if "// THIS FILE IS GENERATED BY ZAP" in line:
96+
log.inf(f"\tRemoving {file}")
97+
file.unlink()
98+
break
99+

0 commit comments

Comments
 (0)