From 3105be205e19d864fc9269bfdb493dd9d4b620f6 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 26 Apr 2024 23:55:33 -0300 Subject: [PATCH] Replace Twenty::Path --- cli/lib/twenty/cli/command/down.rb | 17 ++++++------ cli/lib/twenty/cli/command/up.rb | 9 ++++--- server/lib/twenty/server.rb | 43 ++++++++++++++++++++++++------ server/lib/twenty/server/path.rb | 31 --------------------- 4 files changed, 49 insertions(+), 51 deletions(-) delete mode 100644 server/lib/twenty/server/path.rb diff --git a/cli/lib/twenty/cli/command/down.rb b/cli/lib/twenty/cli/command/down.rb index 7065d64..36bcc5e 100644 --- a/cli/lib/twenty/cli/command/down.rb +++ b/cli/lib/twenty/cli/command/down.rb @@ -3,7 +3,7 @@ class Twenty::Command::Down < Twenty::Command set_banner usage: "twenty down [OPTIONS]", description: "Stop the twenty web server" - include Twenty::Path + prepend Twenty::Command::SQLiteMixin prepend Twenty::Command::RescueMixin @@ -15,19 +15,18 @@ class Twenty::Command::Down < Twenty::Command private def run_command(options) - if File.readable?(pidfile) - Process.kill("SIGINT", Integer(pid)) + if File.readable?(pid) + pid = Integer(File.binread(pid).gsub(/[^\d]/, "")) + Process.kill("SIGINT", pid) else - warn "PID file is not readable." + warn "[x] #{pid} is not readable" end rescue Errno::ESRCH - warn "No such process." - FileUtils.rm(pidfile) + warn "[x] Process not found" + rm(pid) end def pid - @pid ||= File - .binread(pidfile) - .gsub(/[^\d]/, "") + Twenty.pid end end diff --git a/cli/lib/twenty/cli/command/up.rb b/cli/lib/twenty/cli/command/up.rb index 1ef16ed..f76e836 100644 --- a/cli/lib/twenty/cli/command/up.rb +++ b/cli/lib/twenty/cli/command/up.rb @@ -17,7 +17,6 @@ class Twenty::Command::Up < Twenty::Command "Listen on a UNIX socket" include CommonOptionMixin - include Twenty::Path prepend Twenty::Command::MigrationMixin prepend Twenty::Command::SQLiteMixin prepend Twenty::Command::RescueMixin @@ -30,12 +29,16 @@ class Twenty::Command::Up < Twenty::Command private def run_command(options) - File.binwrite(pidfile, Process.pid.to_s) + File.binwrite(pid, Process.pid.to_s) thr = Twenty::Rack.server(options).start thr.join rescue Interrupt thr.kill ensure - FileUtils.rm(pidfile) + FileUtils.rm(pid) + end + + def pid + Twenty.pid end end diff --git a/server/lib/twenty/server.rb b/server/lib/twenty/server.rb index 9e7421b..8c1a70e 100644 --- a/server/lib/twenty/server.rb +++ b/server/lib/twenty/server.rb @@ -3,14 +3,42 @@ module Twenty require "fileutils" require "sequel" - require_relative "server/path" - ## - # @return [String] - # Returns the default path for the SQLite database - def self.default_database - @default_database ||= File.join(Path.datadir, "database.sqlite") - end + extend Module.new { + extend FileUtils + require "tmpdir" + + ## + # @return [String] + # Returns the path for the default SQLite database + def default_database + @default_database ||= File.join(datadir, "database.sqlite") + end + + ## + # @return [String] + # Returns the directory where twenty stores data + def datadir + File.join(Dir.home, ".local", "share", "twenty") + end + + ## + # @return [String] + # Returns the directory where twenty stores temporary data + def tmpdir + File.join(Dir.tmpdir, "twenty") + end + + ## + # @return [String] + # Returns the path to a PID file + def pid + File.join(tmpdir, "server.pid") + end + + mkdir_p(datadir) + mkdir_p(tmpdir) + } ## # Establishes a database connection @@ -34,7 +62,6 @@ module Twenty @connection end - FileUtils.touch(default_database) require_relative "server/graphql" require_relative "server/rack" end diff --git a/server/lib/twenty/server/path.rb b/server/lib/twenty/server/path.rb deleted file mode 100644 index 9bd2b1a..0000000 --- a/server/lib/twenty/server/path.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -module Twenty::Path - require "tmpdir" - extend self - - ## - # @return [String] - # Returns the directory where twenty stores persistent data. - def datadir - File.join(Dir.home, ".local", "share", "twenty") - end - - ## - # @return [String] - # Returns the directory where twenty stores temporary data. - def tmpdir - File.join(Dir.tmpdir, "twenty") - end - - ## - # @return [String] - # Returns the file where twenty can write the PID of - # a web server running in the background. - def pidfile - File.join(tmpdir, "server.pid") - end - - FileUtils.mkdir_p(datadir) - FileUtils.mkdir_p(tmpdir) -end