Module: LittleGhost::Skills::ResourceRoot

Defined in:
lib/little_ghost/skills/resource_root.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.normalize(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/little_ghost/skills/resource_root.rb', line 10

def normalize(value)
  return unless value

  path = value.to_s
  return normalize_workspace_reference(path) if path.start_with?("workspace://")

  unless valid_absolute_path?(path)
    raise ArgumentError, "resource_root must be an absolute path or workspace:// reference"
  end

  File.expand_path(path).freeze
end

.normalize_workspace_reference(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/little_ghost/skills/resource_root.rb', line 23

def normalize_workspace_reference(path)
  logical = path.delete_prefix("workspace://")
  components = logical.split("/", -1)
  name = components.shift
  invalid = name.nil? || !name.match?(/\A[a-zA-Z0-9_-]+\z/) ||
    components.any? { |component| component.empty? || component == "." || component == ".." }
  if invalid || path.include?("\0") || path.include?("\\")
    raise ArgumentError, "resource_root must be an absolute path or workspace:// reference"
  end

  path.dup.freeze
end

.valid_absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/little_ghost/skills/resource_root.rb', line 36

def valid_absolute_path?(path)
  !path.include?("\0") && Pathname.new(path).absolute? &&
    !path.split(File::SEPARATOR).include?("..")
end