RubyTapas #129 - #133
129 Rake
★★
知っている部分は多かった
知らなかった部分は file method
file html_file => md_file do
sh "pandoc -o #{html_file} #{md_file}"
end
dependencyも指定できる
指定のファイル/ディレクトリがあれば実行しないタスクを作る Rakeの基本的な使い方まとめ - うなの日記
他に、directory (mkdir_p), rule, require 'rake/clean', CLOBBER, rdoc
130 Rake File lists
★★
git ls-filesを元にファイルを無視する
files = Rake::FileList["**/*.md", "**/*.markdown"]
files.exclude("~*")
files.exclude(/^scratch\//)
files.exclude do |f|
`git ls-files #{f}`.empty?
end
これすごい拡張子一気に変更する
files.ext(".html")
131 Rake Rules
★★
知らなかった
$ rake -P
rake default
html
rake html
ch1.html
...
Rake.application.options.trace_rules = true
$ rake
Attempting Rule ch1.html => ch1.md
(ch1.html => ch1.md ... EXIST)
rule
rule ".html" => ->(f){source_for_html(f)} do |t|
.md/.markdown のどちらでも動くようにする
def source_for_html(html_file)
SOURCE_FILES.detect{|f| f.ext('') == html_file.ext('')}
end
132 Rake Pathmap
★★★
SOURCE_FILES.ext('html')
SOURCE_FILES.pathmap("%p")
%p - original path
%f - directory部分省いたのfilename
%n - basename (directory無し)
%d - directory # => [".", ".", ".", "subdir", "."]
%x - extension only # => [".md", ".md", ".md", ".md", ".markdown"]
%X - extension以外 # => ["ch1", "ch3", "ch2", "subdir/appendix", "ch4"]
フラグなど追加
ruby_args = load_paths.pathmap("-I%p")
# => "-Imylibs -Iyourlibs -Isharedlibs"
to_sでspace区切り
load_paths.to_s # => "mylibs yourlibs sharedlibs"
pathの置換
OUTPUT_FILES = SOURCE_FILES.pathmap("%{^sources/,outputs/}X.html")
directory部分
cmd = "mkdir -p #{f.pathmap('%d')}"
cmd # => "mkdir -p outputs/subdir"
133 Rake File Operations
★
dependencyにdirectoryも追加
rule ".html" => [->(f){source_for_html(f)}, "outputs"] do |t|
FileUtilsのものは使える
mkdir_p t.name.pathmap("%d")
rm_rf "outputs"
$ ri FileUtils
Rakeシリーズ知らないこと多かったし、簡単だし良かった