1 /*******************************************************************************
2 
3     App version and build information.
4 
5     Copyright:
6         Copyright (c) 2018 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.application.components.Version;
17 
18 import ocean.meta.types.Qualifiers;
19 import ocean.core.Array: startsWith, map;
20 import ocean.core.array.Mutation /* : moveToEnd, sort */;
21 import ocean.text.Util;
22 
23 /*******************************************************************************
24 
25     Associative array which contains version information.
26 
27     Typically this array should contain the keys:
28      * build_author
29      * build_date
30      * dmd
31      * gc
32      * lib_*
33 
34     Where lib_* are considered to be libraries used by this program.
35 
36     This is usually generated automatically, this is why this kind of *duck
37     typing* is used (to avoid a dependency between the generator and this
38     library).
39 
40 *******************************************************************************/
41 
42 public alias istring[istring] VersionInfo;
43 
44 /*******************************************************************************
45 
46     Get the program's name and basic version information as a string.
47 
48     Params:
49         app_name = program's name
50         ver = description of the application's version / revision
51 
52     Returns:
53         String with the version information
54 
55 *******************************************************************************/
56 
57 public istring getVersionString ( istring app_name, VersionInfo ver )
58 {
59     auto v = "version" in ver;
60     if (v !is null)
61         return app_name ~ " version " ~ *v;
62     else
63         return app_name ~ " unkown version";
64 }
65 
66 /*******************************************************************************
67 
68     Get the program's name and extended build information as a string.
69 
70     Params:
71         app_name = program's name
72         ver = description of the application's version / revision
73         single_line = if set to `true`, puts key-value pairs on the same
74             line
75 
76     Returns:
77         String with the version information
78 
79 *******************************************************************************/
80 
81 public istring getBuildInfoString ( istring app_name, VersionInfo ver,
82     bool single_line = false )
83 {
84     istring s = getVersionString(app_name, ver);
85 
86     istring separator;
87     if (single_line)
88         separator = ", ";
89     else
90         separator = "\n";
91 
92     if (ver.length)
93     {
94         auto sorted_names = ver.keys;
95         sorted_names.length = moveToEnd(sorted_names, "version");
96         sorted_names.sort();
97 
98         scope formatter = (istring n)
99         {
100             return n ~ "=" ~ ver[n];
101         };
102 
103         s ~= separator;
104         s ~= sorted_names
105             .map(formatter)
106             .join(separator);
107     }
108 
109     return s;
110 }
111 
112 version (unittest)
113 {
114     import ocean.core.Test;
115 }
116 
117 /*******************************************************************************
118 
119     Test the built version string
120 
121 *******************************************************************************/
122 
123 unittest
124 {
125     VersionInfo info;
126     info["version"] = "v1.0";
127     info["build_author"] = "me";
128     info["build_date"] = "today";
129     info["compiler"] = "dmd3";
130     info["lib_awesome"] = "v10.0";
131     info["lib_sucks"] = "v0.5";
132     info["extra"] = "useful";
133     info["more"] = "info";
134     test!("==")(
135         getVersionString("test", info),
136         "test version v1.0"
137     );
138     test!("==")(
139         getBuildInfoString("test", info),
140         "test version v1.0
141 build_author=me
142 build_date=today
143 compiler=dmd3
144 extra=useful
145 lib_awesome=v10.0
146 lib_sucks=v0.5
147 more=info"
148     );
149     test!("==")(
150         getBuildInfoString("test", info, true),
151         "test version v1.0, " ~
152             "build_author=me, build_date=today, compiler=dmd3, extra=useful, " ~
153             "lib_awesome=v10.0, lib_sucks=v0.5, more=info"
154     );
155 }