Skip to content

Commit

Permalink
feat: add basic interface to create a new project
Browse files Browse the repository at this point in the history
  • Loading branch information
aristotelesbr committed Dec 21, 2023
1 parent 985cef5 commit 664118e
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/lenna/cli/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Aristóteles Coutinho.

require 'optparse'

require 'lennarb/version'

module Lenna
module Cli
# Mediator class for CLI
#
# @private `Since v0.1.0`
#
class App
# @return [Array] Arguments passed to CLI
#
attr_reader :args

# Initialize a new instance of the class
#
# @parameter args [Array] Arguments passed to CLI
#
def initialize(args)
@args = args
end

# Execute the command
#
# @return [void]
#
def execute(strategy)
strategy.is_a?(Commands::Interface) or fail ::ArgumentError

parser!(@args).then { strategy.execute(_1) }
end

private

# Parse the options passed to CLI
#
# @parameter args [Array] Arguments passed to CLI
#
# @return [Hash] Options passed to CLI
#
def parser!(args)
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: lenna [options]'

opts.on('-v', '--version', 'Print version') do
puts Lenna::VERSION
exit
end

opts.on('-h', '--help', 'Print help') do
puts opts
exit
end

opts.on('-n', '--new', 'Create a new app') do
options[:new] = true
end
end.parser!(args)

options
end
end
end
end
64 changes: 64 additions & 0 deletions lib/lenna/cli/commands/create_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'colorize'
require 'erb'
require 'fileutils'
require 'lenna/cli/commands/interface'
require 'lennarb/version'

module Lenna
module Cli
module Commands
# Command for creating a new app
#
# @private `Since v0.1.0`
#
module CreateProject
extend Lenna::Cli::Commands::Interface, self
# Execute the command
#
# @parameter app_name [String] The name of the app
#
# @return [void]
#
def execute(app_name)
return puts 'Please specify an app name' if app_name.nil?

puts "Creating a new app named #{app_name}".green
create_app(app_name)
create_gemfile(app_name)
end

private

# Create a directory for the app
#
# @parameter app_name [String] The name of the app
#
# @return [void]
#
def create_app(app_name) = FileUtils.mkdir_p(app_name)

# Create a new Gemfile for the app. This will be use template
# file in the `templates` directory.
#
# @parameter app_name [String] The name of the app
#
# @return [void]
#
def create_gemfile(app_name)
FileUtils.cd(app_name).tap do
template_data = { version: Lennarb::VERSION }

erb_template =
Lennarb.root.join('lib/lenna/cli/templates/gemfile.erb')
.then { File.read(_1) }
.then { ERB.new(_1) }

File.write('Gemfile', erb_template.result_with_hash(template_data))
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/lenna/cli/commands/interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Lenna
module Cli
module Commands
module Interface
def self.execute(args)
raise NotImplementedError
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/lenna/cli/templates/gemfile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# [https://rubygems.org/gems/lennarb]
# Lenna is a lightweight and experimental web framework for Ruby. It's designed
# to be modular and easy to use. Also, that's how I affectionately call my wife.
gem 'lennarb', '~> <%= version %>'
# [https://rubygems.org/gems/falcon]
# A fast, asynchronous, rack-compatible web server.
gem 'falcon', '~> 0.42.3'

group :development, :test do
end

0 comments on commit 664118e

Please sign in to comment.