private void UpdateVersion(SolutionItem solutionItem, string regexPattern, string assemblyFile, string debugAttribute)
        {
            string text = File.ReadAllText(assemblyFile);

            try
            {
                RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
                Match        match   = Regex.Match(text, regexPattern, options);
                if (!match.Success)
                {
                    Logger.Write(string.Concat(new string[]
                    {
                        "Failed to locate attribute \"",
                        debugAttribute,
                        "\" in file \"",
                        assemblyFile,
                        "\"."
                    }), LogLevel.Error, assemblyFile, 1);
                }
                else
                {
                    Match match2 = Regex.Match(match.Groups["FullVersion"].Value, "(?<Separator>[\\s,.]+)", options);
                    if (!match2.Success)
                    {
                        Logger.Write(string.Concat(new string[]
                        {
                            "Failed to fetch version separator on attribute \"",
                            debugAttribute,
                            "\" in file \"",
                            assemblyFile,
                            "\"."
                        }), LogLevel.Error, assemblyFile, 2);
                    }
                    else
                    {
                        StringVersion stringVersion = null;
                        try
                        {
                            stringVersion = new StringVersion(Regex.Replace(match.Groups["FullVersion"].Value, "[^\\d" + match2.Groups["Separator"].Value + "]+", "0").Replace(match2.Groups["Separator"].Value, "."));
                        }
                        catch (Exception ex)
                        {
                            string text2 = string.Format("Error occured while parsing value of {0} ({1}).\n{2}", debugAttribute, match.Groups["FullVersion"].Value, ex);
                            throw new Exception(text2, ex);
                        }
                        StringVersion stringVersion2 = solutionItem.IncrementSettings.VersioningStyle.Increment(stringVersion, solutionItem.IncrementSettings.IsUniversalTime ? this._buildStartDate.ToUniversalTime() : this._buildStartDate, solutionItem.IncrementSettings.StartDate, solutionItem);
                        if (stringVersion2 != stringVersion)
                        {
                            bool flag = false;
                            if (this._connect.IsCommandLineBuild)
                            {
                                text = text.Remove(match.Groups["FullVersion"].Index, match.Groups["FullVersion"].Length);
                                text = text.Insert(match.Groups["FullVersion"].Index, stringVersion2.ToString());
                                try
                                {
                                    File.WriteAllText(assemblyFile, text);
                                    flag = true;
                                }
                                catch (Exception ex)
                                {
                                    Logger.Write(ex.Message, LogLevel.Warning);
                                    flag = false;
                                }
                            }
                            else
                            {
                                bool   flag2       = !solutionItem.DTE.ItemOperations.IsFileOpen(assemblyFile, null);
                                string replaceText = string.Empty;
                                if (!solutionItem.IncrementSettings.ReplaceNonNumerics && Regex.IsMatch(match.Groups["FullVersion"].Value, "[^\\d" + match2.Groups["Separator"].Value + "]+"))
                                {
                                    string[] array = match.Groups["FullVersion"].Value.Replace(match2.Groups["Separator"].Value, ".").Split(new char[]
                                    {
                                        '.'
                                    });
                                    if (Regex.IsMatch(array[0], "[\\d]+"))
                                    {
                                        array[0] = stringVersion2.Major;
                                    }
                                    if (Regex.IsMatch(array[1], "[\\d]+"))
                                    {
                                        array[1] = stringVersion2.Minor;
                                    }
                                    if (Regex.IsMatch(array[2], "[\\d]+"))
                                    {
                                        array[2] = stringVersion2.Build;
                                    }
                                    if (Regex.IsMatch(array[3], "[\\d]+"))
                                    {
                                        array[3] = stringVersion2.Revision;
                                    }
                                    replaceText = match.Value.Replace(match.Groups["FullVersion"].Value, string.Format("{0}.{1}.{2}.{3}", array).Replace(".", match2.Groups["Separator"].Value));
                                }
                                else
                                {
                                    replaceText = match.Value.Replace(match.Groups["FullVersion"].Value, stringVersion2.ToString(4).Replace(".", match2.Groups["Separator"].Value));
                                }
                                ProjectItem projectItem = this._connect.ApplicationObject.Solution.FindProjectItem(assemblyFile);
                                if (projectItem == null)
                                {
                                    throw new ApplicationException("Failed to find project item \"" + assemblyFile + "\".");
                                }
                                Window window = projectItem.Open("{7651A703-06E5-11D1-8EBD-00A0C90F26EA}");
                                if (window == null)
                                {
                                    throw new ApplicationException("Could not open project item.");
                                }
                                Document document = window.Document;
                                if (document == null)
                                {
                                    throw new ApplicationException("Located project item & window but no document.");
                                }
                                flag = document.ReplaceText(match.Value, replaceText, 0);
                                if (flag2)
                                {
                                    window.Close(vsSaveChanges.vsSaveChangesYes);
                                }
                                else
                                {
                                    document.Save(assemblyFile);
                                }
                            }
                            string text2 = string.Concat(new object[]
                            {
                                solutionItem.Name,
                                " ",
                                debugAttribute,
                                ": ",
                                stringVersion2
                            });
                            if (flag)
                            {
                                text2 += " [SUCCESS]";
                            }
                            else
                            {
                                text2 += " [FAILED]";
                            }
                            Logger.Write(text2, LogLevel.Info);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error occured while updating version.\n" + ex.ToString(), LogLevel.Error, assemblyFile, 1);
            }
        }
Ejemplo n.º 2
0
        private void UpdateVersion(SolutionItem solutionItem, string regexPattern, string assemblyFile, string debugAttribute)
        {
            string filecontent = File.ReadAllText(assemblyFile);

            try
            {
                RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase;

                Match m = Regex.Match(filecontent, regexPattern, options);
                if (!m.Success)
                {
                    Logger.Write("Failed to locate attribute \"" + debugAttribute + "\" in file \"" + assemblyFile + "\".", LogLevel.Error, assemblyFile, 1);
                    return;
                }

                Match sep = Regex.Match(m.Groups["FullVersion"].Value, "(?<Separator>[\\s,.]+)", options);
                if (!sep.Success)
                {
                    Logger.Write("Failed to fetch version separator on attribute \"" + debugAttribute + "\" in file \"" + assemblyFile + "\".", LogLevel.Error, assemblyFile, 2);
                    return;
                }

                StringVersion currentVersion = null;

                try
                {
                    // Make sure we don't have anything what is not a number in the version
                    currentVersion = new StringVersion(Regex.Replace(m.Groups["FullVersion"].Value, "[^\\d" + sep.Groups["Separator"].Value + "]+", "0").Replace(sep.Groups["Separator"].Value, "."));
                }
                catch (Exception ex)
                {
                    string msg = string.Format("Error occured while parsing value of {0} ({1}).\n{2}",
                                               debugAttribute, m.Groups["FullVersion"].Value, ex);

                    throw (new Exception(msg, ex));
                }

                StringVersion newVersion = solutionItem.IncrementSettings.VersioningStyle.Increment(currentVersion,
                                                                                                    solutionItem.IncrementSettings.IsUniversalTime ? _buildStartDate.ToUniversalTime() : _buildStartDate,
                                                                                                    solutionItem.IncrementSettings.StartDate,
                                                                                                    solutionItem);

                if (newVersion != currentVersion)
                {
                    bool success = false;

                    if (_connect.IsCommandLineBuild)
                    {
                        filecontent = filecontent.Remove(m.Groups["FullVersion"].Index, m.Groups["FullVersion"].Length);
                        filecontent = filecontent.Insert(m.Groups["FullVersion"].Index, newVersion.ToString());

                        try
                        {
                            File.WriteAllText(assemblyFile, filecontent);
                            success = true;
                        }
                        catch (Exception ex)
                        {
                            Logger.Write(ex.Message, LogLevel.Warning);
                            success = false;
                        }
                    }
                    else
                    {
                        bool doCloseWindow = !solutionItem.DTE.ItemOperations.IsFileOpen(assemblyFile, null);

                        string replaceWith = string.Empty;

                        if (!solutionItem.IncrementSettings.ReplaceNonNumerics && Regex.IsMatch(m.Groups["FullVersion"].Value, "[^\\d" + sep.Groups["Separator"].Value + "]+"))
                        {
                            //the version number is not only pure numbers...Thread 77828 from philjones88
                            string[] mergedVersion = m.Groups["FullVersion"].Value.Replace(sep.Groups["Separator"].Value, ".").Split('.');

                            if (Regex.IsMatch(mergedVersion[0], "[\\d]+"))
                            {
                                mergedVersion[0] = newVersion.Major;
                            }
                            if (Regex.IsMatch(mergedVersion[1], "[\\d]+"))
                            {
                                mergedVersion[1] = newVersion.Minor;
                            }
                            if (Regex.IsMatch(mergedVersion[2], "[\\d]+"))
                            {
                                mergedVersion[2] = newVersion.Build;
                            }
                            if (Regex.IsMatch(mergedVersion[3], "[\\d]+"))
                            {
                                mergedVersion[3] = newVersion.Revision;
                            }

                            replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, String.Format("{0}.{1}.{2}.{3}", mergedVersion).Replace(".", sep.Groups["Separator"].Value));
                        }
                        else
                        {
                            replaceWith = m.Value.Replace(m.Groups["FullVersion"].Value, newVersion.ToString(4).Replace(".", sep.Groups["Separator"].Value));
                        }

                        ProjectItem projectItem = _connect.ApplicationObject.Solution.FindProjectItem(assemblyFile);

                        if (projectItem == null)
                        {
                            throw (new ApplicationException("Failed to find project item \"" + assemblyFile + "\"."));
                        }

                        Window window = projectItem.Open(Constants.vsViewKindTextView);

                        if (window == null)
                        {
                            throw (new ApplicationException("Could not open project item."));
                        }

                        Document doc = window.Document;

                        if (doc == null)
                        {
                            throw (new ApplicationException("Located project item & window but no document."));
                        }

                        success = doc.ReplaceText(m.Value, replaceWith, 0);

                        if (doCloseWindow)
                        {
                            window.Close(vsSaveChanges.vsSaveChangesYes);
                        }
                        else
                        {
                            doc.Save(assemblyFile);
                        }
                    }
#if OLDSTUFF
                    filecontent = filecontent.Remove(m.Groups["FullVersion"].Index, m.Groups["FullVersion"].Length);
                    filecontent = filecontent.Insert(m.Groups["FullVersion"].Index, newVersion.ToString());

                    success = WriteFileContent(filename, filecontent);
#endif

                    string msg = solutionItem.Name + " " + debugAttribute + ": " + newVersion;
                    if (success)
                    {
                        msg += " [SUCCESS]";
                    }
                    else
                    {
                        msg += " [FAILED]";
                    }
                    Logger.Write(msg, LogLevel.Info);
                }
            }
            catch (Exception ex)
            {
                Logger.Write("Error occured while updating version.\n" + ex.ToString(), LogLevel.Error, assemblyFile, 1);
            }
        }