Module: VivlioStarter::CLI::PreProcessCommands::GeneratedAssetCache

Defined in:
lib/vivlio_starter/cli/pre_process/generated_asset_cache.rb

Overview

生成資産の永続キャッシュ(fetch-or-generate + materialize)

Class Method Summary collapse

Class Method Details

.copy_to_workspace(cache, files, out_dir) ⇒ Object



79
80
81
82
83
# File 'lib/vivlio_starter/cli/pre_process/generated_asset_cache.rb', line 79

def copy_to_workspace(cache, files, out_dir)
  FileUtils.mkdir_p(out_dir)
  files.each { FileUtils.cp(File.join(cache, it), File.join(out_dir, it)) }
  true
end

.dir(kind) ⇒ Object

種別のキャッシュディレクトリ(.cache/vs//)。

Parameters:

  • kind (String)

    資産種別('mermaid' / 'showcase' / 'math')



42
# File 'lib/vivlio_starter/cli/pre_process/generated_asset_cache.rb', line 42

def dir(kind) = File.join(Common.cache_dir, kind)

.fetch(kind, files, out_dir:) {|cache_dir| ... } ⇒ Boolean

キャッシュから生成物一式をワークスペースへ写す。無ければブロックで生成する。

Parameters:

  • kind (String)

    資産種別

  • files (Array<String>)

    期待する生成物のファイル名(例: ["#key.svg", "#key.png"])

  • out_dir (String)

    ワークスペース側の配置先

Yield Parameters:

  • cache_dir (String)

    生成物の書き込み先(キャッシュ dir・作成済み)

Yield Returns:

  • (Boolean)

    生成に成功したか(false なら縮退)

Returns:

  • (Boolean)

    生成物一式が out_dir に揃ったか



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vivlio_starter/cli/pre_process/generated_asset_cache.rb', line 52

def fetch(kind, files, out_dir:)
  return true if files.all? { File.exist?(File.join(out_dir, it)) }

  cache = dir(kind)
  unless files.all? { File.exist?(File.join(cache, it)) }
    FileUtils.mkdir_p(cache)
    return false unless yield(cache)
    # 生成側の書き忘れ(成功を返したのにファイルが無い)は縮退として扱う
    return false unless files.all? { File.exist?(File.join(cache, it)) }
  end

  copy_to_workspace(cache, files, out_dir)
end

.materialize(kind, files, out_dir:) ⇒ Boolean

キャッシュに揃っている生成物一式をワークスペースへ写す(生成はしない)。 呼び出し元が dir(kind) へ自前バッチ生成した後の配布に使う。

Returns:

  • (Boolean)

    生成物一式が out_dir に揃ったか(キャッシュ不足は false)



70
71
72
73
74
75
76
77
# File 'lib/vivlio_starter/cli/pre_process/generated_asset_cache.rb', line 70

def materialize(kind, files, out_dir:)
  return true if files.all? { File.exist?(File.join(out_dir, it)) }

  cache = dir(kind)
  return false unless files.all? { File.exist?(File.join(cache, it)) }

  copy_to_workspace(cache, files, out_dir)
end