Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Extract out the environment code into a separate Environment module.

  • Loading branch information...
commit 6463caf51bb16f5a804f89172b9c499c9f53cc61 1 parent 94adbc9
Charles Lowell authored February 03, 2012
4  lib/jenkins/model/describable.rb
@@ -41,8 +41,8 @@ module DescribeAs
41 41
         # Java class that represents the extension point, which gets eventually set to Descriptor.clazz
42 42
         # :with will use this java class as the type of descriptor.
43 43
         def describe_as cls, options = {}
44  
-          @describe_as_type = verify_java_class(cls).java_class
45  
-          @descriptor_is = verify_java_class(options[:with]).java_class if options[:with]
  44
+          @describe_as_type = verify_java_class(cls)
  45
+          @descriptor_is = verify_java_class(options[:with]) if options[:with]
46 46
         end
47 47
 
48 48
         def describe_as_type
24  lib/jenkins/model/environment.rb
... ...
@@ -0,0 +1,24 @@
  1
+module Jenkins::Model
  2
+  module Environment
  3
+    # Perform setup for a build
  4
+    #
  5
+    # invoked after checkout, but before any `Builder`s have been run
  6
+    # @param [Jenkins::Model::Build] build the build about to run
  7
+    # @param [Jenkins::Launcher] launcher a launcher for the orderly starting/stopping of processes.
  8
+    # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
  9
+    def setup(build, launcher, listener)
  10
+
  11
+    end
  12
+
  13
+    # Optionally perform optional teardown for a build
  14
+    #
  15
+    # invoked after a build has run for better or for worse. It's ok if subclasses
  16
+    # don't override this.
  17
+    #
  18
+    # @param [Jenkins::Model::Build] the build which has completed
  19
+    # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
  20
+    def teardown(build, listener)
  21
+
  22
+    end
  23
+  end
  24
+end
52  lib/jenkins/model/environment_proxy.rb
... ...
@@ -0,0 +1,52 @@
  1
+module Jenkins::Model
  2
+  module EnvironmentProxy
  3
+    extend Jenkins::Plugin::Behavior
  4
+
  5
+    module InstanceMethods
  6
+      def setUp(build, launcher, listener)
  7
+        @object.setup(import(build), import(launcher), import(listener))
  8
+        environment_class = self.class.environment_class || DefaultEnvironment
  9
+        environment_class.new(self, @plugin, @object).tap do |env|
  10
+          env.plugin = @plugin
  11
+          env.impl = @object
  12
+        end
  13
+      rescue Jenkins::Model::Build::Halt
  14
+        nil
  15
+      end
  16
+    end
  17
+
  18
+    module ClassMethods
  19
+      attr_reader :environment_class
  20
+
  21
+      def environment_is(java_class, &block)
  22
+        @environment_class = Class.new(java_class)
  23
+        if block_given?
  24
+          @environment_class.class_eval(&block)
  25
+        end
  26
+        @environment_class.class_eval do
  27
+          include EnvironmentWrapper
  28
+        end
  29
+      end
  30
+    end
  31
+
  32
+    module EnvironmentWrapper
  33
+      attr_accessor :plugin, :impl
  34
+
  35
+      def tearDown(build, listener)
  36
+        @impl.teardown(@plugin.import(build), @plugin.import(listener))
  37
+        true
  38
+      rescue Jenkins::Model::Build::Halt
  39
+        false
  40
+      end
  41
+    end
  42
+
  43
+    class DefaultEnvironment < Java.hudson.model.Environment
  44
+      include EnvironmentWrapper
  45
+      def initialize(proxy, plugin, object)
  46
+        @plugin = plugin
  47
+        @object = object
  48
+      end
  49
+    end
  50
+
  51
+  end
  52
+end
10  lib/jenkins/plugin.rb
@@ -116,10 +116,10 @@ def register_extension(class_or_instance, *args)
116 116
     # @see [Model::Describable]
117 117
     def register_describable(describable_class)
118 118
       on.start do
119  
-        fail "#{describable_class} is not an instance of Describable" unless describable_class.is_a? Model::Describable
120  
-        descriptor_class = descriptor_class.descriptor_is || Jenkins::Model::Descriptor
121  
-        descriptor = descriptor_class.new(ruby_class, self, describable_class.describe_as_type)
122  
-        @descriptors[ruby_class] = descriptor
  119
+        fail "#{describable_class} is not an instance of Describable" unless describable_class < Model::Describable
  120
+        descriptor_class = describable_class.descriptor_is || Jenkins::Model::Descriptor
  121
+        descriptor = descriptor_class.new(describable_class, self, describable_class.describe_as_type.java_class)
  122
+        @descriptors[describable_class] = descriptor
123 123
         register_extension(descriptor)
124 124
       end
125 125
     end
@@ -237,7 +237,7 @@ def fire(event, *args)
237 237
       def callback(listener, *args)
238 238
         listener.call(*args)
239 239
       rescue Exception => e
240  
-        $stderr.warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
  240
+        warn "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
241 241
       end
242 242
     end
243 243
   end
2  lib/jenkins/plugin/proxies.rb
@@ -172,7 +172,7 @@ def self.clear
172 172
 end
173 173
 
174 174
 require 'jenkins/model/describable'
175  
-["action", "build_wrapper", "builder", "publisher", "root_action"].each do |proxy|
  175
+["action", "builder", "publisher", "root_action"].each do |proxy|
176 176
   require "jenkins/plugin/proxies/#{proxy}"
177 177
 end
178 178
 
49  lib/jenkins/plugin/proxies/build_wrapper.rb
... ...
@@ -1,49 +0,0 @@
1  
-
2  
-require 'jenkins/tasks/build_wrapper'
3  
-require 'jenkins/model/build'
4  
-
5  
-module Jenkins
6  
-  class Plugin
7  
-    class Proxies
8  
-
9  
-      ##
10  
-      # Binds the Java hudson.tasks.BuildWrapper API to the idomatic
11  
-      # Ruby API Jenkins::Tasks::BuildWrapper
12  
-
13  
-      class BuildWrapper < Java.hudson.tasks.BuildWrapper
14  
-        include Describable
15  
-        proxy_for Jenkins::Tasks::BuildWrapper
16  
-
17  
-        def setUp(build, launcher, listener)
18  
-          @object.setup(import(build), import(launcher), import(listener))
19  
-          EnvironmentWrapper.new(self, @plugin, @object)
20  
-        rescue Jenkins::Model::Build::Halt
21  
-          nil
22  
-        end
23  
-      end
24  
-
25  
-
26  
-      class EnvironmentWrapper < Java.hudson.tasks.BuildWrapper::Environment
27  
-        attr_accessor :env
28  
-
29  
-        def initialize(build_wrapper, plugin, impl)
30  
-          super(build_wrapper)
31  
-          @plugin = plugin
32  
-          @impl = impl
33  
-        end
34  
-
35  
-        # build wrapper that created this environment
36  
-        def build_wrapper
37  
-          @impl
38  
-        end
39  
-
40  
-        def tearDown(build, listener)
41  
-          @impl.teardown(@plugin.import(build), @plugin.import(listener))
42  
-          true
43  
-        rescue Jenkins::Model::Build::Halt
44  
-          false
45  
-        end
46  
-      end
47  
-    end
48  
-  end
49  
-end
5  lib/jenkins/plugin/runtime.rb
@@ -8,6 +8,8 @@
8 8
 require 'jenkins/plugin/proxies'
9 9
 require 'jenkins/cli/command'
10 10
 require 'jenkins/model'
  11
+require 'jenkins/model/environment'
  12
+require 'jenkins/model/environment_proxy'
11 13
 require 'jenkins/model/action'
12 14
 require 'jenkins/model/root_action'
13 15
 require 'jenkins/model/build'
@@ -16,6 +18,9 @@
16 18
 require 'jenkins/slaves/cloud'
17 19
 require 'jenkins/tasks/builder'
18 20
 require 'jenkins/tasks/build_wrapper'
  21
+require 'jenkins/tasks/build_wrapper_proxy'
19 22
 require 'jenkins/launcher'
20 23
 require 'jenkins/listeners/run_listener'
21 24
 require 'jenkins/listeners/run_listener_proxy'
  25
+require 'jenkins/slaves/node_property'
  26
+require 'jenkins/slaves/node_property_proxy'
24  lib/jenkins/slaves/node_property.rb
... ...
@@ -0,0 +1,24 @@
  1
+module Jenkins::Slaves
  2
+  class NodeProperty
  3
+    include Jenkins::Model
  4
+    include Jenkins::Model::Describable
  5
+
  6
+    def can_take?(buildable)
  7
+    end
  8
+
  9
+    def setup(build, launcher, listener)
  10
+    end
  11
+
  12
+    def teardown(build, listener)
  13
+    end
  14
+
  15
+    class NodePropertyDescriptor < Java.hudson.slaves.NodePropertyDescriptor
  16
+      include Jenkins::Model::RubyDescriptor
  17
+
  18
+      def isApplicable(targetType)
  19
+        true
  20
+      end
  21
+    end
  22
+    describe_as Java.hudson.slaves.NodeProperty, :with => NodePropertyDescriptor
  23
+  end
  24
+end
6  lib/jenkins/slaves/node_property_proxy.rb
... ...
@@ -0,0 +1,6 @@
  1
+module Jenkins::Slaves
  2
+  class NodePropertyProxy < Java.hudson.slaves.NodeProperty
  3
+    include Jenkins::Plugin::Proxies::Describable
  4
+    proxy_for NodeProperty
  5
+  end
  6
+end
23  lib/jenkins/tasks/build_wrapper.rb
@@ -7,30 +7,9 @@ module Tasks
7 7
     # {http://javadoc.jenkins-ci.org/hudson/tasks/BuildWrapper.html}
8 8
     class BuildWrapper
9 9
       include Jenkins::Model
  10
+      include Jenkins::Model::Environment
10 11
       include Jenkins::Model::Describable
11  
-
12 12
       describe_as Java.hudson.tasks.BuildWrapper
13  
-
14  
-      # Perform setup for a build
15  
-      #
16  
-      # invoked after checkout, but before any `Builder`s have been run
17  
-      # @param [Jenkins::Model::Build] build the build about to run
18  
-      # @param [Jenkins::Launcher] launcher a launcher for the orderly starting/stopping of processes.
19  
-      # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
20  
-      def setup(build, launcher, listener)
21  
-
22  
-      end
23  
-
24  
-      # Optionally perform optional teardown for a build
25  
-      #
26  
-      # invoked after a build has run for better or for worse. It's ok if subclasses
27  
-      # don't override this.
28  
-      #
29  
-      # @param [Jenkins::Model::Build] the build which has completed
30  
-      # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
31  
-      def teardown(build, listener)
32  
-
33  
-      end
34 13
     end
35 14
   end
36 15
 end
15  lib/jenkins/tasks/build_wrapper_proxy.rb
... ...
@@ -0,0 +1,15 @@
  1
+module Jenkins::Tasks
  2
+  class BuildWrapperProxy < Java.hudson.tasks.BuildWrapper
  3
+    include Jenkins::Plugin::Proxies::Describable
  4
+    proxy_for Jenkins::Tasks::BuildWrapper
  5
+
  6
+    include Jenkins::Model::EnvironmentProxy
  7
+
  8
+    # BuildWrapper needs a custom Environment class, not sure why
  9
+    environment_is Java.hudson.tasks.BuildWrapper::Environment do
  10
+      def initialize(proxy, plugin, object)
  11
+        super(proxy)
  12
+      end
  13
+    end
  14
+  end
  15
+end
8  spec/jenkins/model/describable_spec.rb
@@ -40,7 +40,7 @@ def @subclass.to_s; "SubClass" end
40 40
       end
41 41
 
42 42
       it "has the same java type as its superclass" do
43  
-        @subclass.describe_as_type.should eql java.lang.Object.java_class
  43
+        @subclass.describe_as_type.should eql java.lang.Object
44 44
       end
45 45
 
46 46
       describe ". a sub-subclass" do
@@ -51,9 +51,9 @@ def @subclass.to_s; "SubClass" end
51 51
         it "is also registered as an extension of the original java type" do
52 52
           @plugin.should have_received(:register_describable).with(@subsubclass)
53 53
         end
54  
-        
  54
+
55 55
         it 'inherits its describe_as_type' do
56  
-          @subsubclass.describe_as_type.should eql java.lang.Object.java_class
  56
+          @subsubclass.describe_as_type.should eql java.lang.Object
57 57
         end
58 58
       end
59 59
     end
@@ -70,7 +70,7 @@ def @subclass.to_s; "SubClass" end
70 70
         lambda {@class.describe_as java.lang.Object, :with => Object}.should raise_error(Jenkins::Model::Describable::DescribableError)
71 71
       end
72 72
       it "inherits the descriptor type" do
73  
-        @subclass.descriptor_is.should eql java.lang.String.java_class
  73
+        @subclass.descriptor_is.should eql java.lang.String
74 74
       end
75 75
     end
76 76
   end
6  spec/jenkins/plugin/proxies/build_wrapper_spec.rb → spec/jenkins/tasks/build_wrapper_proxy_spec.rb
... ...
@@ -1,15 +1,15 @@
1 1
 
2 2
 require 'spec_helper'
3 3
 
4  
-describe Jenkins::Plugin::Proxies::BuildWrapper do
  4
+describe Jenkins::Tasks::BuildWrapperProxy do
5 5
   include ProxyHelper
6 6
 
7  
-  subject {Jenkins::Plugin::Proxies::BuildWrapper}
  7
+  subject {Jenkins::Tasks::BuildWrapperProxy}
8 8
   it {should be_transient(:plugin)}
9 9
 
10 10
   before do
11 11
     @object = mock(Jenkins::Tasks::BuildWrapper)
12  
-    @wrapper = Jenkins::Plugin::Proxies::BuildWrapper.new(@plugin, @object)
  12
+    @wrapper = Jenkins::Tasks::BuildWrapperProxy.new(@plugin, @object)
13 13
   end
14 14
 
15 15
   it "passes in an env file which will be called to " do

0 notes on commit 6463caf

Please sign in to comment.
Something went wrong with that request. Please try again.