Class: Tina4::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tina4/cli.rb

Instance Method Summary collapse

Instance Method Details

#consoleObject



147
148
149
150
151
152
153
154
# File 'lib/tina4/cli.rb', line 147

def console
  require_relative "../tina4"
  Tina4.initialize!(Dir.pwd)
  load_routes(Dir.pwd)

  require "irb"
  IRB.start
end

#init(name = ".") ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/tina4/cli.rb', line 9

def init(name = ".")
  dir = name == "." ? Dir.pwd : File.join(Dir.pwd, name)
  FileUtils.mkdir_p(dir)

  create_project_structure(dir)
  create_sample_files(dir, name == "." ? File.basename(Dir.pwd) : name)

  puts "Tina4 project initialized in #{dir}"
  puts "Run 'cd #{name} && bundle install && tina4 start' to get started" unless name == "."
end

#migrateObject



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
# File 'lib/tina4/cli.rb', line 72

def migrate
  require_relative "../tina4"
  Tina4.initialize!(Dir.pwd)

  db = Tina4.database
  unless db
    puts "No database configured. Set DATABASE_URL in your .env file."
    return
  end

  migration = Tina4::Migration.new(db)

  if options[:create]
    path = migration.create(options[:create])
    puts "Created migration: #{path}"
  elsif options[:rollback]
    migration.rollback(options[:rollback])
    puts "Rolled back #{options[:rollback]} migration(s)"
  else
    results = migration.run
    if results.empty?
      puts "No pending migrations"
    else
      results.each do |r|
        status_icon = r[:status] == "success" ? "OK" : "FAIL"
        puts "  [#{status_icon}] #{r[:name]}"
      end
    end
  end
end

#routesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tina4/cli.rb', line 131

def routes
  require_relative "../tina4"
  Tina4.initialize!(Dir.pwd)
  load_routes(Dir.pwd)

  puts "\nRegistered Routes:"
  puts "-" * 60
  Tina4::Router.routes.each do |route|
    auth = route.auth_handler ? " [AUTH]" : ""
    puts "  #{route.method.ljust(8)} #{route.path}#{auth}"
  end
  puts "-" * 60
  puts "Total: #{Tina4::Router.routes.length} routes\n"
end

#startObject



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
# File 'lib/tina4/cli.rb', line 24

def start
  require_relative "../tina4"

  root_dir = Dir.pwd
  Tina4.initialize!(root_dir)

  # Load route files
  load_routes(root_dir)

  if options[:dev]
    Tina4::DevReload.start(root_dir: root_dir)
    Tina4::ScssCompiler.compile_all(root_dir)
  end

  app = Tina4::RackApp.new(root_dir: root_dir)

  # Try Puma first (production-grade), fall back to WEBrick
  begin
    require "puma"
    require "puma/configuration"
    require "puma/launcher"

    puma_host = options[:host]
    puma_port = options[:port]

    config = Puma::Configuration.new do |user_config|
      user_config.bind "tcp://#{puma_host}:#{puma_port}"
      user_config.app app
      user_config.threads 0, 16
      user_config.workers 0
      user_config.environment "development"
      user_config.log_requests false
      user_config.quiet
    end

    Tina4::Debug.info("Starting Puma server on http://#{puma_host}:#{puma_port}")
    launcher = Puma::Launcher.new(config)
    launcher.run
  rescue LoadError
    Tina4::Debug.info("Puma not found, falling back to WEBrick")
    server = Tina4::WebServer.new(app, host: options[:host], port: options[:port])
    server.start
  end
end

#testObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tina4/cli.rb', line 104

def test
  require_relative "../tina4"
  Tina4.initialize!(Dir.pwd)

  # Load test files
  test_dirs = %w[tests test spec src/tests]
  test_dirs.each do |dir|
    test_dir = File.join(Dir.pwd, dir)
    next unless Dir.exist?(test_dir)
    Dir.glob(File.join(test_dir, "**/*_test.rb")).sort.each { |f| load f }
    Dir.glob(File.join(test_dir, "**/test_*.rb")).sort.each { |f| load f }
  end

  # Also load inline tests from routes
  load_routes(Dir.pwd)

  results = Tina4::Testing.run_all
  exit(1) if results[:failed] > 0 || results[:errors] > 0
end

#versionObject



125
126
127
128
# File 'lib/tina4/cli.rb', line 125

def version
  require_relative "version"
  puts "Tina4 Ruby v#{Tina4::VERSION}"
end