Last active
August 29, 2015 14:04
-
-
Save heracek/1df4bd81d7769bb480a4 to your computer and use it in GitHub Desktop.
Padrino generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# Template for generate clean sample app at Padrino(Compass, SASS, Slim, Asset pipelines, CoffeeScript, etc…) & Angular | |
# @requirements | |
# => Bundler, Padrino-gen, npm, bower | |
# @uses | |
# => | |
# padrino-gen project testapp --template padrino-sprocktes-compass.rb | |
# bundle exec padrino rake -e production assets:precompile | |
# bundle exec padrino start -e production | |
require 'json' | |
#MAIN | |
opts = options.dup | |
opts.delete :template | |
project opts.merge( | |
orm: :activerecord, | |
test: :rspec, | |
mock: :mocha, | |
script: :none, | |
renderer: :slim, | |
stylesheet: :none, # but uses compas! | |
bundle: true, | |
) | |
# Create bower configs | |
bowerrc = { | |
directory: "app/components" | |
} | |
bower_json = { | |
name: @project_name, | |
version: "0.0.0", | |
dependencies: { | |
'jquery' => '1.11.1', | |
'normalize-css' => '3.0.1', | |
} | |
} | |
create_file '.bowerrc', JSON.pretty_generate(bowerrc) | |
create_file 'bower.json', JSON.pretty_generate(bower_json) | |
# Create NPM's package.json | |
package_json = { | |
name: @project_name, | |
version: "0.0.0", | |
dependencies: {}, | |
devDependencies: { | |
bower: "*", | |
} | |
} | |
create_file 'package.json', JSON.pretty_generate(package_json) | |
README = <<-README | |
## Install | |
```bash | |
bundle install | |
npm install | |
bower install | |
``` | |
## Run | |
### Dev | |
```bash | |
bundle exec padrino start | |
``` | |
Open [localhost:3000](http://localhost:3000). | |
### Production | |
```bash | |
bundle exec padrino rake -e production assets:precompile | |
bundle exec padrino start -e production | |
``` | |
README | |
create_file 'README.md', README | |
# Ignore npm/bower specific stuff | |
IGNORE = <<-TEXT | |
/node_modules | |
/dist | |
/.tmp | |
/.sass-cache | |
/app/components | |
!/app/components/**/*[.|-]min.* | |
TEXT | |
append_file '.gitignore', IGNORE | |
GEMS = <<-TEXT | |
gem 'padrino-assets' | |
gem 'compass' | |
gem 'sprockets-sass' | |
gem 'coffee-script' | |
gem 'uglifier' | |
gem 'yui-compressor' | |
gem 'padrino-csrf' | |
TEXT | |
append_file 'Gemfile', GEMS | |
CONFIGRU = <<-TEXT | |
map '/assets' do | |
run Padrino::Assets.environment | |
end | |
TEXT | |
append_file 'config.ru', CONFIGRU | |
%w{lib/tasks app/assets app/assets/javascripts app/assets/images app/assets/stylesheets}.each do |dir| | |
empty_directory dir | |
end | |
%w{public/favicon.ico public/javascripts public/images public/stylesheets app/stylesheets}.each do |dir| | |
remove_file dir | |
end | |
#rake tasks | |
['cleanup', 'clobber', 'compress', 'precompile'].each do |name| | |
get "https://raw.github.com/Cirex/padrino-assets/master/lib/tasks/#{name}.rake", "lib/tasks/#{name}.rake" | |
end | |
gsub_file 'lib/tasks/cleanup.rake', "task :cleanup, :quanity", 'task :cleanup, [:quanity] => :environment' | |
gsub_file 'lib/tasks/cleanup.rake', "args['quanity']", 'args[:quanity]' | |
gsub_file 'lib/tasks/cleanup.rake', "manifest.cleanup(quanity)", 'manifest.clean(quanity)' | |
gsub_file 'lib/tasks/clobber.rake', "task :clobber do", 'task :clobber => :environment do' | |
gsub_file 'lib/tasks/compress.rake', "task :compress do", 'task :compress => :environment do' | |
gsub_file 'lib/tasks/precompile.rake', "task :precompile do", 'task :precompile => :environment do' | |
# Create app.coffee | |
APPJS = <<-TEXT | |
#= require jquery/dist/jquery.min | |
#= require_self | |
console.log 'padrino ok' | |
TEXT | |
create_file 'app/assets/javascripts/application.coffee', APPJS | |
APPCSS = <<-TEXT | |
/* | |
*= require normalize-css/normalize.css | |
*= require_self | |
*/ | |
@import "compass/css3"; | |
h1 { | |
text-align: center; | |
} | |
TEXT | |
create_file 'app/assets/stylesheets/application.scss', APPCSS | |
SPROCKETS = <<-TEXT | |
register Padrino::Assets | |
Padrino::Assets.load_paths << Dir[Padrino.root('app', 'components')] | |
if Padrino.env == :production | |
set :js_compressor, Uglifier.new(mangle: false) | |
set :css_compressor, :yui | |
end | |
register CompassInitializer | |
register Padrino::CSRF | |
enable :prevent_request_forgery | |
TEXT | |
inject_into_file 'app/app.rb', SPROCKETS, after: "register Padrino::Helpers\n" | |
SESSION_KEY = <<-TEXT | |
set :sessions, {key: "_#{@project_name.downcase}_session"} | |
TEXT | |
inject_into_file 'app/app.rb', SESSION_KEY, after: "enable :sessions\n" | |
COMPASS = <<-TEXT | |
module CompassInitializer | |
def self.registered(app) | |
require 'sass/plugin/rack' | |
Compass.configuration do |config| | |
config.project_path = Padrino.root | |
config.sass_dir = "app/assets/stylesheets" | |
config.project_type = :stand_alone | |
config.http_path = "/" | |
end | |
Compass.configure_sass_plugin! | |
Compass.handle_configuration_change! | |
app.use Sass::Plugin::Rack | |
end | |
end | |
TEXT | |
create_file 'lib/compass_plugin.rb', COMPASS | |
# Default routes | |
APP_INIT = <<-TEXT | |
get :index do | |
@title = 'Index' | |
render :index | |
end | |
TEXT | |
inject_into_file 'app/app.rb', APP_INIT, before: "#\n end\n" | |
INDEX_SLIM = <<-INDEX_SLIM | |
h1 hello! | |
INDEX_SLIM | |
create_file 'app/views/index.slim', INDEX_SLIM | |
APPLICATION = <<-TEXT | |
doctype html | |
html | |
head | |
meta[charset="utf-8"] | |
meta[http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"] | |
title = @title | |
meta[name="viewport" content="width=device-width, initial-scale=1.0"] | |
meta[name="author" content=""] | |
meta[name="description" content=""] | |
== csrf_meta_tags | |
== stylesheet_link_tag :application | |
body | |
= yield | |
== javascript_include_tag :application, defer: true | |
TEXT | |
create_file 'app/views/layouts/application.slim', APPLICATION | |
system "cd #{destination_root} && npm install" | |
system "cd #{destination_root} && bower install --save" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment