public void LoadConfig_loads_config_from_IVsTextLines() { var configContents = "<config />"; const string configFilePath = "configfilepath"; var mockTextLines = new Mock <IVsTextLines>(); mockTextLines.Setup( l => l.GetLineText(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), out configContents)); var mockVsHelpers = new Mock <IVsHelpers>(); mockVsHelpers .Setup(h => h.GetDocData(It.IsAny <IServiceProvider>(), It.IsAny <string>())) .Returns(mockTextLines.Object); var mockConfigProjectItem = new Mock <ProjectItem>(); mockConfigProjectItem.Setup(i => i.get_FileNames(It.IsAny <short>())).Returns(configFilePath); var mockVsUtils = CreateMockVsUtils(LangEnum.CSharp, VisualStudioProjectSystem.WebApplication); mockVsUtils .Setup(u => u.FindFirstProjectItemWithName(It.IsAny <ProjectItems>(), It.IsAny <string>())) .Returns(mockConfigProjectItem.Object); var configFileUtils = new ConfigFileUtils( CreateMockProject(LangEnum.CSharp).Object, Mock.Of <IServiceProvider>(), null, mockVsUtils.Object, mockVsHelpers.Object); Assert.Equal(configContents, configFileUtils.LoadConfig().OuterXml); mockVsHelpers.Verify(h => h.GetDocData(It.IsAny <IServiceProvider>(), configFilePath), Times.Once()); }
public void LoadConfig_returns_null_if_config_does_not_exist() { var configFileUtils = new ConfigFileUtils( CreateMockProject(LangEnum.CSharp).Object, Mock.Of <IServiceProvider>(), vsUtils: CreateMockVsUtils(LangEnum.CSharp, VisualStudioProjectSystem.WebApplication).Object); Assert.Null(configFileUtils.LoadConfig()); }
internal static void UpdateConfigForSqlDbFileUpgrade(ConfigFileUtils configFileUtils, Project project, IVsUpgradeLogger logger) { try { var configXmlDoc = configFileUtils.LoadConfig(); if (configXmlDoc != null) // check config file exists { if (ConnectionManager.UpdateSqlDatabaseFileDataSourceInConnectionStrings(configXmlDoc)) { configFileUtils.SaveConfig(configXmlDoc); } } } catch (Exception ex) { // if there were errors above then do not try to change the files on disk - just log the message and return var errMsg = String.Format( CultureInfo.CurrentCulture, Resources.ErrorDuringSqlDatabaseFileUpgrade, configFileUtils.GetConfigPath(), ex.Message); logger.LogMessage((uint)__VSUL_ERRORLEVEL.VSUL_ERROR, project.Name, configFileUtils.GetConfigPath(), errMsg); } }
internal static void SqlCeUpgradeService_OnUpgradeProject(IVsHierarchy hierarchy, IVsUpgradeLogger logger) { if (PackageManager.Package != null && PackageManager.Package.ModelManager != null) { // since this is about retargeting EDMX files on disk, no need to process other file extensions from any converters var fileFinder = new VSFileFinder(EntityDesignArtifact.ExtensionEdmx); fileFinder.FindInProject(hierarchy); var project = VSHelpers.GetProject(hierarchy); // skip the step if it is a miscellaneous project. if (project != null && !VsUtils.IsMiscellaneousProject(project)) { IDictionary<string, object> documentMap = new Dictionary<string, object>(); foreach (var vsFileInfo in fileFinder.MatchingFiles) { try { var projectItem = VsUtils.GetProjectItem(hierarchy, vsFileInfo.ItemId); // Dev 10 bug 648969: skip the process for astoria edmx file. if (EdmUtils.IsDataServicesEdmx(projectItem.get_FileNames(1))) { continue; } // Check whether project item is a linked item var isLinkItem = VsUtils.IsLinkProjectItem(projectItem); if (!isLinkItem) { var doc = MetadataConverterDriver.SqlCeInstance.Convert(SafeLoadXmlFromPath(vsFileInfo.Path)); if (doc != null) { documentMap.Add(vsFileInfo.Path, doc); } } } catch (Exception ex) { var errMsg = String.Format( CultureInfo.CurrentCulture, Resources.ErrorDuringSqlCeUpgrade, vsFileInfo.Path, ex.Message); logger.LogMessage((uint)__VSUL_ERRORLEVEL.VSUL_ERROR, project.Name, vsFileInfo.Path, errMsg); return; } } if (documentMap.Count > 0) { VsUtils.WriteCheckoutXmlFilesInProject(documentMap); } // now update the config file as needed var configFileUtils = new ConfigFileUtils(project, PackageManager.Package); try { var configXmlDoc = configFileUtils.LoadConfig(); if (configXmlDoc != null) // check config file exists { if (ConnectionManager.UpdateSqlCeProviderInConnectionStrings(configXmlDoc)) { configFileUtils.SaveConfig(configXmlDoc); } } } catch (Exception ex) { var errMsg = String.Format( CultureInfo.CurrentCulture, Resources.ErrorDuringSqlCeUpgrade, configFileUtils.GetConfigPath(), ex.Message); logger.LogMessage((uint)__VSUL_ERRORLEVEL.VSUL_ERROR, project.Name, configFileUtils.GetConfigPath(), errMsg); } } } }
internal static Dictionary<string, string> GetExistingConnectionStrings(ConfigFileUtils configFileUtils) { var configXml = configFileUtils.LoadConfig(); var existingConnectionStrings = new Dictionary<string, string>(); if (configXml == null) { // can be null if config does not exist in which case there are no connection strings return existingConnectionStrings; } // note we return all the connection string names to support CodeFirst scenarios foreach (var addElement in configXml.SelectNodes(XpathConnectionStringsAdd).OfType<XmlElement>()) { var name = addElement.GetAttribute(XmlAttrNameName); if (!string.IsNullOrEmpty(name)) { existingConnectionStrings.Add(name, addElement.GetAttribute(XmlAttrNameConnectionString)); } } return existingConnectionStrings; }
// <summary> // Takes our local hash and destructively updates the .config file. // </summary> // <param name="project">DTE Project that owns the .config file we want to look at.</param> private void InsertConnStringsFromHash(Project project) { if (project == null) { throw new ArgumentNullException("project"); } if (project.UniqueName.Equals(Constants.vsMiscFilesProjectUniqueName, StringComparison.Ordinal)) { return; } InitializeConnectionStringsHash(); var configFileUtils = new ConfigFileUtils(project, PackageManager.Package); configFileUtils.GetOrCreateConfigFile(); var configXmlDoc = configFileUtils.LoadConfig(); Dictionary<string, ConnectionString> hash; if (!ConnStringsByProjectHash.TryGetValue(project, out hash)) { var s = String.Format(CultureInfo.CurrentCulture, Resources.ConnectionManager_GetConfigError); VsUtils.LogOutputWindowPaneMessage(project, s); return; } if (hash.Any()) { UpdateEntityConnectionStringsInConfig(configXmlDoc, hash); configFileUtils.SaveConfig(configXmlDoc); } }
// <summary> // Takes our Xml .config file and destructively updates our local hash. // </summary> // <param name="project">DTE Project that owns App.Config we want to look at.</param> internal void ExtractConnStringsIntoHash(Project project, bool createConfig) { if (VsUtils.IsMiscellaneousProject(project)) { return; } var configFileUtils = new ConfigFileUtils(project, PackageManager.Package); if (createConfig) { configFileUtils.GetOrCreateConfigFile(); } var configXmlDoc = configFileUtils.LoadConfig(); if (configXmlDoc != null) { var xmlNodeList = configXmlDoc.SelectNodes(XpathConnectionStringsAddEntity); var stringHash = new Dictionary<string, ConnectionString>(); foreach (XmlNode node in xmlNodeList) { var connStringObj = new ConnectionString(node.Attributes.GetNamedItem(XmlAttrNameConnectionString).Value); stringHash.Add(node.Attributes.GetNamedItem(XmlAttrNameName).Value, connStringObj); } // from msdn: UniqueName: This [property] returns a temporary, unique string value that you can use to // differentiate one project from another. ConnStringsByProjectHash[project] = stringHash; } }
public void LoadConfig_loads_config_from_IVsTextLines() { var configContents = "<config />"; const string configFilePath = "configfilepath"; var mockTextLines = new Mock<IVsTextLines>(); mockTextLines.Setup( l => l.GetLineText(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), out configContents)); var mockVsHelpers = new Mock<IVsHelpers>(); mockVsHelpers .Setup(h => h.GetDocData(It.IsAny<IServiceProvider>(), It.IsAny<string>())) .Returns(mockTextLines.Object); var mockConfigProjectItem = new Mock<ProjectItem>(); mockConfigProjectItem.Setup(i => i.get_FileNames(It.IsAny<short>())).Returns(configFilePath); var mockVsUtils = CreateMockVsUtils(LangEnum.CSharp, VisualStudioProjectSystem.WebApplication); mockVsUtils .Setup(u => u.FindFirstProjectItemWithName(It.IsAny<ProjectItems>(), It.IsAny<string>())) .Returns(mockConfigProjectItem.Object); var configFileUtils = new ConfigFileUtils( CreateMockProject(LangEnum.CSharp).Object, Mock.Of<IServiceProvider>(), null, mockVsUtils.Object, mockVsHelpers.Object); Assert.Equal(configContents, configFileUtils.LoadConfig().OuterXml); mockVsHelpers.Verify(h => h.GetDocData(It.IsAny<IServiceProvider>(), configFilePath), Times.Once()); }
public void LoadConfig_returns_null_if_config_does_not_exist() { var configFileUtils = new ConfigFileUtils( CreateMockProject(LangEnum.CSharp).Object, Mock.Of<IServiceProvider>(), vsUtils: CreateMockVsUtils(LangEnum.CSharp, VisualStudioProjectSystem.WebApplication).Object); Assert.Null(configFileUtils.LoadConfig()); }