Class: Aidp::Metadata::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/metadata/compiler.rb

Overview

Compiles tool metadata into a cached directory structure

Aggregates metadata from all tool files, builds indexes, resolves dependencies, and generates a cached tool_directory.json for fast lookups.

Examples:

Compiling the tool directory

compiler = Compiler.new(directories: [".aidp/skills", ".aidp/templates"])
compiler.compile(output_path: ".aidp/cache/tool_directory.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directories: [], strict: false) ⇒ Compiler

Initialize compiler

Parameters:

  • directories (Array<String>) (defaults to: [])

    Directories to scan

  • strict (Boolean) (defaults to: false)

    Whether to fail on validation errors



26
27
28
29
30
31
32
# File 'lib/aidp/metadata/compiler.rb', line 26

def initialize(directories: [], strict: false)
  @directories = Array(directories)
  @strict = strict
  @tools = []
  @indexes = {}
  @dependency_graph = {}
end

Instance Attribute Details

#dependency_graphObject (readonly)

Compiled directory structure



20
21
22
# File 'lib/aidp/metadata/compiler.rb', line 20

def dependency_graph
  @dependency_graph
end

#indexesObject (readonly)

Compiled directory structure



20
21
22
# File 'lib/aidp/metadata/compiler.rb', line 20

def indexes
  @indexes
end

#toolsObject (readonly)

Compiled directory structure



20
21
22
# File 'lib/aidp/metadata/compiler.rb', line 20

def tools
  @tools
end

Instance Method Details

#build_dependency_graphObject

Build dependency graph



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/aidp/metadata/compiler.rb', line 114

def build_dependency_graph
  Aidp.log_debug("metadata", "Building dependency graph")

  @dependency_graph = {}

  @tools.each do |tool|
    @dependency_graph[tool.id] = {
      dependencies: tool.dependencies,
      dependents: []
    }
  end

  # Build reverse dependencies (dependents)
  @tools.each do |tool|
    tool.dependencies.each do |dep_id|
      next unless @dependency_graph[dep_id]

      @dependency_graph[dep_id][:dependents] << tool.id
    end
  end

  Aidp.log_debug(
    "metadata",
    "Dependency graph built",
    nodes: @dependency_graph.size
  )
end

#build_indexesObject

Build indexes for fast lookups



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aidp/metadata/compiler.rb', line 73

def build_indexes
  Aidp.log_debug("metadata", "Building indexes")

  @indexes = {
    by_id: {},
    by_type: {},
    by_tag: {},
    by_work_unit_type: {}
  }

  @tools.each do |tool|
    # Index by ID
    @indexes[:by_id][tool.id] = tool

    # Index by type
    @indexes[:by_type][tool.type] ||= []
    @indexes[:by_type][tool.type] << tool

    # Index by tags
    tool.applies_to.each do |tag|
      @indexes[:by_tag][tag] ||= []
      @indexes[:by_tag][tag] << tool
    end

    # Index by work unit types
    tool.work_unit_types.each do |wut|
      @indexes[:by_work_unit_type][wut] ||= []
      @indexes[:by_work_unit_type][wut] << tool
    end
  end

  Aidp.log_debug(
    "metadata",
    "Indexes built",
    types: @indexes[:by_type].keys,
    tags: @indexes[:by_tag].keys.size,
    work_unit_types: @indexes[:by_work_unit_type].keys
  )
end

#compile(output_path:) ⇒ Hash

Compile tool directory

Parameters:

  • output_path (String)

    Path to output JSON file

Returns:

  • (Hash)

    Compiled directory structure



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aidp/metadata/compiler.rb', line 38

def compile(output_path:)
  Aidp.log_info("metadata", "Compiling tool directory", directories: @directories, output: output_path)

  # Scan all directories
  scanner = Scanner.new(@directories)
  @tools = scanner.scan_all

  # Validate tools
  validator = Validator.new(@tools)
  validation_results = validator.validate_all

  # Handle validation failures
  handle_validation_results(validation_results)

  # Build indexes and graphs
  build_indexes
  build_dependency_graph

  # Create directory structure
  directory = create_directory_structure

  # Write to file
  write_directory(directory, output_path)

  Aidp.log_info(
    "metadata",
    "Compilation complete",
    tools: @tools.size,
    output: output_path
  )

  directory
end

#create_directory_structureHash

Create directory structure for serialization

Returns:

  • (Hash)

    Directory structure



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aidp/metadata/compiler.rb', line 145

def create_directory_structure
  {
    version: "1.0.0",
    compiled_at: Time.now.iso8601,
    tools: @tools.map(&:to_h),
    indexes: {
      by_type: @indexes[:by_type].transform_values { |tools| tools.map(&:id) },
      by_tag: @indexes[:by_tag].transform_values { |tools| tools.map(&:id) },
      by_work_unit_type: @indexes[:by_work_unit_type].transform_values { |tools| tools.map(&:id) }
    },
    dependency_graph: @dependency_graph,
    statistics: {
      total_tools: @tools.size,
      by_type: @tools.group_by(&:type).transform_values(&:size),
      total_tags: @indexes[:by_tag].size,
      total_work_unit_types: @indexes[:by_work_unit_type].size
    }
  }
end

#handle_validation_results(results) ⇒ Object

Handle validation results

Parameters:

  • results (Array<ValidationResult>)

    Validation results

Raises:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/aidp/metadata/compiler.rb', line 184

def handle_validation_results(results)
  invalid_results = results.reject(&:valid)

  if invalid_results.any?
    Aidp.log_warn(
      "metadata",
      "Validation errors found",
      count: invalid_results.size
    )

    invalid_results.each do |result|
      Aidp.log_error(
        "metadata",
        "Tool validation failed",
        tool_id: result.tool_id,
        file: result.file_path,
        errors: result.errors
      )
    end

    if @strict
      raise Aidp::Errors::ValidationError,
        "#{invalid_results.size} tool(s) failed validation (strict mode enabled)"
    end

    # Remove invalid tools from compilation
    invalid_ids = invalid_results.map(&:tool_id)
    @tools.reject! { |tool| invalid_ids.include?(tool.id) }
  end

  # Log warnings
  results.each do |result|
    next if result.warnings.empty?

    Aidp.log_warn(
      "metadata",
      "Tool validation warnings",
      tool_id: result.tool_id,
      file: result.file_path,
      warnings: result.warnings
    )
  end
end

#write_directory(directory, output_path) ⇒ Object

Write directory to JSON file

Parameters:

  • directory (Hash)

    Directory structure

  • output_path (String)

    Output file path



169
170
171
172
173
174
175
176
177
178
# File 'lib/aidp/metadata/compiler.rb', line 169

def write_directory(directory, output_path)
  # Ensure output directory exists
  output_dir = File.dirname(output_path)
  FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)

  # Write with pretty formatting
  File.write(output_path, JSON.pretty_generate(directory))

  Aidp.log_debug("metadata", "Wrote directory", path: output_path, size: File.size(output_path))
end