Module: Pray::ProjectContext

Defined in:
lib/pray/project_context.rb

Class Method Summary collapse

Class Method Details

.canonicalize_path(base, path) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/pray/project_context.rb', line 59

def canonicalize_path(base, path)
  resolved = Pathname(path).absolute? ? Pathname(path) : Pathname(base).join(path)
  resolved = resolved.expand_path
  resolved.exist? ? resolved.realpath.to_s : resolved.cleanpath.to_s
rescue
  resolved.to_s
end

.env_value(key) ⇒ Object



52
53
54
55
56
57
# File 'lib/pray/project_context.rb', line 52

def env_value(key)
  value = ENV[key]
  return nil if value.nil? || value.strip.empty?

  value
end

.from_current_directoryObject



21
22
23
# File 'lib/pray/project_context.rb', line 21

def from_current_directory
  from_options(ProjectInvocationOptions.new)
end

.from_options(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pray/project_context.rb', line 25

def from_options(options)
  cwd = Dir.pwd
  dotenv = Dotenv.load_dotenv_variables(cwd)
  project_root_hint = options.project_root ||
    env_value(ENV_PROJECT_PATH) ||
    dotenv[ENV_PROJECT_PATH] ||
    cwd
  project_root = canonicalize_path(cwd, project_root_hint)
  manifest_hint = options.manifest_path ||
    env_value(ENV_MANIFEST_PATH) ||
    dotenv[ENV_MANIFEST_PATH] ||
    "Prayfile"
  manifest_path = if Pathname(manifest_hint).absolute?
    canonicalize_path(cwd, manifest_hint)
  else
    canonicalize_path(project_root, manifest_hint)
  end
  environment = (options.environment || env_value(ENV_ENVIRONMENT) || dotenv[ENV_ENVIRONMENT])&.strip
  environment = nil if environment.nil? || environment.empty?

  ProjectInvocationContext.new(
    project_root: project_root,
    manifest_path: manifest_path,
    environment: environment
  )
end