Ejemplo n.º 1
0
        static int Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("Usage: BSOA.FromJSchema <JSchemaPath> <OutputBsoaSchemaPath> <RootTypeName> <OutputNamespace>");
                return(-2);
            }

            try
            {
                string jschemaPath     = args[0];
                string outputPath      = args[1];
                string rootTypeName    = args[2];
                string outputNamespace = args[3];

                Console.WriteLine($"Converting jschema\r\n  '{jschemaPath}' to \r\n  '{outputPath}'...");
                JsonSchema schema = null;

                using (StreamReader sr = File.OpenText(jschemaPath))
                {
                    schema = SchemaReader.ReadSchema(sr, jschemaPath);
                }

                schema = JsonSchema.Collapse(schema);

                Database db = new Database($"{rootTypeName}Database", outputNamespace, rootTypeName);

                Table root = new Table(rootTypeName);
                db.Tables.Add(root);
                AddColumns(root, schema);

                foreach (KeyValuePair <string, JsonSchema> type in schema.Definitions)
                {
                    string tableName = type.Key.ToPascalCase();
                    if (TypeRenames.TryGetValue(tableName, out string renamed))
                    {
                        tableName = renamed;
                    }

                    Table table = new Table(tableName);
                    AddColumns(table, type.Value);
                    db.Tables.Add(table);
                }

                AsJson.Save(outputPath, db, verbose: true);
                Console.WriteLine("Done.");
                Console.WriteLine();

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: {ex}");
                return(-1);
            }
        }
Ejemplo n.º 2
0
        public string Generate(JsonSchema rootSchema)
        {
            _additionalTypesRequiredList.Clear();

            if (_settings.GenerateCloningCode)
            {
                _kindEnumName      = _settings.SchemaName + "NodeKind" + _settings.TypeNameSuffix;
                _nodeInterfaceName = "I" + _settings.SchemaName + "Node" + _settings.TypeNameSuffix;
            }

            _rootSchema = JsonSchema.Collapse(rootSchema);

            if (_fileSystem.DirectoryExists(_settings.OutputDirectory) && !_settings.ForceOverwrite)
            {
                throw Error.CreateException(Resources.ErrorOutputDirectoryExists, _settings.OutputDirectory);
            }

            _fileSystem.CreateDirectory(_settings.OutputDirectory);

            SchemaType rootSchemaType = _rootSchema.SafeGetType();

            if (rootSchemaType != SchemaType.Object)
            {
                throw Error.CreateException(Resources.ErrorNotAnObject, rootSchemaType.ToString().ToLowerInvariant());
            }

            string rootFileText = GenerateClass(_settings.RootClassName, _rootSchema, _settings.SealClasses, _settings.TypeNameSuffix);

            if (_settings.GenerateEqualityComparers)
            {
                GenerateEqualityComparer(_settings.RootClassName, _rootSchema);
            }

            if (_rootSchema.Definitions != null)
            {
                List <KeyValuePair <string, JsonSchema> > typeDefinitions = _rootSchema.Definitions.Where(ShouldGenerateType).ToList();
                GenerateClassesForDefinitions(typeDefinitions);

                if (_settings.GenerateEqualityComparers)
                {
                    GenerateEqualityComparers(typeDefinitions);
                }
            }

            foreach (AdditionalTypeRequiredInfo additionalTypeRequiredInfo in _additionalTypesRequiredList)
            {
                GenerateAdditionalType(additionalTypeRequiredInfo.Hint, additionalTypeRequiredInfo.Schema);
            }

            if (_settings.GenerateCloningCode)
            {
                _pathToFileContentsDictionary[_nodeInterfaceName + _settings.TypeNameSuffix] =
                    GenerateSyntaxInterface(_settings.SchemaName, _kindEnumName, _nodeInterfaceName);

                _pathToFileContentsDictionary[_kindEnumName + _settings.TypeNameSuffix] =
                    GenerateKindEnum(_kindEnumName);

                string rewritingVisitorClassName = _settings.SchemaName + "RewritingVisitor" + _settings.TypeNameSuffix;
                _pathToFileContentsDictionary[rewritingVisitorClassName] =
                    new RewritingVisitorGenerator(
                        _classInfoDictionary,
                        _settings.CopyrightNotice,
                        _settings.SuffixedNamespaceName,
                        rewritingVisitorClassName,
                        _settings.SchemaName,
                        _kindEnumName,
                        _nodeInterfaceName,
                        _generatedClassNames)
                    .GenerateRewritingVisitor();
            }

            foreach (KeyValuePair <string, string> entry in _pathToFileContentsDictionary)
            {
                _fileSystem.WriteAllText(Path.Combine(_settings.OutputDirectory, entry.Key + ".cs"), entry.Value);
            }

            // Returning the text of the file generated from the root schema allows this method
            // to be more easily unit tested.
            return(rootFileText);
        }