Skip to content

Instantly share code, notes, and snippets.

@joselo
Forked from baphled/application.rb
Created April 13, 2012 17:53
Show Gist options
  • Save joselo/2378795 to your computer and use it in GitHub Desktop.
Save joselo/2378795 to your computer and use it in GitHub Desktop.

Revisions

  1. @baphled baphled revised this gist Sep 11, 2011. 2 changed files with 25 additions and 4 deletions.
    2 changes: 2 additions & 0 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Needed if you are not requiring rails/all
    require 'sprockets/railtie'
    27 changes: 23 additions & 4 deletions jasmine_config.rb
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,24 @@
    module Jasmine
    class Config

    include Rake::DSL

    def js_files(spec_filter = nil)
    # remove all generated files
    generated_files_directory = File.expand_path("../generated", __FILE__)
    # @TODO actually remove generated specs
    #
    # Think we need to include the rake DSL to make use of rm_rf now
    #
    rm_rf generated_files_directory, :secure => true

    precompile_app_assets
    compile_jasmine_javascripts

    # this is code from the original jasmine config js_files method - you could also just alias_method_chain it
    spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
    src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) }

    end

    private
    @@ -27,13 +38,21 @@ def precompile_app_assets
    ::Rake.application['environment'].invoke

    # temporarily set the static assets location from public/assets to our spec directory
    ::Rails.application.config.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")
    #
    # This line is now redundant as sprockets does not use static_root to store assets any more
    #
    #::Rails.application.config.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

    # rake won't let you run the same task twice in the same process without re-enabling it
    # so reenable the "clean" task and run it
    # Sprockets seems to think that assets always belong in the public directory
    # Though it's a bit hackish we remove the assets from the public directory after we're done
    #
    # Would be nicer if sprockets allowed for a way for us to precompile our assets in the generated directory
    # Thats something to look into for later.
    #
    ::Rake.application['assets:clean'].reenable
    ::Rake.application['assets:clean'].invoke

    # rake won't let you run the same task twice in the same process without re-enabling it
    # once the assets have been cleared, recompile them into the spec directory
    ::Rake.application['assets:precompile'].reenable
    ::Rake.application['assets:precompile'].invoke
    @@ -49,7 +68,7 @@ def compile_jasmine_javascripts

    Dir.glob(glob).each do |srcfile|
    srcfile = Pathname.new(srcfile)
    destfile = srcfile.sub(root, destination_dir).sub(".coffee", ".js")
    destfile = srcfile.sub(root, destination_dir).sub(".coffee", "")
    FileUtils.mkdir_p(destfile.dirname)
    File.open(destfile, "w") {|f| f.write(CoffeeScript.compile(File.new(srcfile)))}
    end
  2. @baphled baphled revised this gist Sep 1, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jasmine_config.rb
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ def precompile_app_assets
    ::Rake.application['environment'].invoke

    # temporarily set the static assets location from public/assets to our spec directory
    ::Rails.application.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")
    ::Rails.application.config.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

    # rake won't let you run the same task twice in the same process without re-enabling it
    # so reenable the "clean" task and run it
  3. @baphled baphled revised this gist Jul 24, 2011. 2 changed files with 5 additions and 1 deletion.
    4 changes: 4 additions & 0 deletions development.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #
    # Would be nice to have this set within jasmine.yml.
    #
    config.assets.precompile += %w( application.js form.js frontend.js *.css )
    2 changes: 1 addition & 1 deletion jasmine_config.rb
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ def precompile_app_assets
    # this method compiles all of the spec files into js files that jasmine can run
    def compile_jasmine_javascripts
    puts "Compiling jasmine coffee scripts into javascript..."
    root = File.expand_path("../../../../spec/javascripts/coffee", __FILE__)
    root = File.expand_path("../../../../spec/javascripts", __FILE__)
    destination_dir = File.expand_path("../../generated/specs", __FILE__)

    glob = File.expand_path("**/*.coffee", root)
  4. @baphled baphled created this gist Jul 23, 2011.
    74 changes: 74 additions & 0 deletions jasmine_config.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    # spec/javascripts/support/jasmine_config.rb
    # when jasmine starts the server out-of-process, it needs this in order to be able to invoke the asset tasks
    unless Object.const_defined?(:Rake)
    require 'rake'
    load File.expand_path('../../../../Rakefile', __FILE__)
    end

    module Jasmine
    class Config

    def js_files(spec_filter = nil)
    precompile_app_assets
    compile_jasmine_javascripts

    # this is code from the original jasmine config js_files method - you could also just alias_method_chain it
    spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
    src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) }
    end

    private

    # this method compiles all the same javascript files your app will
    def precompile_app_assets
    puts "Precompiling assets..."

    # make sure the Rails environment is loaded
    ::Rake.application['environment'].invoke

    # temporarily set the static assets location from public/assets to our spec directory
    ::Rails.application.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

    # rake won't let you run the same task twice in the same process without re-enabling it
    # so reenable the "clean" task and run it
    ::Rake.application['assets:clean'].reenable
    ::Rake.application['assets:clean'].invoke

    # once the assets have been cleared, recompile them into the spec directory
    ::Rake.application['assets:precompile'].reenable
    ::Rake.application['assets:precompile'].invoke
    end

    # this method compiles all of the spec files into js files that jasmine can run
    def compile_jasmine_javascripts
    puts "Compiling jasmine coffee scripts into javascript..."
    root = File.expand_path("../../../../spec/javascripts/coffee", __FILE__)
    destination_dir = File.expand_path("../../generated/specs", __FILE__)

    glob = File.expand_path("**/*.coffee", root)

    Dir.glob(glob).each do |srcfile|
    srcfile = Pathname.new(srcfile)
    destfile = srcfile.sub(root, destination_dir).sub(".coffee", ".js")
    FileUtils.mkdir_p(destfile.dirname)
    File.open(destfile, "w") {|f| f.write(CoffeeScript.compile(File.new(srcfile)))}
    end
    end

    end
    end

    # Note - this is necessary for rspec2, which has removed the backtrace
    module Jasmine
    class SpecBuilder
    def declare_spec(parent, spec)
    me = self
    example_name = spec["name"]
    @spec_ids << spec["id"]
    backtrace = @example_locations[parent.description + " " + example_name]
    parent.it example_name, {} do
    me.report_spec(spec["id"])
    end
    end
    end
    end