Class: Trokko::Generate

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/trokko/cli.rb

Overview

Generate rails application

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



21
22
23
# File 'lib/trokko/cli.rb', line 21

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#ask_project_nameObject

Raises:

  • (Thor::InvocationError)


25
26
27
28
29
30
31
32
# File 'lib/trokko/cli.rb', line 25

def ask_project_name
  @name = ask('Project name:') unless name
  return unless @name.empty?

  CLI.command_help(shell, 'generate')
  say 'Stop executing task', :red
  raise Thor::InvocationError, '[ERROR] Project name is required'
end

#check_directoryObject

Raises:

  • (Thor::InvocationError)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/trokko/cli.rb', line 45

def check_directory
  say 'check directory...', :yellow
  say File.exist?(name)
  return unless File.exist?(name)
  FileUtils.remove_dir(name) and return if force

  say 'Stop executing task', :red
  raise Thor::InvocationError,
        "[ERROR] Directory '#{name}' already exists. " \
        "If you are sure executing is OK, specify the '--force' option. The directory will be removed."
end

#confirmObject

Raises:

  • (Thor::InvocationError)


34
35
36
37
38
39
40
41
42
43
# File 'lib/trokko/cli.rb', line 34

def confirm
  say "- Project name: #{name}", :cyan
  say "- Ruby version: #{ruby_version}", :cyan
  say "- Database: #{db}", :cyan

  return if ask('Are you sure to execute the settings described above?', limited_to: %w[y n]) == 'y'

  say 'Stop executing task', :red
  raise Thor::InvocationError, 'You choose *NOT* to execute.'
end

#dockerObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/trokko/cli.rb', line 104

def docker
  return if skip_build

  say 'Building docker image...', :yellow
  inside name do
    next if system('docker compose build')

    say 'Stop executing task', :red
    raise Thor::InvocationError, '[ERROR] Failed to build docker image'
  end
end

#docker_composeObject



71
72
73
74
# File 'lib/trokko/cli.rb', line 71

def docker_compose
  say 'Generating docker-compose.yml...', :yellow
  Scaffolders::DockerCompose.new(db: db, thor: self).generate
end

#dockerfileObject



57
58
59
60
61
# File 'lib/trokko/cli.rb', line 57

def dockerfile
  say 'Generating Dockerfile...', :yellow
  Scaffolders::Dockerfile.new(ruby_version: ruby_version, db: db, thor: self).generate
  Scaffolders::Entrypoint.new(thor: self).generate
end

#finishedObject



116
117
118
# File 'lib/trokko/cli.rb', line 116

def finished
  say 'Finished!', :green
end

#gemfileObject



63
64
65
66
67
68
69
# File 'lib/trokko/cli.rb', line 63

def gemfile
  say 'Generating Gemfile...', :yellow
  Scaffolders::Gemfile.new(rails_version: '7.0.0', thor: self).generate
  inside name do
    FileUtils.touch 'Gemfile.lock'
  end
end

#post_rails_newObject



99
100
101
102
# File 'lib/trokko/cli.rb', line 99

def post_rails_new
  say 'Generating database.yml...', :yellow
  Scaffolders::Config::Database.new(db: db, thor: self).generate
end

#rails_newObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/trokko/cli.rb', line 76

def rails_new
  say 'Generating rails application...', :yellow
  inside name do
    unless system(
      "docker compose run --no-deps --entrypoint '' --rm app" \
      " bundle exec rails new . --database=#{db} --force"
    )
      say 'Stop executing task', :red
      raise Thor::InvocationError, '[ERROR] Failed to generate rails application'
    end

    next if File.stat('config').uid == uid

    unless system(
      "docker compose run --no-deps --entrypoint '' --rm app" \
      " chown -R #{uid}:#{gid} ."
    )
      say 'Stop executing task', :red
      raise Thor::InvocationError, '[ERROR] Failed to change owner of generated files'
    end
  end
end