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 jsTreeMap() {
58     return readData("treemap.js");
59 }
60 
61 string jsD3Mini() {
62     return readData("d3.min.js");
63 }
64 
65 string tmplIndexStyle() {
66     return readData("source.css");
67 }
68 
69 string tmplIndexBody() {
70     return readData("source.html");
71 }
72 
73 string tmplDefaultCss() {
74     return readData("default.css");
75 }
76 
77 string schemataHeader() {
78     return readData("schemata_header.h");
79 }
80 
81 string schemataImpl() {
82     return readData("schemata_header.c");
83 }
84 
85 string coverageMapHdr() {
86     return readData("coverage_mmap.h");
87 }
88 
89 string coverageMapImpl() {
90     return readData("coverage_mmap.c");
91 }
92 
93 struct Dashboard {
94     Javascript jquery;
95     Javascript bootstrapJs;
96     Javascript moment;
97     Javascript chart;
98     Css bootstrapCss;
99     Css dashboardCss;
100     Html dashboardHtml;
101 }
102 
103 Dashboard dashboard() {
104     return Dashboard(Javascript(readData("jquery.min.js")), Javascript(readData("bootstrap.min.js")),
105             Javascript(readData("moment.min.js")), Javascript(readData("chart.min.js")),
106             Css(readData("bootstrap.min.css")),
107             Css(readData("dashboard.css")), Html(readData("dashboard.html")));
108 }