From 70b141e5d6fc845e4161d1cdfa46ca15f53fc956 Mon Sep 17 00:00:00 2001 From: Yoichiro Hasebe Date: Thu, 15 Aug 2024 21:54:53 +0900 Subject: [PATCH] Utilitie scripts for development restructured --- bin/monadic_dev | 6 ++- docker/services/ruby/bin/export_db | 4 -- docker/services/ruby/bin/import_db | 4 -- docker/services/ruby/bin/monadic_dev | 67 ++++++++++++++++++++++------ docker/services/ruby/lib/monadic.rb | 4 +- 5 files changed, 60 insertions(+), 25 deletions(-) delete mode 100755 docker/services/ruby/bin/export_db delete mode 100755 docker/services/ruby/bin/import_db diff --git a/bin/monadic_dev b/bin/monadic_dev index 5a79cafb..3feaf54c 100755 --- a/bin/monadic_dev +++ b/bin/monadic_dev @@ -18,10 +18,14 @@ elif [ "$1" == "stop" ]; then elif [ "$1" == "restart" ]; then ./bin/monadic_dev restart --daemonize echo "Monadic script executed with 'restart' argument 🔄" +elif [ "$1" == "export" ]; then + ./bin/monadic_dev export +elif [ "$1" == "import" ]; then + ./bin/monadic_dev import elif [ "$1" == "status" ]; then ./bin/monadic_dev status else - echo "Usage: monadic_dev [start|stop|restart|debug]" + echo "Usage: monadic_dev [start|stop|restart|debug|status|export|import]" ./bin/monadic_dev status fi diff --git a/docker/services/ruby/bin/export_db b/docker/services/ruby/bin/export_db deleted file mode 100755 index 93baeda0..00000000 --- a/docker/services/ruby/bin/export_db +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -DIR=$(cd $(dirname "$0"); pwd) - -pg_dump monadic | gzip > "${DIR}/../data/monadic.gz" diff --git a/docker/services/ruby/bin/import_db b/docker/services/ruby/bin/import_db deleted file mode 100755 index 29398b65..00000000 --- a/docker/services/ruby/bin/import_db +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -DIR=$(cd $(dirname "$0"); pwd) - -dropdb monadic && createdb --locale=C --template=template0 monadic && gunzip -c "${DIR}/../data/monadic.gz" | psql monadic diff --git a/docker/services/ruby/bin/monadic_dev b/docker/services/ruby/bin/monadic_dev index 07f39db5..6667bda3 100755 --- a/docker/services/ruby/bin/monadic_dev +++ b/docker/services/ruby/bin/monadic_dev @@ -3,7 +3,6 @@ # frozen_string_literal: false require "optimist" - require_relative "../lib/monadic/version" # change current directory to the parent directory of the directory containing this file @@ -13,6 +12,9 @@ DEFAULT_PORT = 4567 PID_FILE = __dir__ + "/thin.pid" +# Set the data directory $HOME/monadic/data +DATA_DIR = File.expand_path(File.join(ENV["HOME"], "monadic", "data")) + selenium_image = "selenium/standalone-chrome:latest" if `uname -s`.chomp.include? "Darwin" if `uname -m`.chomp == "arm64" @@ -26,7 +28,7 @@ ENV["HOST_OS"] = `uname -s`.chomp # Parse command line options opts = Optimist.options do version Monadic::VERSION.to_s - banner "Usage: monadic [start|stop|restart] [options]" + banner "Usage: monadic [start|stop|restart|export|input] [options]" opt :daemonize, "Enable or disable daemon mode", default: false opt :log, "Enable or disable logging mode", default: false opt :port, "Specify the port number", type: :integer, default: DEFAULT_PORT @@ -107,6 +109,49 @@ def restart_server(opts) start_server(opts) end +def print_status + if File.exist?(PID_FILE) + pid = File.read(PID_FILE).to_i + begin + Process.kill 0, pid + puts "Server is running" + rescue StandardError + puts "Server is not running" + end + else + puts "Server is not running" + end +end + +def export_document_db + command = <<~SHELL +pg_dump monadic | gzip > "#{DATA_DIR}/monadic.gz" + SHELL + + # run command and get the result with error reporting + res = system(command) + + if res + puts "Document DB exported to 'monadic.gz'" + else + puts "Failed to export document DB" + end +end + +def import_document_db + command = <<~SHELL +dropdb monadic && createdb --locale=C --template=template0 monadic && gunzip -c "#{DATA_DIR}/monadic.gz" | psql monadic + SHELL + + res = system(command) + + if res + puts "Document DB imported from 'monadic.gz'" + else + puts "Failed to import document DB" + end +end + # Parse subcommand subcommand = ARGV.shift @@ -118,18 +163,12 @@ when "stop" stop_server when "restart" restart_server(opts) +when "export" + export_document_db +when "import" + import_document_db when "status" - if File.exist?(PID_FILE) - pid = File.read(PID_FILE).to_i - begin - Process.kill 0, pid - puts "Server is running" - rescue StandardError - puts "Server is not running" - end - else - puts "Server is not running" - end + print_status else - Optimist.die "Unknown subcommand. Use \"start\", \"stop\", or \"restart\"." + Optimist.die "Unknown subcommand. Use \"start\", \"stop\", \"restart\", \"export\", \"import\", or \"status\"" end diff --git a/docker/services/ruby/lib/monadic.rb b/docker/services/ruby/lib/monadic.rb index 2fb4de1b..7ef2946c 100644 --- a/docker/services/ruby/lib/monadic.rb +++ b/docker/services/ruby/lib/monadic.rb @@ -121,17 +121,17 @@ def init_apps if app.settings[:mathjax] initial_prompt_suffix << <<~INITIAL - Use double dollar signs `$$` to enclose MathJax/LaTeX expressions that should be displayed as a separate block; Use single dollar signs `$` before and after the expressions that should appear inline with the text. Without these, the expressions will not render correctly. + Use double dollar signs `$$` to enclose MathJax/LaTeX expressions that should be displayed as a separate block; Use single dollar signs `$` before and after the expressions that should appear inline with the text. Without these, the expressions will not render correctly. Good example: `$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$` for inline expressions `$$[1 + 2 + 3 + … + k + (k + 1) = \frac{k(k + 1)}{2} + (k + 1)]$$` for separate block expressions `$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$` `$$\begin{align} 1 + 2 + … + k + (k+1) &= \frac{k(k+1)}{2} + (k+1)\end{align}$$` - INITIAL prompt_suffix << <<~SUFFIX + IT'S EXTREMELY IMPORTANT TO USE THE CORRECT MATHJAX/LATEX SYNTAX. IF YOU DON'T USE THE CORRECT SYNTAX, THE EQUATIONS WILL NOT RENDER CORRECTLY. SUFFIX end