Class: Stable::Services::AppCreator
- Inherits:
-
Object
- Object
- Stable::Services::AppCreator
- Defined in:
- lib/stable/services/app_creator.rb
Overview
Service for creating new Rails applications
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(name, options) ⇒ AppCreator
constructor
A new instance of AppCreator.
Constructor Details
#initialize(name, options) ⇒ AppCreator
Returns a new instance of AppCreator.
7 8 9 10 |
# File 'lib/stable/services/app_creator.rb', line 7 def initialize(name, ) @name = name @options = end |
Instance Method Details
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/stable/services/app_creator.rb', line 12 def call ruby = @options[:ruby] || RUBY_VERSION port = @options[:port] || next_free_port app_path = File.join(Stable::Paths.projects_dir, @name) domain = "#{@name}.test" # --- Check if app already exists --- config_file = Stable::Paths.app_config_file(@name) abort "App '#{@name}' already exists" if File.exist?(config_file) # --- Register app in registry --- Services::AppRegistry.add( name: @name, path: app_path, domain: domain, port: port, ruby: ruby, started_at: nil, pid: nil ) # --- Ensure Ruby version & RVM --- Ruby.ensure_version(ruby) Ruby.ensure_rvm! # --- Create gemset --- System::Shell.run("bash -lc 'source #{Ruby.rvm_script} && rvm #{ruby} do rvm gemset create #{@name} || true'") rvm_cmd = Ruby.rvm_prefix(ruby, @name) # --- Install Bundler --- System::Shell.run("bash -lc '#{rvm_cmd} gem install bundler --no-document'") # --- Install Rails if missing --- rails_version = @options[:rails] rails_check_cmd = rails_version ? "#{rvm_cmd} gem list -i rails -v #{rails_version}" : "#{rvm_cmd} gem list -i rails" unless system("bash -lc '#{rails_check_cmd}'") puts "Installing Rails #{rails_version || 'latest'}..." install_cmd = rails_version ? "#{rvm_cmd} gem install rails -v #{rails_version}" : "#{rvm_cmd} gem install rails" system("bash -lc '#{install_cmd}'") or abort("Failed to install Rails #{rails_version || ''}") end # --- Create Rails app --- puts "Creating Rails app #{@name} (Ruby #{ruby})..." rails_cmd = "#{rvm_cmd} rails new #{app_path} " \ '--skip-importmap --skip-hotwire --skip-javascript --skip-solid' System::Shell.run("bash -lc '#{rails_cmd}'") # --- Write ruby version/gemset --- Dir.chdir(app_path) do File.write('.ruby-version', "#{ruby}\n") File.write('.ruby-gemset', "#{@name}\n") end # --- Database integration --- if @options[:db] adapter = @options[:mysql] ? :mysql : :postgresql gem_name = adapter == :postgresql ? 'pg' : 'mysql2' gemfile_path = File.join(app_path, 'Gemfile') unless File.read(gemfile_path).include?(gem_name) File.open(gemfile_path, 'a') do |f| f.puts "\n# Added by Stable CLI" f.puts "gem '#{gem_name}'" end puts "✅ Added '#{gem_name}' gem to Gemfile" end # Use correct Ruby/gemset for bundle install System::Shell.run(rvm_run('bundle install --jobs=4 --retry=3', chdir: app_path)) db_adapter = adapter == :mysql ? Database::MySQL : Database::Postgres db_adapter.new(app_name: @name, app_path: app_path, ruby: ruby).setup end # --- Run bundle & DB prepare --- System::Shell.run(rvm_run('bundle install --jobs=4 --retry=3', chdir: app_path)) System::Shell.run(rvm_run('bundle exec rails db:prepare', chdir: app_path)) rails_check = "cd #{app_path} && #{rvm_cmd} bundle exec rails runner \"puts Rails.version\"" rails_version = `bash -lc '#{rails_check}'`.strip.to_f begin rails_ver = Gem::Version.new(rails_version) rescue ArgumentError rails_ver = Gem::Version.new('0.0.0') end # only if Rails >= 7 if rails_ver >= Gem::Version.new('7.0.0') # Gems to re-add optional_gems = { 'importmap-rails' => nil, 'turbo-rails' => nil, 'stimulus-rails' => nil, 'solid_cache' => nil, 'solid_queue' => nil, 'solid_cable' => nil } gemfile = File.join(app_path, 'Gemfile') # Add gems if not already present optional_gems.each do |gem_name, version| next if File.read(gemfile).match?(/^gem ['"]#{gem_name}['"]/) File.open(gemfile, 'a') do |f| f.puts version ? "gem '#{gem_name}', '#{version}'" : "gem '#{gem_name}'" end end # Install all new gems System::Shell.run(rvm_run('bundle install', chdir: app_path)) # Run generators for installed gems generators = [ 'importmap:install', 'turbo:install stimulus:install', 'solid_cache:install solid_queue:install solid_cable:install' ] generators.each do |cmd| System::Shell.run(rvm_run("bundle exec rails #{cmd}", chdir: app_path)) end end # --- Add allowed host for Rails < 7.2 --- if @options[:rails] && @options[:rails].to_f < 7.2 env_file = File.join(app_path, 'config/environments/development.rb') if File.exist?(env_file) content = File.read(env_file) # Append host config inside the Rails.application.configure block updated_content = content.gsub(/Rails\.application\.configure do(.*?)end/mm) do |_match| inner = Regexp.last_match(1) # Prevent duplicate entries unless inner.include?(domain) inner += " # allow local .test host for this app\n config.hosts << '#{domain}'\n" end "Rails.application.configure do#{inner}end" end File.write(env_file, updated_content) else warn "Development environment file not found: #{env_file}" end end # --- Hosts, certs, caddy --- Services::HostsManager.add(domain) Services::CaddyManager.add_app(@name, skip_ssl: @options[:skip_ssl]) Services::CaddyManager.ensure_running! Services::CaddyManager.reload # --- Start Rails server --- puts "Starting Rails server for #{@name} on port #{port}..." log_file = File.join(app_path, 'log', 'stable.log') FileUtils.mkdir_p(File.dirname(log_file)) abort "Port #{port} is already in use. Choose another port." if port_in_use?(port) pid = spawn( 'bash', '-lc', "cd \"#{app_path}\" && #{rvm_cmd} bundle exec rails s -p #{port} -b 127.0.0.1", out: log_file, err: log_file ) Process.detach(pid) AppRegistry.update(@name, started_at: Time.now.to_i, pid: pid) sleep 1.5 wait_for_port(port) prefix = @options[:skip_ssl] ? 'http' : 'https' display_domain = @options[:skip_ssl] ? "#{domain}:#{port}" : domain puts "✔ #{@name} running at #{prefix}://#{display_domain}" end |