# shopapp_rails_new.rb # Rails template for shoplift app # echo "Creating shoplift app $1" # ~/.rvm/scripts/rvm use 2.5.1 # rails new $1 --database=postgresql --skip-turbolinks --skip-test-unit # --skip-bundle --skip-test-unit --template=shopapp_rails_new.rb # gem file remove_file "Gemfile" add_file "Gemfile" add_source 'https://rubygems.org' append_to_file 'Gemfile', "ruby '2.5.1'" gem 'rails', '~> 5.2' gem 'pg', '~> 1.0' gem 'puma' gem 'sass-rails' gem 'shopapp' gem 'uglifier', '>= 1.3.0' gem 'mini_racer' gem 'jbuilder', '~> 2.5' gem 'bootsnap', '>= 1.1.0', require: false gem "audited", "~> 4.7" gem_group :development do gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end run 'bundle install' remove_file "config/database.yml" create_file "config/database.yml", <<~DATABASE default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: #{@app_name}_development test: <<: *default database: #{@app_name}_test production: <<: *default database: #{@app_name}_production username: <%= ENV['#{@app_name.upcase}_DATABASE_USERNAME'] %> password: <%= ENV['#{@app_name.upcase}_DATABASE_PASSWORD'] %> DATABASE route 'shoplift_single_sign_on' generate(:model, "company", "code:string", "name:string", "info:json") generate(:model, "company_state", "company:belongs_to", "code:string", "state:string") generate('audited:install') insert_into_file "app/models/company.rb", " audited\n", :after => "ApplicationRecord\n" insert_into_file "app/models/company_state.rb", " audited\n", :after => "ApplicationRecord\n" rails_command "db:drop", env: 'development' rails_command "db:create", env: 'development' rails_command "db:migrate", env: 'development' remove_file "app/assets/stylesheets/application.css" create_file "app/assets/stylesheets/application.scss", <<~SCSS /* *= require_self * * Insert what you need to this file, or add require_tree". * Replace material with own compiled material from shopwindow project. */ @import "material"; @import "shopapp"; SCSS remove_file "app/assets/javascripts/application.js" create_file "app/assets/javascripts/application.js", <<~JS //= require shopapp3 //= require turbolinks //= require_tree . JS remove_file "app/views/layouts/application.html.erb" create_file "app/views/layouts/application.html.haml", <<~LAYOUT !!! %html %head %meta(content="text/html; charset=UTF-8" http-equiv="Content-Type")/ %meta(charset="utf-8")/ %meta(content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport")/ %link(href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet")/ %link(href="https://fonts.googleapis.com/css?family=Fira+Sans:400,500,700" rel="stylesheet")/ %title #{@app_name} = csrf_meta_tags = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %body = render 'shopapp/shopapp3' do = yield LAYOUT create_file '.gitignore', <<~GITIGNORE # Ignore bundler config. /.bundle # Ignore all logfiles and tempfiles. /log/* /tmp/* !/log/.keep !/tmp/.keep /node_modules /yarn-error.log .byebug_history /import # this is so we can develop shopapp gem inside this project /vendor/shopapp /config/local.yml /public/assets GITIGNORE @app_dev_port = ENV['dev_port'] || 3999 create_file 'config/settings.yml', <<~SETTINGS # Authlift setup port: #{@app_dev_port} # << TODO: CHANGE THIS AND SAME BELLOW IN authlift_redirect_uri shopapp_name: #{@app_name} shopapp_code: #{@app_name}.parameterize.underscore authlift_url: <%= ((Rails.env == 'development') ? 'http://localhost:3031/' : 'https://accounts.shoplift.fi/') %> authlift_app_id: #{@app_id = SecureRandom.hex 32} authlift_app_secret: #{@app_secret = SecureRandom.hex 32} authlift_redirect_uri: http://localhost:#{@app_dev_port}/auth/ authlift_default_app_key: authlift_default_scope: # Override secret key base in local.yml if you do not want to use env. variables # secret_key_base: ## SecureRandom.hex 64 SETTINGS create_file 'config/local.yml', <<~SETTINGS authlift_url: 'https://accounts.shoplift.fi/' deployment: stage: folder: '#{@app_name}-stage/current' server: user: lifter dbhost: dbname: #{@app_name}_stage production: folder: '#{@app_name}-production/current' server: user: lifter dbhost: dbname: #{@app_name}_production SETTINGS create_file "Capfile", <<~FILECONTENT require "capistrano/setup" require "capistrano/deploy" require "capistrano/scm/git" install_plugin Capistrano::SCM::Git require 'capistrano/rvm' require "capistrano/bundler" require "capistrano/rails/assets" require "capistrano/rails/migrations" require "capistrano/passenger" # require "capistrano/yarn" Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } FILECONTENT github_organization = ENV['github_organization'] || 'zwr' github_project_name = ENV['github_project_name'] || @app_name create_file "config/deploy.rb", <<~FILECONTENT lock "~> 3.11.0" set :application, "#{@app_name.capitalize}" set :repo_url, "git@github.com:#{github_organization}/#{github_project_name}.git" set :rvm_ruby_version, '2.5.1' append :linked_files, 'config/local.yml' append :linked_files, 'config/local.yml' append :linked_files, 'config/database.yml' set :deploy_to, "/home/shoplift/#{@app_name}" set :passenger_restart_with_touch, true FILECONTENT create_file "config/deploy/stage.rb", <<~FILECONTENT set :stage, :stage set :rails_env, :production set :branch, "stage" set :rvm_type, :user set :rvm_custom_path, '/usr/share/rvm' set :deploy_to, "/home/lifter/#{@app_name}-stage" server "#{@app_name}.zwr.fi", user: "lifter", roles: %w{app db web} FILECONTENT create_file "config/deploy/production.rb", <<~FILECONTENT set :stage, :production set :rails_env, :production set :branch, "production" set :rvm_type, :user set :rvm_custom_path, '/usr/share/rvm' set :deploy_to, "/home/lifter/#{@app_name}-production" server "#{@app_name}.shoplift.fi", user: "lifter", roles: %w{app db web} FILECONTENT create_file 'lib/templates/rails/scaffold_controller/controller.rb.tt', <<~TEMPLATE <% if namespaced? -%> require_dependency "<%= namespaced_path %>/application_controller" <% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < UserAuthenticatedController before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] # GET <%= route_url %> def index @<%= plural_table_name %> = <%= orm_class.all(class_name) %> end # GET <%= route_url %>/1 def show end # GET <%= route_url %>/new def new @<%= singular_table_name %> = <%= orm_class.build(class_name) %> end # GET <%= route_url %>/1/edit def edit end # POST <%= route_url %> def create @<%= singular_table_name %> = <%= orm_class.build(class_name, "\#{singular_table_name}_params") %> if @<%= orm_instance.save %> redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully created.'" %> else render :new end end # PATCH/PUT <%= route_url %>/1 def update if @<%= orm_instance.update("\#{singular_table_name}_params") %> redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully updated.'" %> else render :edit end end # DELETE <%= route_url %>/1 def destroy @<%= orm_instance.destroy %> redirect_to <%= index_helper %>_url, notice: <%= "'\#{human_name} was successfully destroyed.'" %> end private # Use callbacks to share common setup or constraints between actions. def set_<%= singular_table_name %> @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end # Only allow a trusted parameter "white list" through. def <%= "\#{singular_table_name}_params" %> <%- if attributes_names.empty? -%> params.fetch(:<%= singular_table_name %>, {}) <%- else -%> params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":\#{name}" }.join(', ') %>) <%- end -%> end end <% end -%> TEMPLATE git :init git add: '.' git commit: "-a -m 'shopapp created'" unless ENV['skip_github_remote'] git remote: "add origin git@github.com:#{github_organization}/#{github_project_name}.git" git push: '-u origin master' unless ENV['skip_github_push'] end puts "Shopapp #{@app_name} created!" puts puts "To activate app in Authlift, execute the following:" puts " app = Doorkeeper::Application.find_or_create_by(name: '#{@app_name}')" puts " app.update_attributes uid: '#{@app_id}'," puts " secret: '#{@app_secret}'," puts " redirect_uri: 'http://localhost:#{@app_dev_port}/auth/'" puts " Company.find_by_code(:zwr).applications << app unless Company.find_by_code(:zwr).applications.include? app" puts puts "To see the power of Shopapp, do this (on Mac):" puts puts " $ cd #{@app_name}" puts " $ rails g scaffold dog name:string" puts " $ rake db:migrate" puts " $ open http://localhost:#{@app_dev_port}/dogs ; rails s"