Class: SashimiTanpopo::DSL::EvalContext
- Inherits:
-
Object
- Object
- SashimiTanpopo::DSL::EvalContext
- Defined in:
- lib/sashimi_tanpopo/dsl.rb,
sig/sashimi_tanpopo/dsl.rbs
Instance Method Summary collapse
-
#changed_files ⇒ Hash<String, { before_content: String, after_content: String, mode: String }>
Key: file path, value: Hash.
- #create_new_file(path) {|content, arg0| ... }
-
#dry_run? ⇒ Boolean
Whether dry run.
- #glob_pattern?(pattern) ⇒ Boolean
-
#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:, changed_files:) ⇒ EvalContext
constructor
A new instance of EvalContext.
-
#params ⇒ Hash<Symbol, String>
passed from
--params. - #show_diff(str1, str2)
-
#update_file(pattern, create: false) {|content, arg0| ... }
Update files.
- #update_file_with_glob(pattern) {|content, arg0| ... }
- #update_single_file(content) {|content, arg0| ... } ⇒ String?
Constructor Details
#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:, changed_files:) ⇒ EvalContext
Returns a new instance of EvalContext.
72 73 74 75 76 77 78 79 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 72 def initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:, changed_files:) @__params__ = params @__dry_run__ = dry_run @__target_dir__ = target_dir @__is_update_local__ = is_update_local @__is_colored__ = is_colored @__changed_files__ = changed_files end |
Instance Method Details
#changed_files ⇒ Hash<String, { before_content: String, after_content: String, mode: String }>
Returns key: file path, value: Hash.
107 108 109 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 107 def changed_files @__changed_files__ end |
#create_new_file(path) {|content, arg0| ... }
This method returns an undefined value.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 198 def create_new_file(path, &block) return if glob_pattern?(path) full_file_path = File.join(@__target_dir__, path) return if File.exist?(full_file_path) SashimiTanpopo.logger.info "Checking #{full_file_path}" before_content = "" after_content = update_single_file(before_content, &block) unless after_content SashimiTanpopo.logger.info "#{full_file_path} isn't created" return 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 created (dryrun)" else SashimiTanpopo.logger.info "#{full_file_path} is created" end end |
#dry_run? ⇒ Boolean
Returns Whether dry run.
117 118 119 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 117 def dry_run? @__dry_run__ end |
#glob_pattern?(pattern) ⇒ Boolean
236 237 238 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 236 def glob_pattern?(pattern) ["*", "?", "[", "]", "{", "}", "**/"].any? { |str| pattern.include?(str) } end |
#params ⇒ Hash<Symbol, String>
passed from --params
93 94 95 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 93 def params @__params__ end |
#show_diff(str1, str2)
This method returns an undefined value.
261 262 263 264 265 266 267 268 269 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 261 def show_diff(str1, str2) diff_text = DiffHelper.generate_diff(str1, str2, is_colored: @__is_colored__) SashimiTanpopo.logger.info "diff:" diff_text.each_line do |line| SashimiTanpopo.logger.info line end end |
#update_file(pattern, create: false) {|content, arg0| ... }
This method returns an undefined value.
Update files
145 146 147 148 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 145 def update_file(pattern, create: false, &block) update_file_with_glob(pattern, &block) create_new_file(pattern, &block) if create end |
#update_file_with_glob(pattern) {|content, arg0| ... }
This method returns an undefined value.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 154 def update_file_with_glob(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 |
#update_single_file(content) {|content, arg0| ... } ⇒ String?
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/sashimi_tanpopo/dsl.rb', line 246 def update_single_file(content) after_content = content.dup yield after_content # File isn't changed return nil if after_content == content show_diff(content, after_content) after_content end |