#! /usr/bin/env ruby # frozen_string_literal: true require 'bundler' begin Bundler.setup :default, :test Bundler.require :default, :test rescue Bundler::GemfileNotFound # no-op: we might be installed as a system gem end if ARGV[0] == 'version' require 'praxis/version' puts "Praxis version #{Praxis::VERSION}" exit 0 end if %w[routes docs console].include? ARGV[0] require 'rake' require 'praxis' require 'praxis/tasks' load 'Rakefile' # Ensure that we read the App's Rakefile, to pickup any definitions etc. case ARGV[0] when 'routes' Rake::Task['praxis:routes'].invoke(ARGV[1]) when 'docs' task_name = case ARGV[1] when nil, 'browser' 'praxis:docs:preview' when 'generate' 'praxis:docs:generate' when 'package' 'praxis:docs:package' end Rake::Task[task_name].invoke when 'console' Rake::Task['praxis:console'].invoke end exit 0 end # Thor tasks path_to_praxis = File.expand_path(File.dirname(File.dirname(__FILE__))) path_to_loader = format('%s/tasks/loader.thor', path: path_to_praxis) load path_to_loader class PraxisGenerator < Thor SCAFFOLD_CONFIG_FILE = "#{Dir.pwd}/.praxis_scaffold" # Include a few fake thor action descriptions (for the rake tasks above) so they can show up in the same usage messages desc 'routes [json]', 'Prints the route table of the application. Defaults to table format, but can produce json' def routes; end desc 'docs [generate|browser|package]', <<~HELP Generates API documentation and a Web App to inspect it generate - Generates the JSON docs browser - (default) Generates JSON docs, and automatically starts a Web app to browse them. package - Generates JSON docs, and neatly packages all the necessary static files ready for exporting the browsing app. HELP def docs; end desc 'console', 'Open a console to the application, with its environment loaded' def console; end # Simple helper to go get the existing description for the real action # Usage must still be provided rather than retrieved (since it is not a # straight "usage" from the remote action when arguments are defined ) def self.desc_for(usage_string, klass, action_name, description_prefix = '') action_name = action_name.to_s cmd = klass.commands[action_name] raise "Error, could not find description for #{klass.name}##{action_name}" if cmd.nil? desc usage_string, "#{description_prefix}#{cmd.description}" end desc_for 'new APP_NAME', ::PraxisGen::App, :new def new(app_name) gen = ::PraxisGen::App.new([app_name]) gen.destination_root = app_name gen.invoke_all end desc_for 'example APP_NAME', ::PraxisGen::Example, :example def example(app_name) gen = ::PraxisGen::Example.new([app_name]) gen.destination_root = app_name gen.invoke(:example) end desc_for 'g COLLECTION_NAME', ::PraxisGen::Scaffold, :g # Cannot use the argument below or it will apply to all commands (the action in the class has it) # argument :collection_name, required: false # The options, however, since they're optional are fine (But need to be duplicated from the class :( ) option :base, required: false, desc: 'Module name to enclose all generated files. Empty by default. You can pass things like MyApp, or MyApp::SubModule' option :version, required: false, desc: 'Version string for the API endpoint. This also dictates the directory structure (i.e., v1/endpoints/...))' option :design, type: :boolean, default: true, desc: 'Include the Endpoint and MediaType files for the collection' option :implementation, type: :boolean, default: true, desc: 'Include the Controller and (possibly the) Resource files for the collection (see --no-resource)' option :resource, type: :boolean, default: true, desc: 'Disable (or enable) the creation of the Resource files when generating implementation' option :model, type: :string, enum: %w[activerecord sequel], desc: 'It also generates a model for the given ORM. An empty --model flag will default to activerecord' option :actions, type: :string, default: 'crud', enum: %w[cr cru crud u ud d], desc: 'Specifies the actions to generate for the API. cr=create, u=update, d=delete. Index and show actions are always generated' def g(*args) # Because we cannot share the :collection_name argument, we need to do this check here, before # we "parse" it and pass it to the g command unless args.size == 1 ::PraxisGen::Scaffold.command_help(shell, :g) exit 1 end collection_name, = args ::PraxisGen::Scaffold.new([collection_name], options).invoke(:g) return unless options[:model] # Make it easy to be able to both enable or not enable the creation of the model, by passing --model=... # but also make it easy so that if there is no value for it, it default to activerecord opts = { orm: options[:model] } opts[:orm] = 'activerecord' if opts[:orm] == 'model' # value is model param passed by no value ::PraxisGen::Model.new([collection_name.singularize], opts).invoke(:g) self.class.save_scaffolding_config end # Read and pass the hash around in the class, so callers can read and modify it if desired # Final contents will be saved at the end of scaffolding generation def self.scaffold_config return @current_config if @current_config @current_config = File.exist?(SCAFFOLD_CONFIG_FILE) ? JSON.parse(File.read(SCAFFOLD_CONFIG_FILE), symbolize_names: true) : {} end def self.save_scaffolding_config if File.exist?(SCAFFOLD_CONFIG_FILE) contents_from_file = JSON.parse(File.read(SCAFFOLD_CONFIG_FILE), symbolize_names: true) return if contents_from_file == @current_config end puts "Saving new scaffolding config into #{SCAFFOLD_CONFIG_FILE}" File.write(SCAFFOLD_CONFIG_FILE, JSON.pretty_generate(scaffold_config)) end # Initially, the idea was to build some quick model generator, but I think it's better to keep it # simple and just use the scaffold generator with `--no-implementation --no-design --model` instead # Left here in case we want to rescue it # desc_for "gmodel MODEL_NAME", ::PraxisGen::Model, :g # # Cannot use the argument below or it will apply to all commands (the action in the class has it) # # argument :collection_name, required: false # # The options, however, since they're optional are fine (But need to be duplicated from the class :( ) # option :orm, required: false, default: 'activerecord', enum: ['activerecord','sequel'], # desc: 'Type of ORM model to create.' # def gmodel(*args) # # Because we cannot share the :collection_name argument, we need to do this check here, before # # we "parse" it and pass it to the g command # unless args.size == 1 # ::PraxisGen::Model.command_help(shell,:g) # exit 1 # end # model_name,_ = args # ::PraxisGen::Model.new([model_name],options).invoke(:g) # end end PraxisGenerator.start(ARGV)