LoginSignup
13
3

More than 3 years have passed since last update.

【Week 8】rake, ruby_fourth

Last updated at Posted at 2020-12-27

はじめに

マルチスケールシミュレーション特論の講義メモです.講義メモのインデックスはコチラ

今回の参考資料はチャート式ruby-appendix-IV(rake)です.

チャート式 Ruby

授業内容変更のため,今回は以下の Ruby 開発周辺環境にまとめています.

Ruby 開発周辺情報

参照記事はコチラ

rake

Rake とはmake ruby のことで Rakefile に一連の作業を記述しておくことでコマンドひとつで実行できるようになる.効率厨からしたらとても便利な機能である.

早速だが

> emacs 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
rake hello    # hello NAME

なんか出てきた.これは Rakefile の task を表示している.

正しくは default のタスクの中身を実行している.タスクの中身の rake -T は先ほど述べた Rakefile の task を表示するコマンドである.今回は引数がなかったので default のタスクの中身が実行されたが,引数を hello とすると

> rake hello
Hello !

今回は hello タスクの中身が実行された.タスクの中身を見てみると Hello {name}! が出力されるのがわかるが,この name にはコマンドライン引数が代入される.今回はコマンドライン引数として何も記述しなかったので Hello ! のみが出力された.

> rake hello hoge

ARGV[1]でなくARGV[0]を指定すると第1引数の hello が出力されてしまう.

system 'rake -T' の部分は system call と呼ばれる.

system call

Rakefile に記述する関数で一番多いのは system コマンドを起動する関数である.普段打ってるコマンドを Rakefile に記述すれば作業の効率化ができる.

例えば git の pull と push を自動化してみる.

desc 'git push'
task :push0 do
  p comm = "git add -A"
  system comm
  p comm = "git commit -m 'first commit'"
  system comm
  p comm = "git pull origin main"
  system comm
  p comm = "git push origin main"
  system comm
  exit
end
> rake push0

pull と push の一連の流れがひとつのコマンドで実行できた.しかし文字が勢いよく流れていってどこで何を行っているのかがわかりにくい.出力の中にコマンドが埋もれてしまっているので,colorizeで色付けする.

require 'colorize'

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

コマンドのみ緑で表示されるようになったので見やすくなった.

command_line

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

標準出力などを取り出すには command_line という gem をインストールする.

sudo gem install command_line

※ruby 2.4以降しか動作しないので注意

Dir.glob(path) は ls の代替.command_line と併用することで以下のようなものが作れる.

require 'command_line/global'

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

これで各受講生のユーザディレクトリ直下に README.org があるか確認できるようになった.

Rakefile

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

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

desc 'git pull'
task :pull do
  p comm = "git pull origin main"
  system comm
  exit
end

desc 'git push'
task :push0 do
  p comm = "git add -A"
  system comm
  p comm = "git commit -m 'first commit'"
  system comm
  p comm = "git pull origin main"
  system comm
  p comm = "git push origin main"
  system comm
  exit
end

require 'colorize'

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

require 'command_line/global'

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

org to_html to_platex

html

org ファイルを html で出力したい.emacs で org ファイルを開き,以下のコマンドを打ってみる.

c-c c-e ho # export as [h]tml, [o]ut                                                                    

これで開いた org ファイルの html ファイルが出力される.ただこのままではかなり質素な html ファイルになってしまうので,設定を変更する.

theme-readtheorg.setup というファイルを作る.path は ~/.emacs.d/org-mode/(おそらく org-mode というディレクトリがないので忘れずに作成する.)

$ cd

$ cd .emacs.d

$ mkdir org-mode

$ emacs org-mode/theme-readtheorg.setup                                                         $

theme-readtheorg.setup ファイルの中身は以下のとおり編集する.

# -*- mode: org; -*-

#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/styles/readtheorg/css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/styles/readtheorg/css/readtheorg.css"/>

#+HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
#+HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="https://fniessen.github.io/org-html-themes/styles/lib/js/jquery.stickytableheaders.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="https://fniessen.github.io/org-html-themes/styles/readtheorg/js/readtheorg.js"></script>

bootstrap などを使って簡単に見易い html ファイルが出力されるようになった.

忘れずにhtml で出力したい org ファイルのヘッダーに以下を追加する.

#+SETUPFILE: ~/.emacs.d/org-mode/theme-readtheorg.setup

latex

latex 環境は PC 買い替え以降構築していないので今回は latex への出力は見送る.

次回の講義内容 <2020-11-18 Wed>

次回は チャート式ruby-IV(assert_equal) だそうです.


  • source ~/grad_members_20f/members/e79a93e5b7b1/posts/class/c8_20201111.org
13
3
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
13
3