2
0
sebpatu 11 жил өмнө
parent
commit
bf05f8d032
1 өөрчлөгдсөн 39 нэмэгдсэн , 22 устгасан
  1. 39 22
      std/haxe/macro/Compiler.hx

+ 39 - 22
std/haxe/macro/Compiler.hx

@@ -240,19 +240,17 @@ class Compiler {
 	}
 
 	/**
-		Mark a class (or array of classes) or package or sub class of a module with the metadata @:keep.
-		Be carefull to not call Compiler.include before Compiler.keep!
-		Keep will also include the class by default. you can disable it with include parameter set to false.
-		To keep a sub class of a module, you need to set the module path plus the sub class like this: msignal.Signal.Signal0 (msignal.Signal being the module, and Signal0 the sub type). In that case, it will include the whole module which contains the sub type.
+		Mark a (or array of) package or class or subtype of a module with the metadata @:keep.
+		Keep will also include the class. Keeping a subtype of a module will also include the whole module by default.
+		To keep a subtype of a module, you need to set the module path plus the subtype like for imports, like this: msignal.Signal.Signal0 (msignal.Signal being the module, and Signal0 the sub type).
 	**/
-	public static function keep(?path : String, ?paths : Array<String>, ?include:Bool = true, ?recursive:Bool = true)
+	public static function keep(?path : String, ?paths : Array<String>, ?recursive:Bool = true)
 	{
 		if (null == paths)
 			paths = [];
 		if (null != path)
 			paths.push(path);
-		for (path in paths)
-		{
+		for (path in paths) {
 			var found:Bool = false;
 			var moduleRoot = (path.indexOf(".") < 0)?"":path.substring(0, path.lastIndexOf("."));
 			
@@ -271,36 +269,55 @@ class Compiler {
 					for( file in sys.FileSystem.readDirectory(fullPath) ) {
 						if( StringTools.endsWith(file, ".hx") ) {
 							var module = path + "." + file.substr(0, file.length - 3);
-							keepModule(module, include);
+							keepModule(module);
 						} else if( recursive && sys.FileSystem.isDirectory(fullPath + "/" + file) )
-							keep(path + "." + file, include, true);
+							keep(path + "." + file, true);
 					}
 				} else if(isValidModule){
-					keepModule(path, include);
+					keepModule(path);
 				} else if(isValidSubType){
-					keepSubType(path, include);
+					keepSubType(path);
 				}
 			}
 			
 			if (!found)
-				throw("ERROR: file or directory not found, can't keep: "+path);
+				Context.warning("file or directory not found, can't keep: "+path, Context.currentPos());
 		}
 	}
 
-	private static function keepSubType( path : String, include:Bool ) {				
+	private static function keepSubType( path : String ) 
+	{				
 		var module = path.substring(0, path.lastIndexOf("."));
 		var subType = module.substring(0, module.lastIndexOf(".")) + "." + path.substring(path.lastIndexOf(".") + 1);
-		//trace("INFO: add keep to sub type: " + subType);
-		haxe.macro.Compiler.addMetadata("@:keep", subType);
-		if (include)
-			Context.getModule(module);
+		var types = Context.getModule(module);
+		var found:Bool = false;
+		for (type in types) {
+			switch(type) {
+				case TInst(cls, _):
+					if (cls.toString() == subType) {
+						found = true;
+						cls.get().meta.add(":keep", [], cls.get().pos);
+					}
+				default:
+					//
+			}
+		}
+		
+		if (!found)
+			Context.warning("subtype not found, can't keep: "+path, Context.currentPos());
 	}
 	
-	private static function keepModule( path : String, include:Bool ) {				
-		//trace("INFO: add keep to module: " + path);
-		haxe.macro.Compiler.addMetadata("@:keep", path);
-		if (include)
-			Context.getModule(path);
+	private static function keepModule( path : String ) 
+	{				
+		var types = Context.getModule(path);
+		for (type in types) {
+			switch(type) {
+				case TInst(cls, _):
+					cls.get().meta.add(":keep", [], cls.get().pos);
+				default:
+					//
+			}
+		}
 	}
 
 	/**