camille 0.1.0 → 0.3.0

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: efa884b2cb790a85ad8ae7f2e5462fd2a94d507aad4437c3063af81b333dd2c4
4
- data.tar.gz: e1a1c1a68397a31521704a2fff6044a3f78997418acf5cd4fd5994c9eb5748c3
3
+ metadata.gz: 5779c76d0ca3c695e1e15d0870d94be5739449f7ad1b9422ab9944d139f3a6d1
4
+ data.tar.gz: 855906b19324060e4fb8578451e1fd77f692918f6722a81b9f3fbdc5cbf48fd3
5
5
  SHA512:
6
- metadata.gz: 2c8c279a7e420762d3586a191b08f4e916fe2827d30a7d76702d4dcd92e466efe0cca29b46a8688f198eb6c49193c2698ef7fe87f7fc85e292489e8bf39a27b0
7
- data.tar.gz: c4ba05522f14dc3c80f9dabfddacfe562f32475d278b49ddb49104bb76f8cb72d8032547e0cc25d5f8655c87d6ae0ddecb4e5db7cab4058d27b40236b4fba06a
6
+ metadata.gz: e555aebf740ecd06dcf96928a3aeafe414430a7d439a83679da90d2e60e23a97cd28bc583a473ddab76c60275e31668b76f9220ca2278c21ecceb8f2f11743fa
7
+ data.tar.gz: 00bf823aa9f394c1198cd492f42cd716b8bd59c08fe7e704df64aa92cdb927198ad87761f867de9b59153cba96490190292296755ef1642bd9c759820b00ecc0
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.1.0)
5
- listen (~> 3.3)
4
+ camille (0.3.0)
6
5
  rails (>= 6.1, < 8)
7
6
 
8
7
  GEM
@@ -79,14 +78,10 @@ GEM
79
78
  date (3.3.3)
80
79
  diff-lcs (1.5.0)
81
80
  erubi (1.12.0)
82
- ffi (1.15.5)
83
81
  globalid (1.1.0)
84
82
  activesupport (>= 5.0)
85
83
  i18n (1.12.0)
86
84
  concurrent-ruby (~> 1.0)
87
- listen (3.8.0)
88
- rb-fsevent (~> 0.10, >= 0.10.3)
89
- rb-inotify (~> 0.9, >= 0.9.10)
90
85
  loofah (2.19.1)
91
86
  crass (~> 1.0.2)
92
87
  nokogiri (>= 1.5.9)
@@ -142,9 +137,6 @@ GEM
142
137
  thor (~> 1.0)
143
138
  zeitwerk (~> 2.5)
144
139
  rake (13.0.6)
145
- rb-fsevent (0.11.2)
146
- rb-inotify (0.10.1)
147
- ffi (~> 1.0)
148
140
  rspec (3.12.0)
149
141
  rspec-core (~> 3.12.0)
150
142
  rspec-expectations (~> 3.12.0)
@@ -4,6 +4,7 @@ module Camille
4
4
  module CodeGenerator
5
5
  def self.generate_ts
6
6
  io = StringIO.new
7
+ io.puts "// This file is automatically generated."
7
8
  io.puts Camille::Configuration.ts_header
8
9
  io.puts
9
10
  Camille::Types.literal_lines.each do |line|
@@ -17,10 +18,5 @@ module Camille
17
18
  io.string
18
19
  end
19
20
 
20
- def self.generate_ts_file
21
- if Camille::Configuration.ts_location
22
- File.open(Camille::Configuration.ts_location, 'w'){|f| f.write generate_ts}
23
- end
24
- end
25
21
  end
26
22
  end
@@ -34,9 +34,6 @@ module Camille
34
34
  end
35
35
 
36
36
  def process_action(*)
37
- if exception = Camille::Loader.exception
38
- raise exception
39
- end
40
37
  if endpoint = camille_endpoint
41
38
  params.deep_transform_keys!{|key| key.to_s.underscore}
42
39
  super
@@ -0,0 +1,23 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'install camille'
8
+
9
+ def copy_configuration_file
10
+ copy_file "configuration.rb", "config/camille/configuration.rb"
11
+ end
12
+
13
+ def copy_type_example
14
+ copy_file "type_example.rb", "config/camille/types/example.rb"
15
+ end
16
+
17
+ def copy_schema_example
18
+ copy_file "schema_example.rb", "config/camille/schemas/examples.rb"
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class SchemaGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'generate camille schema'
8
+
9
+ def generate_type_file
10
+ template 'schema_template.erb', "config/camille/schemas/#{@name}.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ Camille.configure do |config|
2
+ # Contents to be placed at the beginning of the output TypeScript file.
3
+ config.ts_header = <<~EOF
4
+ import request from './request'
5
+ EOF
6
+ end
@@ -0,0 +1,35 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Schemas::Examples < Camille::Schema
4
+ include Camille::Types
5
+
6
+ get :find do
7
+ params(
8
+ id: Number
9
+ )
10
+
11
+ response(
12
+ example?: Example
13
+ )
14
+ end
15
+
16
+ get :list do
17
+ response(
18
+ examples: Example[]
19
+ )
20
+ end
21
+
22
+ post :update do
23
+ params(
24
+ id: Number,
25
+ example: Example
26
+ )
27
+
28
+ response(
29
+ success: Boolean,
30
+ errors: {
31
+ message: String
32
+ }[]
33
+ )
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Schemas::<%= class_name %> < Camille::Schema
4
+ include Camille::Types
5
+
6
+ # get :data do
7
+ # params(
8
+ # id: Number
9
+ # )
10
+ # response(
11
+ # name: String
12
+ # )
13
+ # end
14
+ end
@@ -0,0 +1,10 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Types::Example < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(
7
+ id: Number,
8
+ name: String,
9
+ )
10
+ end
@@ -0,0 +1,9 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Types::<%= class_name %> < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(
7
+ # id: Number
8
+ )
9
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class TypeGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'generate camille type'
8
+
9
+ def generate_type_file
10
+ template 'type_template.erb', "config/camille/types/#{@name}.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -10,8 +10,25 @@ module Camille
10
10
  synchronize do
11
11
  loader = Zeitwerk::Loader.new
12
12
  loader.enable_reloading if !app.config.cache_classes
13
- loader.push_dir "#{app.root}/config/camille/types", namespace: Camille::Types
14
- loader.push_dir "#{app.root}/config/camille/schemas", namespace: Camille::Schemas
13
+
14
+ types_dir = "#{app.root}/config/camille/types"
15
+ schemas_dir = "#{app.root}/config/camille/schemas"
16
+
17
+ if Dir.exist? types_dir
18
+ loader.push_dir types_dir, namespace: Camille::Types
19
+ else
20
+ unless inside_generator?
21
+ puts "[Camille Warning] Expected folder `config/camille/types`. Run `rails g camille:install` to fix it."
22
+ end
23
+ end
24
+
25
+ if Dir.exist? schemas_dir
26
+ loader.push_dir schemas_dir, namespace: Camille::Schemas
27
+ else
28
+ unless inside_generator?
29
+ puts "[Camille Warning] Expected folder `config/camille/schemas`. Run `rails g camille:install` to fix it."
30
+ end
31
+ end
15
32
 
16
33
  loader.setup
17
34
  @zeitwerk_loader = loader
@@ -19,21 +36,9 @@ module Camille
19
36
 
20
37
  eager_load
21
38
  construct_controller_name_to_schema_map
22
- Camille::CodeGenerator.generate_ts_file
23
39
  end
24
40
  end
25
41
 
26
- def eager_load
27
- @eager_loading = true
28
- load @configuration_location
29
- @zeitwerk_loader.eager_load
30
- @eager_loading = false
31
- end
32
-
33
- def eager_loading?
34
- @eager_loading
35
- end
36
-
37
42
  def reload_types_and_schemas
38
43
  synchronize do
39
44
  Camille::Loader.loaded_types.clear
@@ -41,14 +46,10 @@ module Camille
41
46
  @zeitwerk_loader.reload
42
47
  eager_load
43
48
  construct_controller_name_to_schema_map
44
- Camille::CodeGenerator.generate_ts_file
45
49
  end
46
50
  end
47
51
 
48
52
  def register_routes router_context
49
- if exception = Camille::Loader.exception
50
- raise exception
51
- end
52
53
  Camille::Loader.loaded_schemas.each do |schema|
53
54
  schema.endpoints.each do |name, endpoint|
54
55
  router_context.public_send(endpoint.verb, endpoint.path, controller: schema.path.gsub(/^\//, ''), action: endpoint.name, as: false)
@@ -74,48 +75,32 @@ module Camille
74
75
  end
75
76
  end
76
77
 
77
- def construct_controller_name_to_schema_map
78
- synchronize do
79
- controller_name_to_schema_map.clear
80
- loaded_schemas.each do |schema|
81
- controller_class_name = "#{schema.klass_name}Controller"
82
- controller_name_to_schema_map[controller_class_name] = schema
78
+ private
79
+ def eager_load
80
+ if File.exist?(@configuration_location)
81
+ load @configuration_location
82
+ else
83
+ unless inside_generator?
84
+ puts "[Camille Warning] Expected file `config/camille/configuration.rb`. Run `rails g camille:install` to fix it."
85
+ end
83
86
  end
87
+ @zeitwerk_loader.eager_load
84
88
  end
85
- end
86
89
 
87
- def reload
88
- synchronize do
89
- begin
90
- @exception = nil
91
- reload_types_and_schemas
92
- Rails.application.reload_routes!
93
-
94
- # just for spec
95
- @last_reload = Time.now
96
- rescue Exception => e
97
- Rails.logger.error e
98
- @exception = e
90
+ def construct_controller_name_to_schema_map
91
+ synchronize do
92
+ controller_name_to_schema_map.clear
93
+ loaded_schemas.each do |schema|
94
+ controller_class_name = "#{schema.klass_name}Controller"
95
+ controller_name_to_schema_map[controller_class_name] = schema
96
+ end
99
97
  end
100
98
  end
101
- end
102
-
103
- def exception
104
- synchronize do
105
- @exception
106
- end
107
- end
108
99
 
109
- def setup_listen app
110
- if !app.config.cache_classes
111
- require 'listen'
112
- listener = Listen.to("#{app.root}/config/camille") do |changed|
113
- #puts "Change detected. Camille reloading..."
114
- Camille::Loader.reload
115
- end
116
- listener.start
100
+ def inside_generator?
101
+ # https://stackoverflow.com/a/53963584
102
+ !(Rails.const_defined?(:Server) || Rails.const_defined?(:Console))
117
103
  end
118
- end
119
104
 
120
105
  end
121
106
 
@@ -0,0 +1,9 @@
1
+ require 'action_controller'
2
+
3
+ module Camille
4
+ class MainController < ActionController::Base
5
+ def endpoints_ts
6
+ render plain: Camille::CodeGenerator.generate_ts
7
+ end
8
+ end
9
+ end
@@ -8,16 +8,28 @@ module Camille
8
8
  ActionController::Base.include(Camille::ControllerExtension)
9
9
 
10
10
  Camille::Loader.setup_zeitwerk_loader(app)
11
- Camille::Loader.setup_listen(app)
12
11
 
13
12
  app.routes.prepend do
13
+ if Rails.env.development?
14
+ get '/camille/endpoints.ts' => 'camille/main#endpoints_ts'
15
+ end
16
+
14
17
  Camille::Loader.register_routes(self)
15
18
  end
16
19
 
20
+ dir = "#{Rails.root}/config/camille"
21
+
22
+ update_checker = ActiveSupport::FileUpdateChecker.new([], {dir => ['rb']}) do
23
+ Camille::Loader.reload_types_and_schemas
24
+ end
25
+
26
+ app.reloaders << update_checker
27
+
17
28
  app.reloader.to_run do
18
29
  require_unload_lock!
19
- Camille::Loader.reload
30
+ update_checker.execute_if_updated
20
31
  end
32
+
21
33
  end
22
34
 
23
35
  end
@@ -2,20 +2,10 @@ module Camille
2
2
  class Schema
3
3
  class AlreadyDefinedError < ::ArgumentError; end
4
4
 
5
- include Camille::Types
6
-
7
5
  def self.endpoints
8
6
  @endpoints ||= {}
9
7
  end
10
8
 
11
- def self.const_missing name
12
- if Camille::Loader.eager_loading?
13
- Camille::Types.const_get(name)
14
- else
15
- super
16
- end
17
- end
18
-
19
9
  def self.path
20
10
  "/#{ActiveSupport::Inflector.underscore klass_name}"
21
11
  end
data/lib/camille/type.rb CHANGED
@@ -2,7 +2,6 @@ module Camille
2
2
  # This class represents all custom types defined by the user.
3
3
  class Type < BasicType
4
4
  class NotImplementedError < ::NotImplementedError; end
5
- include Camille::Types
6
5
 
7
6
  attr_reader :underlying
8
7
 
@@ -30,14 +29,6 @@ module Camille
30
29
  self.class.klass_name.gsub(/::/, '_')
31
30
  end
32
31
 
33
- def self.const_missing name
34
- if Camille::Loader.eager_loading?
35
- Camille::Types.const_get(name)
36
- else
37
- super
38
- end
39
- end
40
-
41
32
  def self.inherited klass
42
33
  Camille::Loader.loaded_types << klass
43
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/camille.rb CHANGED
@@ -28,6 +28,12 @@ require_relative "camille/controller_extension"
28
28
  require_relative "camille/loader"
29
29
  require_relative "camille/configuration"
30
30
  require_relative "camille/code_generator"
31
+ require_relative "camille/main_controller"
32
+
33
+ require "rails/generators"
34
+ require_relative "camille/generators/install_generator"
35
+ require_relative "camille/generators/type_generator"
36
+ require_relative "camille/generators/schema_generator"
31
37
 
32
38
  module Camille
33
39
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-10 00:00:00.000000000 Z
11
+ date: 2023-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,20 +30,6 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '8'
33
- - !ruby/object:Gem::Dependency
34
- name: listen
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '3.3'
40
- type: :runtime
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '3.3'
47
33
  description: ''
48
34
  email:
49
35
  - cichol@live.cn
@@ -65,8 +51,17 @@ files:
65
51
  - lib/camille/controller_extension.rb
66
52
  - lib/camille/core_ext.rb
67
53
  - lib/camille/endpoint.rb
54
+ - lib/camille/generators/install_generator.rb
55
+ - lib/camille/generators/schema_generator.rb
56
+ - lib/camille/generators/templates/configuration.rb
57
+ - lib/camille/generators/templates/schema_example.rb
58
+ - lib/camille/generators/templates/schema_template.erb
59
+ - lib/camille/generators/templates/type_example.rb
60
+ - lib/camille/generators/templates/type_template.erb
61
+ - lib/camille/generators/type_generator.rb
68
62
  - lib/camille/line.rb
69
63
  - lib/camille/loader.rb
64
+ - lib/camille/main_controller.rb
70
65
  - lib/camille/railtie.rb
71
66
  - lib/camille/schema.rb
72
67
  - lib/camille/schemas.rb