public static void ExecuteFiles(IList <string> files, XElement configuration, IZipFileSystem zipFileSystem)
        {
            var filesElement             = configuration.Elements("Files").Single();
            var connectionStringName     = filesElement.Attribute("connectionStringName").Value;
            var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName];

            using (var conn = new SqlConnection(connectionStringSettings.ConnectionString))
            {
                conn.Open();

                using (var trans = conn.BeginTransaction())
                {
                    foreach (var path in files)
                    {
                        using (var s = zipFileSystem.GetFileStream(path))
                        {
                            using (var sr = new StreamReader(s))
                            {
                                var text = sr.ReadToEnd();

                                using (var cmd = conn.CreateCommand())
                                {
                                    cmd.Transaction = trans;
                                    cmd.CommandText = text;

                                    cmd.ExecuteNonQuery();
                                }
                            }
                        }
                    }

                    trans.Commit();
                }
            }
        }
예제 #2
0
        internal static void ValidateXslt(List <PackageFragmentValidationResult> validationResults, Func <XElement> elementProvider, string elementName, Func <XAttribute> xsltPathAttributeProvider, Func <string> xsltFilePathProvider, IZipFileSystem zipFileSystem, bool validateResultingConfigurationFile)
        {
            if (elementProvider() == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingElement")
                    .FormatWith(elementName));
                return;
            }

            string xslFilePath = xsltFilePathProvider();

            if (xslFilePath == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingAttribute")
                    .FormatWith(_xsltFilePathAttributeName), elementProvider());
                return;
            }

            if (zipFileSystem.ContainsFile(xslFilePath) == false)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.PathDoesNotExist")
                    .FormatWith(xslFilePath), xsltPathAttributeProvider());
                return;
            }

            if (!PathUtil.WritePermissionGranted(ConfigurationServices.FileConfigurationSourcePath))
            {
                validationResults.AddFatal(
                    GetResourceString("NotEnoughNtfsPermissions")
                    .FormatWith(ConfigurationServices.FileConfigurationSourcePath));
                return;
            }

            using (Stream xsltFileStream = zipFileSystem.GetFileStream(xslFilePath))
            {
                using (TextReader xsltTextReader = new C1StreamReader(xsltFileStream))
                {
                    XDocument xslt = null;

                    try
                    {
                        xslt = XDocument.Load(xsltTextReader);
                    }
                    catch (Exception ex)
                    {
                        validationResults.AddFatal(
                            GetResourceString("ConfigurationTransformationPackageFragmentInstaller.UnableToParsXslt")
                            .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                    }

                    if (xslt != null && validateResultingConfigurationFile)
                    {
                        try
                        {
                            ConfigurationServices.TransformConfiguration(xslt, true);
                        }
                        //catch (ConfigurationException ex)
                        //{
                        //    validationResults.AddFatal(
                        //        GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                        //        .FormatWith(xsltFilePathProvider(), ex.Message), xsltPathAttributeProvider());
                        //}
                        catch (Exception ex)
                        {
                            validationResults.AddFatal(
                                GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                                .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                        }
                    }
                }
            }
        }
        internal static void ValidateXslt(List<PackageFragmentValidationResult> validationResults, Func<XElement> elementProvider, string elementName, Func<XAttribute> xsltPathAttributeProvider, Func<string> xsltFilePathProvider, IZipFileSystem zipFileSystem, bool validateResultingConfigurationFile)
        {
            if (elementProvider() == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingElement")
                    .FormatWith(elementName));
                return;
            }

            string xslFilePath = xsltFilePathProvider();

            if (xslFilePath == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingAttribute")
                    .FormatWith(_xsltFilePathAttributeName), elementProvider());
                return;
            }

            if (zipFileSystem.ContainsFile(xslFilePath) == false)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.PathDoesNotExist")
                    .FormatWith(xslFilePath), xsltPathAttributeProvider());
                return;
            }

            if(!PathUtil.WritePermissionGranted(ConfigurationServices.FileConfigurationSourcePath))
            {
                validationResults.AddFatal(
                    GetResourceString("NotEnoughNtfsPermissions")
                    .FormatWith(ConfigurationServices.FileConfigurationSourcePath));
                return;
            }

            using (Stream xsltFileStream = zipFileSystem.GetFileStream(xslFilePath))
            {
                using (TextReader xsltTextReader = new C1StreamReader(xsltFileStream))
                {
                    XDocument xslt = null;

                    try
                    {
                        xslt = XDocument.Load(xsltTextReader);
                    }
                    catch (Exception ex)
                    {
                        validationResults.AddFatal(
                            GetResourceString("ConfigurationTransformationPackageFragmentInstaller.UnableToParsXslt")
                            .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                    }

                    if (xslt != null && validateResultingConfigurationFile)
                    {
                        try
                        {
                            ConfigurationServices.TransformConfiguration(xslt, true);
                        }
                        //catch (ConfigurationException ex)
                        //{
                        //    validationResults.AddFatal(
                        //        GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                        //        .FormatWith(xsltFilePathProvider(), ex.Message), xsltPathAttributeProvider());
                        //}
                        catch (Exception ex)
                        {
                            validationResults.AddFatal(
                                GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                                .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                        }
                    }
                }
            }
        }