예제 #1
0
        private void Transform_TestRunner_ExpectSuccess(string source, string transform, string baseline, string expectedLog)
        {
            string src                      = CreateATestFile("source.config", source);
            string transformFile            = CreateATestFile("transform.config", transform);
            string baselineFile             = CreateATestFile("baseline.config", baseline);
            string destFile                 = GetTestFilePath("result.config");
            TestTransformationLogger logger = new TestTransformationLogger();

            XmlTransformableDocument x = new XmlTransformableDocument();

            x.PreserveWhitespace = true;
            x.Load(src);

            Microsoft.Web.XmlTransform.XmlTransformation xmlTransform = new Microsoft.Web.XmlTransform.XmlTransformation(transformFile, logger);

            //execute
            bool succeed = xmlTransform.Apply(x);

            x.Save(destFile);
            xmlTransform.Dispose();
            x.Dispose();
            //test
            Assert.AreEqual(true, succeed);
            CompareFiles(baselineFile, destFile);
            CompareMultiLines(ReadResource(expectedLog), logger.LogText);
        }
예제 #2
0
        public bool TransformXml()
        {
            bool flag = true;
            XmlTransformation        xmlTransformation        = null;
            XmlTransformableDocument xmlTransformableDocument = null;

            try
            {
                try
                {
                    CultureInfo currentCulture = CultureInfo.CurrentCulture;
                    string      bUILDTASKTransformXmlTransformationStart = "Transforming Source File: {0}";
                    object[]    source = new object[] { this.Source };
                    Console.WriteLine(string.Format(currentCulture, bUILDTASKTransformXmlTransformationStart, source));
                    xmlTransformableDocument = this.OpenSourceFile(this.Source);
                    CultureInfo cultureInfo = CultureInfo.CurrentCulture;
                    string      bUILDTASKTransformXmlTransformationApply = "Applying Transform File: {0}";
                    object[]    transform = new object[] { this.Transform };
                    Console.WriteLine(string.Format(cultureInfo, bUILDTASKTransformXmlTransformationApply, this.Transform));
                    xmlTransformation = this.OpenTransformFile(this.Transform);
                    flag = xmlTransformation.Apply(xmlTransformableDocument);
                    if (flag)
                    {
                        CultureInfo currentCulture1 = CultureInfo.CurrentCulture;
                        string      bUILDTASKTransformXmlTransformOutput = "Output File: {0}";
                        Console.WriteLine(string.Format(currentCulture1, bUILDTASKTransformXmlTransformOutput, this.Destination));
                        SaveTransformedFile(xmlTransformableDocument, this.Destination);
                    }
                }
                catch (XmlException xmlException1)
                {
                    XmlException xmlException = xmlException1;
                    string       localPath    = this.Source;
                    if (!string.IsNullOrEmpty(xmlException.SourceUri))
                    {
                        localPath = (new Uri(xmlException.SourceUri)).LocalPath;
                    }
                    Console.WriteLine(string.Format("{0} {1} {2} {3} {4}", localPath, xmlException.LineNumber, xmlException.LinePosition, xmlException.Message));
                    flag = false;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    flag = false;
                }
            }
            finally
            {
                Console.WriteLine(string.Format(CultureInfo.CurrentCulture, (flag ? "Transformation succeeded" : "Transformation failed"), new object[0]), new object[0]);
                if (xmlTransformation != null)
                {
                    xmlTransformation.Dispose();
                }
                if (xmlTransformableDocument != null)
                {
                    xmlTransformableDocument.Dispose();
                }
            }
            return(flag);
        }
예제 #3
0
        public bool TransformXml(string sourceFile, string transformFile, string destFile)
        {
            FileInfo source    = new FileInfo(sourceFile);
            FileInfo transform = new FileInfo(transformFile);
            FileInfo dest      = new FileInfo(destFile);

            if (!source.Exists)
            {
                throw new FileNotFoundException("source file not found", sourceFile);
            }
            if (!transform.Exists)
            {
                throw new FileNotFoundException("transform file not found", transformFile);
            }


            bool succeeded = true;

            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            try {
                document       = OpenSourceFile(source.FullName);
                transformation = OpenTransformFile(transform.FullName, null);
                succeeded      = transformation.Apply(document);
                if (succeeded)
                {
                    document.Save(dest.FullName);
                }
            }
            finally {
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }
예제 #4
0
    // C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.XmlTransform.dll
    public static bool TransformConfig(string sourcePath, string transformPath, string destinationPath, int maxName = 18, int maxDest = 28)
    {
        if (!File.Exists(sourcePath))
        {
            Console.WriteLine("Source file not found: {0}", sourcePath);
            return(false);
        }
        if (!File.Exists(transformPath))
        {
            Console.WriteLine("Transform file not found: {0}", transformPath);
            return(false);
        }
        var document = new XmlTransformableDocument();

        document.PreserveWhitespace = false;
        document.Load(sourcePath);
        var transformation = new XmlTransformation(transformPath);
        var status         = transformation.Apply(document) ? "" : "Failure: ";

        Console.Write("    {0}{1,-" + maxName + "} => {2,-" + maxDest + "}", status, Path.GetFileName(transformPath), Path.GetFileName(destinationPath));
        var ms = new MemoryStream();
        // Apply formatting.
        var xws = new XmlWriterSettings();

        xws.Indent          = true;
        xws.IndentChars     = "\t";
        xws.CheckCharacters = true;
        var xw = XmlWriter.Create(ms, xws);

        document.WriteTo(xw);
        xw.Close();
        var bytes = ms.ToArray();

        // If file is missing or different then...
        if (!File.Exists(destinationPath) || IsDifferent(destinationPath, bytes))
        {
            // Save file.
            File.WriteAllBytes(destinationPath, bytes);
        }
        document.Dispose();
        return(true);
    }
        public override bool Execute()
        {
            try
            {
                XmlTransformableDocument transformableDocument = new XmlTransformableDocument();
                transformableDocument.PreserveWhitespace = true;
                transformableDocument.Load(WebConfigPath);

                using (XmlTransformation xmlTransformation = new XmlTransformation(XdtFilePath))
                    xmlTransformation.Apply(transformableDocument);

                transformableDocument.Save(WebConfigPath);
                transformableDocument.Dispose();
            }
            catch (Exception e)
            {
                Log.LogMessage(MessageImportance.High, e.Message);
                return(false);
            }
            Log.LogMessage(MessageImportance.High, "Invalid assembly bindings removed successfuly!");
            return(true);
        }
예제 #6
0
        public void Execute(Model.ConfigurationFile file)
        {
            var fiProductionConfigurationPath = new FileInfo(file.ProductionConfigurationPath);

            //Remove old file if one exists
            if (fiProductionConfigurationPath.Exists)
            {
                fiProductionConfigurationPath.Delete();
            }

            XmlTransformableDocument doc = null;

            try
            {
                //Apply the transform and save to disk
                doc = transformConfigurationFile(file.BaseConfigurationPath, file.ProductionTransformPath);
                doc.Save(file.ProductionConfigurationPath);
            }
            finally
            {
                doc?.Dispose();
            }
        }
예제 #7
0
        public bool RunXmlTransform(bool isLoggingEnabled = true)
        {
            bool succeeded = true;
            IXmlTransformationLogger logger = null;

            if (isLoggingEnabled && !IgnoreError)
            {
                logger = new TaskTransformationLogger(Log, StackTrace);
            }

            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            try
            {
                logger?.StartSection(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformationStart, Source));
                document = OpenSourceFile(Source);

                logger?.LogMessage(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformationApply, Transform));
                transformation = OpenTransformFile(Transform, logger);

                succeeded = transformation.Apply(document);

                if (succeeded)
                {
                    logger?.LogMessage(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.BUILDTASK_TransformXml_TransformOutput, Destination));

                    SaveTransformedFile(document, Destination);
                }

                if (IgnoreError)
                {
                    return(true);
                }
            }
            catch (System.Xml.XmlException ex)
            {
                if (IgnoreError)
                {
                    return(true);
                }

                string localPath = Source;
                if (!string.IsNullOrEmpty(ex.SourceUri))
                {
                    Uri sourceUri = new Uri(ex.SourceUri);
                    localPath = sourceUri.LocalPath;
                }

                logger?.LogError(localPath, ex.LineNumber, ex.LinePosition, ex.Message);
                succeeded = false;
            }
            catch (Exception ex)
            {
                if (IgnoreError)
                {
                    return(true);
                }

                logger?.LogErrorFromException(ex);
                succeeded = false;
            }
            finally
            {
                logger?.EndSection(string.Format(System.Globalization.CultureInfo.CurrentCulture, succeeded ?
                                                 Resources.BUILDTASK_TransformXml_TransformationSucceeded :
                                                 Resources.BUILDTASK_TransformXml_TransformationFailed));
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }
예제 #8
0
        private void Transform_TestRunner_ExpectFail(string source, string transform, string expectedLog)
        {
            string src = CreateATestFile("source.config", source);
            string transformFile = CreateATestFile("transform.config", transform);
            string destFile = GetTestFilePath("result.config");
            TestTransformationLogger logger = new TestTransformationLogger();

            XmlTransformableDocument x = new XmlTransformableDocument();
            x.PreserveWhitespace = true;
            x.Load(src);

            Microsoft.Web.XmlTransform.XmlTransformation xmlTransform = new Microsoft.Web.XmlTransform.XmlTransformation(transformFile, logger);

            //execute
            bool succeed = xmlTransform.Apply(x);
            x.Save(destFile);
            xmlTransform.Dispose();
            x.Dispose();
            //test
            Assert.AreEqual(false, succeed);
            CompareMultiLines(expectedLog, logger.LogText);
        }
예제 #9
0
        public override bool Execute()
        {
            bool succeeded = true;
            XmlTransformation        transformation = null;
            XmlTransformableDocument document       = null;

            // validate that both Transform and TransformString are not passed in
            if (Transform != null && !string.IsNullOrEmpty(TransformText))
            {
                Log.LogError("Either Transform or TransformString should be passed in, not both.");
                return(false);
            }

            bool loadTransformFromFile = string.IsNullOrEmpty(TransformText);

            try {
                Log.LogMessage(MessageImportance.Low, "Transfroming source file: {0}", Source);

                document = OpenSourceFile(Source.GetMetadata("FullPath"));

                if (loadTransformFromFile)
                {
                    Log.LogMessage(MessageImportance.Low, "Applying Transform File: {0}", Transform);
                    transformation = OpenTransformFile(Transform.GetMetadata("FullPath"), null);
                }
                else
                {
                    Log.LogMessage(MessageImportance.Low, "Applying Transform from TransformText");
                    transformation = OpenTransformText(TransformText, null);
                }

                succeeded = transformation.Apply(document);

                if (succeeded)
                {
                    Log.LogMessage(MessageImportance.Low, "Output File: {0}", Destination);
                    SaveTransformedFile(document, Destination.GetMetadata("FullPath"));
                }
            }
            catch (System.Xml.XmlException ex) {
                string localPath = Source.GetMetadata("FullPath");
                if (!string.IsNullOrEmpty(ex.SourceUri))
                {
                    Uri sourceUri = new Uri(ex.SourceUri);
                    localPath = sourceUri.LocalPath;
                }

                Log.LogErrorFromException(ex);
                succeeded = false;
            }
            catch (Exception ex) {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }
            finally {
                if (transformation != null)
                {
                    transformation.Dispose();
                }
                if (document != null)
                {
                    document.Dispose();
                }
            }

            return(succeeded);
        }