1 /**
2  * download groonga lib
3  *
4  * Author: dokutoku
5  * License: CC0 Universal
6  */
7 module hello;
8 
9 
10 private static import std.algorithm.searching;
11 private static import std.ascii;
12 private static import std.digest;
13 private static import std.digest.sha;
14 private static import std.file;
15 private static import std.stdio;
16 private static import std.path;
17 private static import std.net.curl;
18 private static import std.zip;
19 
20 struct download_info
21 {
22 	string uri;
23 	string sha512_hash;
24 
25 	private bool verify(string file_path) const
26 
27 		do
28 		{
29 			return std.file.exists(file_path) && std.file.isFile(file_path) && std.digest.secureEqual(this.sha512_hash, std.digest.toHexString!(std.ascii.LetterCase.lower)(std.digest.sha.sha512Of(std.file.read(file_path))).idup);
30 		}
31 
32 	private void download(string dir_path) const
33 
34 		in
35 		{
36 			assert(dir_path.length != 0);
37 			assert(std.path.isValidPath(dir_path));
38 		}
39 
40 		do
41 		{
42 			string file_path = std.path.buildNormalizedPath(dir_path, std.path.baseName(this.uri));
43 
44 			if (this.verify(file_path)) {
45 				return;
46 			}
47 
48 			std.net.curl.download(this.uri, file_path);
49 
50 			if (!this.verify(file_path)) {
51 				throw new Exception(`The hash value of the downloaded file is invalid.`);
52 			}
53 		}
54 
55 	void extract_lib(string dir_path) const
56 
57 		in
58 		{
59 			assert(std.path.extension(this.uri) == `.zip`);
60 		}
61 
62 		do
63 		{
64 			this.download(dir_path);
65 			string zip_file = std.path.baseName(this.uri);
66 			auto zip = new std.zip.ZipArchive(std.file.read(zip_file));
67 
68 			foreach (name, am; zip.directory) {
69 				if (!std.algorithm.searching.endsWith(name, `.dll`, `.exe`, `.lib`, `.pdb`)) {
70 					continue;
71 				}
72 
73 				std.file.mkdirRecurse(std.path.dirName(name));
74 				std.file.write(name, zip.expand(am));
75 			}
76 	}
77 }
78 
79 static immutable download_info windows_x86 =
80 {
81 	uri: `https://packages.groonga.org/windows/groonga/groonga-9.0.8-x64-vs2017-with-vcruntime.zip`,
82 	sha512_hash: `4f9c55a81a22cdec04cb4630c8d4497de3b1bc88b5126b00decc2f542f8882e4341efb2daa1366023458f5f301de22788086a2f564227476c345eadda9b47eca`,
83 };
84 
85 static immutable download_info windows_x86_64 =
86 {
87 	uri: `https://packages.groonga.org/windows/groonga/groonga-9.0.8-x86-vs2017-with-vcruntime.zip`,
88 	sha512_hash: `619e4236a2a4d2f70cd2db42f010880a88818fba0788b5b569a37962459872a22155a3951ebbdea4aafa2ef5b0ed5c3231dfe1090c8d711086850a1a16660828`,
89 };
90 
91 /**
92  * ?
93  */
94 void main()
95 
96 	body
97 	{
98 		windows_x86.extract_lib(`./`);
99 		windows_x86_64.extract_lib(`./`);
100 	}