Skip to content

Instantly share code, notes, and snippets.

@vic
Created February 13, 2011 23:26
Show Gist options
  • Select an option

  • Save vic/825298 to your computer and use it in GitHub Desktop.

Select an option

Save vic/825298 to your computer and use it in GitHub Desktop.
Creating an Apache Buildr addon for incremental testing (using guard gem)
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with this
# work for additional information regarding copyright ownership. The ASF
# licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
require 'guard'
require 'guard/guard'
module ::Guard
def self.start_without_guardfile(options = {})
setup(options)
Interactor.init_signal_traps
yield
listener.on_change do |files|
if Watcher.match_files?(guards, files)
run { run_on_change_for_all_guards(files) }
end
end
UI.info "Buildr watching for filesystem changes."
guards.each { |guard| supervised_task(guard, :start) }
listener.start
end
end
module Buildr
module Autotest
include Extension
Guards = Array.new
class CompileGuard < Guard::Guard
attr_reader :project
def initialize(project)
@project = project
super(watchers, options)
end
def compiler
Buildr::Compiler.select(compile_task.compiler)
end
def watchers
compile_task.sources.map do |s|
e = Regexp.escape(Buildr::Util.
relative_path(s.to_s, Dir.pwd) +
File::SEPARATOR)
".*\\." + compiler.source_ext + "$"
Guard::Watcher.new Regexp.new(e)
end
end
def options
{}
end
def run_on_change(paths)
paths.replace []
compile_task.reenable
begin
compile_task.invoke
rescue
compile_task.reenable
end
end
end
class SourceCompileGuard < CompileGuard
def self.applies_to?(project)
project.compile.compiler
end
def compile_task
project.compile
end
Autotest::Guards << self
end
class TestCompileGuard < CompileGuard
def self.applies_to?(project)
project.test.compile.compiler
end
def compile_task
project.test.compile
end
Autotest::Guards << self
end
class ResourcesGuard < Guard::Guard
def self.applies_to?(project)
true
end
attr_reader :project
def initialize(project)
@project = project
super(watchers, options)
end
def resources_task
project.resources
end
def watchers
resources_task.sources.map do |s|
e = Regexp.escape(Buildr::Util.
relative_path(s.to_s, Dir.pwd) +
File::SEPARATOR) + ".*"
Guard::Watcher.new Regexp.new(e)
end
end
def options
{}
end
def run_on_change(paths)
paths.replace []
resources_task.reenable
begin
resources_task.invoke
rescue
resources_task.reenable
end
end
Autotest::Guards << self
end
class TestResourcesGuard < ResourcesGuard
def self.applies_to?(project)
true
end
def resources_task
project.test.resources
end
Autotest::Guards << self
end
class JavaTestGuard < Guard::Guard
def self.applies_to?(project)
project.test.compile.language == :java
end
attr_reader :project
def initialize(project)
@project = project
super(watchers, options)
end
def options
{}
end
def watchers
ct = Buildr::Util.
relative_path(project.compile.target.to_s, Dir.pwd)
tt = Buildr::Util.
relative_path(project.test.compile.target.to_s, Dir.pwd)
ct = Regexp.escape(ct + File::SEPARATOR) + ".*\." +
Buildr::Compiler.select(project.compile.compiler).
target_ext + "$"
tt = Regexp.escape(tt + File::SEPARATOR) + ".*\." +
Buildr::Compiler.select(project.test.compile.compiler).
target_ext + "$"
[ Guard::Watcher.new(Regexp.new(ct)),
Guard::Watcher.new(Regexp.new(tt)) ]
end
def run_on_change(paths)
puts "Running tests from ", paths
paths.replace []
end
Autotest::Guards << self
end
def self.guard_if_applicable(guard_class, project)
if guard_class.applies_to? project
Guard.guards << guard_class.new(project)
end
end
def self.watch(guards, *projects)
Guard.start_without_guardfile do
projects.each do |project|
guards.each { |guard| guard_if_applicable guard, project }
end
end
end
after_define do |project|
task 'autotest' do
Autotest.watch Buildr::Autotest::Guards, project
end
end
end
end
class Project
include Buildr::Autotest
end
task 'autotest' do
Buildr::Autotest.watch Buildr::Autotest::Guards, *Buildr.projects
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment