1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.extensions.extensionemitter;
7 
8 import diamond.extensions.extensiontype;
9 
10 /// Mixin template to emit extensions.
11 mixin template ExtensionEmit(ExtensionType extensionType, string extensionHandler)
12 {
13   /// The function that generates the emit result.
14   private static string emitGenerate()
15   {
16     import diamond.extensions.extensionmanager;
17 
18     string emitResult = "";
19 
20     foreach (emitEntry; getExtensions(extensionType))
21     {
22       import std..string : format;
23       import std.array : replace;
24 
25       static if (
26         extensionType == ExtensionType.applicationStart ||
27         extensionType == ExtensionType.customGrammar ||
28         extensionType == ExtensionType.partParser ||
29         extensionType == ExtensionType.httpSettings ||
30         extensionType == ExtensionType.httpRequest ||
31         extensionType == ExtensionType.handleError ||
32         extensionType == ExtensionType.staticFileExtension
33       )
34       {
35         emitResult ~= q{{
36           import __extension_%s = %s;
37 
38           %s
39         }}.format(emitEntry.name, emitEntry.moduleName,
40           extensionHandler.replace("{{extensionEntry}}", "__extension_" ~ emitEntry.name));
41       }
42       else
43       {
44         emitResult ~= q{
45           import __extension_%s = %s;
46 
47           %s
48         }.format(emitEntry.name, emitEntry.moduleName,
49           extensionHandler.replace("{{extensionEntry}}", "__extension_" ~ emitEntry.name));
50       }
51     }
52 
53     return emitResult;
54   }
55 
56   static if (
57     extensionType == ExtensionType.applicationStart ||
58     extensionType == ExtensionType.customGrammar ||
59     extensionType == ExtensionType.partParser ||
60     extensionType == ExtensionType.httpSettings ||
61     extensionType == ExtensionType.httpRequest ||
62     extensionType == ExtensionType.handleError ||
63     extensionType == ExtensionType.staticFileExtension
64   )
65   {
66     /// The function that contains the generated extension call.
67     void emitExtension()
68     {
69       mixin(emitGenerate());
70     }
71   }
72   else
73   {
74     mixin(emitGenerate());
75   }
76 }