#!/usr/bin/env ruby ################################################################################## # RBCli -- A framework for developing command line applications in Ruby # # Copyright (C) 2018 Andrew Khoury # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # For questions regarding licensing, please contact andrew@blacknex.us # ################################################################################## require "#{File.dirname(__FILE__)}/../lib/rbcli.rb" Rbcli::Configurate.me do scriptname __FILE__.split('/')[-1] version Rbcli::VERSION description 'RBCli initialization tool' autoupdate gem: 'rbcli', force_update: false, message: "Please run `gem update rbcli` to upgrade to the latest version." end require "#{File.dirname(__FILE__)}/../lib/rbcli-tool.rb" class Docs < Rbcli::Command description 'Show Documentation (Beta)' action do readme = "#{File.dirname(__FILE__)}/../README.md" begin CLIMarkdown::Converter.new([readme]) rescue Errno::EPIPE # Empty end end end class License < Rbcli::Command description 'Shows Copyright and License Info' usage nil parameter :full, 'Display Full License Text', default: false action do |params| puts File.read("#{File.dirname(__FILE__)}/../LICENSE.txt") if params[:full] puts <<-EOF RBCli Copyright (C) 2018 Andrew Khoury This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. If you wish to release this library as part of commercial software, please contact andrew@blacknex.us. EOF puts "Type `rbcli license --full' to show the license text." unless params[:full] end end class Init < Rbcli::Command description 'Initialize a skeleton RBCli project.' usage <<-EOF This will generate a new project structure under the current directory. Use -t to specify the type: Standard: A complete RBCli project structure. Recommended for most applications, it provides a framework for organizing code and creating a gem to be installed/distributed. Mini: A single-file RBCli project. All features are supported, but project structure is left to the developer. Recommended for smaller applications, or for integrating RBCli into an existing application. Micro: A single-file, minimal RBCli project. Similar to a mini project, but only the minimal required code is generated. Recommended for rapid prototyping of scripts, for advanced users only. EOF parameter :name, 'Name of project to generate', type: :string, default: File.basename(Dir.pwd), required: true, prompt: 'Enter a name' parameter :type, 'Specify project type', type: :string, permitted: %w(standard micro mini), default: 'standard' parameter :description, 'A description of the project', type: :string, default: 'TODO: Description goes here' action do |params, args, global_opts, config| dest = Dir.pwd # Prepare template vars template_vars = { cmdname: params[:name], description: params[:description], rbcli_version: Rbcli::VERSION } proj = RBCliTool::Project.new(dest, template_vars) # if proj.exists? # RBCliTool.continue_confirmation "The project or file #{params[:name]} already exists; contents will be overwritten." # FileUtils.rm_rf dest # end case params[:type] when 'micro' # Micro: Single File, simplified success = proj.create_micro when 'mini' # Mini: Single File success = proj.create_mini else # Standard; full project structure success = proj.create end # END case params[:type] if success puts "\nInitialization Complete!\n" else puts "\nAn RBCli Project already exists in the current directory tree at: #{proj.exists?}. Please delete the project or run this command from a different directory.\n" end end end class Command < Rbcli::Command description 'Generate an Rbcli Command under the current project' usage <<-EOF This will generate a new command under the current RBCli project. Please run this command from within the project's directory structure. EOF parameter :name, 'Name of command to generate', type: :string, required: true parameter :description, 'A short description of the command', type: :string, default: 'TODO: Description goes here' parameter :usagetext, 'Usage help text for the command', type: :string, default: 'TODO: Usage text goes here' action do |params, args, global_opts, config| # First we check that we are working within a project folder project_root = RBCliTool::Project.find_root(Dir.pwd) RBCliTool.exit_with_error("Can't find the project root in the current path. Please `cd` to your project's directory before running this command, and ensure that the `.rbcli` file is present in its root path.") unless project_root # Prepare template vars template_vars = { name: params[:name], description: params[:description], usage_text: params[:usagetext], rbcli_version: Rbcli::VERSION } generator = RBCliTool::Generator.new(:command, project_root, template_vars) generator.run end end class Script < Rbcli::Command description 'Generate an Rbcli Command with Bash Script under the current project' usage <<-EOF This will generate a new command under the current RBCli project, and a skeleton bash script to code it. Use this option if you prefer to use bash (or another shell) to script your command. Please run this command from within the project's directory structure. EOF parameter :name, 'Name of command to generate', type: :string, required: true parameter :description, 'A short description of the command', type: :string, default: 'TODO: Description goes here' parameter :usagetext, 'Usage help text for the command', type: :string, default: 'TODO: Usage text goes here' action do |params, args, global_opts, config| # First we check that we are working within a project folder project_root = RBCliTool::Project.find_root(Dir.pwd) RBCliTool.exit_with_error("Can't find the project root in the current path. Please `cd` to your project's directory before running this command, and ensure that the `.rbcli` file is present in its root path.") unless project_root # Prepare template vars template_vars = { name: params[:name], description: params[:description], usage_text: params[:usagetext], no_script: false, rbcli_version: Rbcli::VERSION } generator = RBCliTool::Generator.new(:extern, project_root, template_vars) generator.run end end class Extern < Rbcli::Command description 'Generate an Rbcli Command that can wrap an external application' usage <<-EOF This will generate a new command under the current RBCli project Use this option if you need to integrate with a third party application. Please run this command from within the project's directory structure. EOF parameter :name, 'Name of command to generate', type: :string, required: true parameter :description, 'A short description of the command', type: :string, default: 'TODO: Description goes here' parameter :usagetext, 'Usage help text for the command', type: :string, default: 'TODO: Usage text goes here' action do |params, args, global_opts, config| # First we check that we are working within a project folder project_root = RBCliTool::Project.find_root(Dir.pwd) RBCliTool.exit_with_error("Can't find the project root in the current path. Please `cd` to your project's directory before running this command, and ensure that the `.rbcli` file is present in its root path.") unless project_root # Prepare template vars template_vars = { name: params[:name], description: params[:description], usage_text: params[:usagetext], no_script: true, rbcli_version: Rbcli::VERSION } generator = RBCliTool::Generator.new(:extern, project_root, template_vars) generator.run end end class Hook < Rbcli::Command description 'Generate an Rbcli Hook under the current project' usage <<-EOF This will generate one or more execution hooks for the current project. Default: The default hook runs whenever the user does not specify a command. If the hook is not present, the default behavior is to show the help text (same as -h). Pre: The pre-execution hook, which runs code prior to the execution of any command. This allows for actions such as authentiation into remote systems. Post: The post-execution hook, which runs code after the execution of any command. First-run: The first-run hook will run only on the first time the user runs the application. This is useful for initial setup or for welcome messages to be shown. Note that if you enable the first-run hook, you must also enable local_state (see config/storage.rb). Please run this command from within the project's directory structure. EOF parameter :default, 'Generate the default (no-command-entered) hook', type: :boolean, default: false parameter :pre, 'Generate the pre-execution hook', type: :boolean, default: false parameter :post, 'Generate the post-execution hook', type: :boolean, default: false parameter :firstrun, 'Generate the first-run hook', type: :boolean, default: false action do |params, args, global_opts, config| # First we check that we are working within a project folder project_root = RBCliTool::Project.find_root(Dir.pwd) RBCliTool.exit_with_error("Can't find the project root in the current path. Please `cd` to your project's directory before running this command, and ensure that the `.rbcli` file is present in its root path.") unless project_root generated_something = false [:default, :pre, :post, :firstrun].each do |pname| next unless params[pname] generator = RBCliTool::Generator.new(:hook, project_root, pname) generator.run generated_something = true end RBCliTool.exit_with_error("No hooks specified. Please run `rbcli hooks -h` for details on how to generate hooks.") unless generated_something end end Rbcli.parse