1 /**
2  * GnuWin32 installer
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.sys.install.gnuwin32;
15 
16 import std.file;
17 import std.path;
18 import std.string : format, split;
19 
20 import ae.utils.meta : singleton, I;
21 
22 public import ae.sys.install.common;
23 
24 /// Installs a GnuWin32 component.
25 final class GnuWin32Component : Installer
26 {
27 	/// Template for component .zip files.
28 	string urlTemplate = "http://gnuwin32.sourceforge.net/downlinks/%s-%s-zip.php";
29 
30 	/// Component to install.
31 	string componentName;
32 
33 	this(string componentName) { this.componentName = componentName; } ///
34 
35 protected:
36 	@property override string name() { return "%s (GnuWin32)".format(componentName); }
37 	@property override string subdirectory() { return "gnuwin32"; }
38 	@property override string[] binPaths() { return ["bin"]; }
39 
40 	override @property bool installedLocally()
41 	{
42 		auto manifestDir = directory.buildPath("manifest");
43 		return manifestDir.exists && !manifestDir.dirEntries(componentName ~ "-*-bin.ver", SpanMode.shallow).empty;
44 	}
45 
46 	override void atomicInstallImpl()
47 	{
48 		windowsOnly();
49 		if (!directory.exists)
50 			directory.mkdir();
51 		installUrl(urlTemplate.format(componentName, "bin"));
52 		installUrl(urlTemplate.format(componentName, "dep"));
53 		assert(installedLocally);
54 	}
55 
56 	void installUrl(string url)
57 	{
58 		url
59 			.I!saveAs(url.split("/")[$-1][0..$-8] ~ ".zip")
60 			.I!unpack()
61 			.atomicMoveInto(directory);
62 	}
63 }
64 
65 /// `opDispatch` constructor for GnuWin32 component installers.
66 struct GnuWin32
67 {
68 	/// Example: `GnuWin32.make.requireLocal(false);`.
69 	static GnuWin32Component opDispatch(string name)()
70 	{
71 		alias component = singleton!(GnuWin32Component, name);
72 		return component;
73 	}
74 }