Class: Hephaestus::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Extended by:
Forwardable
Includes:
Actions
Defined in:
lib/hephaestus/app_builder.rb

Instance Method Summary collapse

Methods included from Actions

#action_mailer_asset_host, #action_mailer_host, #configure_environment, #expand_json, #gem, #replace_in_file, #replace_in_files

Instance Method Details

#configure_dev_hostingObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hephaestus/app_builder.rb', line 42

def configure_dev_hosting
  config = <<~EOD
    # Let dev server run on GitHub Codespaces
    config.hosts << /[a-z0-9-]+.githubpreview.dev/

    # Let dev server run on ngrok domains
    config.hosts << /[a-z0-9-]+.ngrok.io/
  EOD

  configure_environment("development", config)
end

#configure_time_formatsObject



119
120
121
# File 'lib/hephaestus/app_builder.rb', line 119

def configure_time_formats
  replace_in_file("config/application.rb", /# config.time_zone = .*/, "config.time_zone = \"UTC\"")
end

#copy_setup_scriptsObject



33
34
35
36
37
38
39
40
# File 'lib/hephaestus/app_builder.rb', line 33

def copy_setup_scripts
  directory(Hephaestus.source_path("script"), "script")
  Dir.glob("script/**/*").each do |file|
    next if File.directory?(file)

    chmod(file, 0o755)
  end
end

#create_github_repo(repo_name) ⇒ Object



123
124
125
# File 'lib/hephaestus/app_builder.rb', line 123

def create_github_repo(repo_name)
  run("gh repo create #{repo_name}", exit_on_failure: false)
end

#gemfileObject

it is, apparently, extraordinarly important to keep this as an erb



21
22
23
# File 'lib/hephaestus/app_builder.rb', line 21

def gemfile
  template("Gemfile.erb", "Gemfile")
end

#gitignoreObject



16
17
18
# File 'lib/hephaestus/app_builder.rb', line 16

def gitignore
  copy_file("hephaestus_gitignore", ".gitignore")
end

#raise_on_delivery_errorsObject



25
26
27
28
29
30
31
# File 'lib/hephaestus/app_builder.rb', line 25

def raise_on_delivery_errors
  replace_in_file(
    "config/environments/development.rb",
    "raise_delivery_errors = false",
    "raise_delivery_errors = true",
  )
end

#readmeObject



12
13
14
# File 'lib/hephaestus/app_builder.rb', line 12

def readme
  template("README.md.erb", "README.md")
end

#remove_config_comment_linesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hephaestus/app_builder.rb', line 127

def remove_config_comment_lines
  config_files = [
    "application.rb",
    "environment.rb",
    "environments/development.rb",
    "environments/production.rb",
    "environments/test.rb",
  ]

  config_files.each do |config_file|
    path = Pathname(destination_root).join("config", config_file)
    source = Actions::StripCommentsAction.call(path.read)

    path.write(source)
  end
end

#remove_routes_comment_linesObject



144
145
146
147
148
149
150
# File 'lib/hephaestus/app_builder.rb', line 144

def remove_routes_comment_lines
  replace_in_file(
    "config/routes.rb",
    /Rails\.application\.routes\.draw do.*end/m,
    "Rails.application.routes.draw do\nend",
  )
end

#replace_gemfileObject



101
102
103
104
105
106
107
108
109
# File 'lib/hephaestus/app_builder.rb', line 101

def replace_gemfile
  template("Gemfile.erb", "Gemfile", force: true) do |content|
    if development_env?
      content.gsub(/gem .hephaestus./) { |s| %(#{s}, path: "#{root_path}") }
    else
      content
    end
  end
end

#replace_generic_variablesObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/hephaestus/app_builder.rb', line 158

def replace_generic_variables
  say(set_color("Replacing generic variable names...", :cyan))
  replace_in_files(destination_root, /App(?!lication)/, short_app_name.capitalize)

  replace_in_files(destination_root, %r{(?<!yetto|sw|plugs\.yetto\.)app(?!lication|/|roximate)}, short_app_name.downcase)
  replace_in_file("test/support/api.rb", "/app/", "/#{short_app_name.downcase}/")

  replace_in_files(destination_root, "PlugApp", app_name.underscore.camelcase)
  replace_in_files(destination_root, "plug-app", app_name.dasherize)
  replace_in_files("#{destination_root}/.github/workflows", "plug-app", app_name.dasherize)
  replace_in_files(destination_root, "PLUG_APP", app_name.underscore.upcase)
  replace_in_files(destination_root, "plug_app", app_name.underscore)

  # these are erroneously changed
  replace_in_file("script/server", "/usr/src/#{short_app_name.downcase}", "/usr/src/app")
  replace_in_file("vendor/fly/fly-production.toml", "#{short_app_name.downcase} = ", "app = ")
  replace_in_file("vendor/fly/fly-staging.toml", "#{short_app_name.downcase} = ", "app =")
end

#restore_gemfileObject



111
112
113
# File 'lib/hephaestus/app_builder.rb', line 111

def restore_gemfile
  replace_in_file("Gemfile", /gem "hephaestus".+/, "gem \"hephaestus\"")
end

#ruby_versionObject



115
116
117
# File 'lib/hephaestus/app_builder.rb', line 115

def ruby_version
  create_file(".ruby-version", "#{Hephaestus::RUBY_VERSION}\n")
end

#setup_asset_hostObject



54
55
56
57
58
59
60
61
62
# File 'lib/hephaestus/app_builder.rb', line 54

def setup_asset_host
  config = <<~EOD
    config.public_file_server.headers = {
        "Cache-Control" => "public, max-age=31557600",
      }
  EOD

  configure_environment("production", config)
end

#setup_background_workerObject



64
65
66
67
68
69
70
71
# File 'lib/hephaestus/app_builder.rb', line 64

def setup_background_worker
  config = <<~EOD
    # Use a real queuing backend for Active Job (and separate queues per environment).
    config.active_job.queue_adapter = :sidekiq
  EOD
  configure_environment("production", config)
  configure_environment("staging", config)
end

#setup_slack_loggerObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/hephaestus/app_builder.rb', line 73

def setup_slack_logger
  config = <<~EOD
    config.after_initialize do
      Rails.logger.broadcast_to(SlackWebhookLogger.logger)
    end
  EOD
  configure_environment("production", config)
  configure_environment("staging", config)
  configure_environment("test", config)
end

#setup_sslObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hephaestus/app_builder.rb', line 84

def setup_ssl
  ssl = <<~SSL
    config.force_ssl = true
    config.ssl_options = { hsts: { subdomains: true, preload: true, expires: 1.year } }
  SSL

  replace_in_file(
    "config/environments/production.rb",
    "# config.force_ssl = true",
    ssl,
  )
end

#setup_staging_environmentObject



97
98
99
# File 'lib/hephaestus/app_builder.rb', line 97

def setup_staging_environment
  FileUtils.cp(File.join(destination_root, "config/environments/production.rb"), File.join(destination_root, "config/environments/staging.rb"))
end

#setup_test_environmentObject



152
153
154
155
156
# File 'lib/hephaestus/app_builder.rb', line 152

def setup_test_environment
  remove_dir("test")
  source = File.join(Hephaestus::AppGenerator.source_root, "test")
  directory(source, "test")
end