├── .gitignore ├── .rubocop.yml ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Guardfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── _guard-core ├── console ├── grape ├── guard └── setup ├── grape-cli.gemspec ├── lib └── grape_cli │ ├── actions │ ├── create_application.rb │ ├── setup_database.rb │ └── setup_test_framework.rb │ ├── application_factory.rb │ ├── class_name_generator.rb │ ├── command_generator.rb │ ├── config.rb │ ├── core.rb │ ├── grape_cli.rb │ ├── templates │ ├── application │ │ ├── Gemfile.tt │ │ ├── Rakefile.tt │ │ ├── app │ │ │ ├── api.rb.tt │ │ │ └── resources │ │ │ │ └── hello_world.rb.tt │ │ ├── config.ru.tt │ │ └── config │ │ │ ├── application.rb │ │ │ ├── boot.rb │ │ │ └── environment.rb │ ├── database │ │ ├── postgres.yml.tt │ │ └── sqlite.yml.tt │ ├── minitest │ │ ├── requests │ │ │ └── hello_world_test.rb.tt │ │ └── test_helper.rb │ ├── rspec │ │ ├── requests │ │ │ └── hello_world_spec.rb.tt │ │ └── spec_helper.rb │ └── rspec_config │ └── version.rb └── test ├── grape_cli ├── class_name_generator_test.rb ├── cli_test.rb ├── create_test.rb └── stubs │ ├── fake_generator.rb │ └── test_factory.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | Documentation: 2 | Enabled: false 3 | 4 | Metrics/LineLength: 5 | Max: 120 6 | Exclude: 7 | - grape-cli.gemspec 8 | - bin/_guard-core 9 | 10 | Style/SignalException: 11 | EnforcedStyle: only_raise 12 | 13 | Style/ClassVars: 14 | Enabled: false 15 | 16 | Style/FileName: 17 | Exclude: 18 | - bin/_guard-core 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.5 4 | before_install: gem install bundler -v 1.10.6 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in grape-cli.gemspec 4 | gemspec 5 | 6 | group :development do 7 | gem 'rubocop', '0.34.2' 8 | gem 'guard' 9 | gem 'guard-minitest' 10 | gem 'guard-rubocop' 11 | end 12 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | group :red_green_refactor, halt_on_fail: true do 2 | guard :minitest, all_on_start: false do 3 | watch(%r{^test/(.*)\/?(.*)_test\.rb$}) 4 | watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" } 5 | watch(%r{^test/test_helper\.rb$}) { 'test' } 6 | end 7 | 8 | guard :rubocop, all_on_start: false do 9 | watch(%r{/.+\/.rb$}) 10 | watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) } 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 wswidzinski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grape-cli [![Build Status](https://travis-ci.org/elpassion/grape-cli.svg?branch=master)](https://travis-ci.org/elpassion/grape-cli) [![Code Climate](https://codeclimate.com/github/elpassion/grape-cli/badges/gpa.svg)](https://codeclimate.com/github/elpassion/grape-cli) [![Gem Version](https://badge.fury.io/rb/grape-cli.svg)](http://badge.fury.io/rb/grape-cli) 2 | 3 | Grape-CLI is a gem for bootstraping standalone Grape application. 4 | It is heavily inspired by Rails/Ember generators. 5 | 6 | ## Installation 7 | 8 | Add this line to your application's Gemfile: 9 | 10 | ```ruby 11 | gem 'grape-cli' 12 | ``` 13 | 14 | And then execute: 15 | 16 | $ bundle 17 | 18 | Or install it yourself as: 19 | 20 | $ gem install grape-cli 21 | 22 | ## Usage 23 | 24 | ###New application 25 | Create new Grape app in current directory with provided application name 26 | 27 | $ grape new application_name 28 | 29 | For new command you can provide multiple options: 30 | 31 | ###Test framework 32 | 33 | $ grape new application_name --test=minitest 34 | 35 | 36 | will create new application with minitest as test framework, 37 | running new command without specifying `--test` option will setup application with RSpec as it's default one. 38 | 39 | ###Database 40 | 41 | $ grape new application_name --database=postgres 42 | 43 | 44 | will create new application with postgres as database, 45 | running new command without specifying `--database` option will setup application with sqlite as it's default one. 46 | 47 | ###Start application server 48 | 49 | $ cd application_name 50 | $ grape server 51 | or 52 | 53 | $ grape s 54 | 55 | This command support all rackup parameters. 56 | 57 | Go to http://localhost:9292/ 58 | 59 | ###Start application console 60 | 61 | $ grape console 62 | or 63 | 64 | $ grape c 65 | 66 | ###Run your application tets 67 | 68 | $ grape test 69 | 70 | 71 | ## Contributing 72 | 73 | Bug reports and pull requests are welcome on GitHub at https://github.com/elpassion/grape-cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct. 74 | 75 | 76 | ## License 77 | 78 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 79 | 80 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << 'test' 6 | t.libs << 'lib' 7 | t.test_files = FileList['test/**/*_test.rb'] 8 | end 9 | 10 | task default: :test 11 | -------------------------------------------------------------------------------- /bin/_guard-core: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # This file was generated by Bundler. 4 | # 5 | # The application '_guard-core' is installed as part of a gem, and 6 | # this file is here to facilitate running it. 7 | # 8 | 9 | require 'pathname' 10 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath) 11 | 12 | require 'rubygems' 13 | require 'bundler/setup' 14 | 15 | load Gem.bin_path('guard', '_guard-core') 16 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'bundler/setup' 4 | require 'grape/cli' 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require 'irb' 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/grape: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' 4 | 5 | require 'grape_cli/grape_cli' 6 | 7 | GrapeCli::Core.start(ARGV) 8 | -------------------------------------------------------------------------------- /bin/guard: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # This file was generated by Bundler. 4 | # 5 | # The application 'guard' is installed as part of a gem, and 6 | # this file is here to facilitate running it. 7 | # 8 | 9 | require 'pathname' 10 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath) 11 | 12 | require 'rubygems' 13 | require 'bundler/setup' 14 | 15 | load Gem.bin_path('guard', 'guard') 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /grape-cli.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'grape_cli/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'grape-cli' 8 | spec.version = GrapeCli::VERSION 9 | spec.authors = %w(wswidzinski michalwarda koszcz ghostbuster91) 10 | spec.email = %w(swidzinskiw@gmail.com) 11 | 12 | spec.summary = 'Grape-CLI is a gem for bootstraping standalone Grape application.' 13 | spec.description = 'Grape-CLI is a gem for bootstraping standalone Grape application. It is heavily inspired by Rails/Ember generators.' 14 | spec.homepage = 'http://github.com/elpassion/grape-cli' 15 | spec.license = 'MIT' 16 | 17 | # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or 18 | # delete this section to allow pushing this gem to any host. 19 | if spec.respond_to?(:metadata) 20 | spec.metadata['allowed_push_host'] = 'https://rubygems.org' 21 | else 22 | raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' 23 | end 24 | 25 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 26 | spec.bindir = 'bin' 27 | spec.executables = ['grape'] 28 | spec.require_paths = ['lib'] 29 | 30 | spec.add_dependency 'thor', '~> 0.19' 31 | spec.add_dependency 'bundler', '~> 1.10' 32 | spec.add_dependency 'racksh' 33 | 34 | spec.add_development_dependency 'rake', '~> 10.0' 35 | spec.add_development_dependency 'minitest' 36 | spec.add_development_dependency 'pry' 37 | end 38 | -------------------------------------------------------------------------------- /lib/grape_cli/actions/create_application.rb: -------------------------------------------------------------------------------- 1 | require 'delegate' 2 | 3 | module GrapeCli 4 | module Actions 5 | class CreateApplication < SimpleDelegator 6 | def run(config, destination_path) 7 | source_path = File.join(config[:source_root], 'templates', 'application') 8 | 9 | directory(source_path, destination_path, config) 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/grape_cli/actions/setup_database.rb: -------------------------------------------------------------------------------- 1 | require 'delegate' 2 | 3 | module GrapeCli 4 | module Actions 5 | class SetupDatabase < SimpleDelegator 6 | def run(config) 7 | source_path = File.join(config[:source_root], 'templates', 'database', "#{options[:database]}.yml.tt") 8 | destination_path = File.join(options[:work_dir], config[:app_name], 'config', 'database.yml') 9 | 10 | template(source_path, destination_path, config) 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/grape_cli/actions/setup_test_framework.rb: -------------------------------------------------------------------------------- 1 | module GrapeCli 2 | module Actions 3 | class SetupTestFramework 4 | include Thor::Base 5 | include Thor::Actions 6 | 7 | attr_reader :options, :config 8 | 9 | def self.source_root 10 | File.dirname(__FILE__) 11 | end 12 | 13 | def initialize(args = [], local_options = {}, config = {}) 14 | super 15 | @options = local_options 16 | @config = config 17 | end 18 | 19 | # rubocop:disable Metrics/AbcSize 20 | def run 21 | source_path = File.join(config[:source_root], 'templates', options[:test]) 22 | destination_path = File.join(options[:work_dir], config[:app_name], test_directory_name) 23 | 24 | directory(source_path, destination_path, config) 25 | 26 | copy_rspec_config if options[:test] == 'rspec' 27 | end 28 | 29 | private 30 | 31 | def test_directory_name 32 | { 33 | minitest: 'test', 34 | rspec: 'spec' 35 | }[options[:test].to_sym] 36 | end 37 | 38 | def copy_rspec_config 39 | source_path = File.join(config[:source_root], 'templates', 'rspec_config') 40 | destination_path = File.join(options[:work_dir], config[:app_name], '.rspec') 41 | 42 | template(source_path, destination_path, config) 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/grape_cli/application_factory.rb: -------------------------------------------------------------------------------- 1 | require 'grape_cli/command_generator' 2 | 3 | class ApplicationFactory 4 | @@instance = ApplicationFactory.new 5 | 6 | def self.instance 7 | @@instance 8 | end 9 | 10 | def self.instance=(instance) 11 | @@instance = instance 12 | end 13 | 14 | def command_generator 15 | CommandGenerator.new 16 | end 17 | 18 | def verbose_output 19 | true 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/grape_cli/class_name_generator.rb: -------------------------------------------------------------------------------- 1 | class ClassNameGenerator 2 | def initialize(app_name) 3 | @app_name = app_name 4 | end 5 | 6 | def generate 7 | @app_name.split(/\-|\_/).collect(&:capitalize).join 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/grape_cli/command_generator.rb: -------------------------------------------------------------------------------- 1 | class CommandGenerator 2 | def bundle_install 3 | 'bundle install' 4 | end 5 | 6 | def run_server(args) 7 | "rackup #{args}" 8 | end 9 | 10 | def run_console 11 | 'racksh' 12 | end 13 | 14 | def run_tests 15 | 'bundle exec rake' 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /lib/grape_cli/config.rb: -------------------------------------------------------------------------------- 1 | require 'grape_cli/class_name_generator' 2 | require 'grape_cli/application_factory' 3 | 4 | module GrapeCli 5 | class Config 6 | attr_reader :app_name, :options, :source_root 7 | 8 | def initialize(app_name, source_root, options = {}) 9 | @app_name = app_name 10 | @options = options 11 | @source_root = source_root 12 | end 13 | 14 | def config 15 | { 16 | source_root: source_root, 17 | app_name: app_name, 18 | class_name: ClassNameGenerator.new(app_name).generate, 19 | database: options[:database], 20 | database_gem: database_gem, 21 | rspec: rspec?, 22 | verbose: ApplicationFactory.instance.verbose_output 23 | } 24 | end 25 | 26 | private 27 | 28 | def rspec? 29 | options[:test] == 'rspec' 30 | end 31 | 32 | def database_gem 33 | { 34 | postgres: 'pg', 35 | sqlite: 'sqlite3' 36 | }[options[:database].to_sym] 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/grape_cli/core.rb: -------------------------------------------------------------------------------- 1 | require 'grape_cli/application_factory' 2 | require 'grape_cli/config' 3 | require 'grape_cli/actions/create_application' 4 | require 'grape_cli/actions/setup_database' 5 | require 'grape_cli/actions/setup_test_framework' 6 | 7 | module GrapeCli 8 | class Core < Thor 9 | include Thor::Actions 10 | 11 | desc 'new APP_NAME', 'Create a new Grape application' 12 | 13 | method_option :work_dir, default: Dir.pwd 14 | method_option :database, aliases: 'd', default: 'sqlite', enum: %w(postgres sqlite) 15 | method_option :test, aliases: 't', default: 'rspec', enum: %w(minitest rspec) 16 | 17 | def self.source_root 18 | File.dirname(__FILE__) 19 | end 20 | 21 | # rubocop:disable Metrics/AbcSize 22 | def new(app_name) 23 | config = GrapeCli::Config.new(app_name, GrapeCli::Core.source_root, options).config 24 | application_path = File.join(options[:work_dir], app_name) 25 | 26 | GrapeCli::Actions::CreateApplication.new(self).run(config, application_path) 27 | GrapeCli::Actions::SetupDatabase.new(self).run(config) 28 | GrapeCli::Actions::SetupTestFramework.new(args, options, config).run 29 | 30 | inside application_path do 31 | run(ApplicationFactory.instance.command_generator.bundle_install, config) 32 | end 33 | end 34 | 35 | desc 'server', 'Start the Grape server, alias "s"' 36 | map 's' => 'server' 37 | 38 | def server(args = nil) 39 | puts 'Starting Grape server...' 40 | 41 | run(ApplicationFactory.new.command_generator.run_server(args), verbose: false) 42 | end 43 | 44 | desc 'console', 'Start the Grape console, alias "c"' 45 | map 'c' => 'console' 46 | 47 | def console 48 | puts 'Starting Grape console...' 49 | 50 | run(ApplicationFactory.instance.command_generator.run_console, verbose: false) 51 | end 52 | 53 | desc 'test', 'Start tests' 54 | 55 | def test 56 | run(ApplicationFactory.instance.command_generator.run_tests, verbose: false) 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/grape_cli/grape_cli.rb: -------------------------------------------------------------------------------- 1 | require 'thor' 2 | require 'grape_cli/core' 3 | require 'grape_cli/version' 4 | 5 | module GrapeCli 6 | end 7 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/Gemfile.tt: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem '<%= config[:database_gem] %>' 4 | 5 | gem 'grape', '~> 0.13.0' 6 | 7 | gem 'activerecord', '~> 4.2.1', require: 'active_record' 8 | gem 'grape-activerecord' 9 | gem 'grape-swagger', '~> 0.10.2' 10 | gem 'rack', '~> 1.6.0' 11 | 12 | <% if config[:rspec] %> 13 | group :test do 14 | gem 'rspec', '~> 3.3' 15 | gem 'rack-test', '~> 0.6.3' 16 | end 17 | <% else %> 18 | group :test do 19 | gem 'minitest', '~> 5.8.0' 20 | gem 'minitest-reporters' 21 | gem 'rack-test', '~> 0.6.3' 22 | end 23 | <% end %> 24 | 25 | group :development, :test do 26 | gem 'rake', '~> 10.4.2' 27 | gem 'pry' 28 | end 29 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/Rakefile.tt: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler' 3 | require 'grape/activerecord/rake' 4 | 5 | begin 6 | Bundler.setup(:default, :development) 7 | rescue Bundler::BundlerError => e 8 | $stderr.puts e.message 9 | $stderr.puts 'Run `bundle install` to install missing gems' 10 | exit e.status_code 11 | end 12 | 13 | require 'rake' 14 | <% if config[:rspec] %> 15 | require 'rspec/core/rake_task' 16 | 17 | RSpec::Core::RakeTask.new(:spec) 18 | 19 | task :default => :spec 20 | <% else %> 21 | require "rake/testtask" 22 | 23 | Rake::TestTask.new(:test) do |t| 24 | t.libs << "test" 25 | t.libs << "lib" 26 | t.test_files = FileList['test/**/*_test.rb'] 27 | end 28 | 29 | task :default => :test 30 | <% end %> 31 | 32 | task :environment do 33 | ENV['RACK_ENV'] ||= 'development' 34 | require File.expand_path('../config/environment', __FILE__) 35 | end 36 | 37 | namespace :db do 38 | task :environment do 39 | require File.expand_path('../config/environment', __FILE__) 40 | end 41 | end 42 | 43 | task routes: :environment do 44 | <%= config[:app_name] %>::API.routes.each do |route| 45 | method = route.route_method.ljust(10) 46 | path = route.route_path 47 | puts " #{method} #{path}" 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/app/api.rb.tt: -------------------------------------------------------------------------------- 1 | module <%= config[:class_name] %> 2 | class API < Grape::API 3 | prefix 'api' 4 | format :json 5 | 6 | mount <%= config[:class_name] %>::HelloWorld 7 | 8 | add_swagger_documentation api_version: 'v1' 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/app/resources/hello_world.rb.tt: -------------------------------------------------------------------------------- 1 | module <%= config[:class_name] %> 2 | class HelloWorld < Grape::API 3 | get '/hello_world' do 4 | { message: 'hello world' } 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/config.ru.tt: -------------------------------------------------------------------------------- 1 | require File.expand_path('../config/environment', __FILE__) 2 | 3 | use ActiveRecord::ConnectionAdapters::ConnectionManagement 4 | 5 | run <%= config[:class_name] %>::API.new 6 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/config/application.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'app')) 2 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 3 | 4 | require 'boot' 5 | 6 | Bundler.require :default, ENV['RACK_ENV'] 7 | 8 | Grape::ActiveRecord.configure_from_file!(File.expand_path('../database.yml', __FILE__)) 9 | 10 | Dir[File.expand_path('../../app/models/*.rb', __FILE__)].each { |file| require file } 11 | Dir[File.expand_path('../../app/resources/*.rb', __FILE__)].each { |file| require file } 12 | 13 | require 'api' 14 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/config/boot.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler/setup' 3 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/application/config/environment.rb: -------------------------------------------------------------------------------- 1 | ENV['RACK_ENV'] ||= 'test' 2 | 3 | require File.expand_path('../application', __FILE__) 4 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/database/postgres.yml.tt: -------------------------------------------------------------------------------- 1 | default: &default 2 | adapter: postgresql 3 | encoding: unicode 4 | username: <%= ENV['DATABASE_USERNAME'] %> 5 | password: <%= ENV['DATABASE_PASSWORD'] %> 6 | pool: 5 7 | 8 | development: 9 | <<: *default 10 | database: <%= config[:app_name] %>_development 11 | 12 | test: 13 | <<: *default 14 | database: <%= config[:app_name] %>_test 15 | 16 | production: 17 | url: <%= ENV['DATABASE_URL'] %> 18 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/database/sqlite.yml.tt: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/minitest/requests/hello_world_test.rb.tt: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class HelloWorldTest < MiniTest::Test 4 | include Rack::Test::Methods 5 | 6 | def app 7 | <%= config[:class_name] %>::API 8 | end 9 | 10 | def test_api_get_hello_world 11 | get '/api/hello_world' 12 | assert last_response.ok? 13 | assert_equal 'hello world', JSON.parse(last_response.body)['message'] 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/minitest/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rack/test' 2 | require 'minitest/autorun' 3 | require 'minitest/reporters' 4 | 5 | ENV['RACK_ENV'] = 'test' 6 | 7 | Minitest::Reporters.use!(Minitest::Reporters::ProgressReporter.new, ENV) 8 | 9 | require File.expand_path('../../config/environment', __FILE__) 10 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/rspec/requests/hello_world_spec.rb.tt: -------------------------------------------------------------------------------- 1 | describe 'Hello World Resource' do 2 | include Rack::Test::Methods 3 | 4 | def app 5 | <%= config[:class_name] %>::API 6 | end 7 | 8 | describe 'GET /api/hello_world' do 9 | subject { get '/api/hello_world' } 10 | 11 | it 'returns 200 status code' do 12 | subject 13 | expect(last_response.status).to eq 200 14 | end 15 | 16 | it 'returns proper body' do 17 | subject 18 | expect(JSON.parse(last_response.body)['message']).to eq 'hello world' 19 | end 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/rspec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rack/test' 2 | 3 | ENV['RACK_ENV'] = 'test' 4 | 5 | require File.expand_path('../../config/environment', __FILE__) 6 | 7 | ActiveRecord::Base.logger = nil 8 | 9 | RSpec.configure do |config| 10 | config.mock_with :rspec 11 | config.expect_with :rspec 12 | config.raise_errors_for_deprecations! 13 | config.order = 'random' 14 | end 15 | -------------------------------------------------------------------------------- /lib/grape_cli/templates/rspec_config: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /lib/grape_cli/version.rb: -------------------------------------------------------------------------------- 1 | module GrapeCli 2 | VERSION = '0.2.5' 3 | end 4 | -------------------------------------------------------------------------------- /test/grape_cli/class_name_generator_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'grape_cli/class_name_generator' 3 | 4 | module GrapeCli 5 | class ClassNameGeneratorTest < Minitest::Test 6 | def test_generate_proper_names 7 | assert ClassNameGenerator.new('hello').generate == 'Hello' 8 | assert ClassNameGenerator.new('hello_long_name').generate == 'HelloLongName' 9 | assert ClassNameGenerator.new('hello-long-name').generate == 'HelloLongName' 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/grape_cli/cli_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | module GrapeCli 4 | class Test < Minitest::Test 5 | def test_that_it_has_a_version_number 6 | refute_nil ::GrapeCli::VERSION 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /test/grape_cli/create_test.rb: -------------------------------------------------------------------------------- 1 | require 'pry' 2 | require 'test_helper' 3 | require 'tmpdir' 4 | require './lib/grape_cli/grape_cli' 5 | require './lib/grape_cli/application_factory' 6 | require './test/grape_cli/stubs/fake_generator' 7 | require './test/grape_cli/stubs/test_factory' 8 | 9 | module GrapeCli 10 | class CreateTest < Minitest::Test 11 | def setup 12 | @folder = Dir.mktmpdir 13 | @app_name = 'Koszcz' 14 | @app_folder = File.join(@folder, @app_name) 15 | ApplicationFactory.instance = TestFactory.new 16 | GrapeCli::Core.new([@app_name], work_dir: @folder).invoke(:new) 17 | end 18 | 19 | def teardown 20 | FileUtils.rm_rf(@folder) 21 | end 22 | 23 | def test_creates_a_root_folder 24 | assert Dir.exist?(@app_folder) 25 | end 26 | 27 | def test_creates_a_gemfile 28 | assert File.exist?(File.join(@app_folder, 'Gemfile')) 29 | end 30 | 31 | def test_creates_an_application_folder 32 | assert File.exist?(File.join(@app_folder, 'app')) 33 | assert File.directory?(File.join(@app_folder, 'app')) 34 | end 35 | 36 | def test_copy_default_database_yml 37 | assert File.exist?(File.join(@app_folder, 'config', 'database.yml')) 38 | end 39 | 40 | def test_setup_rspec_as_default 41 | assert File.directory?(File.join(@app_folder, 'spec')) 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /test/grape_cli/stubs/fake_generator.rb: -------------------------------------------------------------------------------- 1 | class FakeGenerator 2 | def bundle_install 3 | '' 4 | end 5 | 6 | def run_server 7 | '' 8 | end 9 | 10 | def run_console 11 | '' 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /test/grape_cli/stubs/test_factory.rb: -------------------------------------------------------------------------------- 1 | class TestFactory < ApplicationFactory 2 | def command_generator 3 | FakeGenerator.new 4 | end 5 | 6 | def verbose_output 7 | false 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' 2 | require 'grape_cli/grape_cli' 3 | 4 | require 'minitest/autorun' 5 | --------------------------------------------------------------------------------