class Md2site::Init

initサブコマンドクラス

Attributes

conf_file[R]

構成ファイルのパス

return [String]

hs[R]

ハッシュ

return [Hash]

template_dir[R]

テンプレートディレクトリ

return [String]

Public Class Methods

new(src_root_dir, mes, verbose) click to toggle source

初期化

@param src_root_dir [String] コピー元ルートディレクトリ @param mes [Messagex] Messagexクラスのインスタンス @param verbose [Boolean] FileUtilsクラスのメソッドのverbose引数に与える値

# File lib/md2site/init.rb, line 31
def initialize(src_root_dir, mes, verbose)
  @mes = mes
  @verbose = verbose
  @src_root_dir = src_root_dir

  @conf_file = CONF_FILE
  @conf_dir = CONF_DIR
  @template_dir = TEMPLATE_DIR
  @root_output_dir = OUTPUT_DIR
  @hs = {
    "rootOutputDir" => @root_output_dir,
    "templateDir" => @template_dir,
    "confDir" => @conf_dir,
    "rootSrcDir" => SRC_DIR,
    "statusFile" => STATUS_FILE,
    "siteFile" => SITE_FILE,
    "settingfile" => SETTING_FILE,
    "dataDir" => DATA_DIR,
    "workDir" => WORK_DIR,
    "materialDir" => MATERIAL_DIR,
    "categoryConfPrefix" => CATEGORY_CONF_PREFIX,
    "defaultTableTampleteFile" => DEFAULT_TABLE_TEMPLATE_FILE,
  }
end

Public Instance Methods

execute_subcommand(option, option_url) click to toggle source

サブコマンド実行

@param option [Struct] オプションストラクト @param option_url [Struct] URLオプションストラクト @return [void]

# File lib/md2site/init.rb, line 62
def execute_subcommand(option, option_url)
  root_dir = option.value
  unless File.exist?(root_dir)
    @mes.exc_make_directory(root_dir) { FileUtils.mkdir_p(root_dir, { verbose: @verbose }) }
  end
  absolute_path_root = File.absolute_path(option.value)

  if option_url
    url = %Q(URL=#{option_url.value})
  else
    url = DEFAULT_URL_SETTING
  end

  @hs[KEY_URL] = url

  td = Testdata.new(@src_root_dir, @template_dir, hs)

  copy_all_files(@src_root_dir, @conf_dir, absolute_path_root, @conf_dir)
  testdata_dir_array = td.testdata_dir_array
  testdata_dir_array.map {|dir| copy_all_files(@src_root_dir, dir, absolute_path_root, @conf_dir) }
  template_dir_array = td.template_dir_array
  copy_templatefile(@src_root_dir, template_dir_array, absolute_path_root, @template_dir)
  files = expand_and_write_files(@src_root_dir, @conf_dir, absolute_path_root, [@conf_file], @hs)
  root_output_path = File.join(absolute_path_root, @root_output_dir)
  @mes.exc_make_directory(root_output_path) { FileUtils.mkdir_p(root_output_path, { verbose: @verbose }) }
  @env = Env.new(files[0], @mes, @verbose)
end

Private Instance Methods

copy_all_files(src_root_dir, src_dir, dest_root_dir, dest_dir) click to toggle source

コピー元のファイル群をコピー先ディレクトリにコピーする

@param src_root_dir [String] コピー元ルートディレクトリ @param src_dir [String] コピー元ディレクトリ @param dest_root_dir [String] コピー先ルートディレクトリ @param dest_dir [String] コピー先ディレクトリ @return [void]

# File lib/md2site/init.rb, line 133
def copy_all_files(src_root_dir, src_dir, dest_root_dir, dest_dir)
  dir = File.join(src_root_dir, src_dir)
  files = Dir[%Q(#{dir}/*)].map do |x|
    fname = File.basename(x)
    dest_path = File.join(dest_root_dir, dest_dir, fname)
    dest_dir_path = File.dirname(dest_path)
    @mes.exc_make_directory(dest_dir_path) { FileUtils.mkdir_p(dest_dir_path, { verbose: @verbose }) }
    @mes.exc_file_copy(x, dest_path) { FileUtils.cp(x, dest_path, { verbose: @verbose }) }
    dest_path
  end
  files
end
copy_templatefile(src_root_dir, src_dir_array, dest_root_dir, dest_dir) click to toggle source

コピー元ディレクトリ下のファイル群をコピー先ディレクトリにコピーする

@param src_root_dir [String] コピー元ルートディレクトリ @param src_dir_array [Array<String>] コピー元ディレクトリ群 @param dest_root_dir [String] コピー先ルートディレクトリ @param dest_dir [String] コピー先ディレクトリ @return [void]

# File lib/md2site/init.rb, line 121
def copy_templatefile(src_root_dir, src_dir_array, dest_root_dir, dest_dir)
  src_dir_array.map {|x| copy_all_files(src_root_dir, x, dest_root_dir, dest_dir) }.flatten
end
expand_and_write_files(src_root_dir, src_dir, dest_root_dir, files, hash) click to toggle source

読込元のファイル群を書込先ディレクトリに変数展開して書き込む

@param src_root_dir [String] 読込元ルートディレクトリ @param src_dir [String] 読込元ディレクトリ @param dest_root_dir [String] 書込先ルートディレクトリ @param files [Array<String>] 変数展開対象ファイル群 @param hash [Hash] 置換用ハッシュ @return [Array<String>] 書き込んだだファイルへのパスの配列

# File lib/md2site/init.rb, line 101
def expand_and_write_files(src_root_dir, src_dir, dest_root_dir, files, hash)
  files.map do |fname|
    src_path = File.join(src_root_dir, src_dir, fname)
    content = Filex::Filex.check_and_expand_file(src_path, hash, @mes)
    dest_dir = File.join(dest_root_dir, src_dir)
    @mes.exc_make_directory(dest_dir) { FileUtils.mkdir_p(dest_dir, { verbose: @verbose }) }
    dest_path = File.join(dest_dir, fname)
    @mes.exc_file_write(dest_path) { File.open(dest_path, "w") {|f| f.puts(content) } }
    dest_path
  end
end