Class: Dockhand::Command

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/dockhand/command.rb

Instance Method Summary collapse

Instance Method Details

#install_gemsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dockhand/command.rb', line 59

def install_gems
  # Support for `BUNDLE_ONLY` was recently added in Bundler 2.4.0, but we can
  # support `BUNDLE_ONLY` for older Bundler versions by converting the value
  # to `BUNDLE_WITHOUT` (and updating `BUNDLE_WITH`).
  #
  # TODO Remove this hack when Bundler >= 2.4.0 is more widespread.
  settings = [:without, :with].to_h { |key| [key, Bundler.settings[key].map(&:to_s)] }
  only = Array(Bundler.settings[:only]).join(":").split(/\W/)
  unless only.empty?
    settings[:without] |= Bundler.definition.groups.map(&:to_s) - only
    settings[:with] &= only
  end

  settings.each do |key, values|
    run "bundle config set --local #{key} #{values.join(":").inspect}"
  end
  run "bundle install", env: { "BUNDLE_FROZEN" => "1" }
  FileUtils.rm_rf("#{Bundler.bundle_path}/cache") if options[:clean]
end

#install_nodeObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dockhand/command.rb', line 86

def install_node
  version_file = Dir["{.node-version,.nvmrc}"].first

  if !version_file && !package_json.dig("engines", "node")
    return if options[:optional] && !package_json_path
    raise <<~ERROR
      Missing Node.js version from `.node-version`, `.nvmrc`, or `package.json`.

      You can create a version file by running the following command in the same directory as `package.json`:

        $ node --version > .node-version
    ERROR
  end

  Dir.mktmpdir do |tmp|
    installer = "#{tmp}/n"
    get "https://raw.githubusercontent.com/tj/n/HEAD/bin/n", installer
    FileUtils.chmod("a+x", installer)
    run installer, "lts" if !version_file && !which("node")
    run installer, "auto", env: { "N_PREFIX" => options[:prefix] }
  end
end

#install_node_modulesObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dockhand/command.rb', line 114

def install_node_modules
  lock_file = Dir["{yarn.lock,package-lock.json,pnpm-lock.yaml}"].first

  if !lock_file
    return if options[:optional] && !package_json_path
    raise "Missing Node.js modules lock file (`yarn.lock`, `package-lock.json`, or `pnpm-lock.yaml`)"
  end

  run "npm install --global corepack" unless which("corepack")
  run "corepack enable"

  case lock_file
  when "yarn.lock"
    run "yarn install --frozen-lockfile"
  when "package-lock.json"
    run "npm ci"
  when "pnpm-lock.yaml"
    run "pnpm install --frozen-lockfile"
  end
end

#install_packages(*packages) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dockhand/command.rb', line 24

def install_packages(*packages)
  packages.concat(essential_buildtime_packages) if options[:buildtime]
  packages.concat(gem_buildtime_packages) if options[:gem_buildtime]
  packages.concat(gem_runtime_packages) if options[:gem_runtime]

  unless packages.empty?
    run "apt-get update -qq"
    run "apt-get install --no-install-recommends --yes", *packages
  end

  FileUtils.rm_rf(["/var/cache/apt", "/var/lib/apt"]) if options[:clean]
end

#prepare_rails_appObject



140
141
142
143
144
145
# File 'lib/dockhand/command.rb', line 140

def prepare_rails_app
  Pathname.glob("bin/**/*") { |path| normalize_binstub(path) if path.file? }
  run "bundle exec bootsnap precompile --gemfile app/ lib/" if gem?("bootsnap")
  run "bin/rails assets:precompile", env: secret_key_base_dummy if rake_task?("assets:precompile")
  FileUtils.rm_rf("tmp/cache/assets") if options[:clean]
end

#rails_entrypoint(*args) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/dockhand/command.rb', line 150

def rails_entrypoint(*args)
  if File.exist?("bin/docker-entrypoint")
    exec("bin/docker-entrypoint", *args)
  else
    run "bin/rails db:prepare" if /\brails s(erver)?$/.match?(args[0..1].join(" "))
    exec(*args)
  end
end

#transmute_to_artifacts(*paths) ⇒ Object



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

def transmute_to_artifacts(*paths)
  paths.each do |path|
    path = File.expand_path(path)
    artifacts_dir = File.expand_path(options[:artifacts_dir])
    artifacts_subpath = "#{artifacts_dir}/#{path}"

    FileUtils.mkdir_p(File.dirname(artifacts_subpath))
    FileUtils.mv(path, artifacts_subpath)
    FileUtils.ln_s(artifacts_subpath, path)
  end
end