1 /**
2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.mutate.backend.resource;
11 
12 import my.resource;
13 import my.path;
14 import my.optional;
15 import my.named_type;
16 
17 @safe:
18 
19 alias Javascript = NamedType!(string, Tag!"Javascript", string.init, TagStringable);
20 alias Html = NamedType!(string, Tag!"Html", string.init, TagStringable);
21 alias Css = NamedType!(string, Tag!"Css", string.init, TagStringable);
22 
23 private string[string] cacheData;
24 
25 private string readData(string r) @safe {
26     import logger = std.experimental.logger;
27     import std.path : buildPath;
28 
29     if (auto v = r in cacheData) {
30         return *v;
31     }
32 
33     const p = buildPath("mutate", r);
34     auto rfile = resolve(dataSearch("dextool"), Path(p));
35     if (rfile.hasValue) {
36         auto txt = readResource(rfile.orElse(ResourceFile(AbsolutePath(r))));
37         cacheData[r] = txt;
38         return txt;
39     }
40 
41     logger.error("Unable to read resource ", p);
42     throw new Exception("Unable to read resource " ~ p);
43 }
44 
45 string jsTableOnClick() {
46     return readData("table_on_click.js");
47 }
48 
49 string jsIndex() {
50     return readData("index.js");
51 }
52 
53 string jsSource() {
54     return readData("source.js");
55 }
56 
57 string jsD3Mini() {
58     return readData("d3.min.js");
59 }
60 
61 string tmplIndexStyle() {
62     return readData("source.css");
63 }
64 
65 string tmplIndexBody() {
66     return readData("source.html");
67 }
68 
69 string tmplDefaultCss() {
70     return readData("default.css");
71 }
72 
73 string schemataHeader() {
74     return readData("schemata_header.h");
75 }
76 
77 string schemataImpl() {
78     return readData("schemata_header.c");
79 }
80 
81 string coverageMapHdr() {
82     return readData("coverage_mmap.h");
83 }
84 
85 string coverageMapImpl() {
86     return readData("coverage_mmap.c");
87 }
88 
89 struct Dashboard {
90     Javascript jquery;
91     Javascript bootstrapJs;
92     Javascript moment;
93     Javascript chart;
94     Css bootstrapCss;
95     Css dashboardCss;
96     Html dashboardHtml;
97 }
98 
99 Dashboard dashboard() {
100     return Dashboard(Javascript(readData("jquery.min.js")), Javascript(readData("bootstrap.min.js")),
101             Javascript(readData("moment.min.js")), Javascript(readData("chart.min.js")),
102             Css(readData("bootstrap.min.css")),
103             Css(readData("dashboard.css")), Html(readData("dashboard.html")));
104 }