Class: SashimiTanpopo::DSL::EvalContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sashimi_tanpopo/dsl.rb,
sig/sashimi_tanpopo/dsl.rbs

Instance Method Summary collapse

Constructor Details

#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:, changed_files:) ⇒ EvalContext

Returns a new instance of EvalContext.

Parameters:

  • params (Hash<Symbol, String>)
  • dry_run (Boolean)
  • is_colored (Boolean)

    Whether show color diff

  • target_dir (String)
  • is_update_local (Boolean)

    Whether update local file in update_file

  • changed_files (Hash<String, { before_content: String, after_content: String, mode: String }>)

    key: file path, value: Hash



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_filesHash<String, { before_content: String, after_content: String, mode: String }>

Returns key: file path, value: Hash.

Examples:

{
  "path/to/changed-file.txt" => {
    before_content: "foo",
    after_content:  "bar",
    mode:           "100644",
  }
}

Returns:

  • (Hash<String, { before_content: String, after_content: String, mode: String }>)

    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.

Parameters:

  • path (String)

Yields:

Yield Parameters:

  • content (String)

    Content of file. If content is changed in block, file will be changed.

  • arg0 (String)

Yield Returns:

  • (void)


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.

Examples:

unless dry_run?
  puts "This will be called when apply mode"
end

Returns:

  • (Boolean)

    Whether dry run



117
118
119
# File 'lib/sashimi_tanpopo/dsl.rb', line 117

def dry_run?
  @__dry_run__
end

#glob_pattern?(pattern) ⇒ Boolean

Parameters:

  • pattern (String)

Returns:

  • (Boolean)


236
237
238
# File 'lib/sashimi_tanpopo/dsl.rb', line 236

def glob_pattern?(pattern)
  ["*", "?", "[", "]", "{", "}", "**/"].any? { |str| pattern.include?(str) }
end

#paramsHash<Symbol, String>

passed from --params

Examples:

Pass params via --params

sashimi_tanpopo local --params name:sue445 --params lang:ja recipe.rb

within recipe.rb

# recipe.rb

params
#=> {name: "sue445", lang: "ja"}

Returns:

  • (Hash<Symbol, String>)


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.

Parameters:

  • str1 (String)
  • str2 (String)


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

Examples:

Update single file if exists

update_file "test.txt" do |content|
  content.gsub!("name", params[:name])
end

Update multiple files if exists

update_file ".github/workflows/*.yml" do |content|
  content.gsub!(/ruby-version: "(.+)"/, %Q{ruby-version: "#{params[:ruby_version]}"})
end

Create new file if file doesn't exist

update_file "new_file.txt", create: true do |content|
  # content
  # # => ""

  content.replace("My name is " + params[:name])
end

Parameters:

  • pattern (String)

    Path to target file (relative path from --target-dir). This supports Dir.glob pattern. (e.g. .github/workflows/*.yml)

  • create (Boolean) (defaults to: false)

    Whether create new file if file doesn't exist

  • create: (Boolean) (defaults to: false)

Yields:

Yield Parameters:

  • content (String)

    Content of file. If content is changed in block, file will be changed.

  • arg0 (String)

Yield Returns:

  • (void)


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.

Parameters:

  • pattern (String)

Yields:

Yield Parameters:

  • content (String)

    Content of file. If content is changed in block, file will be changed.

  • arg0 (String)

Yield Returns:

  • (void)


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?

Parameters:

  • content (String)

Yields:

Yield Parameters:

  • content (String)

    content of file

  • arg0 (String)

Yield Returns:

  • (void)

Returns:

  • (String)

    Content of changed file if file is changed

  • (nil)

    file isn't changed



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