1 /++
2 Information about classes in a Godot-D project.
3 
4 These structs only store the info to be used by Godot-D. Use the class-finder
5 subpackage to actually obtain the info by parsing D files.
6 
7 TODO: replace temporary CSV serialization with proper JSON.
8 +/
9 module godotutil.classes;
10 
11 import std.string;
12 
13 import std.algorithm : map;
14 import std.array : join;
15 import std.range;
16 import std.conv : text;
17 import std.meta;
18 
19 /// Information about what classes were found in a D source file.
20 struct FileInfo
21 {
22 	string name; /// filename relative to the source directory
23 	string hash; /// hash of the file, to avoid re-parsing files that haven't changed
24 	string moduleName;
25 	string mainClass; /// the class with the same name as the module, if it exists
26 	bool hasEntryPoint = false; /// the GodotNativeLibrary mixin is in this file
27 	string[] classes; /// all classes in the file
28 
29 	string toCsv() const
30 	{
31 		string ret = only(name, hash, moduleName, mainClass).join(",");
32 		ret ~= ",";
33 		ret ~= hasEntryPoint.text;
34 		foreach(c; classes)
35 		{
36 			ret ~= ",";
37 			ret ~= c;
38 		}
39 		return ret;
40 	}
41 	static FileInfo fromCsv(string csv)
42 	{
43 		FileInfo ret;
44 		auto s = csv.split(',');
45 		static foreach(v; AliasSeq!("name", "hash", "moduleName", "mainClass"))
46 		{
47 			mixin("ret."~v~" = s.front;");
48 			s.popFront();
49 		}
50 		if(s.front == "true") ret.hasEntryPoint = true;
51 		s.popFront();
52 		foreach(c; s)
53 		{
54 			ret.classes ~= c;
55 		}
56 		return ret;
57 	}
58 }
59 
60 /// 
61 struct ProjectInfo
62 {
63 	FileInfo[] files;
64 
65 	/// the project has a GodotNativeLibrary mixin in one of its files
66 	bool hasEntryPoint() const
67 	{
68 		import std.algorithm.searching : any;
69 		return files.any!(f => f.hasEntryPoint);
70 	}
71 
72 	const(string)[] allClasses() const
73 	{
74 		return files.map!(f => f.classes).join();
75 	}
76 
77 	string toCsv() const
78 	{
79 		return files.map!(f => f.toCsv).join("\n");
80 	}
81 	static ProjectInfo fromCsv(string csv)
82 	{
83 		ProjectInfo ret;
84 		foreach(l; csv.splitLines)
85 		{
86 			ret.files ~= FileInfo.fromCsv(l);
87 		}
88 		return ret;
89 	}
90 }
91 
92