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:, 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

#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

#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

#update_file(pattern, create: false) {|content| ... } ⇒ Object

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

Yield Parameters:

  • content (String)

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



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