lib/generators/dockerfile_generator.rb in dockerfile-rails-1.2.3 vs lib/generators/dockerfile_generator.rb in dockerfile-rails-1.2.4

- old
+ new

@@ -4,18 +4,19 @@ require_relative "../dockerfile-rails/scanner.rb" class DockerfileGenerator < Rails::Generators::Base include DockerfileRails::Scanner - OPTION_DEFAULTS = { + BASE_DEFAULTS = { "bin-cd" => false, "cache" => false, "ci" => false, "compose" => false, "fullstaq" => false, "jemalloc" => false, "label" => {}, + "link" => true, "lock" => true, "mysql" => false, "nginx" => false, "parallel" => false, "platform" => nil, @@ -23,14 +24,17 @@ "precompile" => nil, "prepare" => true, "redis" => false, "root" => false, "sqlite3" => false, + "sudo" => false, "swap" => nil, "yjit" => false, }.then { |hash| Struct.new(*hash.keys.map(&:to_sym)).new(*hash.values) } + OPTION_DEFAULTS = BASE_DEFAULTS.dup + @@labels = {} @@packages = { "base" => [], "build" => [], "deploy" => [] } @@vars = { "base" => {}, "build" => {}, "deploy" => {} } @@args = { "base" => {}, "build" => {}, "deploy" => {} } @@ -66,10 +70,13 @@ end class_option :ci, type: :boolean, default: OPTION_DEFAULTS.ci, desc: "include test gems in bundle" + class_option :link, type: :boolean, default: OPTION_DEFAULTS.lock, + desc: "use COPY --link whenever possible" + class_option :lock, type: :boolean, default: OPTION_DEFAULTS.lock, desc: "lock Gemfile/package.json" class_option :precompile, type: :string, default: OPTION_DEFAULTS.precompile, desc: 'if set to "defer", assets:precompile will be done at deploy time' @@ -123,10 +130,12 @@ desc: "Serve static files with nginx" class_option :root, type: :boolean, default: OPTION_DEFAULTS.root, desc: "Run application as root user" + class_option :sudo, type: :boolean, default: OPTION_DEFAULTS.sudo, + desc: "Install and configure sudo to enable running as rails with full environment" class_option "add-base", type: :array, default: [], desc: "additional packages to install for both build and deploy" class_option "add-build", type: :array, default: [], @@ -210,22 +219,31 @@ template "docker-entrypoint.erb", "bin/docker-entrypoint" chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false template "docker-compose.yml.erb", "docker-compose.yml" if options.compose - template "dockerfile.yml.erb", "config/dockerfile.yml", force: true - if @gemfile.include?("vite_ruby") package = JSON.load_file("package.json") unless package.dig("scripts", "build") package["scripts"] ||= {} - package["scripts"]["build"] ||= "vite build --outDir public" + package["scripts"]["build"] = "vite build --outDir public" say_status :update, "package.json" IO.write("package.json", JSON.pretty_generate(package)) end end + + @dockerfile_config = (@dockerfile_config.to_a - BASE_DEFAULTS.to_h.stringify_keys.to_a).to_h + %w(packages envs args).each do |key| + @dockerfile_config.delete key if @dockerfile_config[key].empty? + end + + if !@dockerfile_config.empty? + template "dockerfile.yml.erb", "config/dockerfile.yml", force: true + elsif File.exist? "config/dockerfile.yml" + remove_file "config/dockerfile.yml" + end end private def render(options) scope = (Class.new do @@ -299,10 +317,14 @@ def install_gems ENV["BUNDLE_IGNORE_MESSAGES"] = "1" gemfile = IO.read("Gemfile") + unless /^\s*source\s/.match?(gemfile) + gemfile = %{source "https://rubygems.org"\n} + gemfile + end + if options.postgresql? || @postgresql system "bundle add pg --skip-install" unless @gemfile.include? "pg" end if options.mysql? || @mysql @@ -341,10 +363,20 @@ system "bundle lock #{add_platforms.join(" ")}" end end end + def base_gems + gems = ["bundler"] + + if options.ci? && options.lock? && @gemfile.include?("debug") + gems += %w(irb reline) - @gemfile + end + + gems.sort + end + def base_packages packages = [] packages += @@packages["base"] if @@packages["base"] if using_execjs? @@ -451,9 +483,12 @@ end end # nginx packages << "nginx" if options.nginx? + + # sudo + packages << "sudo" if options.sudo? packages.sort end def deploy_repos