Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exe/rage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby

require_relative "../lib/rage/cli"
Rage::CLI.start
Rage::CLI::App.start
3 changes: 3 additions & 0 deletions lib/rage-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class << self
alias_method :configuration, :config
end

module CLI
end

module Router
module Strategies
end
Expand Down
155 changes: 12 additions & 143 deletions lib/rage/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,14 @@
require "rack"
require "rage/version"

require "rage/cli/base"
require "rage/cli/skills"
require "rage/cli/openapi"
require "rage/cli/code_generator"
require "rage/cli/new_app_generator"

module Rage
class CLICodeGenerator < Thor
include Thor::Actions

def self.source_root
File.expand_path("templates", __dir__)
end

desc "migration NAME", "Generate a new migration"
def migration(name = nil)
return help("migration") if name.nil?

setup
Rake::Task["db:new_migration"].invoke(name)
end

desc "model NAME", "Generate a new model"
def model(name = nil)
return help("model") if name.nil?

setup
migration("create_#{name.pluralize}")
@model_name = name.classify
template("model-template/model.rb", "app/models/#{name.singularize.underscore}.rb")
end

desc "controller NAME", "Generate a new controller"
def controller(name = nil)
return help("controller") if name.nil?

setup
unless defined?(ActiveSupport::Inflector)
raise LoadError, <<~ERR
ActiveSupport::Inflector is required to run this command. Add the following line to your Gemfile:
gem "activesupport", require: "active_support/inflector"
ERR
end

# remove trailing Controller if already present
normalized_name = name.sub(/_?controller$/i, "")
@controller_name = "#{normalized_name.camelize}Controller"
file_name = "#{normalized_name.underscore}_controller.rb"

template("controller-template/controller.rb", "app/controllers/#{file_name}")
end

private

def setup
@setup ||= begin
require "rake"
load "Rakefile"
end
end
end

class CLI < Thor
module Rage::CLI
class App < Base
def self.exit_on_failure?
true
end
Expand All @@ -74,7 +23,7 @@ def new(path = nil)
return help("new") if options.help? || path.nil?

require "rage/all"
CLINewAppGenerator.start([path, options[:database]])
NewAppGenerator.start([path, options[:database]])
end

desc "s", "Start the app server"
Expand Down Expand Up @@ -232,11 +181,14 @@ def version
end

desc "skills", "Manage coding agent skills"
subcommand "skills", CLISkills
subcommand "skills", Skills

desc "openapi", "OpenAPI validation tools"
subcommand "openapi", OpenAPI

map "generate" => :g
desc "g TYPE", "Generate new code"
subcommand "g", CLICodeGenerator
subcommand "g", CodeGenerator

map "--tasks" => :tasks
desc "--tasks", "See the list of available tasks"
Expand Down Expand Up @@ -273,26 +225,6 @@ def respond_to_missing?(method_name, include_private = false)

private

def environment
require File.expand_path("config/application.rb", Dir.pwd)

if Rage.config.internal.rails_mode
require File.expand_path("config/environment.rb", Dir.pwd)
end
end

def set_env(options)
if options[:environment]
ENV["RAGE_ENV"] = ENV["RAILS_ENV"] = options[:environment]
elsif ENV["RAGE_ENV"]
ENV["RAILS_ENV"] = ENV["RAGE_ENV"]
elsif ENV["RAILS_ENV"]
ENV["RAGE_ENV"] = ENV["RAILS_ENV"]
else
ENV["RAGE_ENV"] = ENV["RAILS_ENV"] = "development"
end
end

def linked_rake_tasks
require "rake"
Rake::TaskManager.record_task_metadata = true
Expand Down Expand Up @@ -371,67 +303,4 @@ def print_event_subscribers_tree(event_class)
end
end
end

class CLINewAppGenerator < Thor::Group
include Thor::Actions
argument :path, type: :string
argument :database, type: :string, required: false

def self.source_root
File.expand_path("templates", __dir__)
end

def setup
@use_database = !database.nil?
end

def create_directory
empty_directory(path)
end

def copy_files
inject_templates
end

def install_database
return unless @use_database

@app_name = path.tr("-", "_").downcase
append_to_file "#{path}/Gemfile", <<~RUBY

gem "#{get_db_gem_name}"
gem "activerecord"
gem "standalone_migrations", require: false
RUBY

inject_templates("db-templates")
inject_templates("db-templates/#{database}")
end

private

def inject_templates(from = nil)
root = "#{self.class.source_root}/#{from}"

Dir.glob("*", base: root).each do |template|
next if File.directory?("#{root}/#{template}")

*template_path_parts, template_name = template.split("-")
template("#{root}/#{template}", [path, *template_path_parts, template_name].join("/"))
end
end

def get_db_gem_name
case database
when "mysql"
"mysql2"
when "trilogy"
"trilogy"
when "postgresql"
"pg"
when "sqlite3"
"sqlite3"
end
end
end
end
27 changes: 27 additions & 0 deletions lib/rage/cli/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Rage::CLI
class Base < Thor
private

def environment
require File.expand_path("config/application.rb", Dir.pwd)

if Rage.config.internal.rails_mode
require File.expand_path("config/environment.rb", Dir.pwd)
end
end

def set_env(options)
if options[:environment]
ENV["RAGE_ENV"] = ENV["RAILS_ENV"] = options[:environment]
elsif ENV["RAGE_ENV"]
ENV["RAILS_ENV"] = ENV["RAGE_ENV"]
elsif ENV["RAILS_ENV"]
ENV["RAGE_ENV"] = ENV["RAILS_ENV"]
else
ENV["RAGE_ENV"] = ENV["RAILS_ENV"] = "development"
end
end
end
end
58 changes: 58 additions & 0 deletions lib/rage/cli/code_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module Rage::CLI
class CodeGenerator < Base
include Thor::Actions

def self.source_root
File.expand_path("../templates", __dir__)
end

desc "migration NAME", "Generate a new migration"
def migration(name = nil)
return help("migration") if name.nil?

setup
Rake::Task["db:new_migration"].invoke(name)
end

desc "model NAME", "Generate a new model"
def model(name = nil)
return help("model") if name.nil?

setup
migration("create_#{name.pluralize}")
@model_name = name.classify
template("model-template/model.rb", "app/models/#{name.singularize.underscore}.rb")
end

desc "controller NAME", "Generate a new controller"
def controller(name = nil)
return help("controller") if name.nil?

setup
unless defined?(ActiveSupport::Inflector)
raise LoadError, <<~ERR
ActiveSupport::Inflector is required to run this command. Add the following line to your Gemfile:
gem "activesupport", require: "active_support/inflector"
ERR
end

# remove trailing Controller if already present
normalized_name = name.sub(/_?controller$/i, "")
@controller_name = "#{normalized_name.camelize}Controller"
file_name = "#{normalized_name.underscore}_controller.rb"

template("controller-template/controller.rb", "app/controllers/#{file_name}")
end

private

def setup
@setup ||= begin
require "rake"
load "Rakefile"
end
end
end
end
66 changes: 66 additions & 0 deletions lib/rage/cli/new_app_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module Rage::CLI
class NewAppGenerator < Thor::Group
include Thor::Actions
argument :path, type: :string
argument :database, type: :string, required: false

def self.source_root
File.expand_path("../templates", __dir__)
end

def setup
@use_database = !database.nil?
end

def create_directory
empty_directory(path)
end

def copy_files
inject_templates
end

def install_database
return unless @use_database

@app_name = path.tr("-", "_").downcase
append_to_file "#{path}/Gemfile", <<~RUBY

gem "#{get_db_gem_name}"
gem "activerecord"
gem "standalone_migrations", require: false
RUBY

inject_templates("db-templates")
inject_templates("db-templates/#{database}")
end

private

def inject_templates(from = nil)
root = "#{self.class.source_root}/#{from}"

Dir.glob("*", base: root).each do |template|
next if File.directory?("#{root}/#{template}")

*template_path_parts, template_name = template.split("-")
template("#{root}/#{template}", [path, *template_path_parts, template_name].join("/"))
end
end

def get_db_gem_name
case database
when "mysql"
"mysql2"
when "trilogy"
"trilogy"
when "postgresql"
"pg"
when "sqlite3"
"sqlite3"
end
end
end
end
20 changes: 20 additions & 0 deletions lib/rage/cli/openapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Rage::CLI
class OpenAPI < Base
desc "validate", "Validate OpenAPI tags and fail if any warnings were produced"
def validate
environment

abort "OpenAPI validation requires a booted application." unless Rage.config.internal.initialized?

warnings = Rage::OpenAPI.__collect_warnings { Rage::OpenAPI.build }

if warnings.any?
abort "#{set_color("𐄂", :red, :bold)} OpenAPI validation failed with #{warnings.size} warning(s)."
else
say "#{set_color("✓", :green, :bold)} OpenAPI validation passed without warnings."
end
end
end
end
Loading
Loading