Class: Souls::Generate

Inherits:
Thor
  • Object
show all
Defined in:
lib/souls/cli/generate/job.rb,
lib/souls/cli/generate/edge.rb,
lib/souls/cli/generate/type.rb,
lib/souls/cli/generate/index.rb,
lib/souls/cli/generate/query.rb,
lib/souls/cli/generate/job_rbs.rb,
lib/souls/cli/generate/manager.rb,
lib/souls/cli/generate/edge_rbs.rb,
lib/souls/cli/generate/mutation.rb,
lib/souls/cli/generate/resolver.rb,
lib/souls/cli/generate/type_rbs.rb,
lib/souls/cli/generate/query_rbs.rb,
lib/souls/cli/generate/rspec_job.rb,
lib/souls/cli/generate/connection.rb,
lib/souls/cli/generate/application.rb,
lib/souls/cli/generate/manager_rbs.rb,
lib/souls/cli/generate/rspec_query.rb,
lib/souls/cli/generate/mutation_rbs.rb,
lib/souls/cli/generate/resolver_rbs.rb,
lib/souls/cli/generate/rspec_factory.rb,
lib/souls/cli/generate/rspec_manager.rb,
lib/souls/cli/generate/connection_rbs.rb,
lib/souls/cli/generate/rspec_mutation.rb,
lib/souls/cli/generate/rspec_resolver.rb

Instance Method Summary collapse

Instance Method Details

#connection(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/connection.rb', line 4

def connection(class_name)
  file_dir = "./app/graphql/types/connections/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.underscore.singularize
  file_path = "./app/graphql/types/connections/#{singularized_class_name}_connection.rb"
  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      class Types::#{singularized_class_name.camelize}Connection < Types::BaseConnection
        edge_type(Types::#{singularized_class_name.camelize}Edge)
      end
    TEXT
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#connection_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/souls/cli/generate/connection_rbs.rb', line 4

def connection_rbs(class_name)
  file_path = ""
  Dir.chdir(Souls.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types/connections/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    singularized_class_name = class_name.underscore.singularize
    file_path = "#{file_dir}#{singularized_class_name}_connection.rbs"
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        module Types
          class #{singularized_class_name.camelize}Connection < Types::BaseConnection
            def self.edge_type: (*untyped) -> untyped
          end
        end
      TEXT
    end
    Souls::Painter.create_file(file_path.to_s)
  end
  file_path
end

#edge(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/edge.rb', line 4

def edge(class_name)
  file_dir = "./app/graphql/types/edges"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.underscore.singularize
  file_path = "./app/graphql/types/edges/#{singularized_class_name}_edge.rb"
  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      class Types::#{singularized_class_name.camelize}Edge < Types::BaseEdge
        node_type(Types::#{singularized_class_name.camelize}Type)
      end
    TEXT
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#edge_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/souls/cli/generate/edge_rbs.rb', line 4

def edge_rbs(class_name)
  file_path = ""
  Dir.chdir(Souls.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types/edges/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    singularized_class_name = class_name.underscore.singularize
    file_path = "#{file_dir}#{singularized_class_name}_edge.rbs"
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        module Types
          class #{singularized_class_name.camelize}Edge < Types::BaseEdge
            def self.edge_type: (*untyped) -> untyped
            def self.node_type: (*untyped) -> untyped
            def self.global_id_field: (*untyped) -> untyped
            def self.connection_type: ()-> untyped
          end
        end
      TEXT
    end
    Souls::Painter.create_file(file_path.to_s)
  end
  file_path
end

#job(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/job.rb', line 5

def job(class_name)
  if options[:mailer]
    create_job_mailer_type(class_name)
    mailgun_mailer(class_name)
  else
    create_job_type(class_name)
    create_job(class_name)
  end
  Souls::Generate.new.invoke(:job_rbs, [class_name], {})
  Souls::Generate.new.invoke(:rspec_job, [class_name], { mailer: options[:mailer] })
end

#job_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
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
# File 'lib/souls/cli/generate/job_rbs.rb', line 4

def job_rbs(class_name)
  file_path = ""
  worker_name = FileUtils.pwd.split("/").last
  Dir.chdir(Souls.get_mother_path.to_s) do
    singularized_class_name = class_name.underscore.singularize
    file_dir = "./sig/#{worker_name}/app/graphql/queries/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}#{singularized_class_name}.rbs"
    sig_type_path = "./sig/#{worker_name}/app/graphql/types"
    FileUtils.mkdir_p(sig_type_path) unless Dir.exist?(sig_type_path)
    type_file_path = "#{sig_type_path}/#{singularized_class_name}_type.rbs"
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        module Queries
          class #{singularized_class_name.camelize} < BaseQuery
            def self.description: (String) -> untyped
            def self.field: (:response, String, null: false) -> untyped
            def self.type: (untyped, null: false) -> untyped
          end
        end
      TEXT
    end
    File.open(type_file_path, "w") do |f|
      f.write(<<~TEXT)
        module Types
          class #{singularized_class_name.camelize}Type < BaseObject
            def self.field: (:response, String, null: true) -> untyped
          end
        end
      TEXT
    end
    Souls::Painter.create_file(file_path.to_s)
    puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [type_file_path.to_s, :white] }])
  end
  file_path
end

#manager(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/manager.rb', line 5

def manager(class_name)
  singularized_class_name = class_name.underscore.singularize
  current_path = FileUtils.pwd
  unless current_path.split("/").last == "api" || current_path.split("/").last == "souls"
    raise(StandardError, "You Are at Wrong Directory! Please Go to Api Directory!")
  end

  create_manager(class_name, options[:mutation])
  Souls::Generate.new.invoke(:manager_rbs, [singularized_class_name], { mutation: options[:mutation] })
  Souls::Generate.new.invoke(:rspec_manager, [singularized_class_name], { mutation: options[:mutation] })
end

#manager_rbs(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/souls/cli/generate/manager_rbs.rb', line 5

def manager_rbs(class_name)
  file_path = ""
  singularized_class_name = class_name.underscore.singularize
  Dir.chdir(Souls.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/mutations/managers/#{singularized_class_name}_manager"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{options[:mutation]}.rbs"
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        module Mutations
          module Managers
            module #{singularized_class_name.camelize}Manager < BaseMutation
              class #{options[:mutation].singularize.camelize}
                def self.description: (String)-> untyped
                def self.argument: (untyped, untyped, untyped)-> untyped
                def self.field: (untyped, untyped, untyped)-> untyped
              end
            end
          end
        end
      TEXT
      Souls::Painter.create_file(file_path.to_s)
    end
  end
  file_path
end

#mutation(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/mutation.rb', line 4

def mutation(class_name)
  singularized_class_name = class_name.singularize

  Dir.chdir(Souls.get_api_path.to_s) do
    file_dir = "./app/graphql/mutations/base"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "./app/graphql/mutations/base/#{singularized_class_name}/create_#{singularized_class_name}.rb"
    return "Mutation already exist! #{file_path}" if File.exist?(file_path)

    create_mutation(class_name: singularized_class_name)
    update_mutation(class_name: singularized_class_name)
    delete_mutation(class_name: singularized_class_name)
    destroy_delete_mutation(class_name: singularized_class_name)
  end
end

#mutation_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/souls/cli/generate/mutation_rbs.rb', line 4

def mutation_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  create_rbs_mutation(class_name: singularized_class_name)
  update_rbs_mutation(class_name: singularized_class_name)
  delete_rbs_mutation(class_name: singularized_class_name)
  destroy_delete_rbs_mutation(class_name: singularized_class_name)
end

#query(class_name) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/souls/cli/generate/query.rb', line 4

def query(class_name)
  file_dir = "./app/graphql/queries/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.singularize
  create_individual_query(class_name: singularized_class_name)
  create_index_query(class_name: singularized_class_name)
end

#query_rbs(class_name) ⇒ Object



4
5
6
7
# File 'lib/souls/cli/generate/query_rbs.rb', line 4

def query_rbs(class_name)
  single_query_rbs(class_name)
  queries_rbs(class_name)
end

#resolver(class_name) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/souls/cli/generate/resolver.rb', line 4

def resolver(class_name)
  singularized_class_name = class_name.singularize.underscore
  file_path = "./app/graphql/resolvers/#{singularized_class_name}_search.rb"
  raise(StandardError, "Resolver already exist! #{file_path}") if File.exist?(file_path)

  resolver_head(class_name: singularized_class_name)
  resolver_params(class_name: singularized_class_name)
  resolver_after_params(class_name: singularized_class_name)
  resolver_before_end(class_name: singularized_class_name)
  resolver_end(class_name: singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#resolver_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/souls/cli/generate/resolver_rbs.rb', line 4

def resolver_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_path = ""
  Dir.chdir(Souls.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/resolvers"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{singularized_class_name}_search.rbs"
    raise(Thor::Error, "Mutation RBS already exist! #{file_path}") if File.exist?(file_path)

    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        class BaseResolver
        end
        class #{singularized_class_name.camelize}Search < BaseResolver
          include SearchObject
          def self.scope: () ?{ () -> nil } -> [Hash[Symbol, untyped]]
          def self.type: (*untyped) -> String
          def self.option: (:filter, type: untyped, with: :apply_filter) -> String
          def self.description: (String) -> String
          def self.types: (*untyped) -> String
          def decode_global_key: (String value) -> Integer
          def apply_filter: (untyped scope, untyped value) -> untyped

          class #{singularized_class_name.camelize}Filter < Souls::Types::BaseInputObject
            String: String
            Boolean: Boolean
            Integer: Integer
          end
        end
      TEXT
    end
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_factory(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/souls/cli/generate/rspec_factory.rb', line 4

def rspec_factory(class_name)
  file_path = "./spec/factories/#{class_name.pluralize}.rb"
  return "RspecFactory already exist! #{file_path}" if File.exist?(file_path)

  singularized_class_name = class_name.singularize
  rspec_factory_head(class_name: singularized_class_name)
  rspec_factory_params(class_name: singularized_class_name)
  rspec_factory_end(class_name: singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_job(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/souls/cli/generate/rspec_job.rb', line 5

def rspec_job(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_dir = "./spec/queries/jobs/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "#{file_dir}/#{singularized_class_name}_spec.rb"
  return "RspecJob already exist! #{file_path}" if File.exist?(file_path)

  if options[:mailer]
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        RSpec.describe("#{singularized_class_name.camelize}") do
          describe "Define #{singularized_class_name.camelize}" do

            let(:query) do
              %(query {
                #{singularized_class_name.camelize(:lower)} {
                    response
                  }
                }
              )
            end

            subject(:result) do
              SoulsApiSchema.execute(query).as_json
            end

            it "return #{singularized_class_name.camelize} response" do
              stub_request(:post, "https://api.mailgun.net/v3/YOUR-MAILGUN-DOMAIN/messages")
                .to_return(status: 200, body: "", headers: {})
        #{'      '}
              a1 = result.dig("data", "#{singularized_class_name.camelize(:lower)}")
              expect(a1).not_to be_empty
              expect(a1).to(include("response" => be_a(String)))
            end
          end
        end
      TEXT
    end
  else
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        RSpec.describe("#{singularized_class_name.camelize}") do
          describe "Define #{singularized_class_name.camelize}" do

            let(:query) do
              %(query {
                #{singularized_class_name.camelize(:lower)} {
                    response
                  }
                }
              )
            end

            subject(:result) do
              SoulsApiSchema.execute(query).as_json
            end

            it "return #{singularized_class_name.camelize} response" do
              a1 = result.dig("data", "#{singularized_class_name.camelize(:lower)}")
              expect(a1).not_to be_empty
              expect(a1).to(include("response" => be_a(String)))
            end
          end
        end
      TEXT
    end
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_manager(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
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
43
44
45
# File 'lib/souls/cli/generate/rspec_manager.rb', line 5

def rspec_manager(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_dir = "./spec/managers/#{singularized_class_name}_manager"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "#{file_dir}/#{options[:mutation]}_spec.rb"
  return "RspecManager already exist! #{file_path}" if File.exist?(file_path)

  File.open(file_path, "w") do |f|
    f.write(<<~TEXT)
      RSpec.describe("#{options[:mutation].singularize.camelize}") do
        describe "Define #{options[:mutation].singularize.camelize}" do

          let(:mutation) do
            %(mutation {
              #{options[:mutation].singularize.camelize(:lower)}(input: {}) {
                  response
                }
              }
            )
          end

          subject(:result) do
            SoulsApiSchema.execute(mutation).as_json
          end

          it "return User response" do
            begin
              a1 = result.dig("data", "#{options[:mutation].singularize.camelize(:lower)}", "response")
              raise unless a1.present?
            rescue StandardError
              raise(StandardError, result)
            end
            expect(a1).to(include("response" => be_a(String)))
          end
        end
      end
    TEXT
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_mutation(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/rspec_mutation.rb', line 4

def rspec_mutation(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/mutations/base/#{singularized_class_name}_spec.rb"
  return "RspecMutation already exist! #{file_path}" if File.exist?(file_path)

  rspec_mutation_head(class_name: singularized_class_name)
  rspec_mutation_after_head(class_name: singularized_class_name)
  rspec_mutation_params(class_name: singularized_class_name)
  rspec_mutation_params_response(class_name: singularized_class_name)
  rspec_mutation_end(class_name: singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
rescue StandardError => e
  raise(StandardError, e)
end

#rspec_query(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/rspec_query.rb', line 4

def rspec_query(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/queries/#{singularized_class_name}_spec.rb"
  return "RspecQuery already exist! #{file_path}" if File.exist?(file_path)

  rspec_query_head(singularized_class_name)
  rspec_query_after_head(singularized_class_name)
  rspec_query_params(singularized_class_name)
  rspec_query_end(singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_resolver(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/rspec_resolver.rb', line 4

def rspec_resolver(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/resolvers/#{singularized_class_name}_search_spec.rb"
  return "Resolver already exist! #{file_path}" if File.exist?(file_path)

  rspec_resolver_head(singularized_class_name)
  rspec_resolver_after_head(singularized_class_name)
  rspec_resolver_params(singularized_class_name)
  rspec_resolver_end(singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#scaffold(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/souls/cli/generate/application.rb', line 5

def scaffold(class_name)
  singularized_class_name = class_name.singularize
  if options[:rbs]
    run_rbs_scaffold(singularized_class_name)
  else
    run_scaffold(singularized_class_name)
  end
  true
end

#scaffold_allObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/souls/cli/generate/application.rb', line 17

def scaffold_all
  puts(Paint["Let's Go SOULs AUTO CRUD Assist!\n", :cyan])
  Souls.get_tables.each do |table|
    if options[:rbs]
      Souls::Generate.new.invoke(:scaffold, [table.singularize], { rbs: options[:rbs] })
    else
      Souls::Generate.new.invoke(:scaffold, [table.singularize], {})
    end
  end
  true
end

#type(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/souls/cli/generate/type.rb', line 4

def type(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./app/graphql/types/#{singularized_class_name}_type.rb"
  return "Type already exist! #{file_path}" if File.exist?(file_path)

  create_type_head(class_name: singularized_class_name)
  create_type_params(class_name: singularized_class_name)
  create_type_end(class_name: singularized_class_name)
  Souls::Painter.create_file(file_path.to_s)
  file_path
end

#type_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/souls/cli/generate/type_rbs.rb', line 4

def type_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_path = ""
  Dir.chdir(Souls.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{singularized_class_name}_type.rbs"
    raise(Thor::Error, "Type RBS already exist! #{file_path}") if File.exist?(file_path)

    params = Souls.get_relation_params(class_name: singularized_class_name)
    File.open(file_path, "w") do |f|
      f.write(<<~TEXT)
        module Types
          class #{singularized_class_name.camelize}Type < BaseObject
            def self.implements: (*untyped) -> untyped
            def self.global_id_field: (:id) -> String
      TEXT
    end
    File.open(file_path, "a") do |f|
      params[:params].each_with_index do |param, i|
        type = Souls.type_check(param[:type])
        type = "[#{type}]" if param[:array]
        rbs_type = Souls.rbs_type_check(param[:type])
        if i.zero?
          if param[:column_name].match?(/$*_id\z/)
            col_name = param[:column_name].gsub("_id", "")
            f.write("    def self.field: (:#{col_name}, untyped, null: false) -> untyped\n")
          else
            f.write("    def self.field: (:#{param[:column_name]}, #{type}, null: true) -> #{rbs_type}\n")
          end
        elsif param[:column_name].match?(/$*_id\z/)
          col_name = param[:column_name].gsub("_id", "")
          f.write("                  | (:#{col_name}, untyped, null: false) -> untyped\n")
        else
          f.write("                  | (:#{param[:column_name]}, #{type}, null: true) -> #{rbs_type}\n")
        end
      end
    end

    File.open(file_path, "a") do |f|
      f.write(<<~TEXT)
          def self.edge_type: () -> untyped
            def self.connection_type: () -> untyped
          end
        end
      TEXT
    end
  end
  Souls::Painter.create_file(file_path.to_s)
  file_path
end