Class: Wellcar::Commands::New

Inherits:
Base
  • Object
show all
Defined in:
lib/wellcar/commands/new.rb

Instance Attribute Summary

Attributes inherited from Base

#app_name, #domain_name, #github_account, #ruby_version, #use_bundler_2

Instance Method Summary collapse

Methods inherited from Base

#build_development_environment, #clean_docker, #core_docker_templates, #create_directories, #create_wellcar_folder, #write_files

Constructor Details

#initialize(app_name, github_account, domain_name) ⇒ New

Returns a new instance of New.



4
5
6
7
8
9
10
# File 'lib/wellcar/commands/new.rb', line 4

def initialize(app_name, , domain_name)
  self.app_name = app_name
  self.domain_name = domain_name
  self. = 
  self.ruby_version = "2.6.6"
  self.use_bundler_2 = true
end

Instance Method Details

#call(options, args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/wellcar/commands/new.rb', line 57

def call(options, args)
  # Here we want to create a new rails app that is set up with docker.
  # We use `rails new` within a `docker run` command but skip a lot of set up so 
  # that further setup steps can be taken later on.
  # What I don't know right now is how steps that are skipped at the start can be
  # initiated in an existing app.
  #
  # My list of pre-requisites for a Rails app are:
  #   * RSpec
  #   * Webpack + yarn
  #   * Rubocop
  #   * PostgreSQL
  #   * Redis
  #   * FactoryBot
  #   * Capybara with a real webdriver
  #   * Devise

  try_full_nuke options[:full_nuke]

  within_app_folder do
    create_wellcar_folder
    create_directories
    write_files
    clean_docker
    perform_setup
    tidy_up_setup
    build_development_environment
  end
  
  puts <<-FINISHED
    Your new Rails application "#{app_name}" has been created with Docker!

    Don't forget to commit everything to git.

    You can use wellcar to interact with the Docker containers and the Rails application as you develop your app.

    Run `wellcar` for help with commands.
    FINISHED
end

#docker_filesObject



12
13
14
15
16
17
18
19
# File 'lib/wellcar/commands/new.rb', line 12

def docker_files
  @files ||= core_docker_templates.push(
    Wellcar::Templates::DockerfileInit.new(ruby_version, app_name),
    Wellcar::Templates::DockerCompose.new(app_name, 
                                          ,
                                          Wellcar::Templates::DockerCompose::INIT_TEMPLATE)
  )
end

#perform_setupObject



41
42
43
44
45
46
47
# File 'lib/wellcar/commands/new.rb', line 41

def perform_setup
  # Create app and install webpacker
  exit 16 unless system "docker-compose up --build new_rails"
  exit 32 unless system "docker-compose up --build bundle"
  exit 64 unless system "docker-compose up --build install_webpacker"
  puts "Rails app created; initializing for Docker\n"
end

#tidy_up_setupObject



49
50
51
52
53
54
55
# File 'lib/wellcar/commands/new.rb', line 49

def tidy_up_setup
  # remove dockerfile.init and docker-compose.yml with initialisation services
  %w(Dockerfile.init docker-compose.yml config/database.yml).each {|f| File.delete(f) if File.file?(f) }
  # Create docker-compose.yml file
  Wellcar::Templates::DockerCompose.new(app_name, ).write
  Wellcar::Templates::DatabaseYaml.new(app_name).write
end

#try_full_nuke(nuke) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/wellcar/commands/new.rb', line 21

def try_full_nuke(nuke)
  if nuke && Dir.exists?(app_name)
    puts "*** Destroying the existing folder #{app_name} ***"
    FileUtils.rm_rf app_name
  elsif Dir.exists? app_name
    puts "Cannot overwrite existing folder #{Dir.pwd}/#{app_name}"
    exit 1
  end
end

#within_app_folder(&block) ⇒ Object

Raises:

  • (NotImplemented)


31
32
33
34
35
36
37
38
39
# File 'lib/wellcar/commands/new.rb', line 31

def within_app_folder(&block)
  raise NotImplemented if block.nil?

  Dir.mkdir app_name

  Dir.chdir(app_name) do
    yield
  end
end