Browse Source

[cs/java] clean output directory of old files

Dan Korostelev 10 years ago
parent
commit
303a937651
5 changed files with 54 additions and 10 deletions
  1. 2 0
      common.ml
  2. 1 0
      extra/CHANGES.txt
  3. 33 6
      gencommon.ml
  4. 9 2
      gencs.ml
  5. 9 2
      genjava.ml

+ 2 - 0
common.ml

@@ -195,6 +195,7 @@ module Define = struct
 		| JsClassic
 		| JsEs5
 		| JsFlatten
+		| KeepOldOutput
 		| Macro
 		| MacroTimes
 		| NekoSource
@@ -272,6 +273,7 @@ module Define = struct
 		| JsClassic -> ("js_classic","Don't use a function wrapper and strict mode in JS output")
 		| JsEs5 -> ("js_es5","Generate JS for ES5-compliant runtimes")
 		| JsFlatten -> ("js_flatten","Generate classes to use fewer object property lookups")
+		| KeepOldOutput -> ("keep_old_output","Keep old source files in the output directory (for C#/Java)")
 		| Macro -> ("macro","Defined when we compile code in the macro context")
 		| MacroTimes -> ("macro_times","Display per-macro timing when used with --times")
 		| NetVer -> ("net_ver", "<version:20-45> Sets the .NET version to be targeted")

+ 1 - 0
extra/CHANGES.txt

@@ -43,6 +43,7 @@
 	neko : create output directory if it does not exist
 	js : inline Math methods and fields
 	cs/java : optimized Reflect.fields on dynamic structures
+	cs/java : haxe will now clear output directory of old files (use -D keep-old-output to keep them)
 	cs : optimized casting of parametrized types
 	cs : beautify c# code output
 

+ 33 - 6
gencommon.ml

@@ -43,7 +43,7 @@
 
 	Weaknesses and TODO's
 *)
-
+open Unix
 open Ast
 open Type
 open Common
@@ -936,7 +936,7 @@ let run_filters gen =
 (* basic generation module that source code compilation implementations can use *)
 (* ******************************************* *)
 
-let write_file gen w source_dir path extension =
+let write_file gen w source_dir path extension out_files =
 	let t = timer "write file" in
 	let s_path = source_dir	^ "/" ^ (snd path) ^ "." ^ (extension) in
 	(* create the folders if they don't exist *)
@@ -955,8 +955,35 @@ let write_file gen w source_dir path extension =
 		output_string f contents;
 		close_out f
 	end;
+
+	out_files := (unique_full_path s_path) :: !out_files;
+
 	t()
 
+
+let clean_files path excludes verbose =
+	let rec iter_files pack dir path = try
+		let file = Unix.readdir dir in
+
+		if file <> "." && file <> ".." then begin
+			let filepath = path ^ "/" ^ file in
+			if (Unix.stat filepath).st_kind = S_DIR then
+				let pack = pack @ [file] in
+				iter_files (pack) (Unix.opendir filepath) filepath;
+				try Unix.rmdir filepath with Unix.Unix_error (ENOTEMPTY,_,_) -> ();
+			else if not (List.mem (unique_full_path filepath) excludes) then begin
+				if verbose then print_endline ("Removing " ^ filepath);
+			 	Sys.remove filepath
+			end
+		end;
+
+		iter_files pack dir path
+	with | End_of_file | Unix.Unix_error _ ->
+		Unix.closedir dir
+	in
+	iter_files [] (Unix.opendir path) path
+
+
 let dump_descriptor gen name path_s module_s =
 	let w = SourceWriter.new_source_writer () in
 	(* dump called path *)
@@ -1093,7 +1120,7 @@ let is_relative cwd rel =
 	If received true, it means that module_gen has generated this content, so the file must be saved.
 	See that it will write a whole module
 *)
-let generate_modules gen extension source_dir (module_gen : SourceWriter.source_writer->module_def->bool) =
+let generate_modules gen extension source_dir (module_gen : SourceWriter.source_writer->module_def->bool) out_files =
 	let cwd = Common.unique_full_path (Sys.getcwd()) in
 	List.iter (fun md_def ->
 		let source_dir =
@@ -1125,11 +1152,11 @@ let generate_modules gen extension source_dir (module_gen : SourceWriter.source_
 		let should_write = module_gen w md_def in
 		if should_write then begin
 			let path = md_def.m_path in
-			write_file gen w source_dir path extension;
+			write_file gen w source_dir path extension out_files
 		end
 	) gen.gcon.modules
 
-let generate_modules_t gen extension source_dir change_path (module_gen : SourceWriter.source_writer->module_type->bool) =
+let generate_modules_t gen extension source_dir change_path (module_gen : SourceWriter.source_writer->module_type->bool) out_files =
 	let source_dir = gen.gcon.file ^ "/" ^ source_dir in
 	List.iter (fun md ->
 		let w = SourceWriter.new_source_writer () in
@@ -1137,7 +1164,7 @@ let generate_modules_t gen extension source_dir change_path (module_gen : Source
 		let should_write = module_gen w md in
 		if should_write then begin
 			let path = change_path (t_path md) in
-			write_file gen w (source_dir ^ "/" ^ (String.concat "/" (fst path))) path extension;
+			write_file gen w (source_dir ^ "/" ^ (String.concat "/" (fst path))) path extension out_files;
 		end
 	) gen.gcon.types
 

+ 9 - 2
gencs.ml

@@ -2913,6 +2913,8 @@ let configure gen =
 	CSharpSpecificSynf.configure gen (CSharpSpecificSynf.traverse gen runtime_cl);
 	CSharpSpecificESynf.configure gen (CSharpSpecificESynf.traverse gen runtime_cl);
 
+	let out_files = ref [] in
+
 	(* copy resource files *)
 	if Hashtbl.length gen.gcon.resources > 0 then begin
 		let src =
@@ -2927,7 +2929,9 @@ let configure gen =
 
 			let f = open_out full_path in
 			output_string f v;
-			close_out f
+			close_out f;
+
+			out_files := (unique_full_path full_path) :: !out_files
 		) gen.gcon.resources;
 	end;
 	(* add resources array *)
@@ -2984,7 +2988,10 @@ let configure gen =
 
 	let parts = Str.split_delim (Str.regexp "[\\/]+") gen.gcon.file in
 	mkdir_recursive "" parts;
-	generate_modules gen "cs" "src" module_gen;
+	generate_modules gen "cs" "src" module_gen out_files;
+
+	if not (Common.defined gen.gcon Define.KeepOldOutput) then
+		clean_files (gen.gcon.file ^ "/src") !out_files gen.gcon.verbose;
 
 	dump_descriptor gen ("hxcs_build.txt") path_s module_s;
 	if ( not (Common.defined gen.gcon Define.NoCompilation || Common.defined gen.gcon Define.UnityStdTarget) ) then begin

+ 9 - 2
genjava.ml

@@ -2283,6 +2283,8 @@ let configure gen =
 	mkdir gen.gcon.file;
 	mkdir (gen.gcon.file ^ "/src");
 
+	let out_files = ref [] in
+
 	(* add resources array *)
 	let res = ref [] in
 	Hashtbl.iter (fun name v ->
@@ -2293,7 +2295,9 @@ let configure gen =
 
 		let f = open_out_bin full_path in
 		output_string f v;
-		close_out f
+		close_out f;
+
+		out_files := (unique_full_path full_path) :: !out_files
 	) gen.gcon.resources;
 	(try
 		let c = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
@@ -2309,7 +2313,10 @@ let configure gen =
 
 	let parts = Str.split_delim (Str.regexp "[\\/]+") gen.gcon.file in
 	mkdir_recursive "" parts;
-	generate_modules_t gen "java" "src" change_path module_gen;
+	generate_modules_t gen "java" "src" change_path module_gen out_files;
+
+	if not (Common.defined gen.gcon Define.KeepOldOutput) then
+		clean_files (gen.gcon.file ^ "/src") !out_files gen.gcon.verbose;
 
 	let path_s_desc path = path_s path [] in
 	dump_descriptor gen ("hxjava_build.txt") path_s_desc (fun md -> path_s_desc (t_infos md).mt_path);