Exemple #1
0
        private void OnGUI()
        {
            var halfWidth = position.width * 0.5f;

            using (new EditorGUILayout.VerticalScope())
            {
                GUILayout.Label("Json Schema", EditorStyles.largeLabel);

                if (GUILayout.Button(">> TO CODE", GUILayout.Width(120)))
                {
                    var backend = new CSharpGenerationBackend();
                    var result  = JsonPropertyContainerSchemaReader.Read(m_SchemaJson);
                    backend.Generate(result);

                    m_CodeContent = backend.Code.ToString();
                }
            }

            m_SchemaJsonScroll = EditorGUILayout.BeginScrollView(
                m_SchemaJsonScroll,
                GUILayout.Height(position.height)
                );
            m_SchemaJson = EditorGUI.TextArea(
                new Rect(0, 0, halfWidth, position.height),
                m_SchemaJson,
                EditorStyles.textArea
                );
            EditorGUILayout.EndScrollView();
            EditorGUI.SelectableLabel(
                new Rect(halfWidth, 0, halfWidth, position.height),
                m_CodeContent,
                EditorStyles.textArea
                );
        }
Exemple #2
0
        private void OnGUI()
        {
            var halfHeight = position.height * 0.1f;

            using (new EditorGUILayout.VerticalScope())
            {
                using (new EditorGUILayout.HorizontalScope())
                {
                    m_AssemblyPath = EditorGUILayout.TextField("Assembly file path: ", m_AssemblyPath);

                    if (!string.IsNullOrWhiteSpace(m_AssemblyPath) && File.Exists(m_AssemblyPath))
                    {
                        var propertyTypeTree = ReflectionPropertyTree.Read(m_AssemblyPath);

                        m_JsonContent = JsonSchema.ToJson(new JsonSchema()
                        {
                            PropertyTypeNodes = propertyTypeTree
                        });

                        var validator        = new JsonSchemaValidator();
                        var validationResult = validator.ValidatePropertyDefinition(m_JsonContent);
                        Debug.Log($"Json Schema validation results: {validationResult.IsValid}, {validationResult.Error}");

                        m_JsonContent = JsonSchema.ToJson(propertyTypeTree);

                        if (!string.IsNullOrEmpty(m_JsonContent))
                        {
                            var backend = new CSharpGenerationBackend();
                            backend.Generate(propertyTypeTree);

                            var generationFilePath = Path.GetDirectoryName(m_AssemblyPath);
                            if (generationFilePath != null)
                            {
                                var jsonFilePath = Path.Combine(
                                    generationFilePath,
                                    $"GeneratedSchema{Path.GetFileNameWithoutExtension(m_AssemblyPath)}.json");
                                File.WriteAllText(jsonFilePath, m_JsonContent);

                                Debug.Log($"Generated JSON schema written to: {jsonFilePath}");

                                var generatedFilePath = Path.Combine(
                                    generationFilePath,
                                    $"GeneratedSchema{Path.GetFileNameWithoutExtension(m_AssemblyPath)}.cs");
                                File.WriteAllText(generatedFilePath, backend.Code);

                                Debug.Log($"Generated code written to: {generatedFilePath}");
                            }
                        }
                    }
                }

                EditorGUI.SelectableLabel(
                    new Rect(0, halfHeight, position.width, position.height - halfHeight),
                    m_JsonContent,
                    EditorStyles.textArea
                    );
            }
        }