rstacruz / sinatra-assetpack

Package your assets transparently in Sinatra.
http://ricostacruz.com/sinatra-assetpack/
MIT License
542 stars 96 forks source link

How do I precompile under Padrino? Rake doesn't work #117

Closed hongliang-goudou closed 11 years ago

hongliang-goudou commented 11 years ago

The Rakefile looks like:

APP_FILE  = 'app/app.rb'
APP_CLASS = 'App'

require 'bundler/setup'
require 'padrino-core/cli/rake'
require 'sinatra/assetpack/rake'

PadrinoTasks.use(:database)
PadrinoTasks.use(:activerecord)
PadrinoTasks.init

The app/app.rb:

module Friendicon
  class App < Padrino::Application
    register Padrino::Contrib::AutoLocale
    register OmniauthInitializer
    register WillPaginate::Sinatra
    register ScssInitializer
    use ActiveRecord::ConnectionAdapters::ConnectionManagement
    register Padrino::Rendering
    register Padrino::Mailer
    register Padrino::Helpers
    register Sinatra::AssetPack

    assets {
      serve '/js',  from: 'js'
      serve '/css', from: 'css'
      serve '/img', from: 'img'

      js :app, '/js/app.js', ['/js/jquery.js', '/js/*.js']

      css :app, '/css/app.css', ['/css/*.css']

      js_compression  :jsmin    # :jsmin | :yui | :closure | :uglify
      css_compression :simple   # :simple | :sass | :yui | :sqwish
    }

And I run:

padrino rake assetpack:build

The result is:

=> Executing Rake assetpack:build ...
/Users/hongliang/Desktop/workspace/friendicon/app/app.rb:2:in `<module:Friendicon>': uninitialized constant Padrino::Application (NameError)
    from /Users/hongliang/Desktop/workspace/friendicon/app/app.rb:1:in `<top (required)>'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:18:in `require'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:18:in `app'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:25:in `block (2 levels) in <top (required)>'
...

Obvisouly for some reason the rake environment is lack of Padrino::Application. So I modified Rakefile again and required everything explicitly: Rakefile

APP_FILE  = 'app/app.rb'
APP_CLASS = 'App'

require 'bundler/setup'
require 'padrino-core/cli/rake'
require 'sinatra/assetpack/rake'
require 'padrino'
require 'padrino-contrib'
require 'padrino-contrib/auto_locale'
require 'will_paginate'
require 'will_paginate/active_record'
require 'will_paginate/view_helpers/sinatra'
include WillPaginate::Sinatra::Helpers

PadrinoTasks.use(:database)
PadrinoTasks.use(:activerecord)
PadrinoTasks.init

And it still doesn't work. There're always dependency or incompatible problems. After I solved all of them, I get this final error and can't go any further:

=> Executing Rake assetpack:build ...
/Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:13:in `const_get': uninitialized constant App (NameError)
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:13:in `block in class_from_string'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:12:in `each'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:12:in `inject'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:12:in `class_from_string'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:19:in `app'
    from /Users/hongliang/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-assetpack-0.2.6/lib/sinatra/assetpack/rake.rb:25:in `block (2 levels) in <top (required)>'

Can anybody help me out of this? Sinatra-AssetPack is a very good and essential part of Sinatra. Why is it so bad compatible with Padrino? I really need the precompile rake work under Padrino. Thanks!

hongliang-goudou commented 11 years ago

OK. I figured it out. To use sinatra-assetpack precompile under Padrino, the Rakefile should be like this:

APP_FILE  = 'app/app.rb'
APP_CLASS = '<Project>::App'

require File.expand_path('config/boot.rb')
require 'bundler/setup'
require 'padrino-core/cli/rake'
require 'sinatra/assetpack/rake'

PadrinoTasks.use(:database)
PadrinoTasks.use(:activerecord)
PadrinoTasks.init

That's it! The importance is to require config/boot.rb. And the rest is that just make sure the spell of your project name is right.

j15e commented 11 years ago

Thanks for reporting! Will add a note in the README about it!