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
 
  # Parameter Normalization
  config[:git_dir] = File.expand_path config[:git_dir]
  config[:tmp_dir] = File.expand_path config[:tmp_dir]
  config[:version_file] = File.expand_path config[:version_file]
  config[:deploy_ignore_files] = config[:deploy_ignore_files].map {|f| File.expand_path File.join(config[:git_dir],f)}

  # Defaults
  @debug = config[:debug]
  @test = config[:test]
  @build_number = 'N/A'

  # 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`"
  ( @deploy_ignore_files = config[:deploy_ignore_files] ).nil? and raise "Invalid Config: deploy_ignore_files not found"
  ( @build_number_pattern = config[:build_number_pattern] ).nil? and raise "Invalid Config: build_number_pattern not found"
  ( @commit_hash_pattern = config[:commit_hash_pattern] ).nil? and raise "Invalid Config: commit_hash_pattern not found"
  ( @version_file = config[:version_file] ).nil? and raise "Invalid Config: version_file not found"

  

  config[:username].nil? and raise "Invalid Config: username not found, please run `sf config`"
  config[:password].nil? and raise "Invalid Config: password not found, please run `sf config`"

  @username = @sandbox == 'prod' ? config[:username] : config[:username] + '.' + @sandbox 
  @server_url = @sandbox == 'prod' ? 'https://login.salesforce.com' : 'https://test.salesforce.com'

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_dir(message = nil) ⇒ Object



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

def clean_git_dir message = nil

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

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 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

#pull(message = nil) ⇒ Object



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
123
124
125
126
# File 'lib/salesforcedeploytool/app.rb', line 89

def pull message = nil

  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 @git_dir

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

  if @debug
    exec_options[:message] += "\n\n"
    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



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
164
165
166
167
168
169
170
171
# File 'lib/salesforcedeploytool/app.rb', line 128

def push

  # Working dir
  Dir.chdir @git_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

  # Deploy code
  exec_options[:message] = @test ?  "INFO: Deploying and Testing code to #{@sandbox}:  " : "INFO: Deploying code to #{@sandbox}:  "
  exec_options[:message]  +=  "\n\n" if @debug

  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)
    content.gsub!(/#{@commit_hash_pattern}/,g.log.first.sha)
    file.seek(0,IO::SEEK_SET)
    file.truncate 0
    file.write content
  end if File.exists? @version_file

end