20
17

More than 5 years have passed since last update.

Rubyでファイルの内容を書き換えるサンプル(置換する)

Last updated at Posted at 2016-02-22

やりたいこと

複数のアプリケーションを一つのサーバで管理している場合に、同じ修正を複数のアプリケーションに行わないといけない場合がある。
その場合の一括文字修正の処理を書いてみました。

サンプルでは www 配下にある、****/app/views/tasks/index.html ファイルの中身の Time.now を Time.new.utc へ変換すします。

実行方法

#作業したいディレクトリのある場所へ移動。
cd /www

# サンプルファイルの作成
vi replacement.rb

以下の内容をreplacement.rbに記述

replacement.rb

# ruby replacement.rb dir_name で実行

# アプリケーションの設置ディレクトリの取得を行うクラス
class FileGet
  attr_accessor :file_name
  attr_reader :dir_list

  def initialize(dir_name)
    @dir_list  = get_dir(dir_name)
    @file_name = "/app/views/tasks/index.html"
  end

  # directoryを取得
  def get_dir(dir_name)
    Dir.glob("#{dir_name}*/")
  end

  # 置換するファイルのパスを取得
  def get_file_list
    file_path_list = []
    @dir_list.each do |dir|
      file_path       = "#{dir}#{@file_name}"
      file_path_list << file_path if File.exist?(file_path)
    end
    return file_path_list
  end
end

# ファイルの置換を行うクラス
class ReplaceText
  attr_accessor :pattern, :replacement
  def initialize(file_list=[])
    @file_list   = file_list
    @pattern     = /Time\.now$/
    @replacement = "Time.now.utc"
  end

  def replace_files
    @file_list.each { |l| exec(l) }
  end

  private

  def exec(path)
    # 読み取り専用でファイルを開き、内容をbufferに代入
    buffer = File.open(path, "r") { |f| f.read() }

    # バックアップ用ファイルを開いて、バッパを書き込む(バックアップ作成)
    File.open("#{path}.bak" , "w") { |f| f.write(buffer) }

    # bufferの中身を変換
    buffer.gsub!(@pattern, @replacement)

    # bufferを元のファイルに書き込む
    File.open(path, "w") { |f| f.write(buffer) }
  end
end

# アプリケーションの設置ディレクトリの取得
file_get  = FileGet.new("./#{ARGV[0]}/")
file_list = file_get.get_file_list

# 置換の実行
if file_list.count > 0
  r = ReplaceText.new(file_list)
  r.replace_files
end

実行する。

# アプリを設置しているディレクトリを指定
ruby replacement.rb ./
20
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
17