8

So here's what I'm attempting to do. I'm building an ember.js application, with a java backend running on GAE.

I'm using handlebars, but I want them divided up into separate files, not just all pasted into the index.html.

Via the ember.js irc I was turned on to rake-pipeline along with minispade

Along with the web filters and a custom handlebars filter I started building the assetfile. I don't know Ruby, or gem files, etc.

So I'm trying to figure out the best way to be able to compile my coffeescript/handlebars files on the fly, minispade them, but keep the individual files accessible while in dev mode so I can debug them. What makes that hard is that the rake pipeline is running on a different port than GAE. So I'm not sure exactly how to handle this. Do I make my index file in GAE point to individual files at the 9292 port (rakep) during development, but in production mode point to the fully concatenated version? I'm not sure.

So I was attempting to do that here: https://gist.github.com/1495740 by having only one section that was triggered by the 'build' flag. Not even sure if that works that way.

I know there's a lot of confusion here. Apologies, like I said I'm not even remotely familiar with the Ruby style of doing things.

Trek Glowacki
  • 3,621
  • 1
  • 17
  • 16
Bob Spryn
  • 17,742
  • 12
  • 68
  • 91

2 Answers2

16

Since you're not a Ruby person, here are the most reliable steps for getting a stock OSX environment set up with rake pipeline:

Step 1: Install bundler

# on OSX, using built-in Ruby
$ sudo gem install bundler --pre

Step 2: Create a Gemfile

# inside your app directory
$ bundle init

# will create a file named Gemfile in the root

Step 3: Add rake-pipeline to the Gemfile

# inside the Gemfile
gem "rake-pipeline-web-filters"

Step 4: Install your gems

$ bundle install --binstubs

Step 5: Set up Assetfile

However you were already doing it...

Step 6: Run Rake::Pipeline

# to run the preview server
$ bin/rakep

# to build your assets
$ bin/rakep build
Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
  • Thanks Yehuda! That was hugely helpful in getting me rolling last night, but going to mark dudleyf's answer as the best as he handheld me through the nitty gritty of my questions there. – Bob Spryn Dec 19 '11 at 18:58
9

Rake::Pipeline.build is the method that evaluates an Assetfile. You can imagine that your entire Assetfile is wrapped inside a Rake::Pipeline.build {} block; you shouldn't ever need to write one inside an Assetfile.

Some of the filters in the docs are hypothetical, most of those docs were written before there were any filters at all. A CoffeeScript compiler has been recently added, though.

As to your main question, I'm not sure there's a clean way to do it with the current rakep implementation. An Assetfile is just Ruby, though, so it's possible to hack something together that should work. Here's how I would write yours:

require "json"
require "rake-pipeline-web-filters"
require "rake-pipeline-web-filters/helpers"

class HandlebarsFilter < Rake::Pipeline::Filter
  def initialize(&block)
    block ||= proc { |input| input.sub(/\.handlebars$/, '.js') }
    super(&block)
  end

  def generate_output(inputs, output)
    inputs.each do |input|
      output.write "return Ember.Handlebars.compile(#{input.read.to_json})"
    end
  end
end

# process all js, css and html files in app/assets
input "assets"

# processed files should be outputted to public
output "public"

# process all coffee files
match "**/*.coffee" do
  # compile all CoffeeScript files. the output file
  # for the compilation should be the input name
  # with the .coffee extension replaced with .js
  coffee_script

  # The coffee_script helper is exactly equivalent to:
  # filter Rake::Pipeline::Web::Filters::CoffeeScriptCompiler
end

match "**/*.js" do
  minispade
  if ENV['RAKEP_ENV'] == "production"
    concat "application.js"
  else
    concat
  end
end

match "**/*.handlebars" do
  filter HandlebarsFilter
  minispade
  concat "templates.js"
end

The if ENV['RAKEP_ENV'] bit reads an environment variable to decide whether to concatenate your JS to a single file.

So now you can run RAKEP_ENV="production" rakep build for a concatenated build, or just rakep build for a development build.

Claude Précourt
  • 907
  • 11
  • 11
dudleyf
  • 353
  • 2
  • 6
  • Thanks dudley. Much appreciated! So I guess the final question that remains (I know there was a lot of them) is how to get those to recompile on the fly in preview mode when my index file is on a different port. It looks like the current solutions are either to address them at the 9292 port, or Yehuda suggested proxying them somehow from GAE. If anyone has thoughts on the best solution that would be cool. The help is greatly appreciated! – Bob Spryn Dec 19 '11 at 18:57
  • So this is almost working perfectly. The weird thing I'm seeing is the coffeescript and handlebars files have their minispade identifier set to a tmp directory (I'm assuming, where the work is being done). http://screencast.com/t/wIXmREcreW Is there a way to set that to a root path such that it is normalized? Likewise my js files, while not pointing to a tmp path, are pointing to the original assets path instead of the public path. I know its just an identifier, but should I expect them to reference the public path? http://screencast.com/t/k9kZNcPo – Bob Spryn Dec 19 '11 at 20:21
  • 1
    The MinispadeFilter takes a :module_id_generator option which allows you to customize the minispade identifier. Unfortunately, if you're new to Ruby, it might get a little hairy. Here's the one I use on one project: `:module_id_generator => proc { |input| input.path.sub(/lib\//, 'timelog/').sub(/\.js$/, '')` which just substitutes the `lib/` at the front of the path for `timelog` and strips the `.js` off the end. There's not much room to explain here, so if you want to ask another question specifically about this, I'll try to answer it there. – dudleyf Dec 20 '11 at 02:21
  • Question asked here: http://stackoverflow.com/questions/8570143/how-do-you-customize-the-identifier-used-by-minispadefilter-in-rake-pipeline – Bob Spryn Dec 20 '11 at 02:36