!Mac OS X-10.15.7!ruby-2.7.1p83
Recall the last bundler for a moment.
rake install:local
Then hogehoge was automatically installed, right? The command rake used there is today's topic.
rake is a ruby version of make and ant. It is designed to be convenient for doing a little work. A typical example is
task :default do
system 'rake -T'
exit
end
desc 'hello NAME'
task :hello do
name = ARGV[1]
puts "Hello #{name}!"
exit
end
is. Put this in a Rakefile
> rake
Please. The task prepared by Rakefile is displayed. It's very similar to the explanation when you hit with qiita or my \ _help without option.
Now let's take a look at the contents of the Rakefile I just wrote.
--description after desc with rake -T -: Default is the behavior when rake is called with no arguments, --Other than that, the name of the task called by option --In the task, describe the operation in the standard ruby language.
is. Variables starting with':' are classes called symbols that take the place of names. What's wrong with it now, but I'll be using it a lot sooner or later.
rake is DSL (Domain Specific Language), that is, the idea of creating a language unique to the place. Therefore, it is not a genuine ruby language, but it has been extended to make it easier to use a little task. The syntax of rake is used in Thor etc. which will be introduced later.
system call
The function most often described in Rakefile is system, I. This is the function that invokes the system command. The reason is that the work to be done by hand is described as it is. For example, isn't it difficult to remember the command when giving (push) or pulling (pull) with git? Until you get used to it, launch the web, search, read the site with different colors, and then repeat until the end and type in the command. I thought it was normal. But the expert programmers are different. It's not good to remember, but it's a good note of things that you might forget. For example, in Rakefile
desc 'git pull'
task :pull do
p comm = "git pull origin main"
system comm
exit
end
If you write, this alone will make it much easier to use.
Furthermore, even if pull fails, you can do it well with push, so
desc 'git push'
task :push do
p comm = "git add -A"
system comm
p comm = "git commit -m \'hoge\'"
system comm
p comm = "git pull origin main"
system comm
p comm = "git push origin main"
system comm
exit
end
Instead of remembering the work that you always do, let Rakefile remember it. The trick is to p comm what the command is.
In addition, I hide many copies because my friends despise me when I get caught.
desc 'git push'
task :push2 do
["git add -A",
"git commit -m \'hoge\'",
"git pull origin main",
"git push origin main"].each do |comm|
p comm #puts comm.green after require 'colorize'
system comm
end
exit
end
In addition, since you will not know where comm is written, it will be easier to see if you use colorize, which is done in the lecture. In the meantime, I also remember the subtleties of git.
command_line
External commands that can be used in system are slow, so they are usually provided in ruby's built-in functions so that they can be run faster. For example, calling ls with a wildcard can be replaced by Dir.glob. The nice thing about this is that you can do things like "try with the first 5".
desc 'make list'
task :mk_list do
# system 'ls -1 ../members/*/README.org'
Dir.glob('../members/*/README.org')[0..4].each do |file|
p file
system "grep qiita_id #{file}"
end
exit
end
I'm trying to create an org table here, but the question is whether the return value from system is not the output, but whether the command was successful.
By the way, to extract standard output (stdout) etc., use the gem command \ _line. As usual,
gem install command_line
Please. This only works with ruby 2.4 and above.
require 'command_line/global'
desc 'make list'
task :mk_list do
targets = "../members/*/README.org"
# system "ls -1 #{targets}"
Dir.glob(targets)[0..5].each do |file|
p file
res = command_line "grep qiita_id #{file}"
p res.stdout
end
exit
end
Then you can take it out.
Now take out the information from here
# input
"../members/daddygongon/README.org"
"#+qiita_id: daddygongon\n"
=> # output
| daddygongon | daddygongon|
The conversion is performed as follows.
Finally, with some additions
require 'command_line/global'
desc 'make list'
task :mk_list do
targets = "../members/*/README.org"
i = 0
Dir.glob(targets).each do |file|
member = file.split('/')[2]
dd = {}
%i{qiita_id qiita_private}.each do |name|
res = command_line "grep #{name.to_s} #{file}"
res.stdout =~ /:\s*(.+)/ # regexp
dd[name] = $1.to_s
end
next if dd[:qiita_id] == ''
print " | [[https://qiita.com/#{dd[:qiita_id]}/private/#{dd[:qiita_private]}][#{member]}]]"
i += 1
puts "" if (i+1)%6==0
end
exit
end
It is said. In this way, information is automatically extracted from files.
I'll explain what I wrote about regexp and more general data extraction methods next week.
org to_html, to_platex
I introduced emacs org-mode in my \ _help. In qiita \ _org, it was output to qiita from the command line, but more generally, a command to output to html or latex is prepared.
c-c c-e ho # export as [h]tml, [o]ut
c-c c-e ll # export [l]atex, [l]atex
You can convert to those formats with. Do you understand that c-c means a command to major-mode?
However, it is difficult to see with the output as it is. Therefore, for html, we recommend a format called readtheorg. In latex, the header is rewritten to the required format.
html
To output in html, refer to https://github.com/fniessen/org-html-themes. I recommend readtheorg. The following theme-readtheorg.setup file
# -*- 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>
In ~ / .emacs.d / org-mode /.
#+SETUPFILE: ~/.emacs.d/org-mode/theme-readtheorg.setup
Please add to the header of the org-file you are going to write. After that, export ho and it's done.
latex
For latex, only those who need to use it should try the steps below.
You can do quite complicated things with lisp provided by org-mode. But it's a bit annoying because you have to learn lisp. Therefore, add an instruction to replace header to the Rakefile introduced earlier. For example, docs / okamoto has an example of using the latex file of the interim examination.
The nice thing about rake is that you can execute these repetitive commands in one shot. Try to save the cost of memorizing coding and commands, rather than worrying about the time it takes to do that. Especially for my thesis, many laboratories will use latex, so please give it a try. Also, since this was originally made by Nishitani Lab, please let me know if you have a better hand.