1 /**
2 Copyright: Copyright (c) 2015-2016 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.pluginlib.plugininforeader;
7 
8 import std.experimental.logger;
9 import pluginlib;
10 
11 enum pluguinPackFolder = `../../pluginpacks`;
12 
13 auto filterEnabledPlugins(Plugins)(Plugins plugins, ref string[] args)
14 {
15 	import std.getopt;
16 	import std.file;
17 	import std.range;
18 	import std.algorithm;
19 	import std.string : format;
20 
21 	string packName;
22 	string packId;
23 
24 	auto helpInformation = getopt(args,
25 		std.getopt.config.passThrough,
26 	    "pack",  &packId);
27 
28 	if (packId.length == 0)
29 		packId = "default";
30 
31 	packName = format("%s/%s.txt", pluguinPackFolder, packId);
32 
33 	PluginInfo*[string] packPlugins;
34 	if (exists(packName))
35 	{
36 		string fileData = cast(string)read(packName);
37 		PluginPack* pack = readPluginPack(fileData);
38 		foreach(plug; pack.plugins)
39 			packPlugins[plug.id] = plug;
40 		infof("Found %s plugins in '%s' pack", packPlugins.length, pack.id);
41 	}
42 	else
43 		infof("Cannot load: %s", packName);
44 
45 	return plugins.filter!(p => p.id in packPlugins);
46 }
47 
48 struct PluginInfo
49 {
50 	string id;
51 	string semver;
52 	string downloadUrl;
53 	PluginInfo*[] dependencies;
54 	PluginInfo*[] dependants;
55 }
56 
57 struct PluginPack
58 {
59 	string id;
60 	string semver;
61 	string filename;
62 	PluginInfo*[] plugins;
63 }
64 
65 PluginInfo* readPluginInfo(string fileData)
66 {
67 	import std.regex : matchFirst, ctRegex;
68 
69 	auto pinfo = new PluginInfo;
70 
71 	auto idCapture = matchFirst(fileData, ctRegex!(`id\s*=\s*"(?P<id>[^"]*)"`, "s"));
72 	pinfo.id = idCapture["id"].idup;
73 
74 	auto semverCapture = matchFirst(fileData, ctRegex!(`semver\s*=\s*"(?P<semver>[^"]*)"`, "s"));
75 	pinfo.semver = semverCapture["semver"].idup;
76 
77 	return pinfo;
78 }
79 
80 PluginPack* readPluginPack(string fileData)
81 {
82 	import std.array : empty;
83 	import std.regex : matchFirst, ctRegex;
84 	import std.string : lineSplitter;
85 
86 	auto pack = new PluginPack;
87 
88 	auto input = fileData.lineSplitter;
89 
90 	if (!input.empty) {
91 		auto packInfo = matchFirst(input.front, ctRegex!(`(?P<id>[^\s]*)\s+(?P<semver>.*)`, "m"));
92 		pack.id = packInfo["id"].idup;
93 		pack.semver = packInfo["semver"].idup;
94 		input.popFront;
95 	}
96 
97 	foreach(line; input)
98 	{
99 		if (line.empty)
100 			continue;
101 
102 		auto pluginInfo = matchFirst(line, ctRegex!(`(?P<id>[^\s]*)\s+(?P<semver>.*)`, "m"));
103 		auto pinfo = new PluginInfo;
104 		pinfo.id = pluginInfo["id"].idup;
105 		pinfo.semver = pluginInfo["semver"].idup;
106 		pack.plugins ~= pinfo;
107 	}
108 
109 	return pack;
110 }