Class: RubyCms::Updater

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

Overview

Pulls new versions of gem-owned files into a host app, scoped to the modules recorded in the app's lockfile. Changed-files-only (content compare). Files matching the host's excludes (gem defaults + .ruby_cms.yml exclude:) are never overwritten.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(manifest:, templates_root:, app_root:) ⇒ Updater

Returns a new instance of Updater.



17
18
19
20
21
# File 'lib/ruby_cms/updater.rb', line 17

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

Instance Method Details

#update(dry_run: false, only: nil) ⇒ Object



23
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
54
55
56
# File 'lib/ruby_cms/updater.rb', line 23

def update(dry_run: false, only: nil)
  keys = installed_keys
  keys &= Array(only).map(&:to_sym) if only
  modules = keys.filter_map {|k| @manifest[k] }
  host_excludes = Lockfile.new(@app_root).excludes

  changed = []
  unchanged = []
  excluded = []
  template_paths(modules).each do |template_rel|
    src = @templates_root.join(template_rel)
    next unless src.file?

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

    dest = @app_root.join(app_rel)
    if dest.exist? && dest.binread == src.binread
      unchanged << app_rel
      next
    end

    changed << app_rel
    next if dry_run

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

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