#!/usr/bin/env ruby SCRIPT_RAILS = File.join('bin', 'rails') require 'tty-prompt' prompt = TTY::Prompt.new prompt.warn('Load Rails App Env now...you may need to wait a seconds') def in_rails_application? File.exists?(SCRIPT_RAILS) end def ok prompt = TTY::Prompt.new prompt.ok("generate complete ! check files now") cli end def fail() prompt = TTY::Prompt.new prompt.warn("generate failed ! check error above") exit(0); end if in_rails_application? app_path = Dir.pwd + '/config/environment' require app_path prompt.ok('Load Rails Application ENV Success!') else prompt.error('You are not in Rails app root,please enter your rails app directory and run the cli tool') exit(0); end require_relative '../lib/blade/setting' def cli prompt = TTY::Prompt.new menu_result = prompt.collect do key(:menu).select('select the menu you want to do') do |menu| menu.choice "Generate yml and setting logic files", 1 menu.choice "Generate Model's crud scaffold", 2 menu.choice "Add graphql base file to your project", 3 menu.choice "Add Doorkeeper Token to project", 4 menu.choice "Add Base File for project [response.rb,application.json.jbuilder, application.helper, base_model_concern]", 0 menu.choice "Generate Swagger doc file by Model Module", 5 end end case menu_result[:menu] when 1 result = prompt.collect do key(:setting_path).ask('The generated config file path', default: '/app/settings') setting_choices = %w(redis sentry database influxdb scout_apm send_cloud elastic_search wechat carrierwave) prompt.say('Please select the config file you want to generate, (Use arrow keys, press Space to select and Enter to finish, and letter keys to filter)') key(:configs).multi_select('check configs', setting_choices, filter: true) key(:yml_path).ask('The generated yml path', default: '/environments') key(:extra_yml_path).ask('Other environment? (Input words like sit,dit,prod)', default: 'dev,sit,dit,prod') do |q| q.convert -> (input) {input.split(/,\s*/)} end end prompt.ok('config complete') prompt.warn("generating files ...plz don't quit") #生成setting logic Blade::BladeSetting.generate_setting_logic(result[:setting_path], result[:configs]) prompt.warn('generating yml files ...') Blade::BladeSetting.generate_setting_yml(result[:yml_path], result[:extra_yml_path], result[:configs]) prompt.ok("generate complete ! check files now") when 2 model_result = prompt.collect do key(:model_name).ask('enter the model name') key(:namespace).ask('enter the controller namespace', default: 'api') end ActiveRecord::Base.connection model = Blade::BladeSetting.get_model(model_result[:model_name].classify) if (model.blank?) prompt.error("Model doesn't exist") exit(0); end prompt.warn("generating files ...please don't quit") Blade::BladeSetting.generate_model_template(model, model_result[:model_name]) Blade::BladeSetting.generate_service_template(model, model_result[:model_name]) Blade::BladeSetting.generate_crud(model, model_result[:model_name], model_result[:namespace]) ok(); when 3 when 4 token_result = prompt.collect do key(:app_name).ask('Enter the app name', default: 'My app') key(:uri).ask('enter the redirect uri', default: 'http://localhost:3000') end if (!defined? Doorkeeper) Blade::BladeSetting.generate_gem('doorkeeper') prompt.error('You should install doorkeeper first') fail() else app = Doorkeeper::Application.new(name: token_result[:app_name], redirect_uri: token_result[:uri], scopes: token_result[:uri]) app.save! prompt.ok("The Uid is #{app.uid}") prompt.ok("The secret key is #{app.secret}") end ok() when 5 doc_result = prompt.collect do key(:model).ask('Enter the module name', default: 'user') end doc_name_result = prompt.collect do key(:name).ask('Enter the module name', default: doc_result[:model]) end Blade::BladeSetting.generate_doc(doc_result[:model], doc_name_result[:name]) ok() when 0 Blade::BladeSetting.generate_base_files ok(); end end cli