protected override void Process(ImportArgs args)
    {
      var pathToConnectionStringsConfig = args.rootPath.PathCombine("Website").PathCombine("App_Config").PathCombine("ConnectionStrings.config");
      var connectionStringsDocument = new XmlDocumentEx();
      connectionStringsDocument.Load(pathToConnectionStringsConfig);
      var connectionsStringsElement = new XmlElementEx(connectionStringsDocument.DocumentElement, connectionStringsDocument);
      ConnectionStringCollection connStringCollection = this.GetConnectionStringCollection(connectionsStringsElement);

      foreach (var conn in connStringCollection)
      {
        if (conn.IsSqlConnectionString)
        {
          var builder = new SqlConnectionStringBuilder(conn.Value)
          {
            IntegratedSecurity = false, 
            DataSource = args.connectionString.DataSource, 
            UserID = args.connectionString.UserID, 
            Password = args.connectionString.Password
          };

          if (args.databaseNameAppend != -1)
          {
            builder.InitialCatalog = builder.InitialCatalog + "_" + args.databaseNameAppend.ToString();
          }
          else
          {
            builder.InitialCatalog = builder.InitialCatalog;
          }

          conn.Value = builder.ToString();
        }
      }

      connStringCollection.Save();
    }
        private static XmlElementEx GetConnectionStringsElement(XmlDocumentEx webConfig)
        {
            var webRootPath = Path.GetDirectoryName(webConfig.FilePath);
              XmlElement configurationNode = webConfig.SelectSingleNode(WebConfig.ConfigurationXPath) as XmlElement;
              Assert.IsNotNull(configurationNode,
            "The {0} element is missing in the {1} file".FormatWith("/configuration", webConfig.FilePath));
              XmlElement webConfigConnectionStrings = configurationNode.SelectSingleNode("connectionStrings") as XmlElement;
              Assert.IsNotNull(webConfigConnectionStrings,
            "The web.config file doesn't contain the /configuration/connectionStrings node");
              XmlAttribute configSourceAttribute = webConfigConnectionStrings.Attributes[WebConfig.ConfigSourceAttributeName];
              if (configSourceAttribute != null)
              {
            string configSourceValue = configSourceAttribute.Value;
            if (!string.IsNullOrEmpty(configSourceValue) && !string.IsNullOrEmpty(webRootPath))
            {
              string filePath = Path.Combine(webRootPath, configSourceValue);
              if (FileSystem.FileSystem.Local.File.Exists(filePath))
              {
            XmlDocumentEx connectionStringsConfig = XmlDocumentEx.LoadFile(filePath);
            XmlElement connectionStrings = connectionStringsConfig.SelectSingleNode("/connectionStrings") as XmlElement;
            if (connectionStrings != null)
            {
              return new XmlElementEx(connectionStrings, connectionStringsConfig);
            }
              }
            }
              }

              return new XmlElementEx(webConfigConnectionStrings, webConfig);
        }
 private Plugin(string pluginFilePath)
 {
     this.PluginFilePath = pluginFilePath;
       this.PluginFolder = Path.GetDirectoryName(pluginFilePath);
       this.AssemblyFilePaths = FileSystem.FileSystem.Local.File.GetNeighbourFiles(pluginFilePath, "*.dll");
       this.PluginXmlDocument = XmlDocumentEx.LoadFile(pluginFilePath);
 }
        protected override void Process(ImportArgs args)
        {
            var pathToConnectionStringsConfig = args.rootPath.PathCombine("Website").PathCombine("App_Config").PathCombine("ConnectionStrings.config");
              var connectionStringsDocument = new XmlDocumentEx();
              connectionStringsDocument.Load(pathToConnectionStringsConfig);
              var connectionsStringsElement = new XmlElementEx(connectionStringsDocument.DocumentElement, connectionStringsDocument);
              ConnectionStringCollection connStringCollection = this.GetConnectionStringCollection(connectionsStringsElement);

              foreach (var conn in connStringCollection)
              {
            if (conn.IsSqlConnectionString)
            {
              var connectionStringBuilder = new SqlConnectionStringBuilder(conn.Value)
              {
            IntegratedSecurity = false,
            DataSource = args.connectionString.DataSource,
            UserID = args.connectionString.UserID,
            Password = args.connectionString.Password
              };

              connectionStringBuilder.InitialCatalog =
              DatabaseNameHelper.GetDatabaseNameFromSiteAndName(
                  args.siteName,
                  DatabaseNameHelper.GetDatabaseNameFromFiles(connectionStringBuilder.InitialCatalog),
                  false);
              conn.Value = connectionStringBuilder.ToString();
            }
            else if (conn.IsMongoConnectionString)
            {
              conn.Value = DatabaseNameHelper.ProcessMongoConnectionString(args.siteName, conn.Value);
            }
              }

              connStringCollection.Save();
        }
        public static XmlElement GetPipelines(XmlDocumentEx document)
        {
            Assert.ArgumentNotNull(document, "document");

              XmlElement pipelinesNode = document.SelectSingleNode("configuration/pipelines") as XmlElement;
              Assert.IsNotNull(pipelinesNode, "Can't find pipelines configuration node");

              return pipelinesNode;
        }
 public ImportWizardArgs(string pathToExportedInstance)
 {
   this.pathToExportedInstance = pathToExportedInstance;
   this.connectionString = ProfileManager.GetConnectionString();
   this.temporaryPathToUnpack = FileSystem.FileSystem.Local.Directory.RegisterTempFolder(FileSystem.FileSystem.Local.Directory.Ensure(Path.GetTempFileName() + "dir"));
   string websiteSettingsFilePath = FileSystem.FileSystem.Local.Zip.ZipUnpackFile(pathToExportedInstance, this.temporaryPathToUnpack, ImportArgs.websiteSettingsFileName);
   XmlDocumentEx websiteSettings = new XmlDocumentEx();
   websiteSettings.Load(websiteSettingsFilePath);
   this.siteName = websiteSettings.GetElementAttributeValue("/appcmd/SITE/site", "name");
   this.virtualDirectoryPhysicalPath = websiteSettings.GetElementAttributeValue("/appcmd/SITE/site/application/virtualDirectory", "physicalPath");
   this.rootPath = FileSystem.FileSystem.Local.Directory.GetParent(this.virtualDirectoryPhysicalPath).FullName;
   this.bindings = this.GetBindings(websiteSettingsFilePath);
 }
    private List<BindingsItem> GetBindings(string websiteSettingsFilePath)
    {
      // Dict {host}/{port}
      List<BindingsItem> result = new List<BindingsItem>();
      XmlDocumentEx websiteSettings = new XmlDocumentEx();
      websiteSettings.Load(websiteSettingsFilePath);
      XmlElement bindingsElement = websiteSettings.SelectSingleElement("/appcmd/SITE/site/bindings");
      if (bindingsElement != null)
      {
        foreach (XmlElement bindingElement in bindingsElement.ChildNodes)
        {
          result.Add(new BindingsItem(bindingElement.Attributes["bindingInformation"].Value.Split(':')[2], int.Parse(bindingElement.Attributes["bindingInformation"].Value.Split(':')[1])));
        }

        return result;
      }

      return null;
    }
    private void ChangeWebsiteSettingsIfNeeded(string path, ImportArgs args)
    {
      XmlDocumentEx websiteSettings = new XmlDocumentEx();
      websiteSettings.Load(path);
      args.siteName = this.CreateNewSiteName(InstanceManager.Instances, args.siteName);
      websiteSettings.SetElementAttributeValue("/appcmd/SITE", "SITE.NAME", args.siteName);
      websiteSettings.SetElementAttributeValue("/appcmd/SITE/site", "name", args.siteName);

      websiteSettings.SetElementAttributeValue("/appcmd/SITE", "bindings", "http/*:80:" + args.siteName);

      // need to change site ID
      args.siteID = this.CreateNewID(args.siteID);
      websiteSettings.SetElementAttributeValue("/appcmd/SITE", "SITE.ID", args.siteID.ToString());
      websiteSettings.SetElementAttributeValue("/appcmd/SITE/site", "id", args.siteID.ToString());

      // change apppool name
      websiteSettings.SetElementAttributeValue("/appcmd/SITE/site/application", "applicationPool", args.appPoolName);
      websiteSettings.SetElementAttributeValue("/appcmd/SITE/site/applicationDefaults", "applicationPool", args.appPoolName);

      // change root folder
      websiteSettings.SetElementAttributeValue("/appcmd/SITE/site/application/virtualDirectory", "physicalPath", args.rootPath + "\\Website");

      // TODO: need to change bindings in right way(maybe with the UI dialog)
      // websiteSettings.SetElementAttributeValue("/appcmd/SITE/site/bindings/binding[@bindingInformation='*:80:" + args.oldSiteName + "']", "bindingInformation", "*:80:" + args.siteName);
      XmlElement bindingsElement = websiteSettings.SelectSingleElement("/appcmd/SITE/site/bindings");
      if (bindingsElement != null)
      {
        bindingsElement.InnerXml = string.Empty;

        // it's a f*****g HACK, I can't work with xml nodes
        foreach (var key in args.bindings.Keys)
        {
          bindingsElement.InnerXml += "<binding protocol=\"http\" bindingInformation=\"*:{1}:{0}\" />".FormatWith(key, args.bindings[key].ToString());
        }

        // foreach (XmlElement bindingElement in bindingsElement.ChildNodes)
        // {

        // //if (bindingElement.Attributes["bindingInformation"].Value.Split(':').Last() != null && bindingElement.Attributes["bindingInformation"].Value.Split(':').Last() != "")
        // //    args.siteBindingsHostnames.Add(bindingElement.Attributes["bindingInformation"].Value.Split(':').Last());
        // //if(bindingElement.Attributes["bindingInformation"].Value.Split(':').Last() != null && bindingElement.Attributes["bindingInformation"].Value.Split(':').Last() != "")
        // //bindingElement.Attributes["bindingInformation"].Value = bindingElement.Attributes["bindingInformation"].Value.Replace(bindingElement.Attributes["bindingInformation"].Value.Split(':').Last(), args.siteName);
        // }
      }

      websiteSettings.Save(websiteSettings.FilePath + ".fixed.xml");
    }
    private void ChangeAppPoolSettingsIfNeeded(string path, ImportArgs args)
    {
      // should be executed before ChangeWebsiteSettingsIfNeeded
      // need to change AppName
      XmlDocumentEx appPoolSettings = new XmlDocumentEx();
      appPoolSettings.Load(path);
      args.appPoolName = this.CreateNewAppPoolName(args.appPoolName);
      appPoolSettings.SetElementAttributeValue("/appcmd/APPPOOL", "APPPOOL.NAME", args.appPoolName);
      appPoolSettings.SetElementAttributeValue("appcmd/APPPOOL/add", "name", args.appPoolName);

      appPoolSettings.Save(appPoolSettings.FilePath + ".fixed.xml");
    }
        private static void WipeSqlServerCredentials(string webRootPath)
        {
            var pathToConnectionStringsConfig = webRootPath.PathCombine("App_Config").PathCombine("ConnectionStrings.config");

              var connectionStringsDocument = new XmlDocumentEx();
              connectionStringsDocument.Load(pathToConnectionStringsConfig);

              var connectionsStringsElement = new XmlElementEx(connectionStringsDocument.DocumentElement, connectionStringsDocument);

              var connectionStrings = new ConnectionStringCollection(connectionsStringsElement);
              connectionStrings.AddRange(connectionsStringsElement.Element.ChildNodes.OfType<XmlElement>().Select(element => new ConnectionString(element, connectionsStringsElement.Document)));

              foreach (var connectionString in connectionStrings)
              {
            if (!connectionString.IsSqlConnectionString)
            {
              continue;
            }

            var builder = new SqlConnectionStringBuilder(connectionString.Value)
            {
              DataSource = string.Empty,
              UserID = string.Empty,
              Password = string.Empty
            };
            connectionString.Value = builder.ToString();
              }

              connectionStrings.Save();
        }