[RUBY] Write the movement of Rakefile in the runbook

Introduction

As part of my graduation research, write a Rakefile for creating a PDF used in the laboratory in the form of a runbook.

Execution environment

What is a runbook

A program that clarifies the procedure and progresses while taking each step. You can write automation step by step as one of Ruby's DSL.

Why runbook instead of Rake?

For a person like me who is a beginner and not yet accustomed to using CUI, executing Rakefile will proceed without permission and you will not know what you are doing. Therefore, by using the runbook, the procedure becomes clear and you can understand what you are doing. Also, with Rakefile, it is difficult if you do not write in the program from the beginning to stop even if you think something is wrong during execution, but with Runbook you can choose whether to continue asking the user for input each time in a finely divided procedure. You can.

Program overview

Runbook basics

The basic is a program consisting of a title, a section, and a step, and the program is written in the step. Since the runbook command has a template for starting, install it and check the basics of the runbook.

> runbook generate runbook my_first_runbook

This will generate my_first_runbook.rb in the directory.

my_first_runbook.rb


require "runbook"

runbook = Runbook.book "My First Runbook" do
  description <<-DESC                                                           
This is a runbook that...                                                       
  DESC

  section "SECTION" do
    step "STEP" do
      # Add statements here                                                     
    end
  end
end

if __FILE__ == $0
  Runbook::Runner.new(runbook).run
else
  runbook
end

When you execute it, you will be asked if you want to continue as shown in the figure below.

> runbook exec my_first_runbook.rb 
Executing My First Runbook...

Description:
This is a runbook that...

Section 1: SECTION

Step 1.1: STEP

Continue? (enter "h" for help) [c,s,j,P,e,h] 
> runbook exec my_first_runbook.rb 
Executing My First Runbook...

Description:
This is a runbook that...

Section 1: SECTION

Step 1.1: STEP

Continue? (enter "h" for help) [c,s,j,P,e,h] c
>> Continue to execute this step
> runbook exec my_first_runbook.rb 
Executing My First Runbook...

Description:
This is a runbook that...

Section 1: SECTION

Step 1.1: STEP

Continue? Continue to execute this step

Ported Rakefile to runbook

The structure of the Rakefile used in the laboratory is mainly --Reading an org file --Convert to latex --Changed latex to report format --Create PDF using platex

Since there are four, write a runbook with these as steps.

make_pdf.rb


Runbook.book "Make PDF" do
  description <<-DESC                                                           
    This is a make PDF from org                                                 
  DESC

  section "Make pdf" do
    step "Load org file" 

    step "Make tex file" 

    step "Load and Convert tex file" 

    step "Make pdf" 
  end
end

Execution result

> runbook exec make_pdf.rb
Executing Make PDF...

Description:
    This is a make PDF from org

Section 1: Make pdf

Step 1.1: Load org file

Continue? Continue to execute this step
Step 1.2: Make tex file

Continue? Continue to execute this step
Step 1.3: Load and Convert tex file

Continue? Continue to execute this step
Step 1.4: Make pdf

Now that we have a template, we will add the contents to it.

I thought it would be easier to stop by dividing the section into one that creates latex and one that converts to PDF, so do so and add the contents.

make_pdf.rb


require "./convert"
require "colorize"

Runbook.book "Make PDF" do
  description <<-DESC
    This is a make PDF from org
  DESC

  section "Make latex" do
    $t_file = "report"
    step "Load org file" do
      note "Load org file"
      ruby_command do
        $file = Dir.glob("*.org")[0].match(/(.*).org/)[1]
        puts "your org file is " + $file.red + "."
      end
    end
    step "Make tex file" do
      note "Make tex file"
      ruby_command do
        system "emacs #{$file}.org --batch -f org-latex-export-to-latex --kill"
      end
    end
    step "Load and Convert tex file" do
      ruby_command do
        $lines = File.readlines("#{$file}.tex")
        $lines = convert_thesis($lines)

        File.open("#{$t_file}.tex", "w") do |f|
        $lines.each { |line| f.print line }
      end
    end
  end

  section "Make PDF" do
    step "Make pdf" do
      note "Make pdf"
      ruby_command do
        commands = ["platex #{$t_file}.tex",
                    "bibtex #{$t_file}.tex",
                    "platex #{$t_file}.tex",
                    "dvipdfmx #{$t_file}.dvi"]
        commands.each { |com| system com }
      end
    end
  end
end

Now you have a runbook program that reads the org file in the directory with "Load org file", converts it to latex, and creates a PDF.

--convert.rb is a program for using the function of convert_thesis used in "Load and Convert tex file". The content is to convert latex created from org into a template for reporting.

convert.rb


def convert_thesis(lines)
  head = <<'EOS'
  \documentclass[a4j,twocolumn]{jsarticle}
  \usepackage[dvipdfmx]{graphicx}
  \usepackage{url}

  \setlength{\textheight}{275mm}
  \headheight 5mm
  \topmargin -30mm
  \textwidth 185mm
  \oddsidemargin -15mm
  \evensidemargin -15mm
  \pagestyle{empty}


  \begin{document} 

  \title{}
  \author{Department\hspace{5mm}Student number\hspace{5mm} your name}
  \date{}

  \maketitle
EOS
  head2 = <<'EOS'
  {\small\setlength\baselineskip{15pt}	%References are line spacing with smaller letters
  \begin{thebibliography}{9}
  \bibitem{} 
  \end{thebibliography}
  }
  \end{document}
EOS
  new_line = [head]
  lines[31..-1].each do |line|
    new_line << line
  end

  new_line.each do |line|
    line.gsub!('\end{document}', head2)
    line.gsub!('\tableofcontents', "")
  end
  return new_line
end

Pointed out

Pointed out by laboratory members

--I want to be able to select when there are multiple org files. --The files (.aux and .div) created in the directory are hard to see even though you haven't changed them yourself, so please delete them or put them together.

Allows you to select org in "Load org file".

make_pdf.rb


require "./convert"
require "colorize"

Runbook.book "Make PDF" do

...

$t_file = "report"
    step "Load org file" do
      note "Load org file"
      ruby_command do
        str = Dir.glob("*.org")
        str.each do |name|
          puts "your org file is " + name.red + " ? (y or n)"
          res = $stdin.gets.chomp

          if res == "y"
            $file = name.match(/(.*).org/)[1]
            break
          elsif res == "n"
            if name == str[str.size - 1]
              puts "This directory not have the objective file".red
              exit
            end
          end
        end
      end
    end

...

end

The name of the org file is displayed as a message on the terminal, and you can select it by entering "y" or "n". If all are "n", it will be forcibly terminated.

Next, at the very end, I made "Move report" and moved the created report to a directory called report.

make_pdf.rb


require "./convert"
require "colorize"

Runbook.book "Make PDF" do

...

section "Make PDF" do
    step "Make pdf" do

...

    step "Move report" do
      note "Move report"
      ruby_command do
        commands = ["mkdir report",
                    "mv -f #{$t_file}.* ./report",
                    "open ./report/#{$t_file}.pdf"]
        commands.each { |com| system com }
      end
    end
  end
end

Pointed out by the professor

――It was pointed out that you should check if there is a way to do everything non-stop without inputting.

There was a description in the manual in Github of gem runbook.

> runbook exec --auto my_runbook.rb

If you execute it with this, you can execute it to the end without input.

How to use

Introduction of runbook

Install with gem. gem install runbook

Execution method

If you do runbook exec runbook.rb, runbook.rb will be executed. --runbook.rb is your program. --Can be executed in any directory if you know the path of the program location

Recommended Posts

Write the movement of Rakefile in the runbook
Order of processing in the program
Get the result of POST in Java
The identity of params [: id] in rails
The story of AppClip support in Anyca
The story of writing Java in Emacs
[Order method] Set the order of data in Rails
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
Import files of the same hierarchy in Java
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
Write a test by implementing the story of Mr. Nabeats in the world with ruby
Get the URL of the HTTP redirect destination in Java
Specify the encoding of static resources in Spring Boot
Count the number of occurrences of a string in Ruby
Format of the log output by Tomcat itself in Tomcat 8
In Time.strptime,% j (total date of the year) is
Write ABNF in Java and pass the email address
Prepare the execution environment of Tomcat in IntelliJ Community
Access the war file in the root directory of Tomcat
How to write Scala from the perspective of Java
Return the execution result of Service class in ServiceResponse class
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Ruby] The role of subscripts in learning elements in arrays
Let's write a Qiita article in org-mode of Emacs !!
Get the name of the test case in the JUnit test class
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Review the multilingual support (i18n) of the ios app in 3 minutes
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
The story of an Illegal State Exception in Jetty.
Examine the boundaries of the "32GB memory wall" in Elasticsearch
[Java] Get the file in the jar regardless of the environment
The objects in the List were references, right? Confirmation of
Personal summary of the guys often used in JUnit 4
Set the maximum number of characters in UITextField in RxSwift
Change the storage quality of JPEG images in Java
Get the URL of the HTTP redirect destination in Ruby
Summarize the additional elements of the Optional class in Java 9
[Android] Hook the tap of the anchor link in WebViewClient
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
Even if I write the setting of STRICT_QUOTE_ESCAPING in CATALINA_OPTS in tomcat8.5, it is not reflected.
Output in multiples of 3
The idea of jQuery
Was done in the base year of the Java calendar week
Check the dependency of a specific maven artifact in Coursier
Put the file in the properties of string in spring xml configuration
Install by specifying the version of Django in the Docker environment
Determine that the value is a multiple of 〇 in Ruby
Setting the baseURL in the axios module of Docker environment Nuxt
What is the representation of domain knowledge in the [DDD] model?