Esempio n. 1
0
        static void Main(string[] args)
        {
            var opts = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, opts))
            {
                return;
            }

            var dict = new Dictionary <string, string>();

            if (!File.Exists(opts.InputFile))
            {
                Console.WriteLine("Input file {0} doesn't exists", opts.InputFile);
                return;
            }

            if (!File.Exists(opts.TransformFile))
            {
                Console.WriteLine("Transform file {0} doesn't exists", opts.TransformFile);
                return;
            }

            foreach (var item in opts.Arguments)
            {
                if (item.IndexOf("=") < 1)
                {
                    Console.WriteLine("Invalid key-value pair: {0}", item);

                    Console.WriteLine(opts.GetHelp());
                    return;
                }
                else
                {
                    var index = item.IndexOf("=");
                    var key   = item.Substring(0, index);
                    var value = item.Substring(index + 1);

                    dict.Add(key, value);
                }
            }

            TransformDocument transform;

            using (var fs = new FileStream(opts.TransformFile, FileMode.Open, FileAccess.Read))
            {
                transform = TransformDocument.LoadFrom(fs);
            }

            transform.ApplyArguments(dict);

            var doc = new XmlTransformableDocument();

            doc.Load(opts.InputFile);

            transform.ApplyTo(doc);

            using (var writer = XmlWriter.Create(opts.OutputFile, new XmlWriterSettings {
                Indent = true
            }))
            {
                doc.WriteTo(writer);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var opts = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, opts))
            {
                return;
            }

            var dict = new Dictionary<string, string>();

            if (!File.Exists(opts.InputFile))
            {
                Console.WriteLine("Input file {0} doesn't exists", opts.InputFile);
                return;
            }

            if (!File.Exists(opts.TransformFile))
            {
                Console.WriteLine("Transform file {0} doesn't exists", opts.TransformFile);
                return;
            }

            foreach (var item in opts.Arguments)
            {
                if (item.IndexOf("=") < 1)
                {
                    Console.WriteLine("Invalid key-value pair: {0}", item);

                    Console.WriteLine(opts.GetHelp());
                    return;
                }
                else
                {
                    var index = item.IndexOf("=");
                    var key = item.Substring(0, index);
                    var value = item.Substring(index + 1);

                    dict.Add(key, value);
                }
            }

            TransformDocument transform;

            using (var fs = new FileStream(opts.TransformFile, FileMode.Open, FileAccess.Read))
            {
                transform = TransformDocument.LoadFrom(fs);
            }

            transform.ApplyArguments(dict);

            var doc = new XmlTransformableDocument();
            doc.Load(opts.InputFile);

            transform.ApplyTo(doc);

            using (var writer = XmlWriter.Create(opts.OutputFile, new XmlWriterSettings { Indent = true }))
            {
                doc.WriteTo(writer);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var opts = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, opts))
            {
                return;
            }

            var dict = new Dictionary<string, string>();

            if (!File.Exists(opts.InputFile))
            {
                Console.WriteLine("Input file {0} doesn't exists", opts.InputFile);
                return;
            }

            var contentsBeforeTransformation = File.ReadAllBytes(opts.InputFile);

            if (!File.Exists(opts.TransformFile))
            {
                Console.WriteLine("Transform file {0} doesn't exists", opts.TransformFile);
                return;
            }

            foreach (var item in opts.Arguments)
            {
                if (item.IndexOf("=") < 1)
                {
                    Console.WriteLine("Invalid key-value pair: {0}", item);

                    Console.WriteLine(opts.GetHelp());
                    return;
                }
                else
                {
                    var index = item.IndexOf("=");
                    var key = item.Substring(0, index);
                    var value = item.Substring(index + 1);

                    dict.Add(key, value);
                }
            }

            TransformDocument transform;

            using (var fs = new FileStream(opts.TransformFile, FileMode.Open, FileAccess.Read))
            {
                transform = TransformDocument.LoadFrom(fs);
            }

            transform.ApplyArguments(dict);

            var doc = new XmlTransformableDocument();
            doc.Load(opts.InputFile);

            transform.ApplyTo(doc);

            var outputStream = new MemoryStream();
            using (var writer = XmlWriter.Create(outputStream, new XmlWriterSettings {Indent = true}))
            {
                doc.WriteTo(writer);
            }

            var contentsAfterTransformation = outputStream.ToArray();

            if (!contentsBeforeTransformation.SequenceEqual(contentsAfterTransformation))
            {
                Console.WriteLine("Updating '{0}'", opts.OutputFile);
                outputStream.Position = 0;
                using (var fs = File.Create(opts.OutputFile))
                {
                    outputStream.CopyTo(fs);
                    fs.Close();
                }
            }
            else
            {
                Console.WriteLine("No changes made");
            }
        }