|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System.Xml;
-
- public class BuildParameters
- {
- public BuildParameters(ICakeContext context)
- {
- Context = context;
- }
-
- public ICakeContext Context { get; }
- public BuildVersion Version { get; private set; }
- public string Configuration { get; private set; }
- public bool IsTagged { get; private set; }
- public bool IsCI { get; private set; }
- public DirectoryPathCollection Projects { get; set; }
- public DirectoryPathCollection TestProjects { get; set; }
- public FilePathCollection ProjectFiles { get; set; }
- public FilePathCollection TestProjectFiles { get; set; }
-
- public static BuildParameters Create(ICakeContext context)
- {
- var buildParameters = new BuildParameters(context);
- buildParameters.Initialize();
- return buildParameters;
- }
-
- public string FullVersion()
- {
- return Version.VersionWithSuffix();
- }
-
- private void Initialize()
- {
- InitializeCore();
- InitializeVersion();
- }
-
- private void InitializeCore()
- {
- Projects = Context.GetDirectories("./src/*");
- TestProjects = Context.GetDirectories("./test/*");
- ProjectFiles = Context.GetFiles("./src/*/*.csproj");
- TestProjectFiles = Context.GetFiles("./test/*/*.csproj");
-
- var buildSystem = Context.BuildSystem();
- if (!buildSystem.IsLocalBuild)
- {
- IsCI = true;
- if ((buildSystem.IsRunningOnAppVeyor && buildSystem.AppVeyor.Environment.Repository.Tag.IsTag) ||
- (buildSystem.IsRunningOnTravisCI && string.IsNullOrWhiteSpace(buildSystem.TravisCI.Environment.Build.Tag)))
- {
- IsTagged = true;
- }
- }
-
- Configuration = Context.Argument("Configuration", "Debug");
- if (IsCI)
- {
- Configuration = "Release";
- }
- }
-
- private void InitializeVersion()
- {
- var versionFile = Context.File("./build/version.props");
- var content = System.IO.File.ReadAllText(versionFile.Path.FullPath);
-
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(content);
-
- var versionMajor = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionMajor").InnerText;
- var versionMinor = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionMinor").InnerText;
- var versionPatch = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionPatch").InnerText;
- var versionQuality = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionQuality").InnerText;
- versionQuality = string.IsNullOrWhiteSpace(versionQuality) ? null : versionQuality;
-
- var suffix = versionQuality;
- if (!IsTagged)
- {
- suffix += (IsCI ? "preview-" : "dv-") + Util.CreateStamp();
- }
- suffix = string.IsNullOrWhiteSpace(suffix) ? null : suffix;
-
- Version =
- new BuildVersion(int.Parse(versionMajor), int.Parse(versionMinor), int.Parse(versionPatch), versionQuality);
- Version.Suffix = suffix;
- }
- }
-
- public class BuildVersion
- {
- public BuildVersion(int major, int minor, int patch, string quality)
- {
- Major = major;
- Minor = minor;
- Patch = patch;
- Quality = quality;
- }
-
- public int Major { get; set; }
- public int Minor { get; set; }
- public int Patch { get; set; }
- public string Quality { get; set; }
- public string Suffix { get; set; }
-
- public string VersionWithoutQuality()
- {
- return $"{Major}.{Minor}.{Patch}";
- }
-
- public string Version()
- {
- return VersionWithoutQuality() + (Quality == null ? string.Empty : $"-{Quality}");
- }
-
- public string VersionWithSuffix()
- {
- return Version() + (Suffix == null ? string.Empty : $"-{Suffix}");
- }
- }
|