private string ExpandTemplate(string regexTemplate, string replaceTemplate, string whichVersion, string contents, VersionParts incomingVersion, VersionFormat format = VersionFormat.File) { try { var regex = new Regex(string.Format(regexTemplate, whichVersion)); var result = regex.Match(contents); if (result == Match.Empty) { return(contents); } var versionTemplateInFile = ParseVersionString(result.Groups[1].Value, format); var newVersion = MergeTemplates(incomingVersion, versionTemplateInFile); SafeLog("StampAssemblies: Merging existing {0} with incoming {1} to produce {2}.", versionTemplateInFile.ToString(), incomingVersion.ToString(), newVersion); return(regex.Replace(contents, string.Format(replaceTemplate, whichVersion, newVersion))); } catch (Exception e) { Log.LogError("Could not parse the {0} attribute, which should be something like 0.7.*.* or 1.0.0.0", whichVersion); Log.LogErrorFromException(e); throw; } }
public string MergeTemplates(VersionParts incoming, VersionParts existing) { VersionParts result = new VersionParts { parts = (string[])existing.parts.Clone(), Prerelease = incoming.Prerelease ?? existing.Prerelease }; for (int i = 0; i < result.parts.Length; i++) { if (incoming.parts[i] != "*") { result.parts[i] = incoming.parts[i]; } else if (existing.parts[i] == "*") { result.parts[i] = "0"; } } return(result.ToString()); }
private string ExpandTemplate(string whichAttribute, string contents, VersionParts incomingVersion, bool allowHashAsRevision) { try { var regex = new Regex(string.Format(@"\[assembly\: {0}\(""(.+)""", whichAttribute)); var result = regex.Match(contents); if (result == Match.Empty) return contents; var versionTemplateInFile = ParseVersionString(result.Groups[1].Value, allowHashAsRevision); var newVersion = MergeTemplates(incomingVersion, versionTemplateInFile); SafeLog("StampAssemblies: Merging existing {0} with incoming {1} to produce {2}.", versionTemplateInFile.ToString(), incomingVersion.ToString(), newVersion); return regex.Replace(contents, string.Format(@"[assembly: {0}(""{1}""", whichAttribute, newVersion)); } catch (Exception e) { Log.LogError("Could not parse the {0} attribute, which should be something like 0.7.*.* or 1.0.0.0", whichAttribute); Log.LogErrorFromException(e); throw e; } }