-
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.
Merge pull request #18 from aristotelesbr/development
Development
- Loading branch information
Showing
15 changed files
with
445 additions
and
5 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
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
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
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
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 |
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,97 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2023, by Aristóteles Coutinho. | ||
|
||
require 'console' | ||
|
||
# This middleware is used to reload files in development mode. | ||
# | ||
module Lenna | ||
module Middleware | ||
module Default | ||
class Reload | ||
attr_accessor :directories, :files_mtime | ||
|
||
# Initializes a new instance of Middleware::Default::Reload. | ||
# | ||
# @parameter directories [Array] An array of directories to monitor. | ||
# | ||
# @return [Middleware::Default::Reload] A new instance of Middleware::Default::Reload. | ||
def initialize(directories = []) | ||
self.files_mtime = {} | ||
self.directories = directories | ||
|
||
monitor_directories(directories) | ||
end | ||
|
||
# Calls the middleware. | ||
# | ||
# @parameter req [Rack::Request] The request. | ||
# @parameter _res [Rack::Response] The response. | ||
# @parameter next_middleware [Proc] The next middleware. | ||
# | ||
# @return [void] | ||
# | ||
def call(_req, _res, next_middleware) | ||
reload_if_needed | ||
|
||
next_middleware.call | ||
rescue ::StandardError => error | ||
::Console.error(self, error) | ||
end | ||
|
||
private | ||
|
||
# Reloads files if needed. | ||
# | ||
# @return [void] | ||
# | ||
def reload_if_needed | ||
modified_files = check_for_modified_files | ||
|
||
reload_files(modified_files) unless modified_files.empty? | ||
end | ||
|
||
# Monitors directories for changes. | ||
# | ||
# @parameter directories [Array] An array of directories to monitor. | ||
# | ||
# @return [void] | ||
# | ||
def monitor_directories(directories) | ||
directories.each do |directory| | ||
::Dir.glob(directory).each { |file| files_mtime[file] = ::File.mtime(file) } | ||
end | ||
end | ||
|
||
# Checks for modified files. | ||
# | ||
# @return [Array] An array of modified files. | ||
# | ||
# @example | ||
# check_for_modified_files #=> ["/path/to/file.rb"] | ||
# | ||
def check_for_modified_files | ||
@files_mtime.select do |file, last_mtime| | ||
::File.mtime(file) > last_mtime | ||
end.keys | ||
end | ||
|
||
# Reloads files. | ||
# | ||
# @parameter files [Array] An array of files(paths) to reload. | ||
# | ||
# @return [void] | ||
# | ||
def reload_files(files) | ||
files.each do |file| | ||
::Console.debug("Reloading #{file}") | ||
::Kernel.load file | ||
@files_mtime[file] = ::File.mtime(file) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.