Class: Kdep::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/path_resolver.rb

Overview

Resolves user-supplied relative file paths from app.yml against either the deploy dir (default) or the repo root that contains kdep/. Mode is selected by the top-level ‘paths_from:` field.

Instance Method Summary collapse

Constructor Details

#initialize(deploy_dir:, kdep_dir:, paths_from:) ⇒ PathResolver

Returns a new instance of PathResolver.



8
9
10
11
12
# File 'lib/kdep/path_resolver.rb', line 8

def initialize(deploy_dir:, kdep_dir:, paths_from:)
  @deploy_dir = deploy_dir
  @repo_root  = File.dirname(kdep_dir)
  @mode       = paths_from.to_s == "repo_root" ? :repo_root : :target
end

Instance Method Details

#exists?(path) ⇒ Boolean

Checks either origin. Used by validators that don’t care WHICH origin holds the file, only that it exists somewhere sensible.

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/kdep/path_resolver.rb', line 39

def exists?(path)
  return false if path.nil?
  return File.exist?(path) if Pathname.new(path).absolute?

  File.exist?(File.expand_path(path, primary_base)) ||
    File.exist?(File.expand_path(path, fallback_base))
end

#resolve(path, warn_on_fallback: false) ⇒ Object

Returns the absolute filesystem path for a relative input. If the resolved path does not exist at the configured origin but DOES exist at the other origin, resolves at the other and optionally warns — this catches the common “forgot paths_from: repo_root” mistake.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kdep/path_resolver.rb', line 18

def resolve(path, warn_on_fallback: false)
  return nil if path.nil?
  return path if Pathname.new(path).absolute?

  primary = File.expand_path(path, primary_base)
  return primary if File.exist?(primary)

  fallback = File.expand_path(path, fallback_base)
  if File.exist?(fallback)
    if warn_on_fallback
      warn "path '#{path}' not found under #{@mode}; falling back to #{other_mode}. " \
           "Set paths_from: #{other_mode} or fix the path."
    end
    return fallback
  end

  primary
end