Class: Synthra::CLI::Commands::Export
- Defined in:
- lib/synthra/cli/commands/export.rb
Overview
Export command - exports schemas to various formats
Instance Method Summary collapse
- #build_export_options(registry) ⇒ Object private
- #call ⇒ Object
- #determine_output_path(schema_name) ⇒ Object private
- #export_all(registry, format) ⇒ Object private
- #export_single(registry, schema_name, format) ⇒ Object private
- #write_output(output, schema_name) ⇒ Object private
Constructor Details
This class inherits a constructor from Synthra::CLI::Commands::Base
Instance Method Details
#build_export_options(registry) ⇒ Object (private)
114 115 116 117 118 119 120 121 |
# File 'lib/synthra/cli/commands/export.rb', line 114 def (registry) { registry: registry, count: [:count], seed: [:seed], mode: [:mode] || :random }.compact end |
#call ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/synthra/cli/commands/export.rb', line 15 def call schema_name = args.shift registry = Registry.new load_path(registry, [:dir], validate: false) format = [:export_format] output = if [:all] export_all(registry, format) else unless schema_name error "Usage: synthra export <SchemaName> [options]" error " synthra export --all [options]" return EXIT_PARSE_ERROR end export_single(registry, schema_name, format) end return EXIT_PARSE_ERROR unless output write_output(output, schema_name) EXIT_SUCCESS rescue KeyError error "Unknown schema: #{schema_name}" $stderr.puts "Available schemas: #{registry.names.join(", ")}" EXIT_PARSE_ERROR end |
#determine_output_path(schema_name) ⇒ Object (private)
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/synthra/cli/commands/export.rb', line 135 def determine_output_path(schema_name) if [:output] [:output] elsif [:auto_file] output_dir = [:output_dir] || "." filename = Synthra::Export.auto_filename( [:all] ? "all_schemas" : schema_name, [:export_format] ) File.join(output_dir, filename) end end |
#export_all(registry, format) ⇒ Object (private)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/synthra/cli/commands/export.rb', line 46 def export_all(registry, format) case format when "typescript", "ts" Synthra::Export::Typescript.export_all(registry) when "javascript", "js" Synthra::Export::Javascript.export_all(registry) when "python", "py", "pydantic" Synthra::Export::Python.export_all(registry, style: [:python_style]) when "dataclass" Synthra::Export::Python.export_all(registry, style: :dataclass) when "sql", "sql-ddl" Synthra::Export::Sql.export_all(registry, dialect: [:sql_dialect]) when "graphviz", "dot" Synthra::Export::Graphviz.new(nil, registry: registry).export when "graphql" Synthra::Export::Graphql.export_all(registry) else error "Bulk export not supported for format: #{format}" nil end end |
#export_single(registry, schema_name, format) ⇒ Object (private)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/synthra/cli/commands/export.rb', line 68 def export_single(registry, schema_name, format) schema = registry.schema(schema_name) export_opts = (registry) case format # Schema export formats when "json-schema" Synthra::Export::JsonSchema.new(schema, **export_opts).export when "typescript", "ts" Synthra::Export::Typescript.new(schema, **export_opts).export when "javascript", "js" Synthra::Export::Javascript.new(schema, **export_opts).export when "python", "py", "pydantic" Synthra::Export::Python.new(schema, **export_opts.merge(style: [:python_style])).export when "dataclass" Synthra::Export::Python.new(schema, **export_opts.merge(style: :dataclass)).export when "sql", "sql-ddl" Synthra::Export::Sql.new(schema, registry: registry, dialect: [:sql_dialect]).export when "graphviz", "dot" Synthra::Export::Graphviz.new(schema, **export_opts).export when "graphql" Synthra::Export::Graphql.new(schema, **export_opts).export # Data export formats when "json", "json-data" Synthra::Export::JsonData.new(schema, **export_opts.merge(pretty: [:pretty])).export when "csv" Synthra::Export::Csv.new(schema, **export_opts).export when "sql-insert", "insert" Synthra::Export::SqlInsert.new(schema, **export_opts.merge( dialect: [:sql_dialect], batch_insert: [:batch_insert] )).export when "yaml", "yml" Synthra::Export::YamlData.new(schema, **export_opts).export when "xml" Synthra::Export::XmlData.new(schema, **export_opts.merge( root: [:root], item: [:item] )).export else error "Unknown export format: #{format}" nil end end |
#write_output(output, schema_name) ⇒ Object (private)
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/synthra/cli/commands/export.rb', line 123 def write_output(output, schema_name) output_path = determine_output_path(schema_name) if output_path FileUtils.mkdir_p(File.dirname(output_path)) if File.dirname(output_path) != "." File.write(output_path, output) success "Exported to #{output_path}" else puts output end end |