foobara-typescript-remote-command-generator 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88862a0a23e8083e35cb4532b39dacd37ddc676106ef0256f4d44078b5e46e60
4
- data.tar.gz: e40985f3adf7022efdf3cf85e655092909b35b9037c05edcd0f98106a6923d26
3
+ metadata.gz: '0778047e7295db9856a2f2c5907aa62e320857f514a29b68bd9eaf9a5f0c9df4'
4
+ data.tar.gz: 2a2e997115c42c315725afe2632c73359819a642d48f311d8efb14fee53798d9
5
5
  SHA512:
6
- metadata.gz: e0ed06bab827629f9d05ddc065ff8ccc63a640f1cfa29cd3d2007a0e2aeab3db78505a4459feae8fccf0764b3817ce2cee9dffd58c4c05baa158e68949d4e1bf
7
- data.tar.gz: c41a5cc46c96afa5cf537caba003925c880e861363060fd1859e6b95cd7a7bb747a435dc183c9c90bf52c976d7fd26741e40adc99b662f1d93390158a46f4713
6
+ metadata.gz: cdc5356fa30759987e1d854a7aab83aa9ad0bb7dd3194aa2d8156cf9871990e2d2904bd6bd3a2f2fb487e7125fe81d6677180f85726b7887300451bb71a97b74
7
+ data.tar.gz: 1bb61a8659f0816ffa2ebbd42065b2e3f34471820012a5c5528081229d83be1e857155c4a5655457890b5c30d622340e4b68b9b8e26b6f71734f3315c62aa0c5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [unreleased] - 2024-08-2x
2
+
3
+ - Add type generators to command result generator
4
+ - Add type generators to domain generator
5
+
6
+ ## [0.0.2] - 2024-08-21
7
+
8
+ - Add a TypeGenerator for custom non-model types
9
+
1
10
  ## [0.0.1] - 2024-06-17
2
11
 
3
12
  - Add Apache-2.0 license
@@ -14,18 +14,14 @@ module Foobara
14
14
  "Command/Inputs.ts.erb"
15
15
  end
16
16
 
17
- def model_generators
18
- @model_generators ||= inputs_types_depended_on.select(&:model?).uniq.map do |model|
19
- if model.entity?
20
- Services::EntityGenerator.new(model)
21
- else
22
- Services::ModelGenerator.new(model)
23
- end
17
+ def type_generators
18
+ @type_generators ||= inputs_types_depended_on.reject(&:builtin?).uniq.map do |type|
19
+ TypeGenerator.new(type)
24
20
  end
25
21
  end
26
22
 
27
23
  def dependencies
28
- model_generators
24
+ type_generators
29
25
  end
30
26
  end
31
27
  end
@@ -53,6 +53,22 @@ module Foobara
53
53
  end
54
54
  end
55
55
 
56
+ def type_generators
57
+ @type_generators ||= begin
58
+ type = result_type
59
+ type = type.to_type if result_type.is_a?(Manifest::TypeDeclaration)
60
+
61
+ if !type.builtin? && !type.model?
62
+ # TODO: Test this!!
63
+ # :nocov:
64
+ [TypeGenerator.new(type)]
65
+ # :nocov:
66
+ else
67
+ []
68
+ end
69
+ end
70
+ end
71
+
56
72
  def atom?
57
73
  serializers&.any? { |s| s == "Foobara::CommandConnectors::Serializers::AtomicSerializer" }
58
74
  end
@@ -72,7 +88,7 @@ module Foobara
72
88
  end
73
89
 
74
90
  def dependencies
75
- model_generators
91
+ model_generators + type_generators
76
92
  end
77
93
  end
78
94
  end
@@ -34,6 +34,18 @@ module Foobara
34
34
  end
35
35
  end
36
36
 
37
+ def type_generators
38
+ @type_generators ||= begin
39
+ # TODO: create a Manifest::Domain#custom_types
40
+ only_custom_types = domain_manifest.types.reject(&:model?)
41
+ only_custom_types.reject!(&:builtin?)
42
+
43
+ only_custom_types.map do |type|
44
+ TypeGenerator.new(type)
45
+ end
46
+ end
47
+ end
48
+
37
49
  def model_generators
38
50
  @model_generators ||= begin
39
51
  only_models = domain_manifest.models.reject(&:entity?)
@@ -45,7 +57,7 @@ module Foobara
45
57
  end
46
58
 
47
59
  def dependencies
48
- [*command_generators, *model_generators, *entity_generators, *organization]
60
+ [*command_generators, *model_generators, *entity_generators, *type_generators, *organization]
49
61
  end
50
62
 
51
63
  def domain_name
@@ -50,6 +50,7 @@ module Foobara
50
50
  end
51
51
 
52
52
  def dependencies
53
+ # Why don't we need models and custom types?
53
54
  types_depended_on.select(&:entity?)
54
55
  end
55
56
 
@@ -1,9 +1,10 @@
1
1
  require_relative "typescript_from_manifest_base_generator"
2
+ require_relative "type_generator"
2
3
 
3
4
  module Foobara
4
5
  module RemoteGenerator
5
6
  class Services
6
- class ModelGenerator < TypescriptFromManifestBaseGenerator
7
+ class ModelGenerator < TypeGenerator
7
8
  class << self
8
9
  def new(relevant_manifest)
9
10
  return super unless self == ModelGenerator
@@ -26,57 +27,12 @@ module Foobara
26
27
  ["Model", "Model.ts.erb"]
27
28
  end
28
29
 
29
- def scoped_full_path(points = nil)
30
- full_path = model_manifest.scoped_full_path
31
-
32
- if points
33
- start_at = full_path.size - points - 1
34
- full_path[start_at..]
35
- else
36
- full_path
37
- end
38
- end
39
-
40
30
  def model_name(points = nil)
41
- if points
42
- scoped_full_path(points).join(".")
43
- else
44
- scoped_path.join(".")
45
- end
46
- end
47
-
48
- # Do models have associations??
49
- def model_generators
50
- types_depended_on.select(&:model?).map do |model|
51
- Services::ModelGenerator.new(model)
52
- end
53
- end
54
-
55
- def dependencies
56
- model_generators
31
+ type_name(points)
57
32
  end
58
33
 
59
34
  def model_name_downcase
60
- model_name[0].downcase + model_name[1..]
61
- end
62
-
63
- def attributes_type_ts_type
64
- association_depth = AssociationDepth::AMBIGUOUS
65
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
66
- end
67
-
68
- def atom_attributes_ts_type
69
- association_depth = AssociationDepth::ATOM
70
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
71
- end
72
-
73
- def aggregate_attributes_ts_type
74
- association_depth = AssociationDepth::AGGREGATE
75
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
76
- end
77
-
78
- def attribute_names
79
- attributes_type.attribute_names
35
+ type_name_downcase
80
36
  end
81
37
  end
82
38
  end
@@ -0,0 +1,112 @@
1
+ require_relative "typescript_from_manifest_base_generator"
2
+
3
+ module Foobara
4
+ module RemoteGenerator
5
+ class Services
6
+ class TypeGenerator < TypescriptFromManifestBaseGenerator
7
+ class << self
8
+ def new(relevant_manifest)
9
+ return super unless self == TypeGenerator
10
+
11
+ if relevant_manifest.entity?
12
+ EntityGenerator.new(relevant_manifest)
13
+ elsif relevant_manifest.model?
14
+ ModelGenerator.new(relevant_manifest)
15
+ else
16
+ super
17
+ end
18
+ end
19
+ end
20
+
21
+ alias type_manifest relevant_manifest
22
+
23
+ def target_path
24
+ [*domain.scoped_full_path, "types", "#{type_name}.ts"]
25
+ end
26
+
27
+ def template_path
28
+ ["Type", "Type.ts.erb"]
29
+ end
30
+
31
+ def scoped_full_path(points = nil)
32
+ full_path = type_manifest.scoped_full_path
33
+
34
+ if points
35
+ start_at = full_path.size - points - 1
36
+ full_path[start_at..]
37
+ else
38
+ full_path
39
+ end
40
+ end
41
+
42
+ def type_name(points = nil)
43
+ if points
44
+ scoped_full_path(points).join(".")
45
+ else
46
+ scoped_path.join(".")
47
+ end
48
+ end
49
+
50
+ def type_guts
51
+ guts = if declaration_data.key?("one_of")
52
+ declaration_data["one_of"].map do |enum_item|
53
+ value_to_ts_value(enum_item)
54
+ end.join(" | ")
55
+ else
56
+ # :nocov:
57
+ raise "Haven't implemented other types yet"
58
+ # :nocov:
59
+ end
60
+
61
+ if declaration_data["allows_nil"]
62
+ # TODO: add a custom type to the fixture manifest that includes allows_nil
63
+ # :nocov:
64
+ guts += " | undefined"
65
+ # :nocov:
66
+ end
67
+
68
+ guts
69
+ end
70
+
71
+ def model_generators
72
+ @model_generators ||= types_depended_on.select(&:model?).map do |model|
73
+ Services::ModelGenerator.new(model)
74
+ end
75
+ end
76
+
77
+ def custom_type_generators
78
+ @custom_type_generators ||= types_depended_on.reject(&:builtin?).reject(&:model?).map do |type|
79
+ Services::TypeGenerator.new(type)
80
+ end
81
+ end
82
+
83
+ def dependencies
84
+ custom_type_generators + model_generators
85
+ end
86
+
87
+ def type_name_downcase
88
+ type_name[0].downcase + type_name[1..]
89
+ end
90
+
91
+ def attributes_type_ts_type
92
+ association_depth = AssociationDepth::AMBIGUOUS
93
+ foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
94
+ end
95
+
96
+ def atom_attributes_ts_type
97
+ association_depth = AssociationDepth::ATOM
98
+ foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
99
+ end
100
+
101
+ def aggregate_attributes_ts_type
102
+ association_depth = AssociationDepth::AGGREGATE
103
+ foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
104
+ end
105
+
106
+ def attribute_names
107
+ attributes_type.attribute_names
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -66,6 +66,8 @@ module Foobara
66
66
  Services::ProcessorClassGenerator
67
67
  when Manifest::RootManifest
68
68
  Services::RootManifestGenerator
69
+ when Manifest::Type
70
+ Services::TypeGenerator
69
71
  else
70
72
  # :nocov:
71
73
  raise "Not sure how build a generator for a #{manifest}"
@@ -224,6 +226,10 @@ module Foobara
224
226
  else
225
227
  if type_declaration.model?
226
228
  model_to_ts_model_name(type_declaration, association_depth:, initial:)
229
+ elsif can_convert_type_declaration_to_ts_type?(type_declaration)
230
+ type = type_declaration.to_type
231
+
232
+ custom_type_to_ts_type_name(type)
227
233
  end
228
234
  end
229
235
  end
@@ -245,6 +251,22 @@ module Foobara
245
251
  end
246
252
  end
247
253
 
254
+ def can_convert_type_declaration_to_ts_type?(type_declaration)
255
+ type = type_declaration.to_type
256
+
257
+ return false if BuiltinTypes.builtin_reference?(type.reference)
258
+
259
+ allowed_keys = %w[type one_of allows_nil]
260
+
261
+ declaration_data = type.declaration_data
262
+
263
+ bad_keys = declaration_data.keys - allowed_keys
264
+
265
+ return false unless bad_keys.empty?
266
+
267
+ BuiltinTypes.builtin_reference?(declaration_data["type"])
268
+ end
269
+
248
270
  def attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS)
249
271
  guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration|
250
272
  " #{attribute_name}#{"?" unless attributes.required?(attribute_name)}: #{
@@ -280,6 +302,13 @@ module Foobara
280
302
  dependency_group.non_colliding_type(generator)
281
303
  end
282
304
 
305
+ def custom_type_to_ts_type_name(type)
306
+ type = type.to_type if type.is_a?(Manifest::TypeDeclaration)
307
+ generator = TypeGenerator.new(type)
308
+
309
+ dependency_group.non_colliding_type(generator)
310
+ end
311
+
283
312
  def ==(other)
284
313
  self.class == other.class && path == other.path && root_manifest == other.root_manifest
285
314
  end
@@ -0,0 +1,5 @@
1
+ <% dependency_roots.each do |dependency_root| %>
2
+ import { <%= dependency_root.ts_instance_name %> } from "<%= path_to_root %><%= dependency_root.import_path %>"
3
+ <% end %>
4
+
5
+ export type <%= type_name %> = <%= type_guts %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-typescript-remote-command-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-18 00:00:00.000000000 Z
11
+ date: 2024-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foobara
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description:
41
+ description:
42
42
  email:
43
43
  - azimux@gmail.com
44
44
  executables: []
@@ -82,6 +82,7 @@ files:
82
82
  - src/remote_generator/services/organization_manifest_generator.rb
83
83
  - src/remote_generator/services/processor_class_generator.rb
84
84
  - src/remote_generator/services/root_manifest_generator.rb
85
+ - src/remote_generator/services/type_generator.rb
85
86
  - src/remote_generator/services/typescript_from_manifest_base_generator.rb
86
87
  - src/remote_generator/services/unloaded_entity_generator.rb
87
88
  - src/remote_generator/write_typescript_to_disk.rb
@@ -105,6 +106,7 @@ files:
105
106
  - templates/ModelVariants.ts.erb
106
107
  - templates/Organization.ts.erb
107
108
  - templates/Organization/config.ts.erb
109
+ - templates/Type/Type.ts.erb
108
110
  - templates/base/DataPath.ts
109
111
  - templates/base/Entity.ts
110
112
  - templates/base/Error.ts
@@ -120,7 +122,7 @@ metadata:
120
122
  source_code_uri: https://github.com/foobara/typescript-remote-command-generator
121
123
  changelog_uri: https://github.com/foobara/typescript-remote-command-generator/CHANGELOG.md
122
124
  rubygems_mfa_required: 'true'
123
- post_install_message:
125
+ post_install_message:
124
126
  rdoc_options: []
125
127
  require_paths:
126
128
  - lib
@@ -136,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
138
  version: '0'
137
139
  requirements: []
138
140
  rubygems_version: 3.4.10
139
- signing_key:
141
+ signing_key:
140
142
  specification_version: 4
141
143
  summary: Generates remote commands for Typescript from a foobara manifest
142
144
  test_files: []