1 module app;
2 
3 import std.stdio;
4 import std.array : empty, front;
5 import std.algorithm.iteration : map;
6 
7 import dsemver.ast;
8 import dsemver.buildinterface;
9 import dsemver.compare;
10 import dsemver.git;
11 import dsemver.options;
12 import dsemver.semver;
13 
14 int main(string[] args) {
15 	getOptOptions(args);
16 
17 	string latest = getOptions().old;
18 	if(!getOptions().projectPath.empty
19 			&& getOptions().buildNextSemVer
20 			&& latest.empty
21 		)
22 	{
23 		latest = buildInterface("latest");
24 	}
25 
26 	if(!getOptions().testParse.empty) {
27 		auto tp = parse(getOptions().testParse);
28 	}
29 
30 	if(!getOptions().old.empty && !getOptions().neu.empty) {
31 		auto old = parse(getOptions().old);
32 		auto neu = parse(getOptions().neu);
33 		auto onrs = compareOldNew(old, neu);
34 		const onr = summarize(onrs);
35 
36 		auto nors = compareOldNew(neu, old);
37 		const nor = summarize(nors);
38 		writefln("%s + %s = %s", onr, nor, combine(onr, nor));
39 	}
40 
41 	string latestTagFn = getOptions().neu;
42 	SemVer latestSemVer;
43 
44 	if((getOptions().buildLastestTag || getOptions().buildNextSemVer)
45 			&& latestTagFn.empty
46 		)
47 	{
48 		const c = isClean(getOptions().projectPath);
49 		if(!c) {
50 			writefln("the git of the project '%s' has uncommited changes"
51 					~ " this is not supported"
52 					, getOptions().projectPath);
53 			return 1;
54 		}
55 		auto tags = getTags(getOptions().projectPath);
56 		if(tags.empty) {
57 			writefln("No tags that match a semver found in '%s'"
58 					, getOptions().projectPath);
59 			return 1;
60 		}
61 
62 		scope(exit) {
63 			checkoutRef(getOptions().projectPath, "master");
64 		}
65 
66 		checkoutRef(getOptions().projectPath, tags.front.gitRef);
67 		latestTagFn = buildInterface(tags.front.semver.toString());
68 		latestSemVer = tags.front.semver;
69 	}
70 
71 	if(getOptions().buildNextSemVer) {
72 		if(latest.empty) {
73 			writefln("No latest dsemver file available");
74 			return 1;
75 		}
76 
77 		if(latestTagFn.empty) {
78 			writefln("No latest git tag dsemver file available");
79 			return 1;
80 		}
81 
82 		auto old = parse(latestTagFn);
83 		auto neu = parse(latest);
84 		auto onrs = compareOldNew(old, neu);
85 
86 		if(getOptions().verbose) {
87 			writefln("%--(%s\n%)", onrs.map!(i => i.reason));
88 		}
89 
90 		const onr = summarize(onrs);
91 
92 		auto nors = compareOldNew(neu, old);
93 		const nor = summarize(nors);
94 		const com = combine(onr, nor);
95 
96 		if(getOptions().verbose) {
97 			writefln("%s + %s = %s", onr, nor, com);
98 		}
99 
100 		if(latestSemVer != SemVer.init) {
101 			latestSemVer.preRelease = [];
102 			latestSemVer.buildIdentifier = [];
103 			if(latestSemVer.major == 0) {
104 				latestSemVer = SemVer(1, 0, 0);
105 			} else if(com == ResultValue.major) {
106 				latestSemVer = SemVer(latestSemVer.major + 1, 0, 0);
107 			} else if(com == ResultValue.minor) {
108 				latestSemVer.minor++;
109 			} else if(com == ResultValue.equal) {
110 				latestSemVer.patch++;
111 			}
112 			writefln("\nNext release should have version '%s'\n", latestSemVer);
113 		}
114 	}
115 	return 0;
116 }