Class: ReactManifest::GitignorePatcher

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/react_manifest/gitignore_patcher.rb

Overview

Ensures the generated manifest dir is gitignored and that a committed .keep keeps the directory present on a fresh clone. Idempotent and best-effort: a filesystem error logs a warning and never raises.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods included from Logging

#log_debug, #log_info, #log_warn

Constructor Details

#initialize(config = ReactManifest.configuration) ⇒ GitignorePatcher

Returns a new instance of GitignorePatcher.



12
13
14
# File 'lib/react_manifest/gitignore_patcher.rb', line 12

def initialize(config = ReactManifest.configuration)
  @config = config
end

Instance Method Details

#patch!Object



16
17
18
# File 'lib/react_manifest/gitignore_patcher.rb', line 16

def patch!
  Result.new(appended: ensure_gitignore_entry, keep_created: ensure_keep)
end

#patternObject

Path (relative to Rails.root) that should be ignored, e.g. "app/assets/javascripts/ux_manifests/*.js".



35
36
37
38
39
# File 'lib/react_manifest/gitignore_patcher.rb', line 35

def pattern
  subdir = @config.normalized_manifest_subdir
  base   = subdir.empty? ? @config.output_dir : File.join(@config.output_dir, subdir)
  File.join(base, "*.js")
end

#reconcile!Object

patch! plus a one-time hint to untrack previously-committed manifests when the .gitignore entry was just added (the gem never runs git itself). Returns true iff the .gitignore entry was appended.



23
24
25
26
27
28
29
30
31
# File 'lib/react_manifest/gitignore_patcher.rb', line 23

def reconcile!
  result = patch!
  if result.appended
    base = File.dirname(pattern)
    log_info "If #{base}/*.js were previously committed, untrack them once: " \
             "git rm --cached #{base}/*.js"
  end
  result.appended
end