Class: Rubee::CLI::Attach

Inherits:
Object
  • Object
show all
Defined in:
lib/rubee/cli/attach.rb

Class Method Summary collapse

Class Method Details

.attach(argv) ⇒ Object



11
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
# File 'lib/rubee/cli/attach.rb', line 11

def attach(argv)
  new_app_name = argv[1]

  # create project folde
  if new_app_name.nil?
    color_puts('Please indicate app name.', color: :red)
    exit(1)
  end

  if new_app_name == 'rubee'
    color_puts("Error: App 'rubee' name is reserved", color: :red)
    exit(1)
  end
  target_dir = File.join(Rubee::APP_ROOT, new_app_name)

  if Dir.exist?(target_dir)
    color_puts("Error: App #{new_app_name} already exists!", color: :red)
    exit(1)
  end

  # create controllers models view folders
  FileUtils.mkdir_p(target_dir)
  # creare controllers models views dirs
  ['controllers', 'models', 'views'].each do |dir|
    FileUtils.mkdir_p("#{target_dir}/#{dir}")
  end

  config_file = <<~CONFIG_FILE
    Rubee::Configuration.setup(env = :test, app = :#{new_app_name}) do |config|
    end
    Rubee::Configuration.setup(env = :developmenti, app = :#{new_app_name}) do |config|
    end
    Rubee::Configuration.setup(env = :production, app = :#{new_app_name}) do |config|
    end
  CONFIG_FILE

  File.open("#{target_dir}/#{new_app_name}_configuration.rb", 'w') do |file|
    file.puts config_file
  end

  # create app routes.rb file
  route_file = <<~ROUTE_FILE
    Rubee::Router.draw do |router|
    end
  ROUTE_FILE

  File.open("#{target_dir}/#{new_app_name}_routes.rb", 'w') do |file|
    file.puts route_file
  end

  # copy views
  copy_files(
    File.join(Rubee::ROOT_PATH, '/lib/app/views'),
    "#{target_dir}/views",
    %w[welcome_header.erb welcome_show.erb App.tsx],
    %w[utils],
    app_name: new_app_name
  )

  # create namespace module
  module_content = <<~RUBY
    module #{new_app_name.camelize}
    end
  RUBY

  File.open("#{target_dir}/#{new_app_name.snakeize}_namespace.rb", 'w') { |file| file.write(module_content) }
  color_puts("App #{new_app_name} attached!", color: :green)

  # create react app file
  react_app_file = <<~REACT_APP_FILE
    import React from 'react';

    export default function #{new_app_name.camelize}App() {
      return (
        <h1>#{new_app_name.camelize}App</h1>
      );
    }
  REACT_APP_FILE

  File.open("#{target_dir}/views/#{new_app_name.camelize}App.tsx", 'w') do |file|
    file.puts react_app_file
  end
end

.call(command, argv) ⇒ Object



7
8
9
# File 'lib/rubee/cli/attach.rb', line 7

def call(command, argv)
  send(command, argv)
end