├── Gemfile ├── lib └── cuba │ ├── generator │ ├── version.rb │ └── cli.rb │ ├── templates │ ├── rack_config.erb │ ├── gemfile.erb │ ├── api.erb │ ├── db.erb │ └── app.erb │ └── generator.rb ├── .travis.yml ├── .gitignore ├── bin └── cuba ├── Rakefile ├── spec └── cuba_generator_spec.rb ├── LICENSE.txt ├── cuba-generator.gemspec └── README.md /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /lib/cuba/generator/version.rb: -------------------------------------------------------------------------------- 1 | module Cuba 2 | class Generator 3 | VERSION = "0.0.7" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | cache: bundler 3 | rvm: 4 | - "2.1.4" 5 | - "jruby" 6 | script: 'bundle exec rake' 7 | -------------------------------------------------------------------------------- /lib/cuba/templates/rack_config.erb: -------------------------------------------------------------------------------- 1 | <%= 2 | <<-TEMPLATE 3 | require "./#{project_name}" 4 | 5 | run Cuba 6 | TEMPLATE 7 | %> -------------------------------------------------------------------------------- /lib/cuba/templates/gemfile.erb: -------------------------------------------------------------------------------- 1 | <%= 2 | <<-TEMPLATE 3 | 4 | source "https://rubygems.org" 5 | 6 | gem 'cuba' 7 | gem 'data_mapper' 8 | gem 'dm-postgres-adapter' 9 | 10 | TEMPLATE 11 | %> -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.bundle 11 | *.so 12 | *.o 13 | *.a 14 | mkmf.log 15 | .idea/ 16 | .DS_Store 17 | -------------------------------------------------------------------------------- /bin/cuba: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | #!/usr/bin/env ruby 4 | 5 | # encoding: utf-8 6 | 7 | $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib') 8 | require 'cuba/generator/cli' 9 | Cuba::Cli.new.run 10 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new(:spec) do |task| 5 | task.rspec_opts = ['--color', '--format progress', '--order random'] 6 | end 7 | 8 | task :default => :spec 9 | -------------------------------------------------------------------------------- /spec/cuba_generator_spec.rb: -------------------------------------------------------------------------------- 1 | require 'cuba/generator' 2 | 3 | describe Cuba::Generator do 4 | subject { Cuba::Generator.new('myProject') } 5 | 6 | it 'create_dir' do 7 | expect(Dir).to receive(:mkdir) 8 | subject.create_dir 9 | end 10 | 11 | it 'create_config_file' do 12 | expect(File).to receive(:open).with('./myproject/config.ru', 'w+') 13 | subject.create_config_file 14 | end 15 | 16 | it 'create_cuba_file' do 17 | expect(File).to receive(:open).with('./myproject/myproject.rb', 'w+') 18 | subject.create_cuba_file 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/cuba/templates/api.erb: -------------------------------------------------------------------------------- 1 | <%= 2 | <<-TEMPLATE 3 | require 'cuba' 4 | require 'json' 5 | 6 | # If you need extra protection. 7 | # require 'rack/protection' 8 | # Cuba.use Rack::Protection 9 | # Cuba.use Rack::Protection::RemoteReferrer 10 | 11 | # To launch just type: 'rackup' in your console 12 | Cuba.define do 13 | on get do 14 | 15 | on root do 16 | data = { first_name: 'Hello', last_name: 'World' } 17 | res.headers["Content-Type"] = "application/json; charset=utf-8" 18 | res.write data.to_json 19 | end 20 | 21 | end 22 | end 23 | TEMPLATE 24 | %> 25 | -------------------------------------------------------------------------------- /lib/cuba/templates/db.erb: -------------------------------------------------------------------------------- 1 | <%= 2 | <<-TEMPLATE 3 | 4 | require 'data_mapper' 5 | 6 | DataMapper.setup(:default, 'postgres://username@localhost/db_name') 7 | 8 | # Here's an example User model 9 | # class User 10 | # include DataMapper::Resource 11 | # 12 | # property :id, Serial # An auto-increment integer key 13 | # property :email, String # A varchar type string, for short strings 14 | # property :created_at, DateTime # A DateTime, for any date you might like. 15 | # property :updated_at, DateTime 16 | # end 17 | 18 | DataMapper.finalize 19 | DataMapper.auto_upgrade! 20 | 21 | TEMPLATE 22 | %> 23 | -------------------------------------------------------------------------------- /lib/cuba/templates/app.erb: -------------------------------------------------------------------------------- 1 | <%= 2 | <<-TEMPLATE 3 | require 'cuba' 4 | 5 | # If you need extra protection. 6 | # require 'rack/protection' 7 | # Cuba.use Rack::Protection 8 | # Cuba.use Rack::Protection::RemoteReferrer 9 | 10 | Cuba.use Rack::Session::Cookie, :secret => "#{SecureRandom.base64(64)}" 11 | 12 | # Cuba includes a plugin called Cuba::Render that provides a couple of helper methods for rendering templates. 13 | # require "cuba/render" 14 | # Cuba.plugin(Cuba::Render) 15 | 16 | # This plugin uses Tilt, which serves as an interface to a bunch of different Ruby template engines 17 | # (ERB, Haml, Sass, CoffeeScript, etc.), so you can use the template engine of your choice. 18 | 19 | # require 'erb' 20 | # Cuba.settings[:render][:template_engine] = "erb" 21 | # Cuba.settings[:render][:views] = "./views" 22 | 23 | # To launch just type: 'rackup' in your console 24 | Cuba.define do 25 | on get do 26 | 27 | on root do 28 | res.write "Hello #{project_name}" 29 | end 30 | 31 | end 32 | end 33 | TEMPLATE 34 | %> 35 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Serdar Doğruyol 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /lib/cuba/generator/cli.rb: -------------------------------------------------------------------------------- 1 | require 'commander' 2 | require_relative 'version' 3 | require_relative '../generator' 4 | 5 | module Cuba 6 | class Cli 7 | include Commander::Methods 8 | 9 | def run 10 | program :name, 'cuba' 11 | program :version, Cuba::Generator::VERSION 12 | program :description, 'Application Generator for Cuba framework.' 13 | 14 | command :new do |c| 15 | c.syntax = 'cuba new [options]' 16 | c.description = 'Creates a new Cuba app' 17 | c.option '--type STRING', String, 'Creates an app with preferred type' 18 | c.option '--database STRING', String, 'Setups a database configuration with DataMapper' 19 | c.action do |args, options| 20 | if options.type 21 | generator = Cuba::Generator.new(ARGV[1], options.type) 22 | generator.create_database_file if options.database == 'postgresql' 23 | else 24 | generator = Cuba::Generator.new(ARGV[1], :app) 25 | generator.create_database_file if options.database == 'postgresql' 26 | end 27 | end 28 | end 29 | run! 30 | end 31 | end 32 | end -------------------------------------------------------------------------------- /cuba-generator.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'cuba/generator/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'cuba-generator' 8 | spec.version = Cuba::Generator::VERSION 9 | spec.authors = ['Serdar Dogruyol'] 10 | spec.email = ['dogruyolserdar@gmail.com'] 11 | spec.summary = %q{Application Generator for Cuba framework.} 12 | spec.description = %q{Helps create cuba projects through a minimalist generator.} 13 | spec.homepage = '' 14 | spec.license = 'MIT' 15 | 16 | spec.files = `git ls-files -z`.split("\x0") 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ['lib'] 20 | 21 | spec.add_development_dependency 'bundler', '~> 1.7' 22 | spec.add_development_dependency 'rake', '~> 10.0' 23 | spec.add_development_dependency 'rspec', '~> 3.1.0' 24 | spec.add_dependency 'cuba', '~> 3.4.0' 25 | spec.add_runtime_dependency 'commander', '~> 4.3.0' 26 | end 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Gem Version](https://badge.fury.io/rb/cuba-generator.svg)](http://badge.fury.io/rb/cuba-generator) 2 | # cuba-generator 3 | 4 | cuba-generator is a generator to help creating Cuba projects. 5 | 6 | [Cuba](https://github.com/soveran/cuba) is a micro-framework for Ruby based on Rack. 7 | 8 | ## Installation 9 | 10 | gem install cuba-generator 11 | 12 | ## Usage 13 | 14 | cuba new [projectName] [options] 15 | 16 | Additionally you can specify the type of your app via ***--type*** . E.g to generate an API starting point. 17 | 18 | cuba new [projectName] --type api 19 | 20 | To generate an application with a Postgresql configuration setup use ***-database*** option (via Datamapper) 21 | 22 | cuba new [projectName] --database postgresql 23 | 24 | By default ***--type*** is 'app'. 25 | 26 | ## Contributing 27 | 28 | 1. Fork it ( https://github.com/Sdogruyol/cuba-generator/fork ) 29 | 2. Create your feature branch (`git checkout -b my-new-feature`) 30 | 3. Commit your changes (`git commit -am 'Add some feature'`) 31 | 4. Push to the branch (`git push origin my-new-feature`) 32 | 5. Create a new Pull Request 33 | 34 | ## Thanks 35 | 36 | This project is inspired from [cuba-libre](https://github.com/gdurelle/cuba-libre). 37 | -------------------------------------------------------------------------------- /lib/cuba/generator.rb: -------------------------------------------------------------------------------- 1 | require 'cuba/generator/version' 2 | require 'securerandom' 3 | require 'erb' 4 | require 'ostruct' 5 | 6 | module Cuba 7 | class Generator 8 | APPROOT = File.expand_path(File.dirname(__FILE__)) 9 | 10 | def initialize(name, type) 11 | @project_name = name.downcase 12 | @type = type 13 | create_dir 14 | create_config_file 15 | create_cuba_file 16 | puts "Created your Cuba #{@type} at /#{@project_name} directory. Rock on!" 17 | end 18 | 19 | def create_dir 20 | Dir.mkdir(@project_name) 21 | end 22 | 23 | def create_config_file 24 | File.open("./#{@project_name}/config.ru", 'w+') do |file| 25 | file.write setup_config 26 | end 27 | end 28 | 29 | def create_cuba_file 30 | File.open("./#{@project_name}/#{@project_name}.rb", 'w+') do |file| 31 | file.write setup_cuba 32 | end 33 | end 34 | 35 | def create_database_file 36 | File.open("./#{@project_name}/database.rb", 'w+') do |file| 37 | file.write setup_database 38 | end 39 | File.open("./#{@project_name}/Gemfile", 'w+') do |file| 40 | file.write setup_gemfile 41 | end 42 | end 43 | 44 | private 45 | 46 | def setup_cuba 47 | if @type.to_sym == :app 48 | create_template 'app' 49 | elsif @type.to_sym == :api 50 | create_template 'api' 51 | end 52 | end 53 | 54 | def setup_config 55 | create_template 'rack_config' 56 | end 57 | 58 | def setup_database 59 | create_template 'db' 60 | end 61 | 62 | def setup_gemfile 63 | create_template 'gemfile' 64 | end 65 | 66 | def create_template(name) 67 | template = File.read File.join(APPROOT, 'templates/', "#{name}.erb") 68 | erb(template, {project_name: @project_name}) 69 | end 70 | 71 | def erb(template, vars) 72 | ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding }) 73 | end 74 | end 75 | end 76 | --------------------------------------------------------------------------------