1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2018-2019 Aurora Free Open Source Software.
10 
11 This file is part of the Aurora Free Open Source Software. This
12 organization promote free and open source software that you can
13 redistribute and/or modify under the terms of the GNU Lesser General
14 Public License Version 3 as published by the Free Software Foundation or
15 (at your option) any later version approved by the Aurora Free Open Source
16 Software Organization. The license is available in the package root path
17 as 'LICENSE' file. Please review the following information to ensure the
18 GNU Lesser General Public License version 3 requirements will be met:
19 https://www.gnu.org/licenses/lgpl.html .
20 
21 Alternatively, this file may be used under the terms of the GNU General
22 Public License version 3 or later as published by the Free Software
23 Foundation. Please review the following information to ensure the GNU
24 General Public License requirements will be met:
25 http://www.gnu.org/licenses/gpl-3.0.html.
26 
27 NOTE: All products, services or anything associated to trademarks and
28 service marks used or referenced on this file are the property of their
29 respective companies/owners or its subsidiaries. Other names and brands
30 may be claimed as the property of others.
31 
32 For more info about intellectual property visit: aurorafoss.org or
33 directly send an email to: contact (at) aurorafoss.org .
34 */
35 
36 module aurorafw.core.versionmanager;
37 
38 //TODO: Documentation
39 
40 @safe
41 struct Version {
42 	uint major;
43 	uint minor;
44 	uint rev;
45 
46 	this(uint major, uint minor, uint rev = 1)
47 	{
48 		this.major = major;
49 		this.minor = minor;
50 		this.rev = rev;
51 	}
52 
53 	this(string strVer)
54 	{
55 		import std.array : split, replace;
56 		import std.conv : to;
57 
58 		auto sstrVer = strVer.split("-");
59 		assert(sstrVer.length > 0 && sstrVer.length <= 2);
60 		if(sstrVer.length == 2)
61 			this.rev = to!uint(sstrVer[1]);
62 
63 		{
64 			auto mver = sstrVer[0].split(".");
65 			assert(mver.length == 2);
66 
67 			this.major = to!uint(mver[0]);
68 			this.minor = to!uint(mver[1]);
69 		}
70 	}
71 
72 	static auto fromString(string strVer)()
73 	{
74 		enum sret = Version(strVer);
75 		return sret;
76 	}
77 }
78 
79 enum VersionFlag : byte {
80 	NONE = 0,
81 	REVIEW = 1,
82 	FORCE = 1 << 1
83 }
84 
85 pragma(inline) @property @safe string toString(Version ver, byte flag = VersionFlag.REVIEW)
86 {
87 	import std.conv : to;
88 	string ret = to!string(ver.major) ~ "." ~ to!string(ver.minor);
89 	return ((ver.rev > 1 && ((flag & VersionFlag.REVIEW) != 0)) || ((flag & VersionFlag.FORCE) != 0)) ? ret ~ "-" ~ to!string(ver.rev) : ret;
90 }
91 
92 @safe
93 @("Versioning: simple")
94 unittest {
95 	Version ver = Version(1, 0);
96 	assert(ver.major == 1);
97 	assert(ver.minor == 0);
98 	assert(ver.rev == 1); //default
99 }
100 
101 @safe
102 @("Versioning: string")
103 unittest {
104 	assert(Version(1, 1, 2).toString == "1.1-2");
105 	assert(Version(1, 1, 2).toString(VersionFlag.NONE) == "1.1");
106 	assert(Version(1, 3).toString == "1.3");
107 	assert(Version(1, 3).toString(VersionFlag.FORCE) == "1.3-1");
108 }
109 
110 @safe
111 @("Versioning: compile-time evaluation")
112 unittest {
113 	enum Version ver = Version("1.5-2");
114 	assert(ver.major == 1);
115 	assert(ver.minor == 5);
116 	assert(ver.rev == 2);
117 
118 	assert(Version.fromString!("1.5-2") == ver);
119 }