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
75
# 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

  @__diffy_format__ = is_colored ? :color : :text
end

Instance Method Details

#changed_filesHash<String, { before_content: String, after_content: String, mode: String }>

Returns key: file path, value: Hash.

Returns:

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

    key: file path, value: Hash



94
95
96
# File 'lib/sashimi_tanpopo/dsl.rb', line 94

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



104
105
106
# File 'lib/sashimi_tanpopo/dsl.rb', line 104

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


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

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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sashimi_tanpopo/dsl.rb', line 123

def update_file(pattern, &block)
  Dir.glob(pattern).each do |path|
    full_file_path = File.join(@__target_dir__, path)
    before_content = File.read(full_file_path)

    SashimiTanpopo.logger.info "Checking #{full_file_path}"

    after_content = update_single_file(path, &block)

    unless after_content
      SashimiTanpopo.logger.info "#{full_file_path} isn't changed"
      next
    end

    changed_files[path] = {
      before_content: before_content,
      after_content:  after_content,
      mode:           File.stat(full_file_path).mode.to_s(8)
    }

    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