/// <summary> /// Reads user-configurable parameters out of web.config. /// </summary> /// <param name="file"> Path to web.config. </param> /// <returns> Parameter set that needs to be preserved. </returns> private static WebConfigParameters GetWebConfig(string file) { Log(" Saving original web.config parameters."); WebConfigParameters wcp = new WebConfigParameters(); XmlDocument webConfig = new XmlDocument(); webConfig.Load(file); XPathNavigator cursor = webConfig.CreateNavigator(); XPathNodeIterator appSettings = cursor.Select("/configuration/appSettings/add"); foreach (XPathNavigator appSetting in appSettings) { string name = appSetting.GetAttribute("key", ""); if ("maxLineLength".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.MaxLineLength = appSetting.GetAttribute("value", ""); continue; } if ("maxLineNumberLength".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.MaxLineNumberLength = appSetting.GetAttribute("value", ""); continue; } if ("maxDescriptionLength".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.MaxDescriptionLength = appSetting.GetAttribute("value", ""); continue; } if ("maxReviewCommentLength".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.MaxReviewCommentLength = appSetting.GetAttribute("value", ""); continue; } if ("fonts".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.Fonts = appSetting.GetAttribute("value", ""); continue; } if ("spacesPerTab".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.SpacesPerTab = appSetting.GetAttribute("value", ""); continue; } if ("allowTabOverride".Equals(name, StringComparison.OrdinalIgnoreCase)) { wcp.AllowTabOverride = appSetting.GetAttribute("value", ""); continue; } if ("diffExe".Equals(name, StringComparison.OrdinalIgnoreCase)) { // diffExe is handled elsewhere continue; } wcp.OptionalValues[name] = appSetting.GetAttribute("value", ""); } return wcp; }
/// <summary> /// Sets things in the new web.config file. /// </summary> /// <param name="installParams"> Installation parameters. </param> private static void FixupWebConfig(InstallParameters installParams, WebConfigParameters wcp) { Log(" Fixing up web.config."); string file = Path.Combine(installParams.WebsitePath != null ? installParams.WebsitePath : Path.Combine(installParams.InstallTarget, "web"), "web.config"); XmlDocument webConfig = new XmlDocument(); webConfig.Load(file); XPathNavigator cursor = webConfig.CreateNavigator(); XPathNodeIterator connections = cursor.Select("/configuration/connectionStrings/add"); foreach (XPathNavigator connection in connections) { if ("DataConnectionString".Equals(connection.GetAttribute("name", ""), StringComparison.OrdinalIgnoreCase)) { connection.MoveToAttribute("connectionString", ""); connection.SetValue("Data Source=" + installParams.Database + ";Initial Catalog=CodeReview;Integrated Security=True"); } } XPathNodeIterator appSettings = cursor.Select("/configuration/appSettings/add"); foreach (XPathNavigator appSetting in appSettings) { string name = appSetting.GetAttribute("key", ""); if ("diffExe".Equals(name, StringComparison.OrdinalIgnoreCase)) { appSetting.MoveToAttribute("value", ""); appSetting.SetValue(installParams.UnixUtilsDiff != null ? installParams.UnixUtilsDiff : Path.Combine(installParams.InstallTarget, @"unxutils\usr\local\wbin\diff.exe")); continue; } if (wcp == null) continue; string targetValue = null; if ("maxLineLength".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.MaxLineLength; else if ("maxLineNumberLength".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.MaxLineNumberLength; else if ("maxDescriptionLength".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.MaxDescriptionLength; else if ("maxReviewCommentLength".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.MaxReviewCommentLength; else if ("fonts".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.Fonts; else if ("spacesPerTab".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.SpacesPerTab; else if ("allowTabOverride".Equals(name, StringComparison.OrdinalIgnoreCase)) targetValue = wcp.AllowTabOverride; if (targetValue == null) { // This particular setting was not found; it could be in // the dictionary of optional parameters. if (wcp.OptionalValues.Keys.Contains(name)) { targetValue = wcp.OptionalValues[name]; wcp.OptionalValues.Remove(name); } else { continue; } } string currentValue = appSetting.GetAttribute("value", ""); if (targetValue.Equals(currentValue)) continue; appSetting.MoveToAttribute("value", ""); appSetting.SetValue(targetValue); } if (wcp != null) { XPathNavigator appSettingsKey = cursor.SelectSingleNode("/configuration/appSettings"); foreach (string key in wcp.OptionalValues.Keys) appSettingsKey.AppendChild("<add key=\"" + key + "\" value=\"" + wcp.OptionalValues[key] + "\" />"); } webConfig.Save(file); }