Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Start generating...");

            var properties = PrepareGenerationProperties(args);

            var generator = new ByJsonSchemaGenerator();

            if (properties.InSingleFile)
            {
                var result = generator.GenerateSingleItem(properties.Properties);

                File.WriteAllText(properties.Out + $"\\{result.FileName}.cs", result.Code);
            }
            else
            {
                var results = generator.GenerateSeparateItems(properties.Properties);

                foreach (var item in results)
                {
                    File.WriteAllText(properties.Out + $"\\{item.FileName}.cs", item.Code);
                }
            }

            Console.WriteLine("Done!");
        }
Exemple #2
0
        private IEnumerable <GenerationResult> GenerateCode(string schema, string nameSpace, string schemaName)
        {
            if (_packageOptions.RedefineNameSpace && string.IsNullOrWhiteSpace(_packageOptions.NameSpace))
            {
                VsShellUtilities.ShowMessageBox(
                    _serviceProvider,
                    "Redefined namespace should not be Empty",
                    "Generation Failed!",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

                return(Enumerable.Empty <GenerationResult>());
            }

            var properties = new GenerationProperties
            {
                JsonSchema = schema,
                IsSealed   = _packageOptions.IsSealed,
                NameSpace  = _packageOptions.RedefineNameSpace ? _packageOptions.NameSpace : nameSpace
            };

            try
            {
                if (_packageOptions.InSingleFile)
                {
                    var result = _generator.GenerateSingleItem(properties);
                    result.FileName = schemaName;

                    return(new[] { result });
                }
                else
                {
                    return(_generator.GenerateSeparateItems(properties));
                }
            }
            catch (CodeGeneratorFailException exception)
            {
                VsShellUtilities.ShowMessageBox(
                    _serviceProvider,
                    string.IsNullOrEmpty(exception.ObjectDefinitionSource) ? string.Empty : $"Root node of achema:\n{exception.ObjectDefinitionSource}",
                    $"Error: {exception.Message}",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

                return(Enumerable.Empty <GenerationResult>());
            }
            catch (Exception exception)
            {
                VsShellUtilities.ShowMessageBox(
                    _serviceProvider,
                    exception.Message,
                    $"Unhandled Exception!",
                    OLEMSGICON.OLEMSGICON_INFO,
                    OLEMSGBUTTON.OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);

                return(Enumerable.Empty <GenerationResult>());
            }
        }