Module: Strata::CLI::Utils::ImportManager

Defined in:
lib/strata/cli/utils/import_manager.rb

Overview

Manages imported files and tracks external imports in strata.lock.

Constant Summary collapse

LOCK_FILE =
"strata.lock"

Class Method Summary collapse

Class Method Details

.check_changed_imports(project_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/strata/cli/utils/import_manager.rb', line 69

def check_changed_imports(project_path)
  lock_data = load_lock_file(project_path)
  return [] unless lock_data["imports"]

  changed = []
  lock_data["imports"].each do |entry|
    next unless entry["source"] && File.exist?(entry["source"])

    current_hash = file_hash(entry["source"])
    next unless current_hash != entry["hash"]

    changed << {
      path: entry["path"],
      source: entry["source"],
      old_hash: entry["hash"],
      new_hash: current_hash
    }
  end

  changed
end

.file_hash_public(file_path) ⇒ Object



181
182
183
# File 'lib/strata/cli/utils/import_manager.rb', line 181

def file_hash_public(file_path)
  file_hash(file_path)
end

.generate_import_commit_hash(project_path) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/strata/cli/utils/import_manager.rb', line 116

def generate_import_commit_hash(project_path)
  lock_data = load_lock_file(project_path)
  return nil unless lock_data["imports"]&.any?

  # Create a hash from all import hashes combined
  import_hashes = lock_data["imports"].map { |e| "#{e["path"]}:#{e["hash"]}" }.sort.join("|")
  Digest::SHA256.hexdigest(import_hashes)[0..15]
end

.load_lock_file_public(project_path) ⇒ Object

Public accessors for testing/debugging



176
177
178
# File 'lib/strata/cli/utils/import_manager.rb', line 176

def load_lock_file_public(project_path)
  load_lock_file(project_path)
end

.refresh_external_imports(project_path) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/strata/cli/utils/import_manager.rb', line 91

def refresh_external_imports(project_path)
  lock_data = load_lock_file(project_path)
  return [] unless lock_data["imports"]

  refreshed = []
  lock_data["imports"].each do |entry|
    next unless entry["source"] && File.exist?(entry["source"])

    current_hash = file_hash(entry["source"])
    next unless current_hash != entry["hash"]

    entry["hash"] = current_hash
    entry["imported_at"] = Time.now.utc.iso8601
    refreshed << {
      path: entry["path"],
      source: entry["source"],
      hash: current_hash
    }
  end

  save_lock_file(project_path, lock_data) if refreshed.any?

  refreshed
end

.resolve_with_fallback(import_path, base_dir, project_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strata/cli/utils/import_manager.rb', line 55

def resolve_with_fallback(import_path, base_dir, project_path)
  # Validate path is relative
  validate_import_path(import_path)

  resolved_path = File.expand_path(import_path, base_dir)

  raise Strata::MissingImportError, "Import file not found: #{import_path}" unless File.exist?(resolved_path)

  # Track external imports in lock file
  track_external_import(import_path, resolved_path, project_path)

  resolved_path
end

.track_external_import(import_path, resolved_path, project_path) ⇒ Object



24
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
51
52
53
# File 'lib/strata/cli/utils/import_manager.rb', line 24

def track_external_import(import_path, resolved_path, project_path)
  return resolved_path unless external_import?(resolved_path, project_path)

  lock_data = load_lock_file(project_path)
  current_hash = file_hash(resolved_path)

  # Find existing entry by path (as specified in YAML)
  existing = find_existing_import(import_path, lock_data)

  if existing && existing["hash"] == current_hash
    # Hash unchanged, no update needed
    return resolved_path
  end

  # Update or add entry
  entry = {
    "path" => import_path,
    "source" => resolved_path,
    "hash" => current_hash,
    "imported_at" => Time.now.utc.iso8601
  }

  lock_data["imports"] ||= []
  lock_data["imports"].reject! { |e| e["path"] == import_path }
  lock_data["imports"] << entry

  save_lock_file(project_path, lock_data)

  resolved_path
end

.validate_import_path(import_path) ⇒ Object



18
19
20
21
22
# File 'lib/strata/cli/utils/import_manager.rb', line 18

def validate_import_path(import_path)
  return unless Pathname.new(import_path).absolute?

  raise Strata::InvalidImportPathError, "Import paths must be relative, not absolute: #{import_path}"
end