This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionUpdater.cs
94 lines (85 loc) · 2.79 KB
/
VersionUpdater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
namespace UpVer
{
public class VersionUpdater
{
private readonly Settings _settings;
private readonly string _filepath;
private readonly XDocument _config;
public VersionUpdater(Settings settings)
{
_settings = settings;
_filepath = settings.File ?? ConfigurationManager.AppSettings["File"];
_config = XDocument.Load(_filepath);
}
public Changes Process()
{
var changes = new Changes();
if (_settings.Major)
{
changes.Major = Inc("major");
changes.Minor = Set("minor", 0);
changes.Patch = Set("patch", 0);
} else if (_settings.Minor)
{
changes.Major = Changes.NoChange(Get("major"));
changes.Minor = Inc("minor");
changes.Patch = Set("patch", 0);
} else if (_settings.Patch)
{
changes.Major = Changes.NoChange(Get("major"));
changes.Minor = Changes.NoChange(Get("minor"));
changes.Patch = Inc("patch");
}
else if (_settings.Read)
{
changes.Major = Changes.NoChange(Get("major"));
changes.Minor = Changes.NoChange(Get("minor"));
changes.Patch = Changes.NoChange(Get("patch"));
}
_config.Save(_filepath);
return changes;
}
private Change Inc(string part)
{
var val = Get(part);
var change = new Change
{
From = val,
To = ++val
};
Set(part, change.To);
return change;
}
private int Get(string part)
{
var elem = GetProperty("version." + part);
return Get(elem.Attribute("value"));
}
private int Get(XAttribute attr)
{
return int.Parse(attr.Value);
}
private Change Set(string part, int val)
{
var elem = GetProperty("version." + part);
return Set(elem, val);
}
private Change Set(XElement elem, int val)
{
var attr = elem.Attribute("value");
var change = new Change { From = Get(attr), To = val };
attr.SetValue(val);
return change;
}
private IEnumerable<XElement> _properties = null;
private XElement GetProperty(string name)
{
if (_properties == null) _properties = _config.Element("project").Elements("property");
return _properties.Single(x => x.Attribute("name").Value == name);
}
}
}