Exemplo n.º 1
0
        /// <summary>
        /// Dispatches to the right handler method for writing generated code to output file(s).
        /// </summary>
        /// <param name="generateOptions"></param>
        internal static void HandleGenerateCode(GenerateOptions generateOptions)
        {
            var settingsFromFile = generateOptions.GetConfigInstance(ProgressReporter);
            var settings         = settingsFromFile ?? XObjectsCoreGenerator.LoadLinqToXsdSettings();

            if (settingsFromFile != null)
            {
                PrintLn("Configuration file(s) loaded...".Gray());
            }

            settings.EnableServiceReference = generateOptions.EnableServiceReference;

            var textWriters = generateOptions.AutoConfig
                ? XObjectsCoreGenerator.Generate(generateOptions.SchemaFiles)
                : XObjectsCoreGenerator.Generate(generateOptions.SchemaFiles, settings);

            if (generateOptions.Output.IsEmpty())
            {
                PrintLn("No output directory given: defaulting to same directory as XSD file(s).".Gray());
                generateOptions.Output = "-1";
            }

            var hasCsExt = Path.GetExtension(generateOptions.Output).EndsWith(".cs");

            // merge the output into a single file
            if (hasCsExt)
            {
                GenerateCodeDispatcher.HandleWriteOutputToSingleFile(generateOptions, textWriters);
            }
            else
            {
                GenerateCodeDispatcher.HandleWriteOutputToMultipleFiles(generateOptions, textWriters);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Used specifically for unit testing, invokes the
        /// <see cref="XObjectsCoreGenerator.Generate(IEnumerable{string},LinqToXsdSettings)"/>
        /// method for generating C# code.
        /// </summary>
        /// <param name="xsdFileName"></param>
        /// <returns></returns>
        public static SourceText GenerateSourceText(string xsdFileName)
        {
            var possibleSettingsFile = $"{xsdFileName}.config";
            KeyValuePair <string, TextWriter> code = File.Exists(possibleSettingsFile)
                ? XObjectsCoreGenerator.Generate(xsdFileName, possibleSettingsFile)
                : XObjectsCoreGenerator.Generate(xsdFileName, default(string));

            return(SourceText.From(code.Value.ToString()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Used specifically for unit testing, invokes the
        /// <see cref="XObjectsCoreGenerator.Generate(XmlSchemaSet,LinqToXsdSettings)"/>
        /// method for generating C# code.
        /// </summary>
        /// <param name="xmlSchemaSet"></param>
        /// <param name="xsdFileName">Required for loading any configuration files. Accepts relative and absolute.</param>
        /// <returns></returns>
        public static SourceText GenerateSourceText(XmlSchemaSet xmlSchemaSet, string xsdFileName)
        {
            var           possibleSettingsFile = $"{xsdFileName}.config";
            Configuration config = File.Exists(possibleSettingsFile)
                ? Configuration.Load(possibleSettingsFile)
                : Configuration.GetBlankConfigurationInstance();
            var settings = config.ToLinqToXsdSettings();

            var code = XObjectsCoreGenerator.Generate(xmlSchemaSet, settings);

            return(SourceText.From(code.ToString()));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Dispatches to the right handler method for writing generated code to output file(s).
        /// </summary>
        /// <param name="generateOptions"></param>
        internal static void HandleGenerateCode(GenerateOptions generateOptions)
        {
            var settings = generateOptions.GetConfigInstance(ProgressReporter) ?? XObjectsCoreGenerator.LoadLinqToXsdSettings();

            if (generateOptions.GetConfigInstance() != null)
            {
                Console.WriteLine("Configuration file(s) loaded...");
            }

            settings.EnableServiceReference = generateOptions.EnableServiceReference;

            var textWriters = generateOptions.AutoConfig
                ? XObjectsCoreGenerator.Generate(generateOptions.SchemaFiles)
                : XObjectsCoreGenerator.Generate(generateOptions.SchemaFiles, settings);

            if (generateOptions.Output.IsEmpty())
            {
                if (generateOptions.FoldersWereGiven)
                {
                    Console.WriteLine("No output directory given: defaulting to same directory as XSD file(s).");
                    generateOptions.Output = "-1";
                }
                else
                {
                    generateOptions.Output = Environment.CurrentDirectory;
                    Console.WriteLine($"No output directory given: defaulting to current working directory: {Environment.CurrentDirectory}.");
                }
            }

            var hasCsExt = Path.GetExtension(generateOptions.Output).EndsWith(".cs");

            if (hasCsExt) // merge the output into a single file
            {
                GenerateCodeDispatcher.HandleWriteOutputToSingleFile(generateOptions.Output, textWriters);
            }
            else
            {
                GenerateCodeDispatcher.HandleWriteOutputToMultipleFiles(generateOptions.Output, textWriters);
            }
        }
Exemplo n.º 5
0
        public void ToClassStringWritersTest()
        {
            var xmlSpecXsd           = @"XMLSpec\xmlspec.xsd";
            var xmlSpecXsdConfigFile = @"XMLSpec\xmlspec.xsd.config";
            var xmlSpecXsdConfig     = Configuration.Load(xmlSpecXsdConfigFile);
            var xmlSpecSchemaSet     = FileSystemUtilities.PreLoadXmlSchemas(xmlSpecXsd);

            Assert.IsNotNull(xmlSpecSchemaSet);
            Assert.IsTrue(xmlSpecSchemaSet.IsCompiled);

            var ccu = XObjectsCoreGenerator.GenerateCodeCompileUnit(xmlSpecSchemaSet,
                                                                    xmlSpecXsdConfig.ToLinqToXsdSettings());

            var classStringWriters = ccu.ToClassStringWriters().ToList();

            Assert.IsNotEmpty(classStringWriters);

            foreach (var one in classStringWriters)
            {
                var classString = one.ToString();

                Assert.IsNotEmpty(classString);
            }
        }