Class: SalesforceDeployTool::App

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ App

Returns a new instance of App.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/salesforcedeploytool/app.rb', line 8

def initialize config
 
  # Config file validation:
  ( @git_repo = config[:git_repo] ).nil? and raise "Invalid Config: git_repo not found"
  ( @git_dir = config[:git_dir] ).nil? and raise "Invalid Config: git_dir not found"
  ( @sandbox = config[:sandbox] ).nil? and raise "Invalid Config: sandbox not found"
  ( @password = config[:password] ).nil? and raise "Invalid Config: password not found, please run `sf config`"
  ( @username = config[:username] ).nil? and raise "Invalid Config: username not found, please run `sf config`"
  ( @password = config[:password] ).nil? and raise "Invalid Config: password not found, please run `sf config`"

  # Parameter Normalization
  @git_dir = File.expand_path config[:git_dir]
  @tmp_dir = File.expand_path config[:tmp_dir]
  @version_file = File.join(@git_dir,config[:version_file]) if !config[:version_file].nil?
  @deploy_ignore_files = config[:deploy_ignore_files].map {|f| File.expand_path File.join(config[:git_dir],f)} if ! config[:deploy_ignore_files].nil?
  @build_number_pattern = config[:build_number_pattern]
  @commit_hash_pattern = config[:commit_hash_pattern]
  @buildxml_dir = config[:buildxml_dir]
  @username = @sandbox == 'prod' ? @username : @username + '.' + @sandbox 
  @server_url = config[:salesforce_url]

  # Defaults
  @debug ||= config[:debug]
  @test ||= config[:test]
  @build_number ||= 'N/A'
  @version_file ||= false
  @buildxml_dir ||= ''
  @build_number_pattern ||= false
  @commit_hash_pattern ||= false
  @deploy_ignore_files ||= []

end

Instance Attribute Details

#build_numberObject

Returns the value of attribute build_number.



5
6
7
# File 'lib/salesforcedeploytool/app.rb', line 5

def build_number
  @build_number
end

#testObject

Returns the value of attribute test.



6
7
8
# File 'lib/salesforcedeploytool/app.rb', line 6

def test
  @test
end

Instance Method Details

#clean_git_dirObject



79
80
81
82
83
84
85
# File 'lib/salesforcedeploytool/app.rb', line 79

def clean_git_dir

  Dir[File.join(@git_dir,'src','*')].each do |dir|
    FileUtils.rm_rf dir unless dir =~ /.*package.xml$/
  end

end

#clean_versionObject



56
57
58
59
60
61
# File 'lib/salesforcedeploytool/app.rb', line 56

def clean_version

  g = Git.open(@git_dir)
  g.checkout @version_file if @version_file && File.exists?(@version_file)

end

#cloneObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/salesforcedeploytool/app.rb', line 63

def clone

  if Dir.exists? File.join(@git_dir,'.git')
    return
  end

  begin
    Git.clone(@git_repo, File.basename(@git_dir), :path => File.dirname(@git_dir))
  rescue => e
    STDERR.puts "ERROR: A problem occured when cloning #{@git_repo}, error was\n\n"
    puts e
    exit 1
  end

end

#pullObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/salesforcedeploytool/app.rb', line 87

def pull

  env_vars = ""
  env_vars += " SF_USERNAME=" + @username
  env_vars += " SF_PASSWORD=" + @password
  env_vars += " SF_SERVERURL=" + @server_url
  cmd = " ant retrieveCode"

  full_cmd = env_vars + cmd

  Dir.chdir File.join(@git_dir,@buildxml_dir)

  exec_options = {
    :stderr => @debug,
    :stdout => @debug,
    :spinner => ! @debug,
    :okmsg => "OK",
    :failmsg => "FAILED",
  }

  if @debug
    exec_options[:okmsg] = nil
    exec_options[:failmsg] = nil
  end

  # Pull the code
  exit_code = myexec full_cmd, exec_options

  # Delete files to be ignored:
  @deploy_ignore_files.each do |file|
    FileUtils.rm file if File.exists? file
  end

  exit exit_code if exit_code != 0

end

#pushObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/salesforcedeploytool/app.rb', line 124

def push

  # Working dir
  Dir.chdir File.join(@git_dir,@buildxml_dir)

  # Set env variables to run ant
  env_vars = ""
  env_vars += " SF_USERNAME=" + @username
  env_vars += " SF_PASSWORD=" + @password
  env_vars += " SF_SERVERURL=" + @server_url

  # myexec options
  exec_options = {
    :stderr   =>  @debug,
    :stdout   =>  @debug,
    :spinner  =>  ! @debug,
    :okmsg    =>  "OK",
    :failmsg  =>  "FAILED",
  }

  if @debug
    exec_options[:okmsg]    =   nil
    exec_options[:failmsg]  =   nil
  end

  cmd = @test ? " ant deployAndTestCode" : " ant deployCode"
  full_cmd = env_vars + cmd

  # Delete files to be ignored:
  @deploy_ignore_files.each do |file|
    FileUtils.rm file if File.exists? file
  end

  # Push the code
  exit_code = myexec full_cmd, exec_options

  # exit with exit_code
  exit exit_code if exit_code != 0

end

#set_versionObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/salesforcedeploytool/app.rb', line 41

def set_version

  g = Git.open(@git_dir)

  File.open(@version_file,'r+') do |file|
    content = file.read
    content.gsub!(/#{@build_number_pattern}/,@build_number) if @build_number_pattern
    content.gsub!(/#{@commit_hash_pattern}/,g.log.first.sha) if @commit_hash_pattern
    file.seek(0,IO::SEEK_SET)
    file.truncate 0
    file.write content
  end if @version_file && File.exists?(@version_file)

end