1 /**
2 * Copyright © Yurai Web Framework 2021
3 * License: MIT (https://github.com/YuraiWeb/yurai/blob/main/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module yurai.prebuilding.prebuildcontrollers;
7 
8 void prebuildControllers()
9 {
10   import std.file : write, dirEntries, SpanMode, readText;
11   import std.algorithm : filter, startsWith, endsWith;
12   import std.array : split, replace, join;
13   import std.string : strip, format;
14 
15   string[] controllerModules = [];
16 
17   foreach (string name; dirEntries("controllers", SpanMode.depth).filter!(f => f.name.endsWith(".d")))
18   {
19     string content = readText(name);
20     auto lines = content.replace("\r", "\n").split("\n");
21 
22     bool foundModuleStatement;
23 
24     foreach (line; lines)
25     {
26       if (!line || !line.length)
27       {
28         continue;
29       }
30 
31       auto entries = line.split(";");
32 
33       foreach (ref entry; entries)
34       {
35         entry = entry.strip;
36 
37         if (entry.startsWith("module "))
38         {
39           auto moduleName = entry[7 .. $].strip;
40 
41           if (moduleName != "controllers")
42           {
43             controllerModules ~= "\"" ~ moduleName ~ "\"";
44           }
45 
46           foundModuleStatement = true;
47           break;
48         }
49       }
50 
51       if (foundModuleStatement)
52       {
53         break;
54       }
55     }
56   }
57 
58   enum finalModule = `module yurai.prebuild.controllersmap;
59 
60   enum Yurai_ControllerModules = [
61   %s
62   ];`;
63 
64   auto moduleCode = finalModule.format(controllerModules.join(",\r\n"));
65 
66   write("prebuild/controllersmap.d", moduleCode);
67 }