From 664118eece3cdf12962fd5a00f406d53c9b9417e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Thu, 21 Dec 2023 11:02:50 -0300 Subject: [PATCH] feat: add basic interface to create a new project --- lib/lenna/cli/app.rb | 71 ++++++++++++++++++++++++ lib/lenna/cli/commands/create_project.rb | 64 +++++++++++++++++++++ lib/lenna/cli/commands/interface.rb | 13 +++++ lib/lenna/cli/templates/gemfile.erb | 14 +++++ 4 files changed, 162 insertions(+) create mode 100644 lib/lenna/cli/app.rb create mode 100644 lib/lenna/cli/commands/create_project.rb create mode 100644 lib/lenna/cli/commands/interface.rb create mode 100644 lib/lenna/cli/templates/gemfile.erb diff --git a/lib/lenna/cli/app.rb b/lib/lenna/cli/app.rb new file mode 100644 index 0000000..783f123 --- /dev/null +++ b/lib/lenna/cli/app.rb @@ -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 diff --git a/lib/lenna/cli/commands/create_project.rb b/lib/lenna/cli/commands/create_project.rb new file mode 100644 index 0000000..8902d56 --- /dev/null +++ b/lib/lenna/cli/commands/create_project.rb @@ -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 diff --git a/lib/lenna/cli/commands/interface.rb b/lib/lenna/cli/commands/interface.rb new file mode 100644 index 0000000..df061f8 --- /dev/null +++ b/lib/lenna/cli/commands/interface.rb @@ -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 diff --git a/lib/lenna/cli/templates/gemfile.erb b/lib/lenna/cli/templates/gemfile.erb new file mode 100644 index 0000000..d6a8a4c --- /dev/null +++ b/lib/lenna/cli/templates/gemfile.erb @@ -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