protected override Version DoExecute(ITaskContextInternal context) { _productRootDir = context.Properties.Get <string>(BuildProps.ProductRootDir, "."); if (string.IsNullOrEmpty(_productRootDir)) { _productRootDir = "."; } string projectVersionFileName; if (!string.IsNullOrEmpty(_projectVersionFileName)) { projectVersionFileName = Path.Combine(_productRootDir, _projectVersionFileName); } else { _productId = context.Properties.Get <string>(BuildProps.ProductId); projectVersionFileName = Path.Combine(_productRootDir, $"{_productId}.ProjectVersion.txt"); } if (!File.Exists(projectVersionFileName)) { throw new InvalidOperationException($"Project version file '{projectVersionFileName}' is missing."); } Version buildVersion; using (Stream stream = File.Open(projectVersionFileName, FileMode.Open)) { using (StreamReader reader = new StreamReader(stream)) { string versionAsString = reader.ReadLine(); buildVersion = new Version(versionAsString); } } if (!_doNotSaveVersionToSession) { context.SetBuildVersion(buildVersion); } DoLogInfo($"Project build version (from file): {buildVersion}"); return(buildVersion); }
protected override Version 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}"); } Version buildVersion = null; 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); } } buildVersion = 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(buildVersion); } DoLogInfo($"Project build version (from file): {buildVersion}"); return(buildVersion); }