Class: SBCI::Job

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

Constant Summary collapse

ATTRIBUTES =
[
  :host, :port, :branch_name, :job_name, :path, :force
].freeze
DEFAULTS =
{
  :host => "localhost",
  :port => 8080,
  :path => "",
  :job_name => self.repo_name,
  :force => false
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Job

Returns a new instance of Job.



53
54
55
56
57
58
59
60
61
62
# File 'lib/sbci.rb', line 53

def initialize(args = nil)
  args.delete_if{ |k,v| v.nil? || v == ""} if args.is_a?(Hash)
  args = args ? DEFAULTS.merge(args) : DEFAULTS

  ATTRIBUTES.each do |attr|
    if (args.key?(attr))
      instance_variable_set("@#{attr}", args[attr])
    end
  end
end

Class Method Details

.branch_nameObject



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

def self.branch_name
  Grit::Head.current(get_repo).name
end

.get_repoObject



33
34
35
# File 'lib/sbci.rb', line 33

def self.get_repo
  Grit::Repo.new(".")
end

.repo_nameObject



24
25
26
27
# File 'lib/sbci.rb', line 24

def self.repo_name
  conf = Grit::Config.new(get_repo)
  conf["remote.origin.url"].gsub(/\.git$/,"").split("/").last
end

Instance Method Details

#config_exists?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/sbci.rb', line 118

def config_exists?
  File.exists?(config_file_path)
end

#config_file_dirObject



112
113
114
115
116
# File 'lib/sbci.rb', line 112

def config_file_dir
  file_dir = File.join(SBCI.pwd, self.path)
  FileUtils.mkdir_p(file_dir)
  file_dir
end

#config_file_pathObject



108
109
110
# File 'lib/sbci.rb', line 108

def config_file_path
  File.join(config_file_dir, "#{self.job_name}.xml")
end

#createObject



64
65
66
67
68
# File 'lib/sbci.rb', line 64

def create
  prompt(SBCI::MESSAGES[:config_already_exists], config_exists?) do
    FileUtils.cp(SBCI::INITIAL_CONFIG_PATH, config_file_path)
  end
end

#deleteObject



100
101
102
103
104
105
106
# File 'lib/sbci.rb', line 100

def delete
  begin
    RestClient.post(URI.escape("#{host_with_port}/job/#{self.job_name}/doDelete"), {})
  rescue RestClient::Found
    true
  end
end

#delete_configObject



126
127
128
# File 'lib/sbci.rb', line 126

def delete_config
  FileUtils.rm(config_file_path) if File.exists?(config_file_path)
end

#dumpObject



92
93
94
95
96
97
98
# File 'lib/sbci.rb', line 92

def dump
  prompt(SBCI::MESSAGES[:config_already_exists], config_exists?) do
    response = RestClient.get(URI.escape("#{host_with_port}/job/#{self.job_name}/config.xml"))
    File.open(config_file_path, "w") {|f| f.write(response.body) }
    response
  end
end

#host_with_portObject



122
123
124
# File 'lib/sbci.rb', line 122

def host_with_port
  "#{host}:#{port}"
end

#publishObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sbci.rb', line 70

def publish
  already_published = published?
  url = if already_published
    "#{host_with_port}/job/#{job_name}/config.xml"
  else
    "#{host_with_port}/createItem?name=#{job_name}"
  end
  prompt(SBCI::MESSAGES[:config_already_published], already_published) do
    RestClient.post(URI.escape(url), File.read(config_file_path), :content_type => :xml)
  end
end

#published?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/sbci.rb', line 82

def published?
  begin
    RestClient.get(URI.escape("#{host_with_port}/job/#{self.job_name}/config.xml"))
  rescue
    false
  else
    true
  end
end