Class: Brut::CLI::Apps::New::App

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/new/app.rb,
lib/brut/cli/apps/new/old_app.rb

Defined Under Namespace

Classes: Segment

Constant Summary collapse

LOGO_WIDE =
%{
#################                                     ####      #################      ################
##################                                    ####      ##################     #################
#####        #####                                    ####      #####        ######    #####       #####
#####        #####    ####  ####  ####       ####  ##########   #####         #####    #####       #####
#####      ######     ##########  ####       ####  ##########   #####        #####     #####     ######
################      ######      ####       ####     ####      #################      ################
#####       ######    #####       ####       ####     ####      #################      #####      ######
#####         #####   ####        ####       ####     ####      #####        #####     #####        #####
#####         #####   ####        ####       ####     ####      #####        #####     #####        #####
#####        ######   ####        #####     #####     ####      #####         ####     #####       ######
##################    ####         ##############     #######   #####         ####     #################
###############       ####          #######  ####      ######   #####         #####    ##############
}.strip
LOGO_NARROW =
%{
#################                                     ####
##################                                    ####
#####        #####                                    ####
#####        #####    ####  ####  ####       ####  ##########
#####      ######     ##########  ####       ####  ##########
################      ######      ####       ####     ####
#####       ######    #####       ####       ####     ####
#####         #####   ####        ####       ####     ####
#####         #####   ####        ####       ####     ####
#####        ######   ####        #####     #####     ####
##################    ####         ##############     #######
###############       ####          #######  ####      ######
}.strip

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #options, #puts, #stdin, #system!, #theme

Constructor Details

#initialize(current_dir:, app_options:, out:, err:) ⇒ App

Returns a new instance of App.



4
5
6
7
8
9
10
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
# File 'lib/brut/cli/apps/new/old_app.rb', line 4

def initialize(current_dir:, app_options:, out:, err:)
  @out = out
  @app_options = app_options

  @out.puts "Creating app with these options:\n"
  @out.puts "App name:      #{app_options.app_name}"
  @out.puts "App ID:        #{app_options.app_id}"
  @out.puts "Prefix:        #{app_options.prefix}"
  @out.puts "Organization:  #{app_options.organization}"
  @out.puts "Include demo?  #{app_options.demo}\n"

  if app_options.dry_run?
    @out.puts "Dry Run"
    Brut::CLI::Apps::New::Ops::BaseOp.dry_run = true
  end

  templates_dir = Pathname(
    Gem::Specification.find_by_name("brut").gem_dir
  ) / "templates"

  @base = Brut::CLI::Apps::New::Base.new(
    app_options:,
    current_dir:,
    templates_dir:
  )
  @segments = [
    Brut::CLI::Apps::New::Segments::BareBones.new(
      app_options:,
      current_dir:,
      templates_dir:,
    ),
  ]
  if app_options.demo?
    @segments << Brut::CLI::Apps::New::Segments::Demo.new(
      app_options:,
      current_dir:,
      templates_dir:
    )
  end
  app_options.segments.each do |segment|
    case segment
    when "heroku"
      @segments << Brut::CLI::Apps::New::Segments::Heroku.new(
        project_root: @base.project_root,
        templates_dir:
      )
    when "sidekiq"
      @segments << Brut::CLI::Apps::New::Segments::Sidekiq.new(
        project_root: @base.project_root,
        templates_dir:
      )
    else
      raise "Segment #{segment} is not supported"
    end
  end
  @segments.sort!
end

Instance Method Details

#acceptsObject



40
41
42
43
44
45
# File 'lib/brut/cli/apps/new/app.rb', line 40

def accepts = [
  Brut::CLI::Apps::New::Prefix,
  Brut::CLI::Apps::New::AppId,
  Brut::CLI::Apps::New::Organization,
  [ Pathname, ->(value) { Pathname(value) } ],
]

#args_descriptionObject



9
# File 'lib/brut/cli/apps/new/app.rb', line 9

def args_description = "app_name"

#create!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brut/cli/apps/new/old_app.rb', line 62

def create!
  @out.puts "Creating Base app"
  @base.create!
  @segments.each do |segment|
    @out.puts "Creating segment: #{segment.class.friendly_name}"
    segment.add!
  end
  @out.puts "#{@app_options.app_name} was created\n\n"
  @out.puts "Time to get building:"
  @out.puts "1. cd #{@app_options.app_name}"
  @out.puts "2. dx/build"
  @out.puts "3. dx/start"
  @out.puts "4. [ in another terminal ] dx/exec bash"
  @out.puts "5. [ inside the Docker container ] bin/setup"
  @out.puts "6. [ inside the Docker container ] bin/dev"
  @out.puts "7. Visit http://localhost:6502 in your browser"
  @out.puts "8. [ inside the Docker container ] bin/setup help # to see more commands"
end

#descriptionObject



8
# File 'lib/brut/cli/apps/new/app.rb', line 8

def description = "Create a Brut App or modify an existing one with new segments"

#nameObject



7
# File 'lib/brut/cli/apps/new/app.rb', line 7

def name = "new"

#optsObject



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
# File 'lib/brut/cli/apps/new/app.rb', line 47

def opts = [
  [
    "--dir=DIR",
    Pathname,
    "Path where you want your app created. Default is the current directory",
  ],
  [ 
    "--app-id=ID",
    Brut::CLI::Apps::New::AppId,
    "App identifier, which must be able to be used as a hostname or other Internet identifier. Derived from your app name, if omitted",
  ],
  [
    "--organization=ORG",
    Brut::CLI::Apps::New::Organization,
    "Organization name, e.g. what you'd use for GitHub. Defaults to the app-id value",
  ],
  [
    "--[no-]interactive",
    "Set if you want to be prompted before the app is actually created",
  ],
  [
    "--prefix=PREFIX",
    Brut::CLI::Apps::New::Prefix,
    "Two-character prefix for external IDs and autonomous custom elements. Derived from your app-id, if omitted.",
  ],
  [ 
    "--segments=SEGMENTS",
    Array,
    "Comma-delimited list of segment names to add additional behavior to your new app. Current values: heroku, sidekiq, demo",
  ],
  [ 
    "--dry-run",
    "Only show what would happen, don't actually do anything",
  ],
  [
    "--[no-]demo",
    "Include, or not, additional files that demonstrate Brut's features (default is true for now)",
  ],
]

#runObject



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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/brut/cli/apps/new/app.rb', line 87

def run

  app_name = argv[0]

  if !app_name
    error "app_name is required"
    return 1
  end

  if terminal.cols >= LOGO_WIDE.lines[0].length
    puts theme.title.render("WELCOME TO")
    puts
    puts theme.title.render(LOGO_WIDE)
  elsif terminal.cols >= LOGO_NARROW.lines[0].length
    puts theme.title.render("WELCOME TO")
    puts
    puts theme.title.render(LOGO_NARROW)
  else
    puts theme.title.render("WELCOME TO BRUT")
  end

  options.set_default(:app_id, Brut::CLI::Apps::New::AppId.from_app_name(app_name))
  options.set_default(:prefix, Brut::CLI::Apps::New::Prefix.from_app_id(options.app_id))
  options.set_default(:organization, Brut::CLI::Apps::New::Prefix.from_app_id(options.app_id))
  options.set_default(:demo, true)
  options.set_default(:dir,Pathname.pwd)
  options.set_default(:interactive, true)

  segment_names = Set.new(options.segments)
  if options.demo?
    segment_names << "demo"
  end

  current_dir = options.dir.expand_path
  versions = Brut::CLI::Apps::New::Versions.new

  templates_dir = Pathname(
    Gem::Specification.find_by_name("brut").gem_dir
  ) / "templates"

  base = Brut::CLI::Apps::New::Base.new(
    app_name:,
    options:,
    versions:,
    current_dir:,
    templates_dir:
  )
  segments = [
    Brut::CLI::Apps::New::Segments::BareBones.new(
      app_name:,
      options:,
      versions:,
      current_dir:,
      templates_dir:,
    ),
  ]
  if options.demo?
    segments << Brut::CLI::Apps::New::Segments::Demo.new(
      app_name:,
      options:,
      versions:,
      current_dir:,
      templates_dir:
    )
  end
  segment_names.each do |segment|
    case segment
    when "heroku"
      segments << Brut::CLI::Apps::New::Segments::Heroku.new(
        project_root: base.project_root,
        templates_dir:
      )
    when "sidekiq"
      segments << Brut::CLI::Apps::New::Segments::Sidekiq.new(
        project_root: base.project_root,
        templates_dir:
      )
    when "demo"
      # handled above
    else
      raise "Segment #{segment} is not supported"
    end
  end
  segments.sort!

  info "Creating a new Brut app with these options:"
  rows = [
    ["App Name",  app_name],
    ["Path to New App", current_dir / app_name],
    ["App Id", options.app_id ],
    ["Prefix", options.prefix ],
    ["Organization", options.organization ],
    ["Segments", segments.map(&:class).map(&:segment_name).join(", ") ],
  ]
  rows.each do |(attr,val)|
    info "  #{attr}: #{val}"
  end
  if options.interactive?
    puts
    puts theme.subheader.render("Options for your new app:")
    table = Lipgloss::Table.new.headers(["Attribute", "Value"]).
      rows(rows).
      border(:rounded).
      style_func(rows: rows.size, columns: 2) do |row,column|
        if row == Lipgloss::Table::HEADER_ROW
          if column == 0
            Lipgloss::Style.new.inherit(theme.header).align_horizontal(:right).padding_right(1).padding_left(1)
          else
            Lipgloss::Style.new.inherit(theme.header).align_horizontal(:left).padding_right(1).padding_left(1)
          end
        elsif column == 0
          Lipgloss::Style.new.inherit(theme.subheader).align_horizontal(:right).padding_right(1).padding_left(1)
        else
          Lipgloss::Style.new.inherit(theme.none).align_horizontal(:left).padding_right(1).padding_left(1)
        end
      end

    puts table.render

    puts "Proceed? (y/n): "
    answer = stdin.gets.strip.downcase
    if answer != "y"
      puts theme.warning.render("Aborting app creation")
      return 0
    end
  end

  if options.dry_run?
    info "Dry Run only"
    Brut::CLI::Apps::New::Ops::BaseOp.dry_run = true
  end

  info "Creating Base app"
  base.create!
  segments.each do |segment|
    info "Creating segment: #{segment.class.friendly_name}"
    segment.add!
  end

  puts
  print theme.header.render("Your app ")
  print theme.subheader.render(app_name)
  print theme.header.render(" was created - time to get building!")
  puts
  puts

  in_computer = Lipgloss::List.new.items(
    [
      theme.code.render("cd #{current_dir / app_name}"),
      theme.code.render("dx/build"),
      theme.code.render("dx/start"),
      "#{theme.weak.render("(in another terminal)")} #{theme.code.render('dx/exec bash')}",
    ]
  ).enumerator(:arabic).enumerator_style(theme.bullet(1))
  in_docker = Lipgloss::List.new.items(
    [
      theme.code.render("bin/setup"),
      theme.code.render("bin/dev"),
    ]
  ).enumerator(:arabic).enumerator_style(theme.bullet(1))
  list = Lipgloss::List.new.items([
    "On your computer:\n#{in_computer.render}",
    "Inside the Docker container:\n#{in_docker.render}",
    "Navigate to #{theme.url.render('http://localhost:6502')} to see your app",
    "Inside the Docker container, try #{theme.code.render('bin/setup')} help to find more commands",
  ]).enumerator(:arabic).
  enumerator_style(theme.bullet(0))
  puts list.render
  puts
end