Class: JenkinsJob::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins_job/project.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Project

Returns a new instance of Project.



6
7
8
9
10
11
12
13
14
15
# File 'lib/jenkins_job/project.rb', line 6

def initialize dir
  raise "#{dir} does not exists" unless File.exists? dir
  raise "#{dir} is not a directory" unless File.directory?(dir)
  
  @config_file = File.join(dir, 'config.xml')
  if !File.exists?(@config_file)
    raise "#{dir} doesn't contain config.xml file"
  end
  @dir = dir
end

Instance Method Details

#cleanObject

removes all the useless files generated by hudson, keeps only config.xml



18
19
20
21
22
23
24
# File 'lib/jenkins_job/project.rb', line 18

def clean
  Dir.chdir(@dir) do
    ['builds', 'last*', '*.log', 'next*'].each do |pattern|
      FileUtils.rm_rf Dir.glob(pattern)
    end
  end
end

#disableObject

disable the project



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jenkins_job/project.rb', line 39

def disable
  doc = REXML::Document.new(File.open(@config_file, 'r'))
  disabled_node = doc.elements['project/disabled']
  if disabled_node.nil?
    disabled_node = REXML::Element.new 'disabled'
    disabled_node.text = 'true'
    project_node = doc.elements['project']
    project_node << disabled_node
  else
    if !disabled_node.text.downcase == 'true'
      return
    else
      disabled_node.text = 'true'
    end
  end
  File.open(@config_file, File::WRONLY|File::TRUNC) do |file|
    doc.write file
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/jenkins_job/project.rb', line 30

def disabled?
  doc = REXML::Document.new(File.open(@config_file, 'r'))
  disabled_node = doc.elements['project/disabled']
  return false if disabled_node.nil?
  return true if disabled_node.text.downcase == 'true'
  false
end

#enableObject

enable the project



60
61
62
63
64
65
66
67
68
69
# File 'lib/jenkins_job/project.rb', line 60

def enable
  doc = REXML::Document.new(File.open(@config_file, 'r'))
  disabled_node = doc.elements['project/disabled']
  if !disabled_node.nil?
    disabled_node.text = "false"
    File.open(@config_file, File::WRONLY|File::TRUNC) do |file|
      doc.write file
    end
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/jenkins_job/project.rb', line 26

def enabled?
  !disabled?
end

#setting(name) ⇒ Object

Returns a REXML::Element identified by project/name



72
73
74
75
# File 'lib/jenkins_job/project.rb', line 72

def setting name
  doc = REXML::Document.new(File.open(@config_file, 'r'))
  doc.elements["project/#{name}"]
end

#update_setting(name, node) ⇒ Object

Update setting name: the name of the setting node: the REXML::Element to insert



80
81
82
83
84
85
86
87
88
89
# File 'lib/jenkins_job/project.rb', line 80

def update_setting name, node
  doc = REXML::Document.new(File.open(@config_file, 'r'))
  old = doc.elements["project/#{name}"]
  doc.root.elements.delete(old) unless old.nil?
  project_node = doc.elements['project']
  project_node << node
  File.open(@config_file, File::WRONLY|File::TRUNC) do |file|
    doc.write file
  end
end