natra 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +4 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +14 -0
  8. data/Gemfile.lock +167 -0
  9. data/Guardfile +75 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +60 -0
  12. data/Rakefile +6 -0
  13. data/bin/console +6 -0
  14. data/bin/natra +7 -0
  15. data/bin/setup +6 -0
  16. data/lib/extensions/string.rb +37 -0
  17. data/lib/natra.rb +9 -0
  18. data/lib/natra/cli.rb +20 -0
  19. data/lib/natra/generators/app/app_generator.rb +150 -0
  20. data/lib/natra/generators/app/templates/Dockerfile +24 -0
  21. data/lib/natra/generators/app/templates/Gemfile +35 -0
  22. data/lib/natra/generators/app/templates/Guardfile +20 -0
  23. data/lib/natra/generators/app/templates/README.md +52 -0
  24. data/lib/natra/generators/app/templates/Rakefile +4 -0
  25. data/lib/natra/generators/app/templates/app/controllers/application_controller.rb +14 -0
  26. data/lib/natra/generators/app/templates/app/models/.gitkeep +0 -0
  27. data/lib/natra/generators/app/templates/app/views/layout.erb.tt +20 -0
  28. data/lib/natra/generators/app/templates/app/views/welcome.erb +22 -0
  29. data/lib/natra/generators/app/templates/bin/ci +6 -0
  30. data/lib/natra/generators/app/templates/bin/setup +7 -0
  31. data/lib/natra/generators/app/templates/config.ru +11 -0
  32. data/lib/natra/generators/app/templates/config/db.yml +35 -0
  33. data/lib/natra/generators/app/templates/config/environment.rb +19 -0
  34. data/lib/natra/generators/app/templates/config/initializers/database.rb +11 -0
  35. data/lib/natra/generators/app/templates/config/initializers/oj.rb +5 -0
  36. data/lib/natra/generators/app/templates/config/initializers/redis.rb +7 -0
  37. data/lib/natra/generators/app/templates/config/puma.rb +11 -0
  38. data/lib/natra/generators/app/templates/config/redis.yml +14 -0
  39. data/lib/natra/generators/app/templates/docker-compose.yml +22 -0
  40. data/lib/natra/generators/app/templates/gitignore +12 -0
  41. data/lib/natra/generators/app/templates/public/favicon.ico +0 -0
  42. data/lib/natra/generators/app/templates/public/images/.gitkeep +0 -0
  43. data/lib/natra/generators/app/templates/public/images/corneal-small.png +0 -0
  44. data/lib/natra/generators/app/templates/public/javascripts/.gitkeep +0 -0
  45. data/lib/natra/generators/app/templates/public/stylesheets/main.css +115 -0
  46. data/lib/natra/generators/app/templates/rspec +2 -0
  47. data/lib/natra/generators/app/templates/rubocop.yml +79 -0
  48. data/lib/natra/generators/app/templates/secrets.env +16 -0
  49. data/lib/natra/generators/app/templates/spec/application_controller_spec.rb +13 -0
  50. data/lib/natra/generators/app/templates/spec/spec_helper.rb +36 -0
  51. data/lib/natra/generators/controller/controller_generator.rb +37 -0
  52. data/lib/natra/generators/controller/templates/controller.rb.erb +37 -0
  53. data/lib/natra/generators/controller/templates/views/edit.html.erb +1 -0
  54. data/lib/natra/generators/controller/templates/views/index.html.erb +1 -0
  55. data/lib/natra/generators/controller/templates/views/new.html.erb +1 -0
  56. data/lib/natra/generators/controller/templates/views/show.html.erb +1 -0
  57. data/lib/natra/generators/model/migration.rb.erb +10 -0
  58. data/lib/natra/generators/model/model.rb.erb +2 -0
  59. data/lib/natra/generators/model/model_generator.rb +59 -0
  60. data/lib/natra/generators/scaffold/scaffold_generator.rb +22 -0
  61. data/lib/natra/version.rb +3 -0
  62. data/natra.gemspec +41 -0
  63. metadata +180 -0
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "natra"
5
+ require "pry"
6
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+
3
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
4
+
5
+ require "natra"
6
+
7
+ Natra::CLI.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,37 @@
1
+ module Natra
2
+ module Extensions
3
+ module String
4
+ def camel_case
5
+ return self.gsub(/^./) { |l| l.capitalize } if !match(/[_-]/)
6
+ altered_self = self.downcase.capitalize
7
+ altered_self.scan(/[_-][a-zA-Z]/).each do |match|
8
+ altered_self.gsub!(match, match[1].upcase)
9
+ end
10
+
11
+ altered_self
12
+ end
13
+
14
+ def camel_case!
15
+ self.replace camel_case
16
+ end
17
+
18
+ def directory_name
19
+ self.downcase.gsub(/[^a-z|\-|\_]/, '')
20
+ end
21
+
22
+ def file_name
23
+ self.gsub(/[\-| ]/, '_').
24
+ gsub(/([A-Z]+|[A-Z][a-z])/) { |x| "_#{x}" }.
25
+ sub(/^_/, "").
26
+ gsub(/_{2,}+/, "_").
27
+ downcase
28
+ end
29
+
30
+ def file_name!
31
+ self.replace file_name
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ String.send(:include, Natra::Extensions::String)
@@ -0,0 +1,9 @@
1
+ require "extensions/string"
2
+ require "natra/generators/app/app_generator"
3
+ require "natra/generators/model/model_generator"
4
+ require "natra/generators/scaffold/scaffold_generator"
5
+ require "natra/generators/controller/controller_generator"
6
+ require "natra/version"
7
+ require "natra/cli"
8
+
9
+ module Natra; end
@@ -0,0 +1,20 @@
1
+ require 'thor'
2
+ module Natra
3
+ class CLI < Thor
4
+ desc '-v', 'Show Natra version number'
5
+ map %w[-v --version] => :version
6
+ # USAGE: Natra -v
7
+ def version
8
+ say "Natra #{Natra::VERSION}"
9
+ end
10
+ # register(class_name, subcommand_alias, usage_list_string, description_string)
11
+ register Natra::Generators::AppGenerator, 'new', 'new APP_PATH', 'Creates a new Sinatra application'
12
+ register Natra::Generators::ModelGenerator, 'model', 'model NAME', 'Generate a model'
13
+ register Natra::Generators::ControllerGenerator, 'controller', 'controller NAME', 'Generate a controller'
14
+ register Natra::Generators::ScaffoldGenerator, 'scaffold', 'scaffold NAME', 'Generate a model with its associated views and controllers'
15
+
16
+ def self.exit_on_failure
17
+ true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,150 @@
1
+ # encoding: UTF-8
2
+ require "thor/group"
3
+
4
+ module Natra
5
+ module Generators
6
+ class AppGenerator < Thor::Group
7
+ include Thor::Actions
8
+
9
+ desc "Creates a new Sinatra application"
10
+ argument :name, :type => :string, :desc => "The name of the new application"
11
+ class_option :capistrano, :type => :boolean, :desc => "Include Capistrano configuration"
12
+ class_option :database, :aliases => "-d", :default => "", :desc => "The type of database to use"
13
+ class_option :redis, :type => :boolean, :desc => "Include Redis configuration"
14
+ class_option :rvm, :type => :boolean, :desc => "Create .ruby-version (ruby-2.1.0) and .ruby-gemset"
15
+ class_option :bundle, :type => :boolean, :desc => "Run bundle after generating the app"
16
+ class_option :git, :type => :boolean, :desc => "Initialize a Git repository"
17
+
18
+ # Creates instance variables from options passed to natra.
19
+ def setup
20
+ @app_path = name.directory_name
21
+ @name = name.file_name
22
+
23
+ options.each do |key, value|
24
+ instance_variable_set "@#{key.to_s}".to_sym, value
25
+ end
26
+ end
27
+
28
+ def self.source_root
29
+ File.expand_path(File.join(File.dirname(__FILE__), "templates"))
30
+ end
31
+
32
+ # Create empty directories
33
+ def create_empty_directories
34
+ %w{config/initializers lib spec}.each do |dir|
35
+ empty_directory File.join(@app_path, dir)
36
+ end
37
+
38
+ empty_directory File.join(@app_path, 'db/migrate')
39
+
40
+ create_file File.join(@app_path, "lib", ".gitkeep")
41
+ template "config/environment.rb", File.join(@app_path, "config/environment.rb")
42
+ end
43
+
44
+ def create_public_directory
45
+ template "public/favicon.ico", File.join(@app_path, "public/favicon.ico")
46
+ end
47
+
48
+ def create_app_directory
49
+ %w{app/controllers app/views app/models}.each do |dir|
50
+ directory dir, File.join(@app_path, dir)
51
+ end
52
+ end
53
+
54
+ def create_app_spec
55
+ template "spec/application_controller_spec.rb", File.join(@app_path, "spec/application_controller_spec.rb")
56
+ end
57
+
58
+ def create_spec_helper
59
+ template "spec/spec_helper.rb", File.join(@app_path, "spec/spec_helper.rb")
60
+ end
61
+
62
+ def create_config
63
+ template "config.ru", File.join(@app_path, "config.ru")
64
+ end
65
+
66
+ def create_gemfile
67
+ template "Gemfile", File.join(@app_path, "Gemfile")
68
+ end
69
+
70
+ def create_rakefile
71
+ template "Rakefile", File.join(@app_path, "Rakefile")
72
+ end
73
+
74
+ def create_readme
75
+ template("README.md", File.join(@app_path, "README.md"))
76
+ end
77
+
78
+ def create_db_config
79
+ template("config/db.yml", File.join(@app_path, "config/db.yml")) unless @database.empty?
80
+ end
81
+
82
+ def create_database_initializer
83
+ template("config/initializers/database.rb", File.join(@app_path, "config/initializers/database.rb")) unless @database.empty?
84
+ end
85
+
86
+ def create_redis_config
87
+ copy_file("config/redis.yml", File.join(@app_path, "config/redis.yml")) if @redis
88
+ end
89
+
90
+ def create_redis_initializer
91
+ template("config/initializers/redis.rb", File.join(@app_path, "config/initializers/redis.rb")) if @redis
92
+ end
93
+
94
+ def create_gitignore
95
+ copy_file "gitignore", File.join(@app_path, '.gitignore')
96
+ end
97
+ def create_rspec
98
+ copy_file 'rspec', File.join(@app_path, '.rspec')
99
+ end
100
+ def create_rubocop
101
+ copy_file 'rubocop.yml', File.join(@app_path, '.rubocop.yml')
102
+ end
103
+ def create_docker
104
+ copy_file 'Dockerfile', File.join(@app_path, 'Dockerfile')
105
+ end
106
+ def create_docker_compose
107
+ template('docker-compose.yml', File.join(@app_path, "docker-compose.yml"))
108
+ end
109
+ def create_guardfile
110
+ copy_file 'Guardfile',File.join(@app_path, 'Guardfile')
111
+ end
112
+ def create_spec_support
113
+ create_file File.join(@app_path, "spec/support/", ".keep")
114
+ end
115
+ def create_secrets
116
+ template('secrets.env',File.join(@app_path, "secrets.env"))
117
+ end
118
+
119
+ def create_capistrano_config
120
+ if @capistrano
121
+ inside(@app_path) do
122
+ run('cap install')
123
+ end
124
+ end
125
+ end
126
+
127
+ def create_rvm_gemset
128
+ if @rvm
129
+ create_file(File.join(@app_path, '.ruby-version'), 'ruby-2.1.0')
130
+ create_file(File.join(@app_path, '.ruby-gemset'), @app_path)
131
+
132
+ @bundle = false
133
+ puts "You need to run 'bundle install' manually."
134
+ end
135
+ end
136
+
137
+ def initialize_git_repo
138
+ inside(@app_path) do
139
+ run('git init .') if @git
140
+ end
141
+ end
142
+
143
+ def install_dependencies
144
+ inside(@app_path) do
145
+ run('bundle') if @bundle
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,24 @@
1
+ # base layer
2
+ FROM ruby AS base
3
+
4
+ WORKDIR /usr/src/app
5
+
6
+ RUN gem install bundler
7
+
8
+ COPY Gemfile /usr/src/app/
9
+
10
+ # development and test layer with all dependencies
11
+ FROM base AS development
12
+
13
+ RUN bundle install -j5 --without staging production
14
+
15
+ COPY . /usr/src/app
16
+
17
+ # release layer
18
+ FROM base AS release
19
+
20
+ RUN bundle install -j5 --without development test
21
+
22
+ COPY . /usr/src/app
23
+
24
+ CMD puma
@@ -0,0 +1,35 @@
1
+ source 'http://rubygems.org'
2
+ ruby '2.5.3'
3
+ gem 'sinatra'
4
+ gem 'activerecord', '~> 4.2', '>= 4.2.6', :require => 'active_record'
5
+ gem 'sinatra-activerecord', :require => 'sinatra/activerecord'
6
+ gem 'rake'
7
+ gem 'rack-timeout'
8
+ gem 'require_all'
9
+ gem 'sqlite3'
10
+ gem 'thin'
11
+ gem 'shotgun'
12
+ gem 'scout_apm'
13
+ gem 'seed-fu'
14
+ gem 'pry'
15
+ gem 'bcrypt'
16
+ gem "tux"
17
+ gem 'oj'
18
+ group :development, :test do
19
+ gem 'pry'
20
+ gem 'pry-byebug'
21
+ gem 'shotgun'
22
+ end
23
+ group :test do
24
+ gem 'awesome_print'
25
+ gem 'database_cleaner'
26
+ gem 'factory_bot'
27
+ gem 'faker'
28
+ gem 'guard-rspec'
29
+ gem 'guard-rubocop'
30
+ gem 'rack-test'
31
+ gem 'simplecov'
32
+ gem 'rspec'
33
+ gem 'capybara'
34
+ gem 'rack-test'
35
+ end
@@ -0,0 +1,20 @@
1
+ guard :rspec, cmd: 'rspec', all_after_pass: true, all_on_start: true do
2
+ require 'guard/rspec/dsl'
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # RSpec files
6
+ rspec = dsl.rspec
7
+ watch(rspec.spec_helper) { rspec.spec_dir }
8
+ watch(rspec.spec_support) { rspec.spec_dir }
9
+ watch(rspec.spec_files)
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+
12
+ # Ruby files
13
+ ruby = dsl.ruby
14
+ dsl.watch_spec_files_for(ruby.lib_files)
15
+ end
16
+
17
+ guard :rubocop, cli: ['--display-cop-names'] do
18
+ watch(/.+\.rb$/)
19
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
20
+ end
@@ -0,0 +1,52 @@
1
+ # <%= @name.camel_case %> service
2
+
3
+ ## Commands
4
+ ```
5
+ natra -v # Show Natra version number
6
+ natra help [COMMAND] # Describe available commands or one specific command
7
+ natra new APP-NAME # Creates a new Sinatra application
8
+ natra model NAME # Generate a model
9
+ natra controller NAME # Generate a controller
10
+ natra scaffold NAME # Generate a model with its associated views and controllers
11
+ ```
12
+ The controller generator also have an optional views flag `--no-views` to create controllers without views.
13
+
14
+ ## Using Natra
15
+
16
+ To generate your app:
17
+
18
+ natra new APP-NAME
19
+
20
+ After natra is done generating your app, run `bundle install` from your app's directory:
21
+
22
+ cd APP-NAME
23
+ bundle install
24
+
25
+ ## Development
26
+
27
+ This project is configured to run in Docker containers facilitated by `docker-compose`. [nib](https://github.com/technekes/nib) is our tool of choice for interacting with `docker-compose` in development.
28
+
29
+
30
+ ### Running the app
31
+
32
+ Use [nib](https://github.com/technekes/nib) to build and start up the web app.
33
+
34
+ ```sh
35
+ nib build --pull
36
+ nib up
37
+ ```
38
+
39
+ ### Running Tests
40
+
41
+ The app is configured to use [guard](https://github.com/guard/guard) facilitate running the tests as files change. To start up guard run:
42
+
43
+ ```sh
44
+ nib guard web
45
+ ```
46
+
47
+ Alternatively the specs can be run as a one-off with:
48
+
49
+ ```sh
50
+ nib rspec web
51
+ ```
52
+
@@ -0,0 +1,4 @@
1
+ ENV["SINATRA_ENV"] ||= "development"
2
+
3
+ require_relative './config/environment'
4
+ require 'sinatra/activerecord/rake'
@@ -0,0 +1,14 @@
1
+ require './config/environment'
2
+
3
+ class ApplicationController < Sinatra::Base
4
+
5
+ configure do
6
+ set :public_folder, 'public'
7
+ set :views, 'app/views'
8
+ end
9
+
10
+ get "/" do
11
+ erb :welcome
12
+ end
13
+
14
+ end
@@ -0,0 +1,20 @@
1
+ <!DOCTYPE html>
2
+ <head>
3
+ <meta charset="utf-8" />
4
+ <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
5
+
6
+ <title><%= @name.camel_case %></title>
7
+
8
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
+ </head>
10
+ <body>
11
+ <div class="wrapper">
12
+
13
+ <%%= yield %>
14
+
15
+ <footer class="branding">
16
+ <small>&copy; <%= Time.now.year %> <strong><%= @name.camel_case %></strong></small>
17
+ </footer>
18
+ </div>
19
+ </body>
20
+ </html>
@@ -0,0 +1,22 @@
1
+ <div class="container">
2
+ <div class="col-md-8 col-md-offset-2">
3
+ <h2>Sinatra Template Default Page</h2>
4
+
5
+ <div class="content">
6
+ <p>Welcome to the Sinatra Template! If you're seeing this page, then everything is working
7
+ as expected. To get started, delete this file (<code>app/views/welcome.erb)</code> and begin adding
8
+ your own actions to <code>application_controller.rb</code>. For more information, see the <a href="https://github.com/thirunjuguna/natra">README</a>.</p>
9
+ </div>
10
+
11
+ <div class="sidebar">
12
+ <h3>Environment</h3>
13
+ <ul>
14
+ <li><b>Ruby:</b> <%= RUBY_VERSION %></li>
15
+ <li><b>Environment:</b> <%= ENV["RACK_ENV"] %></li>
16
+ <li><b>Server:</b> <%= @env["SERVER_SOFTWARE"] %></li>
17
+ <li><b>Port:</b> <%= @env["SERVER_PORT"] %></li>
18
+ </ul>
19
+ </div>
20
+
21
+ </div>
22
+ </div>