Class: Securial::CLI
- Inherits:
-
Object
- Object
- Securial::CLI
- Defined in:
- lib/securial/cli.rb
Overview
Command-line interface for the Securial gem.
This class provides the command-line functionality for Securial, enabling users to create new Rails applications with Securial pre-installed and configured. It handles command parsing, flag processing, and orchestrates the setup of new applications.
Class Method Summary collapse
-
.start(argv) ⇒ Integer
Entry point for the CLI application.
Instance Method Summary collapse
-
#add_securial_gem(app_name) ⇒ void
private
Adds the Securial gem to the application’s Gemfile.
-
#create_option_parser ⇒ OptionParser
private
Creates and configures the option parser.
-
#create_rails_app(app_name, rails_options) ⇒ Integer
private
Creates a new Rails application.
-
#handle_commands(argv) ⇒ Integer
private
Processes commands from the command line arguments.
-
#handle_flags(argv) ⇒ nil, Integer
private
Processes option flags from the command line arguments.
-
#handle_new_command(argv) ⇒ Integer
private
Handles the ‘new’ command for creating Rails applications.
-
#install_gems(app_name) ⇒ Integer
private
Installs gems for the application using Bundler.
-
#install_securial(app_name) ⇒ Integer
private
Installs and configures Securial in the application.
-
#mount_securial_engine(app_name) ⇒ void
private
Mounts the Securial engine in the application’s routes.
-
#print_final_instructions(app_name) ⇒ void
private
Prints final setup instructions after successful installation.
-
#run(command, chdir: nil) ⇒ Integer
private
Runs a system command with optional directory change.
-
#securial_new(app_name, rails_options) ⇒ void
private
Creates a new Rails application with Securial pre-installed.
-
#show_version ⇒ void
private
Displays the current Securial version.
-
#start(argv) ⇒ Integer
Processes command line arguments and executes the appropriate action.
Class Method Details
.start(argv) ⇒ Integer
Entry point for the CLI application.
Creates a new CLI instance and delegates to its start method.
36 37 38 |
# File 'lib/securial/cli.rb', line 36 def self.start(argv) new.start(argv) end |
Instance Method Details
#add_securial_gem(app_name) ⇒ void (private)
This method returns an undefined value.
Adds the Securial gem to the application’s Gemfile.
197 198 199 200 201 |
# File 'lib/securial/cli.rb', line 197 def add_securial_gem(app_name) puts "📦 Adding Securial gem to Gemfile" gemfile_path = File.join(app_name, "Gemfile") File.open(gemfile_path, "a") { |f| f.puts "\ngem 'securial'" } end |
#create_option_parser ⇒ OptionParser (private)
Creates and configures the option parser.
Sets up the command-line options and their handlers.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/securial/cli.rb', line 128 def create_option_parser OptionParser.new do |opts| opts. = "Usage: securial [options] <command> [command options]\n\n" opts.separator "" opts.separator "Commands:" opts.separator " new APP_NAME [rails_options...] # Create a new Rails app with Securial pre-installed" opts.separator "" opts.separator "Options:" opts.on("-v", "--version", "Show Securial version") do show_version exit(0) end opts.on("-h", "--help", "Show this help message") do puts opts exit(0) end end end |
#create_rails_app(app_name, rails_options) ⇒ Integer (private)
Creates a new Rails application.
187 188 189 190 |
# File 'lib/securial/cli.rb', line 187 def create_rails_app(app_name, ) rails_command = ["rails", "new", app_name, *] run(rails_command) end |
#handle_commands(argv) ⇒ Integer (private)
Processes commands from the command line arguments.
Identifies the command (e.g., ‘new’) and delegates to the appropriate handler method for that command.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/securial/cli.rb', line 89 def handle_commands(argv) cmd = argv.shift case cmd when "new" handle_new_command(argv) else puts create_option_parser 1 end end |
#handle_flags(argv) ⇒ nil, Integer (private)
Processes option flags from the command line arguments.
Parses options like –version and –help, executing their actions if present and removing them from the argument list.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/securial/cli.rb', line 68 def handle_flags(argv) parser = create_option_parser begin parser.order!(argv) nil # Continue to command handling rescue OptionParser::InvalidOption => e warn "ERROR: Illegal option(s): #{e.args.join(' ')}" puts parser 1 end end |
#handle_new_command(argv) ⇒ Integer (private)
Handles the ‘new’ command for creating Rails applications.
Validates that an application name is provided, then delegates to securial_new to create the application with Securial.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/securial/cli.rb', line 109 def handle_new_command(argv) app_name = argv.shift if app_name.nil? puts "ERROR: Please provide an app name." puts create_option_parser return 1 end securial_new(app_name, argv) 0 end |
#install_gems(app_name) ⇒ Integer (private)
Installs gems for the application using Bundler.
208 209 210 |
# File 'lib/securial/cli.rb', line 208 def install_gems(app_name) run("bundle install", chdir: app_name) end |
#install_securial(app_name) ⇒ Integer (private)
Installs and configures Securial in the application.
217 218 219 220 221 |
# File 'lib/securial/cli.rb', line 217 def install_securial(app_name) puts "🔧 Installing Securial" run("bin/rails generate securial:install", chdir: app_name) run("bin/rails db:migrate", chdir: app_name) end |
#mount_securial_engine(app_name) ⇒ void (private)
This method returns an undefined value.
Mounts the Securial engine in the application’s routes.
228 229 230 231 232 233 234 235 236 |
# File 'lib/securial/cli.rb', line 228 def mount_securial_engine(app_name) puts "🔗 Mounting Securial engine in routes" routes_path = File.join(app_name, "config/routes.rb") routes = File.read(routes_path) updated = routes.sub("Rails.application.routes.draw do") do |match| "#{match}\n mount Securial::Engine => '/securial'" end File.write(routes_path, updated) end |
#print_final_instructions(app_name) ⇒ void (private)
This method returns an undefined value.
Prints final setup instructions after successful installation.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/securial/cli.rb', line 243 def print_final_instructions(app_name) puts <<~INSTRUCTIONS 🎉 Securial has been successfully installed in your Rails app! ✅ Your app is ready at: ./#{app_name} ➡️ Next steps: cd #{app_name} ⚙️ Optional: Configure Securial in config/initializers/securial.rb rails server INSTRUCTIONS end |
#run(command, chdir: nil) ⇒ Integer (private)
Runs a system command with optional directory change.
Executes the provided command, optionally in a different directory, and handles success/failure conditions.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/securial/cli.rb', line 265 def run(command, chdir: nil) puts "→ #{command.inspect}" result = if chdir Dir.chdir(chdir) { system(*command) } else system(*command) end unless result abort("❌ Command failed: #{command}") end 0 end |
#securial_new(app_name, rails_options) ⇒ void (private)
This method returns an undefined value.
Creates a new Rails application with Securial pre-installed.
Orchestrates the process of creating a Rails application, adding the Securial gem, installing dependencies, and configuring the application.
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/securial/cli.rb', line 170 def securial_new(app_name, ) puts "🏗️ Creating new Rails app: #{app_name}" create_rails_app(app_name, ) add_securial_gem(app_name) install_gems(app_name) install_securial(app_name) mount_securial_engine(app_name) print_final_instructions(app_name) end |
#show_version ⇒ void (private)
This method returns an undefined value.
Displays the current Securial version.
154 155 156 157 158 159 |
# File 'lib/securial/cli.rb', line 154 def show_version require "securial/version" puts "Securial v#{Securial::VERSION}" rescue LoadError puts "Securial version information not available." end |
#start(argv) ⇒ Integer
Processes command line arguments and executes the appropriate action.
This method handles both option flags (like –version) and commands (like ‘new’), delegating to specialized handlers and returning the appropriate exit code.
49 50 51 52 53 54 55 56 |
# File 'lib/securial/cli.rb', line 49 def start(argv) # Process options and exit if a flag was handled result = handle_flags(argv) exit(result) if result # Otherwise handle commands exit(handle_commands(argv)) end |