示例#1
0
        private void OnAppConfigFileChanged(object sender, AppConfigFileChangedEventArgs appConfigFileChangedEventArgs)
        {
//#if !DEBUG
            try
            {
//#endif
                if (_building || ((ConfigWatcher)sender).IsBuilding)
                {
                    return;
                }
                var    project = appConfigFileChangedEventArgs.Project;
                string projectFullName;
                try { projectFullName = project.FullName; } catch { return; }
                if (!project.IsAvailable() || !File.Exists(projectFullName))
                {
                    return;
                }
                var fileInfo          = new FileInfo(appConfigFileChangedEventArgs.AppConfigFile);
                var projectProperties = new ProjectProperties(project);
                if (projectProperties.InlineAppCfgTransforms != true)
                {
                    return;
                }
                var baseFileFullPath = appConfigFileChangedEventArgs.AppConfigFile;
                // $(MSBuildProjectDirectory)\$(ConfigDir)\$(AppCfgType).Base.config
                var tmpBaseFileFullPath = Path.Combine(project.GetDirectory(),
                                                       projectProperties.ConfigDir, projectProperties.AppCfgType + ".Base.config");
                if (File.Exists(tmpBaseFileFullPath))
                {
                    baseFileFullPath = tmpBaseFileFullPath;
                }
                lock (appConfigFileChangedMessageLock)
                {
                    var tmpGenFilePath = fileInfo.FullName + ".tmp";
                    var modifiedGeneratedConfigFileContents = File.ReadAllText(fileInfo.FullName);
                    if (!string.IsNullOrEmpty(baseFileFullPath) &&
                        baseFileFullPath != appConfigFileChangedEventArgs.AppConfigFile &&
                        DateTime.Now - _lastModifiedNotification > TimeSpan.FromSeconds(15) &&
                        Regex.Replace(modifiedGeneratedConfigFileContents, @"\s", "") != Regex.Replace(File.ReadAllText(baseFileFullPath), @"\s", ""))
                    {
                        // 1. re-run TransformXml
                        var xfrmFile = Path.Combine(project.GetDirectory(), projectProperties.ConfigDir,
                                                    projectProperties.AppCfgType + "."
                                                    + project.ConfigurationManager.ActiveConfiguration.ConfigurationName + ".config");
                        if (!File.Exists(xfrmFile))
                        {
                            File.Copy(baseFileFullPath, tmpGenFilePath, true);
                        }
                        else
                        {
                            var xfrm = new MSBuildXmlTransformer
                            {
                                Source      = baseFileFullPath,
                                Transform   = xfrmFile,
                                Destination = tmpGenFilePath
                            };
                            xfrm.Execute();
                        }
                        // 2. load transformxml output as string
                        var generatedConfigFileContents = File.ReadAllText(tmpGenFilePath);
                        // 3. delete tmp
                        File.Delete(tmpGenFilePath);
                        // 4. compare
                        if (generatedConfigFileContents == modifiedGeneratedConfigFileContents)
                        {
                            return;
                        }
                        var baseFileRelativePath = FileUtilities.GetRelativePath(
                            Directory.GetParent(project.GetDirectory()).FullName, baseFileFullPath, trimDotSlash: true);
                        MessageBox.Show(GetNativeWindow(),
                                        "The " + fileInfo.Name + " file has been modified, but "
                                        + "this is a generated file. You will need to immediately identify the changes that were made and "
                                        + "propagate them over to " + baseFileRelativePath, fileInfo.Name, MessageBoxButtons.OK,
                                        MessageBoxIcon.Exclamation);
                        _lastModifiedNotification = DateTime.Now;
                        Dte.ExecuteCommand("Tools.DiffFiles", "\"" + fileInfo.FullName + "\" \"" + baseFileFullPath + "\"");
                        _building = false;
                    }
                }
//#if !DEBUG
            }
            catch (Exception e)
            {
                Dte.GetLogger().LogError(e.GetType().Name + " - " + e.ToString());
            }
//#endif
        }
示例#2
0
        private void OnAppConfigFileChanged(object sender, AppConfigFileChangedEventArgs appConfigFileChangedEventArgs)
        {
//#if !DEBUG
            try
            {
//#endif
                if (_building || ((ConfigWatcher)sender).IsBuilding) return;
                var project = appConfigFileChangedEventArgs.Project;
                string projectFullName;
                try { projectFullName = project.FullName; } catch { return; }
                if (!project.IsAvailable() || !File.Exists(projectFullName)) return;
                var fileInfo = new FileInfo(appConfigFileChangedEventArgs.AppConfigFile);
                var projectProperties = new ProjectProperties(project);
                if (projectProperties.InlineAppCfgTransforms != true)
                    return;
                var baseFileFullPath = appConfigFileChangedEventArgs.AppConfigFile;
                // $(MSBuildProjectDirectory)\$(ConfigDir)\$(AppCfgType).Base.config
                var tmpBaseFileFullPath = Path.Combine(project.GetDirectory(),
                    projectProperties.ConfigDir, projectProperties.AppCfgType + ".Base.config");
                if (File.Exists(tmpBaseFileFullPath)) baseFileFullPath = tmpBaseFileFullPath;
                lock (appConfigFileChangedMessageLock)
                {
                    var tmpGenFilePath = fileInfo.FullName + ".tmp";
                    var modifiedGeneratedConfigFileContents = File.ReadAllText(fileInfo.FullName);
                    if (!string.IsNullOrEmpty(baseFileFullPath) &&
                        baseFileFullPath != appConfigFileChangedEventArgs.AppConfigFile
                        && DateTime.Now - _lastModifiedNotification > TimeSpan.FromSeconds(15)
                        && Regex.Replace(modifiedGeneratedConfigFileContents, @"\s", "") != Regex.Replace(File.ReadAllText(baseFileFullPath), @"\s", ""))
                    {
                        // 1. re-run TransformXml
                        var xfrmFile = Path.Combine(project.GetDirectory(), projectProperties.ConfigDir,
                            projectProperties.AppCfgType + "."
                            + project.ConfigurationManager.ActiveConfiguration.ConfigurationName + ".config");
                        if (!File.Exists(xfrmFile))
                        {
                            File.Copy(baseFileFullPath, tmpGenFilePath, true);
                        }
                        else
                        {
                            var xfrm = new MSBuildXmlTransformer
                            {
                                Source = baseFileFullPath,
                                Transform = xfrmFile,
                                Destination = tmpGenFilePath
                            };
                            xfrm.Execute();
                        }
                        // 2. load transformxml output as string
                        var generatedConfigFileContents = File.ReadAllText(tmpGenFilePath);
                        // 3. delete tmp
                        File.Delete(tmpGenFilePath);
                        // 4. compare
                        if (generatedConfigFileContents == modifiedGeneratedConfigFileContents) return;
                        var baseFileRelativePath = FileUtilities.GetRelativePath(
                            Directory.GetParent(project.GetDirectory()).FullName, baseFileFullPath, trimDotSlash: true);
                        MessageBox.Show(GetNativeWindow(),
                            "The " + fileInfo.Name + " file has been modified, but "
                            + "this is a generated file. You will need to immediately identify the changes that were made and "
                            + "propagate them over to " + baseFileRelativePath, fileInfo.Name, MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
                        _lastModifiedNotification = DateTime.Now;
                        Dte.ExecuteCommand("Tools.DiffFiles", "\"" + fileInfo.FullName + "\" \"" + baseFileFullPath + "\"");
                        _building = false;
                    }
                }
//#if !DEBUG
            }
            catch (Exception e)
            {
                Dte.GetLogger().LogError(e.GetType().Name + " - " + e.ToString());
            }
//#endif
        }