Beispiel #1
0
        protected override BuildVersion DoExecute(ITaskContextInternal context)
        {
            string productRootDir = context.Properties.Get <string>(BuildProps.ProductRootDir, ".");
            string productId      = context.Properties.Get <string>(BuildProps.ProductId, null);

            if (productId != null)
            {
                _projectVersionFiles.Add($"{productId}.ProjectVersion.txt");
                _projectVersionFiles.Add($"{productId}.ProjectVersion.md");
            }

            _projectVersionFiles.AddRange(_defaultprojectVersionFiles);
            string projectVersionFilePath = null;

            foreach (var projectVersionFile in _projectVersionFiles)
            {
                var filePath = Path.Combine(productRootDir, projectVersionFile);
                if (File.Exists(filePath))
                {
                    projectVersionFilePath = filePath;
                    break;
                }
            }

            if (projectVersionFilePath == null)
            {
                string defaultLocations = string.Empty;
                foreach (var projectVersionFile in _projectVersionFiles)
                {
                    defaultLocations = $"{Path.Combine(productRootDir, projectVersionFile)}{Environment.NewLine}";
                }

                throw new InvalidOperationException($"Project version file is missing. Set 'ProjectVersionFileName' or use one of the default locations: {Environment.NewLine}{defaultLocations}");
            }

            string  versionQuality = null;
            Version version        = null;

            context.LogInfo($"Fetching version from file: {projectVersionFilePath}");
            using (Stream stream = File.Open(projectVersionFilePath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line;
                    bool   versionFound = false;

                    while ((line = reader.ReadLine()) != null)
                    {
                        try
                        {
                            if (_prefixToRemove != null && line.StartsWith(_prefixToRemove))
                            {
                                line = line.Substring(_prefixToRemove.Length);
                            }

                            line = line.Trim();

                            if (_allowSuffix)
                            {
                                var index = line.IndexOf(' ');
                                if (index > 0)
                                {
                                    line = line.Remove(index);
                                }
                            }

                            if (line.Contains("-"))
                            {
                                var splitedVersion = line.Split('-');
                                if (splitedVersion.Length > 2)
                                {
                                    throw new TaskExecutionException("Only one dash is allowed for version quality.", 6);
                                }

                                version        = new Version(splitedVersion[0].Trim());
                                versionQuality = splitedVersion[1].Trim();
                            }
                            else
                            {
                                version = new Version(line);
                            }

                            versionFound = true;

                            break;
                        }
                        catch (Exception)
                        {
                        }
                    }

                    if (!versionFound)
                    {
                        throw new TaskExecutionException($"Version information not found in file '{projectVersionFilePath}' File should contaion line with version e.g. '1.0.0.0'", -53);
                    }
                }
            }

            if (!_doNotSaveVersionToSession)
            {
                context.SetBuildVersion(version);
                context.SetBuildVersionQuality(versionQuality);
            }

            var buildVersion = new BuildVersion()
            {
                Version        = version,
                VersionQuality = versionQuality
            };

            DoLogInfo($"Project version fetched: {buildVersion.Version}");
            return(buildVersion);
        }