Class: SashimiTanpopo::DSL::EvalContext

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

Instance Method Summary collapse

Constructor Details

#initialize(params:, dry_run:, is_colored:, target_dir:, is_update_local:) ⇒ 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



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_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



102
103
104
# File 'lib/sashimi_tanpopo/dsl.rb', line 102

def changed_files
  @__changed_files__ ||= {}
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



112
113
114
# File 'lib/sashimi_tanpopo/dsl.rb', line 112

def dry_run?
  @__dry_run__
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>)


88
89
90
# File 'lib/sashimi_tanpopo/dsl.rb', line 88

def params
  @__params__
end

#update_file(pattern) {|content| ... } ⇒ Object

Update files if exists

Examples:

Update single file

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

Update multiple files

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

Parameters:

  • pattern (String)

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

Yield Parameters:

  • content (String)

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



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