Class: SashimiTanpopo::DSL::EvalContext
- Inherits:
-
Object
- Object
- SashimiTanpopo::DSL::EvalContext
- Defined in:
- lib/sashimi_tanpopo/dsl.rb
Instance Method Summary collapse
-
#changed_files ⇒ Hash<String, { before_content: String, after_content: String, mode: String }>
Key: file path, value: Hash.
-
#dry_run? ⇒ Boolean
Whether dry run.
-
#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:) ⇒ EvalContext
constructor
A new instance of EvalContext.
-
#params ⇒ Hash<Symbol, String>
passed from
--params. -
#update_file(pattern) {|content| ... } ⇒ Object
Update files if exists.
Constructor Details
#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:) ⇒ EvalContext
Returns a new instance of EvalContext.
68 69 70 71 72 73 74 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 68 def initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:) @__params__ = params @__dry_run__ = dry_run @__target_dir__ = target_dir @__is_update_local__ = is_update_local @__is_colored__ = is_colored end |
Instance Method Details
#changed_files ⇒ Hash<String, { before_content: String, after_content: String, mode: String }>
Returns key: file path, value: Hash.
102 103 104 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 102 def changed_files @__changed_files__ ||= {} end |
#dry_run? ⇒ Boolean
Returns Whether dry run.
112 113 114 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 112 def dry_run? @__dry_run__ end |
#params ⇒ Hash<Symbol, String>
passed from --params
88 89 90 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 88 def params @__params__ end |
#update_file(pattern) {|content| ... } ⇒ Object
Update files if exists
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 131 def update_file(pattern, &block) Dir.glob(pattern).each do |path| full_file_path = File.join(@__target_dir__, path) next unless File.exist?(full_file_path) before_content = if changed_files[path] changed_files[path][:after_content] else File.read(full_file_path) end SashimiTanpopo.logger.info "Checking #{full_file_path}" after_content = update_single_file(before_content, &block) unless after_content SashimiTanpopo.logger.info "#{full_file_path} isn't changed" next end File.write(full_file_path, after_content) if !dry_run? && @__is_update_local__ if changed_files[path] changed_files[path][:after_content] = after_content else changed_files[path] = { before_content: before_content, after_content: after_content, mode: File.stat(full_file_path).mode.to_s(8) } end if dry_run? SashimiTanpopo.logger.info "#{full_file_path} will be changed (dryrun)" else SashimiTanpopo.logger.info "#{full_file_path} is changed" end end end |