Class: EIVO::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/eivo/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.namespace(name = nil) ⇒ Object



11
12
13
# File 'lib/generators/eivo/install_generator.rb', line 11

def self.namespace(name = nil)
  @namespace ||= super.sub('e_i_v_o', 'eivo')
end

.next_migration_number(dirname) ⇒ Object

Implement the required interface for Rails::Generators::Migration.



16
17
18
19
# File 'lib/generators/eivo/install_generator.rb', line 16

def self.next_migration_number(dirname)
  next_migration_number = current_migration_number(dirname) + 1
  ActiveRecord::Migration.next_migration_number(next_migration_number)
end

Instance Method Details

#create_initializer_fileObject



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
# File 'lib/generators/eivo/install_generator.rb', line 21

def create_initializer_file
  @application = Rails.application.class.module_parent_name

  remove_dir 'config/environments'

  copy_file 'Gemfile'
  copy_file 'Procfile'
  copy_file '.env.example'
  copy_file '.ruby-version'
  copy_file '.rubocop.yml'

  create_restart_files

  template 'config/application.rb'
  copy_file 'config/puma.rb'
  copy_file 'config/database.example.yml'
  template 'config/database.local.yml', 'config/database.yml', skip: true

  copy_file 'app/controllers/application_controller.rb'

  migration_template 'db/migrate/enable_pgcrypto_extension.rb', 'db/migrate/enable_pgcrypto_extension.rb', skip: true
  migration_template 'db/migrate/enable_pg_stat_statements_extension.rb', 'db/migrate/enable_pg_stat_statements_extension.rb', skip: true
  migration_template 'db/migrate/enable_btree_gin_extension.rb', 'db/migrate/enable_btree_gin_extension.rb', skip: true
  migration_template 'db/migrate/enable_unaccent_extension.rb', 'db/migrate/enable_unaccent_extension.rb', skip: true

  route "mount EIVO::Engine => '/'"
end

#create_restart_filesObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/eivo/install_generator.rb', line 49

def create_restart_files
  restart_code = <<~'EIVO_COMMAND'
    ### EIVO begin ###

    bundle exec rails assets:precompile
    bundle exec bin/webpack
    bundle exec pumactl -F config/puma.rb restart

    ### EIVO end ###
  EIVO_COMMAND

  stop_code = <<~'EIVO_COMMAND'
    ### EIVO begin ###

    bundle exec pumactl -F config/puma.rb stop

    ### EIVO end ###
  EIVO_COMMAND

  header_production = <<~'EIVO_COMMAND'
    #!/usr/bin/env bash
    export RACK_ENV="production"
    export RAILS_ENV="production"
    export NODE_ENV="production"
    export RAILS_DAEMONIZE="true"

  EIVO_COMMAND

  header_staging = <<~'EIVO_COMMAND'
    #!/usr/bin/env bash
    export RACK_ENV="staging"
    export RAILS_ENV="staging"
    export NODE_ENV="production"
    export RAILS_DAEMONIZE="true"

  EIVO_COMMAND

  create_file 'restart_production.sh', header_production, skip: true
  create_file 'stop_production.sh', header_production, skip: true
  create_file 'restart_staging.sh', header_staging, skip: true
  create_file 'stop_staging.sh', header_staging, skip: true

  # Remove old code if present
  regexp = /\n### EIVO begin ###.*### EIVO end ###/m
  gsub_file 'restart_production.sh', regexp, ''
  gsub_file 'restart_staging.sh', regexp, ''
  gsub_file 'stop_production.sh', regexp, ''
  gsub_file 'stop_staging.sh', regexp, ''

  # Inject new code
  inject_into_file 'restart_production.sh', restart_code, after: header_production
  inject_into_file 'stop_production.sh', stop_code, after: header_production
  inject_into_file 'restart_staging.sh', restart_code, after: header_staging
  inject_into_file 'stop_staging.sh', stop_code, after: header_staging

  chmod 'restart_production.sh', 0o755
  chmod 'stop_production.sh', 0o755
  chmod 'restart_staging.sh', 0o755
  chmod 'stop_staging.sh', 0o755
end