Permalink
Browse files

Extract out the environment code into a separate Environment module.

  • Loading branch information...
1 parent 94adbc9 commit 6463caf51bb16f5a804f89172b9c499c9f53cc61 @cowboyd cowboyd committed Feb 3, 2012
@@ -41,8 +41,8 @@ module DescribeAs
# Java class that represents the extension point, which gets eventually set to Descriptor.clazz
# :with will use this java class as the type of descriptor.
def describe_as cls, options = {}
- @describe_as_type = verify_java_class(cls).java_class
- @descriptor_is = verify_java_class(options[:with]).java_class if options[:with]
+ @describe_as_type = verify_java_class(cls)
+ @descriptor_is = verify_java_class(options[:with]) if options[:with]
end
def describe_as_type
@@ -0,0 +1,24 @@
+module Jenkins::Model
+ module Environment
+ # Perform setup for a build
+ #
+ # invoked after checkout, but before any `Builder`s have been run
+ # @param [Jenkins::Model::Build] build the build about to run
+ # @param [Jenkins::Launcher] launcher a launcher for the orderly starting/stopping of processes.
+ # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
+ def setup(build, launcher, listener)
+
+ end
+
+ # Optionally perform optional teardown for a build
+ #
+ # invoked after a build has run for better or for worse. It's ok if subclasses
+ # don't override this.
+ #
+ # @param [Jenkins::Model::Build] the build which has completed
+ # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
+ def teardown(build, listener)
+
+ end
+ end
+end
@@ -0,0 +1,52 @@
+module Jenkins::Model
+ module EnvironmentProxy
+ extend Jenkins::Plugin::Behavior
+
+ module InstanceMethods
+ def setUp(build, launcher, listener)
+ @object.setup(import(build), import(launcher), import(listener))
+ environment_class = self.class.environment_class || DefaultEnvironment
+ environment_class.new(self, @plugin, @object).tap do |env|
+ env.plugin = @plugin
+ env.impl = @object
+ end
+ rescue Jenkins::Model::Build::Halt
+ nil
+ end
+ end
+
+ module ClassMethods
+ attr_reader :environment_class
+
+ def environment_is(java_class, &block)
+ @environment_class = Class.new(java_class)
+ if block_given?
+ @environment_class.class_eval(&block)
+ end
+ @environment_class.class_eval do
+ include EnvironmentWrapper
+ end
+ end
+ end
+
+ module EnvironmentWrapper
+ attr_accessor :plugin, :impl
+
+ def tearDown(build, listener)
+ @impl.teardown(@plugin.import(build), @plugin.import(listener))
+ true
+ rescue Jenkins::Model::Build::Halt
+ false
+ end
+ end
+
+ class DefaultEnvironment < Java.hudson.model.Environment
+ include EnvironmentWrapper
+ def initialize(proxy, plugin, object)
+ @plugin = plugin
+ @object = object
+ end
+ end
+
+ end
+end
View
@@ -116,10 +116,10 @@ def register_extension(class_or_instance, *args)
# @see [Model::Describable]
def register_describable(describable_class)
on.start do
- fail "#{describable_class} is not an instance of Describable" unless describable_class.is_a? Model::Describable
- descriptor_class = descriptor_class.descriptor_is || Jenkins::Model::Descriptor
- descriptor = descriptor_class.new(ruby_class, self, describable_class.describe_as_type)
- @descriptors[ruby_class] = descriptor
+ fail "#{describable_class} is not an instance of Describable" unless describable_class < Model::Describable
+ descriptor_class = describable_class.descriptor_is || Jenkins::Model::Descriptor
+ descriptor = descriptor_class.new(describable_class, self, describable_class.describe_as_type.java_class)
+ @descriptors[describable_class] = descriptor
register_extension(descriptor)
end
end
@@ -237,7 +237,7 @@ def fire(event, *args)
def callback(listener, *args)
listener.call(*args)
rescue Exception => e
- $stderr.warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
+ warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
end
end
end
@@ -172,7 +172,7 @@ def self.clear
end
require 'jenkins/model/describable'
-["action", "build_wrapper", "builder", "publisher", "root_action"].each do |proxy|
+["action", "builder", "publisher", "root_action"].each do |proxy|
require "jenkins/plugin/proxies/#{proxy}"
end
@@ -1,49 +0,0 @@
-
-require 'jenkins/tasks/build_wrapper'
-require 'jenkins/model/build'
-
-module Jenkins
- class Plugin
- class Proxies
-
- ##
- # Binds the Java hudson.tasks.BuildWrapper API to the idomatic
- # Ruby API Jenkins::Tasks::BuildWrapper
-
- class BuildWrapper < Java.hudson.tasks.BuildWrapper
- include Describable
- proxy_for Jenkins::Tasks::BuildWrapper
-
- def setUp(build, launcher, listener)
- @object.setup(import(build), import(launcher), import(listener))
- EnvironmentWrapper.new(self, @plugin, @object)
- rescue Jenkins::Model::Build::Halt
- nil
- end
- end
-
-
- class EnvironmentWrapper < Java.hudson.tasks.BuildWrapper::Environment
- attr_accessor :env
-
- def initialize(build_wrapper, plugin, impl)
- super(build_wrapper)
- @plugin = plugin
- @impl = impl
- end
-
- # build wrapper that created this environment
- def build_wrapper
- @impl
- end
-
- def tearDown(build, listener)
- @impl.teardown(@plugin.import(build), @plugin.import(listener))
- true
- rescue Jenkins::Model::Build::Halt
- false
- end
- end
- end
- end
-end
@@ -8,6 +8,8 @@
require 'jenkins/plugin/proxies'
require 'jenkins/cli/command'
require 'jenkins/model'
+require 'jenkins/model/environment'
+require 'jenkins/model/environment_proxy'
require 'jenkins/model/action'
require 'jenkins/model/root_action'
require 'jenkins/model/build'
@@ -16,6 +18,9 @@
require 'jenkins/slaves/cloud'
require 'jenkins/tasks/builder'
require 'jenkins/tasks/build_wrapper'
+require 'jenkins/tasks/build_wrapper_proxy'
require 'jenkins/launcher'
require 'jenkins/listeners/run_listener'
require 'jenkins/listeners/run_listener_proxy'
+require 'jenkins/slaves/node_property'
+require 'jenkins/slaves/node_property_proxy'
@@ -0,0 +1,24 @@
+module Jenkins::Slaves
+ class NodeProperty
+ include Jenkins::Model
+ include Jenkins::Model::Describable
+
+ def can_take?(buildable)
+ end
+
+ def setup(build, launcher, listener)
+ end
+
+ def teardown(build, listener)
+ end
+
+ class NodePropertyDescriptor < Java.hudson.slaves.NodePropertyDescriptor
+ include Jenkins::Model::RubyDescriptor
+
+ def isApplicable(targetType)
+ true
+ end
+ end
+ describe_as Java.hudson.slaves.NodeProperty, :with => NodePropertyDescriptor
+ end
+end
@@ -0,0 +1,6 @@
+module Jenkins::Slaves
+ class NodePropertyProxy < Java.hudson.slaves.NodeProperty
+ include Jenkins::Plugin::Proxies::Describable
+ proxy_for NodeProperty
+ end
+end
@@ -7,30 +7,9 @@ module Tasks
# {http://javadoc.jenkins-ci.org/hudson/tasks/BuildWrapper.html}
class BuildWrapper
include Jenkins::Model
+ include Jenkins::Model::Environment
include Jenkins::Model::Describable
-
describe_as Java.hudson.tasks.BuildWrapper
-
- # Perform setup for a build
- #
- # invoked after checkout, but before any `Builder`s have been run
- # @param [Jenkins::Model::Build] build the build about to run
- # @param [Jenkins::Launcher] launcher a launcher for the orderly starting/stopping of processes.
- # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
- def setup(build, launcher, listener)
-
- end
-
- # Optionally perform optional teardown for a build
- #
- # invoked after a build has run for better or for worse. It's ok if subclasses
- # don't override this.
- #
- # @param [Jenkins::Model::Build] the build which has completed
- # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
- def teardown(build, listener)
-
- end
end
end
end
@@ -0,0 +1,15 @@
+module Jenkins::Tasks
+ class BuildWrapperProxy < Java.hudson.tasks.BuildWrapper
+ include Jenkins::Plugin::Proxies::Describable
+ proxy_for Jenkins::Tasks::BuildWrapper
+
+ include Jenkins::Model::EnvironmentProxy
+
+ # BuildWrapper needs a custom Environment class, not sure why
+ environment_is Java.hudson.tasks.BuildWrapper::Environment do
+ def initialize(proxy, plugin, object)
+ super(proxy)
+ end
+ end
+ end
+end
@@ -40,7 +40,7 @@ def @subclass.to_s; "SubClass" end
end
it "has the same java type as its superclass" do
- @subclass.describe_as_type.should eql java.lang.Object.java_class
+ @subclass.describe_as_type.should eql java.lang.Object
end
describe ". a sub-subclass" do
@@ -51,9 +51,9 @@ def @subclass.to_s; "SubClass" end
it "is also registered as an extension of the original java type" do
@plugin.should have_received(:register_describable).with(@subsubclass)
end
-
+
it 'inherits its describe_as_type' do
- @subsubclass.describe_as_type.should eql java.lang.Object.java_class
+ @subsubclass.describe_as_type.should eql java.lang.Object
end
end
end
@@ -70,7 +70,7 @@ def @subclass.to_s; "SubClass" end
lambda {@class.describe_as java.lang.Object, :with => Object}.should raise_error(Jenkins::Model::Describable::DescribableError)
end
it "inherits the descriptor type" do
- @subclass.descriptor_is.should eql java.lang.String.java_class
+ @subclass.descriptor_is.should eql java.lang.String
end
end
end
@@ -1,15 +1,15 @@
require 'spec_helper'
-describe Jenkins::Plugin::Proxies::BuildWrapper do
+describe Jenkins::Tasks::BuildWrapperProxy do
include ProxyHelper
- subject {Jenkins::Plugin::Proxies::BuildWrapper}
+ subject {Jenkins::Tasks::BuildWrapperProxy}
it {should be_transient(:plugin)}
before do
@object = mock(Jenkins::Tasks::BuildWrapper)
- @wrapper = Jenkins::Plugin::Proxies::BuildWrapper.new(@plugin, @object)
+ @wrapper = Jenkins::Tasks::BuildWrapperProxy.new(@plugin, @object)
end
it "passes in an env file which will be called to " do

0 comments on commit 6463caf

Please sign in to comment.