debbbbie Writings

深藏功与名

Follow me onGitHub

Capistrano 编译 assets 的时候报错

Capistrano 编译 assets 的时候报错

很多人都碰到过类似错误,就是编译及 deploy 都是正确的,但 precompile 老是报这样的错误(StackOverflow也有很多这种问题):

这个‘错误’信息是 rake assets:precompile 的默认输出内容。那么怎么避免显示这条默认内容呢。 只需在 precompile 的后边加上参数 -q即可。

* executing "cd /home/deploy/tomahawk/releases/20120208222225 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
servers: ["ip_address"]
[ip_address] executing command*** [err :: ip_address] /opt/ruby/bin/ruby /opt/ruby/bin/rake assets:precompile:nondigest RAILS_ENV=production RAILS_GROUPS=assets

源码分析如下,如果你有兴趣,可以一起看一看:

    # :gem_path/actionpack/lib/sprockets/assets.rake
    namespace :assets do

      # task entry, it will call invoke_or_reboot_rake_task
      task :precompile do
        invoke_or_reboot_rake_task "assets:precompile:all"
      end

      # it will call ruby_rake_task
      def invoke_or_reboot_rake_task(task)
        ruby_rake_task task
      end

      # it will call ruby
      def ruby_rake_task(task, fork = true)
        env    = ENV['RAILS_ENV'] || 'production'
        groups = ENV['RAILS_GROUPS'] || 'assets'
        args   = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
        ruby(*args)
      end
    end

    # :gem_path/rake/file_utils.rb
    module FileUtils

      # it will call sh
      def ruby(*args,&block)
        options = (Hash === args.last) ? args.pop : {}
        sh(*([RUBY] + args + [options]), &block)
      end

      # it will call set_verbose_option
      # and if options[:verbose] == true, it do not output cmd
      #   but default of options[:verbose] is an object
      def sh(*cmd, &block)
        # ...
        set_verbose_option(options)
        # ...
        Rake.rake_output_message cmd.join(" ") if options[:verbose]
        # ...
      end

      # default of options[:verbose] is Rake::FileUtilsExt::DEFAULT, which is an object
      def set_verbose_option(options) # :nodoc:
        unless options.key? :verbose
          options[:verbose] =
            Rake::FileUtilsExt.verbose_flag == Rake::FileUtilsExt::DEFAULT ||
            Rake::FileUtilsExt.verbose_flag
        end
      end
    end

    # :gem_path/rake/file_utils_ext.rb
    module Rake
      module FileUtilsExt
        DEFAULT = Object.new
      end
    end

    # :gem_path/rake/application.rb
              # the only to solve the disgusting output when run `rake assets:precompile`
              #   is add a `-q` option.
              ['--quiet', '-q',
                "Do not log messages to standard output.",
                lambda { |value| Rake.verbose(false) }
              ],
              ['--verbose', '-v',
                "Log message to standard output.",
                lambda { |value| Rake.verbose(true) }
              ],

也可看我在 stackoverflow 中的回答: - Capistrano compile assets error - assets:precompile:nondigest?