Class: RubyCms::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cms/syncer.rb

Overview

Pushes gem-owned files from a host app back into the gem's templates dir. Only files declared in the manifest are touched (app-specific files ignored).

By default sync only UPDATES files the gem already ships. Manifest globs such as helpers/**/* also match app-specific files (a public-site helper, a marketing Stimulus controller) that the gem does not own — absorbing those would pollute the gem. New files are skipped and reported; pass include_new to deliberately pull them in (e.g. when genuinely adding a file to the gem).

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(manifest:, source_root:, templates_root:) ⇒ Syncer

Returns a new instance of Syncer.



21
22
23
24
25
# File 'lib/ruby_cms/syncer.rb', line 21

def initialize(manifest:, source_root:, templates_root:)
  @manifest = manifest
  @source_root = Pathname.new(source_root)
  @templates_root = Pathname.new(templates_root)
end

Instance Method Details

#sync(dry_run: false, include_new: false) ⇒ Object



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
61
62
63
# File 'lib/ruby_cms/syncer.rb', line 27

def sync(dry_run: false, include_new: false)
  changed = []
  unchanged = []
  skipped_new = []
  excluded = []
  host_excludes = Lockfile.new(@source_root).excludes

  template_paths.each do |template_rel|
    app_rel = PathMap.to_app(template_rel)
    if Excludes.excluded?(app_rel, host_excludes)
      excluded << app_rel
      next
    end

    src = @source_root.join(app_rel)
    next unless src.file?

    dest = @templates_root.join(template_rel)
    unless dest.exist? || include_new
      skipped_new << template_rel
      next
    end

    if dest.exist? && dest.binread == src.binread
      unchanged << template_rel
      next
    end

    changed << template_rel
    next if dry_run

    FileUtils.mkdir_p(dest.dirname)
    dest.binwrite(src.binread)
  end

  Result.new(changed:, unchanged:, skipped_new:, excluded:)
end