Module: DocOpsLab::Dev::CastOps

Defined in:
lib/docopslab/dev/cast_ops.rb

Overview

Sync/Cast operations: synchronize canonical blocks from prime templates into project-local target files using Sourcerer::Sync.

Prime templates define canonical (universal-prefixed) blocks. Target files receive those blocks on each sync, preserving all local content. On first-time init the whole prime is rendered and written as the new target.

Configuration is driven by templates: in the project manifest. The list of template-to-target procedures lives under templates.manifest.

Class Method Summary collapse

Class Method Details

.init_cast_targets(context, target_filter: nil) ⇒ Hash{String => Sourcerer::Sync::Cast::CastResult}

Bootstrap new target files from prime templates. Skips targets that already exist (use sync to update those).

Parameters:

  • context (DocOpsLab::Dev)

    caller context

  • target_filter (String, nil) (defaults to: nil)

    restrict to entries whose target matches

Returns:

  • (Hash{String => Sourcerer::Sync::Cast::CastResult})


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/docopslab/dev/cast_ops.rb', line 68

def init_cast_targets context, target_filter: nil
  castings, global_data = load_castings(context, target_filter)
  return {} unless castings

  puts '🆕 Initializing cast targets...'

  results = {}
  castings.each do |entry|
    prime_path = resolve_prime(entry)
    next unless prime_path

    target_path = entry['target']

    if File.exist?(target_path)
      puts "  ⏭️  Skipped #{target_path} (already exists; use labdev:sync:templates to update)"
      next
    end

    data   = build_data(global_data, entry)
    result = Sourcerer::Sync.init(prime_path, target_path, data: data)

    results[target_path] = result
    report_result(result, init: true)
  end

  puts "✅ Initialized #{results.size} cast target(s)"
  results
end

.sync_cast_targets(context, target_filter: nil, dry_run: false) ⇒ Hash{String => Sourcerer::Sync::Cast::CastResult}

Sync canonical blocks from prime templates into all configured target files.

Parameters:

  • context (DocOpsLab::Dev)

    caller context (for manifest access)

  • target_filter (String, nil) (defaults to: nil)

    restrict to entries whose target matches

  • dry_run (Boolean) (defaults to: false)

    compute diff but do not write

Returns:

  • (Hash{String => Sourcerer::Sync::Cast::CastResult})


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
54
55
56
57
58
59
60
# File 'lib/docopslab/dev/cast_ops.rb', line 25

def sync_cast_targets context, target_filter: nil, dry_run: false
  castings, global_data = load_castings(context, target_filter)
  return {} unless castings

  label = dry_run ? '🔍 Dry-run: diffing cast targets...' : '🔄 Syncing cast targets...'
  puts label

  results = {}
  castings.each do |entry|
    prime_path = resolve_prime(entry)
    next unless prime_path

    target_path = entry['target']

    unless File.exist?(target_path)
      puts "  ⚠️  Target not found: #{target_path} (run labdev:init:templates to bootstrap)"
      next
    end

    data             = build_data(global_data, entry)
    canonical_prefix = entry.fetch('canonical_prefix', 'universal-')

    result = Sourcerer::Sync.sync(
      prime_path,
      target_path,
      data: data,
      canonical_prefix: canonical_prefix,
      dry_run: dry_run)

    results[target_path] = result
    report_result(result, dry_run: dry_run)
  end

  puts "✅ Synced #{results.count { |_, r| r.applied_changes.any? }} cast target(s)" unless dry_run
  results
end