[Week 8] rake, ruby_fourth

Introduction

This is a lecture memo of the special lecture on multi-scale simulation. The index of lecture memos is here

The reference material this time is Chart type ruby-appendix-IV (rake).

Chart type Ruby

Due to changes in the lesson content, this time we have summarized it in the following Ruby development peripheral environment.

Ruby development peripheral information

Reference article is here

rake

Rake is make ruby, and by describing a series of work in Rakefile, it can be executed with one command. This is a very convenient function from the viewpoint of efficiency kitchen.

Immediately

> emacs Rakefile

Edit the Rakefile with. For the time being, describe the following contents.

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

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

When I run rake

> rake
rake hello    # hello NAME

Something came out. This shows the task of Rakefile.

Correctly, the contents of the default task are being executed. The contents of the task, rake -T, is the command to display the task of the Rakefile mentioned earlier. Since there was no argument this time, the contents of the default task were executed, but if the argument is hello

> rake hello
Hello !

This time, the contents of the hello task were executed. If you look at the contents of the task, you can see that Hello {name}! Is output, but command line arguments are assigned to this name. Since nothing was described as a command line argument this time, only Hello! Was output.

> rake hello hoge

If ARGV [0] is specified instead of ARGV [1], the first argument hello will be output.

The part of system'rake -T' is called system call.

system call

The most common function described in Rakefile is the function that starts the system command. You can improve work efficiency by writing the commands you normally type in the Rakefile.

For example, try automating git pull and 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

A series of pull and push flows could be executed with one command. However, it is difficult to understand where and what the characters are doing because the characters are flowing vigorously. Since the command is buried in the output, color it with 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

Only commands are displayed in green, making it easier to see.

command_line

Execution of external commands using system is slow, so it can be run faster by using the built-in functions of ruby ​​that are already prepared.

To extract standard output etc., install a gem called command \ _line.

sudo gem install command_line

Dir.glob (path) is an alternative to ls. The following can be created by using it together with 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

Now you can check if there is a README.org directly under each student's user directory.

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

I want to output an org file as html. Open the org file with emacs and type the following command.

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

This will output the html file of the opened org file. However, if it is left as it is, it will be a fairly simple html file, so change the setting.

Create a file called theme-readtheorg.setup. path is ~/.emacs.d/org-mode/(probably there is no directory called org-mode, so don't forget to create it.)

$ cd

$ cd .emacs.d

$ mkdir org-mode

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

Edit the contents of the theme-readtheorg.setup file as follows.

# -*- 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>

An html file that is easy to read can now be output using bootstrap.

Don't forget to add the following to the header of the org file you want to output as html.

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

latex

Since the latex environment has not been built since the replacement of the PC, the output to latex will be postponed this time.

Next lecture content & lt; 2020-11-18 Wed & gt;

Next time, it will be Chart type ruby-IV (assert \ _equal).


Recommended Posts

[Week 8] rake, ruby_fourth
[Week 9] ruby, ruby_fourth
Rake