Class: Canopus::Task::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/task/configuration.rb

Constant Summary collapse

RELATIVE_PATH =
File.join(".canopus", "tasks.jsonc")
MAX_BYTES =
1_048_576
MAX_TASKS =
128
MAX_PROBLEM_MATCHERS =
128
MAX_DEPTH =
20
MAX_COLLECTION_SIZE =
1_024
MAX_STRING_BYTES =
65_536
MAX_EXPANDED_BYTES =
1_048_576
MAX_ENVIRONMENT_BYTES =
1_048_576
VARIABLE =
/\$\{([^{}]+)\}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, environment: ENV) ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
25
26
27
# File 'lib/canopus/task/configuration.rb', line 22

def initialize(root:, environment: ENV)
  @root = canonical_root(root)
  @environment = snapshot_environment(environment)
  @problem_matchers = {}.freeze
  @tasks = load_tasks
end

Instance Attribute Details

#problem_matchers ⇒ Object (readonly)

Returns the value of attribute problem_matchers.



20
21
22
# File 'lib/canopus/task/configuration.rb', line 20

def problem_matchers
  @problem_matchers
end

#root ⇒ Object (readonly)

Returns the value of attribute root.



20
21
22
# File 'lib/canopus/task/configuration.rb', line 20

def root
  @root
end

#tasks ⇒ Object (readonly)

Returns the value of attribute tasks.



20
21
22
# File 'lib/canopus/task/configuration.rb', line 20

def tasks
  @tasks
end

Instance Method Details

#problem_matcher(name) ⇒ Object



29
# File 'lib/canopus/task/configuration.rb', line 29

def problem_matcher(name) = @problem_matchers[name]

#resolve(task, file: nil, line_number: nil, selected_text: nil) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/canopus/task/configuration.rb', line 31

def resolve(task, file: nil, line_number: nil, selected_text: nil)
  source = task.is_a?(String) ? @tasks.find { |item| item["label"] == task } :
    @tasks.find { |item| item.equal?(task) }
  raise Error, "unknown task" unless source

  variables = context(file, line_number, selected_text)
  budget = [MAX_EXPANDED_BYTES]
  labels = @tasks.map do |entry|
    expand_string(entry.fetch("label"), variables, budget).tap do |label|
      bounded_identifier(label, "expanded task label", 256)
    end
  end
  raise Error, "expanded task labels must be unique" unless labels.uniq.length == labels.length

  resolved = expand_value(source, variables, budget)
  bounded_identifier(resolved.fetch("label"), "expanded task label", 256)
  raise Error, "task executable must not be empty" if resolved.fetch("command").first.empty?
  resolved["cwd"] = project_path(resolved.fetch("cwd", @root), "cwd")
  raise Error, "task cwd must be a directory" unless File.directory?(resolved["cwd"])
  freeze_value(resolved)
end