jenkins-plugin-runtime 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,26 @@ module Jenkins
18
18
 
19
19
  def initialize(native = nil)
20
20
  @native = native
21
+ @variables = {}
22
+ end
23
+
24
+ # Gets a build value. Each build stores a map of key,value
25
+ # pairs which can be used by each build step in the pipeline
26
+ #
27
+ # @param [String|Symbol] key
28
+ # @return [Object] value
29
+ def [](key)
30
+ @variables[key.to_s]
31
+ end
32
+
33
+ # Sets a build value. Each build has a map of key,value
34
+ # pairs which allow build steps to share information
35
+ #
36
+ # @param [String|Symbol] key
37
+ # @param [Object] value
38
+ # @return [Object] value
39
+ def []=(key, value)
40
+ @variables[key.to_s] = value
21
41
  end
22
42
 
23
43
  def workspace
@@ -55,8 +55,15 @@ module Jenkins
55
55
  @impl.new
56
56
  end
57
57
 
58
+ # compute the path name of views for this class
58
59
  def name_to_path
59
- @impl.name.split('::').join('/')
60
+ # camel case to underscore conversion taken from ActiveSupport::Inflector::underscore,
61
+ # which is MIT-licensed.
62
+ @impl.name.split('::').join('/').gsub(/::/, '/').
63
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
64
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
65
+ tr("-", "_").
66
+ downcase
60
67
  end
61
68
  end
62
69
 
@@ -152,8 +152,8 @@ module Jenkins
152
152
  #
153
153
  # @param [Object] internal the object on the Ruby side of the link
154
154
  # @param [java.lang.Object] external the object on the Java side of the link
155
- def link(internal, external)
156
- @proxies.link internal, external
155
+ def linkout(internal, external)
156
+ @proxies.linkout internal, external
157
157
  end
158
158
 
159
159
  def load_models
@@ -5,7 +5,7 @@ module Jenkins
5
5
 
6
6
  ExportError = Class.new(StandardError)
7
7
  ImportError = Class.new(StandardError)
8
- NativeJavaObject = Struct.new(:native)
8
+ OpaqueJavaObject = Struct.new(:native)
9
9
 
10
10
  # Maps JRuby objects part of the idomatic Ruby API
11
11
  # to a plain Java object representation and vice-versa.
@@ -49,20 +49,20 @@ module Jenkins
49
49
  # @param [Object] object the object to bring in from the outside
50
50
  # @return the best representation of that object for this plugin
51
51
  def import(object)
52
- if ref = @ext2int[object]
53
- return ref.get() if ref.get()
52
+ if proxy = deref(@ext2int, object)
53
+ return proxy
54
54
  end
55
55
  cls = object.class
56
56
  while cls do
57
57
  if internal_class = @@extcls2intcls[cls]
58
58
  internal = internal_class.new(object)
59
- link(internal, object)
59
+ linkin(internal, object)
60
60
  return internal
61
61
  end
62
62
  cls = cls.superclass
63
63
  end
64
- internal = NativeJavaObject.new(object)
65
- link(internal, object)
64
+ internal = OpaqueJavaObject.new(object)
65
+ linkin(internal, object)
66
66
  return internal
67
67
  end
68
68
 
@@ -73,35 +73,73 @@ module Jenkins
73
73
  # @return [java.lang.Object] the Java wrapper that provides an interface to `object`
74
74
  # @throw [ExportError] if no suitable Java representation can be found
75
75
  def export(object)
76
- if ref = @int2ext[object]
77
- return ref.get() if ref.get()
76
+ if proxy = deref(@int2ext, object)
77
+ return proxy
78
78
  end
79
79
 
80
80
  cls = object.class
81
81
  while cls do
82
- if proxy_class = @@intcls2extcls[cls]
83
- proxy = proxy_class.new(@plugin, object)
84
- link(object, proxy)
85
- return proxy
82
+ if external_class = @@intcls2extcls[cls]
83
+ external = external_class.new(@plugin, object)
84
+ linkout(object, external)
85
+ return external
86
86
  end
87
87
  cls = cls.superclass
88
88
  end
89
89
  raise ExportError, "unable to find suitable Java Proxy for #{object.inspect}"
90
90
  end
91
91
 
92
- ##
93
- # Link a plugin-local Ruby object to an external Java object such that they will
94
- # be subsituted for one another when passing values back and forth between Jenkins
95
- # and this plugin. An example of this is associating the Ruby Jenkins::Launcher object
96
- # with an equivalent
92
+
93
+ # Forms an incoming reference to an internal Ruby object from an
94
+ # external Java object. These two objects will be referentially
95
+ # equivalent whenever they are passed back and forth between the Ruby
96
+ # plugin and the Jenkins runtime.
97
+ #
98
+ # The fact that this reference is "in" means
99
+ # that GC-wise, the internal Ruby object depends on the external
100
+ # Java object. In other words, it will not be garbage collected
101
+ # until the Java object is.
102
+ #
103
+ # This behavior is important for things like AbstractBuild objects. We
104
+ # don't want our Ruby `Build` object that corresponds to it to be GC'd
105
+ # until the `hudson.model.AbstractBuild` it represents is itself
106
+ # collected. That allows us to maintain state on the Ruby object, and know
107
+ # that the same state will be accessible wherever.
108
+ #
109
+ # Only weak refereces are maintained to the external Java object.
97
110
  #
98
111
  # @param [Object] internal the object on the Ruby side of the link
99
112
  # @param [java.lang.Object] external the object on the Java side of the link
100
- def link(internal, external)
113
+ def linkin(internal, external)
101
114
  @int2ext.put(internal, java.lang.ref.WeakReference.new(external))
115
+ @ext2int.put(external, internal)
116
+ end
117
+
118
+ # Forms an outgoing reference from an internal Ruby object to an
119
+ # external Java object. These two objects will be referentially
120
+ # equivalent whenever they are passed back and forth between the Ruby
121
+ # plugin and the Jenkins runtime.
122
+ #
123
+ # The fact that this reference goes "out" means
124
+ # that GC-wise, the external Java object depends on the internal
125
+ # Ruby object. In other words, The Java object will not be garbage
126
+ # collected until the local Ruby object is.
127
+ #
128
+ # Only weak refereces are maintained to the internal Ruby object.
129
+ #
130
+ # @param [Object] internal the object on the Ruby side of the link
131
+ # @param [java.lang.Object] external the object on the Java side of the link
132
+ def linkout(internal, external)
133
+ @int2ext.put(internal, external)
102
134
  @ext2int.put(external, java.lang.ref.WeakReference.new(internal))
103
135
  end
104
136
 
137
+ def deref(reflist, object)
138
+ if ref = reflist[object]
139
+ ref.is_a?(java.lang.ref.Reference) ? ref.get() : ref
140
+ end
141
+ end
142
+
105
143
  ##
106
144
  # Associated the the Ruby class `internal_class` with the Java class `external_class`.
107
145
  #
@@ -15,11 +15,10 @@ module Jenkins
15
15
  include Jenkins::Plugin::Proxy
16
16
 
17
17
  def setUp(build, launcher, listener)
18
- env = {}
19
- @object.setup(import(build), import(launcher), import(listener), env)
20
- EnvironmentWrapper.new(self, @plugin, @object, env)
18
+ @object.setup(import(build), import(launcher), import(listener))
19
+ EnvironmentWrapper.new(self, @plugin, @object)
21
20
  rescue Jenkins::Model::Build::Halt
22
- nil
21
+ nil
23
22
  end
24
23
 
25
24
  def getDescriptor
@@ -35,15 +34,14 @@ module Jenkins
35
34
 
36
35
  class EnvironmentWrapper < Java.hudson.tasks.BuildWrapper::Environment
37
36
 
38
- def initialize(build_wrapper, plugin, impl, env)
37
+ def initialize(build_wrapper, plugin, impl)
39
38
  super(build_wrapper)
40
39
  @plugin = plugin
41
40
  @impl = impl
42
- @env = env
43
41
  end
44
42
 
45
43
  def tearDown(build, listener)
46
- @impl.teardown(@plugin.import(build), @plugin.import(listener), @env)
44
+ @impl.teardown(@plugin.import(build), @plugin.import(listener))
47
45
  true
48
46
  rescue Jenkins::Model::Build::Halt
49
47
  false
@@ -56,7 +56,7 @@ module Jenkins
56
56
  # so that they remain referentially equivalent.
57
57
  def read_completed
58
58
  @plugin = Java.jenkins.model.Jenkins.getInstance().getPlugin(@pluginid).getNativeRubyPlugin()
59
- @plugin.link @object, self
59
+ @plugin.linkout @object, self
60
60
  end
61
61
 
62
62
  end
@@ -1,7 +1,7 @@
1
1
  module Jenkins
2
2
  class Plugin
3
3
  module Runtime
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.11"
5
5
  end
6
6
  end
7
7
  end
@@ -17,8 +17,7 @@ module Jenkins
17
17
  # @param [Jenkins::Model::Build] build the build about to run
18
18
  # @param [Jenkins::Launcher] launcher a launcher for the orderly starting/stopping of processes.
19
19
  # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
20
- # @param [Hash] env a place to store information needed by #teardown
21
- def setup(build, launcher, listener, env)
20
+ def setup(build, launcher, listener)
22
21
 
23
22
  end
24
23
 
@@ -29,8 +28,7 @@ module Jenkins
29
28
  #
30
29
  # @param [Jenkins::Model::Build] the build which has completed
31
30
  # @param [Jenkins::Model::Listener] listener channel for interacting with build output console
32
- # @param [Hash] env contains anything that #setup needs to tell #teardown about
33
- def teardown(build, listener, env)
31
+ def teardown(build, listener)
34
32
 
35
33
  end
36
34
  end
@@ -36,4 +36,25 @@ describe Jenkins::Model::Build do
36
36
  it "can abort" do
37
37
  expect {@build.abort "aborting"}.should raise_error(Java.hudson.AbortException, "aborting")
38
38
  end
39
+
40
+ describe "hash-y interface" do
41
+ before do
42
+ @val = Object.new
43
+ @build['val'] = @val
44
+ end
45
+
46
+ it "gets" do
47
+ @build['val'].should be @val
48
+ end
49
+
50
+ it "sets" do
51
+ @build['val'] = :foo
52
+ @build['val'].should be :foo
53
+ end
54
+
55
+ it "has symbol/string indifferent access" do
56
+ @build[:val].should be @val
57
+ end
58
+ end
59
+
39
60
  end
@@ -14,12 +14,12 @@ describe Jenkins::Plugin::Proxies::BuildWrapper do
14
14
 
15
15
  it "passes in an env file which will be called to " do
16
16
  env = nil
17
- @object.should_receive(:setup).with(@build, @launcher, @listener, an_instance_of(Hash)) do |*args|
17
+ @object.should_receive(:setup).with(@build, @launcher, @listener) do |*args|
18
18
  env = args.last
19
19
  end
20
20
  environment = @wrapper.setUp(@jBuild, @jLauncher, @jListener)
21
21
 
22
- @object.should_receive(:teardown).with(@build, @listener, env)
22
+ @object.should_receive(:teardown).with(@build, @listener)
23
23
  environment.tearDown(@jBuild, @jListener)
24
24
  end
25
25
 
@@ -50,7 +50,7 @@ describe Jenkins::Plugin::Proxies do
50
50
  describe "when a wrapper has already existed" do
51
51
  before do
52
52
  @proxy = Object.new
53
- @proxies.link @object, @proxy
53
+ @proxies.linkin @object, @proxy
54
54
  end
55
55
 
56
56
  it "finds the same proxy on export" do
@@ -138,7 +138,7 @@ describe Jenkins::Plugin::Proxies do
138
138
  @umappable = java.lang.Object.new
139
139
  @import = @proxies.import(@umappable)
140
140
  end
141
- it "maps it do an opaque native java object structure" do
141
+ it "maps it to an opaque native java object structure" do
142
142
  @import.native.should be @umappable
143
143
  end
144
144
  it "reuses the same opaque proxy on subsequent imports" do
@@ -34,7 +34,7 @@ describe "a class with #{Jenkins::Plugin::Proxy} mixed in" do
34
34
  end
35
35
 
36
36
  it "reconstructs the @plugin field" do
37
- @plugin.should_receive(:link).with(@impl, @proxy)
37
+ @plugin.should_receive(:linkout).with(@impl, @proxy)
38
38
  @proxy.read_completed
39
39
  @proxy.plugin.should be(@plugin)
40
40
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jenkins-plugin-runtime
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.10
5
+ version: 0.1.11
6
6
  platform: ruby
7
7
  authors:
8
8
  - Charles Lowell
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-26 00:00:00 -05:00
13
+ date: 2011-10-31 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency