Class: Jenkins::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins/plugin.rb,
lib/jenkins/plugin/proxy.rb,
lib/jenkins/plugin/proxies.rb,
lib/jenkins/plugin/specification.rb,
lib/jenkins/plugin/proxies/action.rb,
lib/jenkins/plugin/proxies/builder.rb,
lib/jenkins/plugin/runtime/version.rb,
lib/jenkins/plugin/proxies/publisher.rb,
lib/jenkins/plugin/proxies/build_step.rb,
lib/jenkins/plugin/proxies/root_action.rb,
lib/jenkins/plugin/proxies/build_wrapper.rb

Overview

Acts as the primary gateway between Ruby and Jenkins There is one instance of this object for the entire plugin

On the Java side, it contains a reference to an instance of RubyPlugin. These two objects talk to each other to get things done.

Each running ruby plugin has exactly one instance of ‘Jenkins::Plugin`

Defined Under Namespace

Modules: Proxy, Runtime Classes: OpaqueJavaObject, Proxies, Specification

Constant Summary collapse

ExportError =
Class.new(StandardError)
ImportError =
Class.new(StandardError)
SpecificationError =
Class.new(StandardError)
SpecificationNotFound =
Class.new(SpecificationError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(java) ⇒ Plugin

Initializes this plugin by reading the models.rb file. This is a manual registration process Where ruby objects register themselves with the plugin In the future, this process will be automatic, but I haven’t decided the best way to do this yet.

Parameters:

  • java (org.jenkinsci.ruby.RubyPlugin)

    a native java RubyPlugin



38
39
40
41
42
43
# File 'lib/jenkins/plugin.rb', line 38

def initialize(java)
  @java = @peer = java
  @start = @stop = proc {}
  @descriptors = {}
  @proxies = Proxies.new(self)
end

Instance Attribute Details

#descriptorsObject (readonly)

A list of all the hudson.model.Descriptor objects of which this plugin is aware *indexed by Wrapper class*

This is used so that wrappers can always have a single place to go when they are asked for a descriptor. That way, wrapper instances can always return the descriptor associated with their class.

This may go away.



26
27
28
# File 'lib/jenkins/plugin.rb', line 26

def descriptors
  @descriptors
end

#peerObject (readonly)

the instance of jenkins.ruby.RubyPlugin with which this Plugin is associated



29
30
31
# File 'lib/jenkins/plugin.rb', line 29

def peer
  @peer
end

Class Method Details

.initialize(java) ⇒ Jenkins::Plugin

Initialize the singleton instance that will run for a ruby plugin. This method is designed to be called by the Java side when setting up the ruby plugin

Returns:



49
50
51
52
53
54
# File 'lib/jenkins/plugin.rb', line 49

def self.initialize(java)
  #TODO: check for double initialization?!?
  @instance = new(java)
  @instance.load_models
  return @instance
end

.instanceJenkins::Plugin

Get the singleton instance associated with this plugin

This is useful when code in the plugin needs to get a reference to the plugin in which it is running e.g.

Jenkins::Plugin.instance #=> the running plugin

Returns:



64
65
66
# File 'lib/jenkins/plugin.rb', line 64

def self.instance
  @instance
end

Instance Method Details

#export(object) ⇒ Object

Reflect a native Ruby object into its External Java form.

Delegates to ‘Proxies` for the heavy lifting.

Parameters:

  • object (Object)

    the object



145
146
147
# File 'lib/jenkins/plugin.rb', line 145

def export(object)
  @proxies.export object
end

#import(object) ⇒ Object

Reflect an Java object coming from Jenkins into the context of this plugin If the object is originally from the ruby plugin, and it was previously exported, then it will unwrap it. Otherwise, it will just use the object as a normal Java object.

Parameters:

  • object (Object)

    the object to bring in from the outside

Returns:

  • the best representation of that object for this plugin



135
136
137
# File 'lib/jenkins/plugin.rb', line 135

def import(object)
  @proxies.import object
end

#linkout(internal, external) ⇒ Object

Link a plugin-local Ruby object to an external Java object.

see ‘Proxies#link`

Parameters:

  • internal (Object)

    the object on the Ruby side of the link

  • external (java.lang.Object)

    the object on the Java side of the link



155
156
157
# File 'lib/jenkins/plugin.rb', line 155

def linkout(internal, external)
  @proxies.linkout internal, external
end

#load_modelsObject



159
160
161
162
163
164
# File 'lib/jenkins/plugin.rb', line 159

def load_models
  path = @java.getModelsPath().getPath()
  # TODO: can we access to Jenkins console logger?
  puts "Trying to load models from #{path}"
  load_file_in_dir(path)
end

#nameObject

unique identifier for this plugin in the Jenkins server



110
111
112
# File 'lib/jenkins/plugin.rb', line 110

def name
  @peer.getWrapper().getShortName()
end

#register_describable(ruby_class, java_class) ⇒ Object

Register a ruby class as a Jenkins extension point of a particular java type

This method is invoked automatically as part of the auto-registration process, and should not need to be invoked by plugin code.

Parameters:

  • ruby_class (Class)

    the class implementing the extension point

  • java_class (java.lang.Class)

    that Jenkins will see this extention point as



103
104
105
106
107
# File 'lib/jenkins/plugin.rb', line 103

def register_describable(ruby_class, java_class)
  descriptor = Jenkins::Model::Descriptor.new(ruby_class, self, java_class)
  @descriptors[ruby_class] = descriptor
  @peer.addExtension(descriptor)
end

#register_extension(class_or_instance, *args) ⇒ Object

Registers a singleton extension point directly with Jenkins. Extensions registered via this method are different than those registered via ‘register_describable` in that there are only one instance of them, and so things like configuration construction, and validation do not apply.

This method accepts either an instance of the extension point or a class implementing the extension point. If a class is provided, it will attempt to construct an instance with the arguments provided. e.g.

# construct an instance
plugin.register_extension SomeRootAction, "gears.png"
# pass in a preconfigured instance
ext = MyGreatExtension.build do |c|
  c.name "fantastic"
  c.fizzle :foo
end
plugin.register_extension ext

Parameters:

  • extension (Class|Object)

    the extension to register

  • arguments (...)

    to pass to



90
91
92
93
# File 'lib/jenkins/plugin.rb', line 90

def register_extension(class_or_instance, *args)
  extension = class_or_instance.is_a?(Class) ? class_or_instance.new : class_or_instance
  @peer.addExtension(export(extension))
end

#startObject

Called once when Jenkins first initializes this plugin currently does nothing, but plugin startup hooks would go here.



117
118
119
# File 'lib/jenkins/plugin.rb', line 117

def start
  @start.call()
end

#stopObject

Called one by Jenkins (via RubyPlugin) when this plugin is shut down. Currently this does nothing, but plugin shutdown hooks would go here.



124
125
126
# File 'lib/jenkins/plugin.rb', line 124

def stop
  @stop.call()
end