Add "option", "hook" concepts

This commit is contained in:
0x1eef 2024-04-27 01:31:01 -03:00
parent 1b98c2dce9
commit e89687d445
16 changed files with 115 additions and 61 deletions

View file

@ -4,14 +4,12 @@ require "cmd"
class Twenty::Command < Cmd
##
# mixins
require_relative "command/mixin/common_option_mixin"
require_relative "command/mixin/migration_mixin"
require_relative "command/mixin/sqlite_mixin"
require_relative "command/mixin/rescue_mixin"
# Hooks
require_relative "command/option"
require_relative "command/hook"
##
# commands
# Commands
require_relative "command/up"
require_relative "command/down"
require_relative "command/connect"

View file

@ -5,9 +5,13 @@ class Twenty::Command::Connect < Twenty::Command
description: "Connect a project to twenty"
set_option "-p PATH", "--path PATH", "The path to a project", default: nil
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Hooks
# Run order:
# Rescue -> SQLiteConn -> RequireMigration -> command
prepend Hook::RequireMigration
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -3,10 +3,18 @@
class Twenty::Command::Console < Twenty::Command
set_banner usage: "twenty console [OPTIONS]",
description: "Start the twenty developer console"
include CommonOptionMixin
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Option(s)
include Option::Database
##
# Hooks
# Run order:
# Rescue -> SQLiteConn -> RequireMigration -> command
prepend Hook::RequireMigration
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -4,9 +4,12 @@ class Twenty::Command::Disconnect < Twenty::Command
set_banner usage: "twenty disconnect [OPTIONS]",
description: "Disconnect a project from twenty"
set_option "-p PATH", "--path PATH", "The path to a project", default: nil
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Hooks
prepend Hook::RequireMigration
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -4,8 +4,12 @@ class Twenty::Command::Down < Twenty::Command
set_banner usage: "twenty down [OPTIONS]",
description: "Stop the twenty web server"
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Hooks
# Run order:
# Rescue -> SQLiteConn -> command
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -0,0 +1,5 @@
module Twenty::Command::Hook
require_relative "hook/require_migration"
require_relative "hook/sqlite_conn"
require_relative "hook/rescue"
end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
module Twenty::Command::Hook
module RequireMigration
def run_command(...)
if pending_migrations?
warn "There are pending migrations.\n" \
"Run \"twenty migrate\" first.\n"
exit(1)
else
super(...)
end
end
private
def pending_migrations?
Twenty::Migration.pending_migrations?
end
end
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Twenty::Command
module RescueMixin
module Twenty::Command::Hook
module Rescue
FRAME_MAX = 15
def run(...)

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Twenty::Command::Hook
module SQLiteConn
def run_command(options)
path = options.database || Twenty.default_database
Twenty.establish_connection(path:)
super(options)
end
end
end

View file

@ -5,9 +5,16 @@ class Twenty::Command::Migrate < Twenty::Command
description: "Migrate the database"
set_option "-t TARGET", "--target TARGET", "The target version", default: nil
include CommonOptionMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Options
include Option::Database
##
# Hooks
# Run order:
# Rescue -> SQLiteConn -> command.
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -1,19 +0,0 @@
# frozen_string_literal: true
module Twenty::Command::MigrationMixin
def run_command(...)
if pending_migrations?
warn "There are pending migrations.\n" \
"Run \"twenty migrate\" first.\n"
exit(1)
else
super(...)
end
end
private
def pending_migrations?
Twenty::Migration.pending_migrations?
end
end

View file

@ -1,11 +0,0 @@
# frozen_string_literal: true
module Twenty::Command::SQLiteMixin
def run_command(options)
path = options.database || Twenty.default_database
Twenty.establish_connection(path:)
require "twenty/server/migration"
require "twenty/server/model"
super(options)
end
end

View file

@ -0,0 +1,3 @@
module Twenty::Command::Option
require_relative "option/database"
end

View file

@ -0,0 +1,13 @@
module Twenty::Command::Option
module Database
def self.included(mod)
mod.module_eval do
set_option "-d PATH",
"--database PATH",
"The path to an alternate SQLite database",
as: String,
default: nil
end
end
end
end

View file

@ -16,10 +16,17 @@ class Twenty::Command::Up < Twenty::Command
"--unix PATH",
"Listen on a UNIX socket"
include CommonOptionMixin
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
##
# Option(s)
include Option::Database
##
# Hooks
# Run order:
# Rescue -> SQLiteConn -> RequireMigration -> command
prepend Hook::RequireMigration
prepend Hook::SQLiteConn
prepend Hook::Rescue
def run
options = parse_options(argv)

View file

@ -1,13 +1,11 @@
# frozen_string_literal: true
module Twenty
require "fileutils"
require "sequel"
extend Module.new {
require "tmpdir"
require "fileutils"
extend self
extend FileUtils
require "tmpdir"
##
# @return [String]
@ -63,6 +61,8 @@ module Twenty
@connection
end
require "sequel"
require_relative "server/migration"
require_relative "server/graphql"
require_relative "server/rack"
end