77import sys
88
99from textwrap import dedent
10+ from pathlib import Path
1011
1112from west import log
1213from 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"\t Removing { file } " )
97+ file .unlink ()
98+ break
99+
0 commit comments