-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic interface to create a new project
- Loading branch information
1 parent
985cef5
commit 664118e
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |