LoginSignup
10
0

More than 3 years have passed since last update.

Rakeで自動化&Export

Last updated at Posted at 2020-11-18

Document Links

Rake

Rakeってなんぞや

わからないので,とりあえず調べて,有志の力を借りる.

  • RakeはMakeによく似た機能を持つRubyで書かれたシンプルなビルドツール
  • makeコマンドのRubyでの代替,Rakefileにtaskを記述していく

とかなんとか.

詳しくは,

講義資料を見ると,

前回のbundlerを少し思い出してください.

$ rake install:local

とすると自動的にhogehogeがinstallされましたよね.

と記述されている...私は上手くいかなかったが,まあ,とりあえず進めよう.

上述したが,Rakefileにtaskを記入していくと,

$ emacs -nw Rakefile

で,Rakefile内に以下を記述.

task :default do
  system 'rake -T'
  exit
end

desc 'hello NAME'
task :hello do
  name = ARGV[1]
  puts "Hello #{name}!"
  exit
end

実行結果

$ rake
rake hello  # hello NAME

rakeって打つと,Rakefileに記述したtaskを教えてくれる.

なぜかって言うと,

task :default do
  system 'rake -T'
  exit
end

で,rakeが引数なしで入力されたときのtaskを書いてるから.すなわち,rake -Tが走ってる.

では,他のtaskを動かすには?

実行結果

$ rake hello World
Hello World!

こんな感じで,taskをこなしてくれる.

では早速taskを記述!,とその前に,

desc 'hello NAME'
task :hello do
  name = ARGV[1]
  puts "Hello #{name}!"
  exit
end

この部分,"ARGV[1]"になってる.

でもこれが正しい.*注意

System Call

  • Rakefileに記述することの多い関数はsystem (systemコマンドを起動する関数).

コマンドもtaskにできるみたい.以前の記事でシェルスクリプトの紹介をしたが,Rubyでもこんなのがあるとは...

例えば,gitに共有するときの流れで,

  • git pull origin main
  • git add -A
  • git commit -m "hogehoge"
  • git push origin main

って一個ずつ打ってたわけだけど,taskを書くと

desc 'git pull push'
task :push do
  p comm = "git pull origin main"
  system comm
  p comm = "git add -A"
  system comm
  p comm = "git commit -m #{ARGV[1]}"
  system comm
  p comm = "git push origin main"
  system comm
  exit
end

実行結果

$ rake push hogehoge
git pull origin main   
   ・
   ・
   ・

ってな感じで,pullからpushまでやってくれる.便利ですなぁ...

"p comm"しておく(putsでもいい)と,コマンドを出力するからわかりやすいよ.

ちなみに,

desc 'git pull push2'
task :push2 do
  ["git add -A",
   "git commit -m \'hoge\'",
   "git pull origin main",
   "git push origin main"].each do |comm|
    p comm
    system comm
  end
  exit
end

とも記述できる.

ほかにも,コマンドをcolorizeしたいなら,

require 'colorize'
desc 'git pull push2'
task :push2_color do
  ["git add -A",
   "git commit -m \'hoge\'",
   "git pull origin main",
   "git push origin main"].each do |comm|
    puts comm.green
    system comm
  end
  exit
end

で,コマンドがgreenにcolorizeされる.

Command_Line

systemで使える外部のコマンドは動作が遅いので,より高速で動かせるようにrubyの組み込み関数に用意されているとのこと.

例えば,皆のREADME.orgがあるかを見たいとき,

require 'command_line/global'
desc 'make list'
task :mk_list do
  #system 'ls -1 ../**/README.org'
  target = "../**/README.org"
  Dir.glob(target).each do |file|
    p file
  end
  exit
end   

てな感じで,配列に入った情報を列挙してるみたい.

コマンドを組み込んで,qiita_idを取り出すなら,

require 'command_line/global'
desc 'make list'
task :mk_list do
  #system 'ls -1 ../**/README.org'
  target = "../**/README.org"
  Dir.glob(target).each do |file|
    p file
    res = command_line "grep qiita_id #{file}"
    p res.stdout
  end
  exit
end   

標準出力(stdout)などを取り出すのはcommand_lineというgemを使うらしいので,

$ sudo gem install command_line

をしましょう.

他にもいろいろできるみたいだけど,それは次回以降.

Export

emacsでhtmlとかにexport

Org-Modeで記述した記事はHtmlとかにもexportできる.

$ emacs -nw hoge.org
c-c c-e ho # export as [h]tml, [o]ut
c-c c-e ll # export [l]atex, [l]atex 

これでhtmlやlatexにexportできる.

便利ですなぁ...

htmlizeをinstallしろと云われた

htmlにexportしようとしたら,

Please install htmlize from https://github.com/hniksic/emacs-htmlize

と云われた.(Emacsのversionは26.3)

M-x package-list-packages

でpackage一覧が出るが,そこにはhtmlizeの文字が無かった...

様々な記事を探して試して数時間,ついにできた.

やったことは

  • ~/.emacsに以下を記述
  • emacsでhtmlizeのinstall
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
            (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (when no-ssl
    (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
  ;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
  ;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
  (when (< emacs-major-version 24)
    ;; For important compatibility libraries like cl-lib
    (add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)

2行目,"windows-nt ms-dos"とかある.(私はLinux環境だが,これで出来たので,問題ないかと...)

有識者の方は情報求む

ほいで,

$ emacs -nw
M-x package-install
htmlize

でhtmlizeがinstallでき,exportできるようになった.

締め

今回はrakeを使ったtask管理について学んだ.

次回,Assert&Rubular


  • source ~/school/multi/my_ruby/grad_members_20f/members/evendemiaire/post/rake.org
10
0
0

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
10
0