1 /*
2  * Copyright (c) 2017-2019 sel-project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  *
22  */
23 module starter;
24 
25 import std.algorithm : canFind, filter;
26 import std.array : array;
27 import std.conv : to;
28 import std.json : JSONValue, parseJSON;
29 import std.stdio : write, writeln;
30 
31 import selery.about : Software;
32 import selery.config : Config;
33 
34 import config;
35 
36 void start(ConfigType type, ref string[] args, void delegate(Config) startFunction) {
37 
38 	if(args.canFind("--about") || args.canFind("-a")) {
39 
40 		import std.system : os, endian;
41 		
42 		import pluginloader : info;
43 		
44 		JSONValue[string] json;
45 		json["type"] = cast(string)type;
46 		json["portable"] = portable;
47 		json["software"] = Software.toJSON();
48 		json["system"] = ["os": JSONValue(os.to!string), "endian": JSONValue(endian.to!string), "bits": JSONValue(size_t.sizeof*8)];
49 		json["build"] = ["d": ["date": JSONValue(__DATE__), "time": JSONValue(__TIME__), "timestamp": JSONValue(__TIMESTAMP__), "vendor": JSONValue(__VENDOR__), "version": JSONValue(__VERSION__)]];
50 		if(type == ConfigType.default_) json["plugins"] = parseJSON(info);
51 		else json["plugins"] = parseJSON(info).array.filter!(a => a["target"].str == type).array;
52 		static if(__traits(compiles, import("build_git.json"))) json["build"]["git"] = parseJSON(import("build_git.json"));
53 		static if(__traits(compiles, import("build_ci.json"))) json["build"]["ci"] = parseJSON(import("build_ci.json"));
54 		debug json["build"]["debug"] = true;
55 		else json["build"]["debug"] = false;
56 		version(X86) json["system"]["arch"] = "x86";
57 		else version(X86_64) json["system"]["arch"] = "x86-64";
58 		else version(ARM) json["system"]["arch"] = "arm";
59 		else version(AArch64) json["system"]["arch"] = "aarch64";
60 		else json["system"]["arch"] = "unknown";
61 		if(args.canFind("--min")) write(JSONValue(json).toString());
62 		else writeln(JSONValue(json).toPrettyString());
63 
64 	} else if(args.canFind("--changelog") || args.canFind("-c")) {
65 		
66 		static if(__traits(compiles, import("notes.txt")) && __traits(compiles, import("version.txt")) && Software.displayVersion == import("version.txt")) {
67 			import std..string : strip, split, endsWith;
68 			writeln("Release notes for ", Software.name, " ", Software.displayVersion, ":\n");
69 			foreach(note ; import("notes.txt").split("\\n\\n")) {
70 				string[] lines;
71 				foreach(line ; note.split("\\n")) {
72 					if(lines.length && !lines[$-1].endsWith(".")) lines[$-1] ~= " " ~ line.strip;
73 					else lines ~= line.strip;
74 				}
75 				foreach(line ; lines) {
76 					writeln(line);
77 				}
78 				writeln();
79 			}
80 		} else {
81 			writeln("Release notes were not included in this build.");
82 		}
83 		
84 	} else {
85 	
86 		Config config = loadConfig(type, args);
87 
88 		if(!args.canFind("--init") && !args.canFind("-i")) startFunction(config);
89 		
90 	}
91 
92 }