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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/salesforcedeploytool/app.rb', line 7

def initialize config
  
  @debug = config[:debug]
  @test = config[:test]

  @build_number = 'N/A'
  ( @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"

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

  @version_file = File.join(@git_dir,config[:version_file])
  @username = @sandbox == 'prod' ? config[:username] : config[:username] + '.' + @sandbox 
  @server_url = @sandbox == 'prod' ? 'https://login.salesforce.com' : 'https://test.salesforce.com'

  self.clone if ! Dir.exists? File.join(@git_dir,'.git')

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

Instance Method Details

#clean_git_dir(message = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/salesforcedeploytool/app.rb', line 70

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



47
48
49
50
51
52
# File 'lib/salesforcedeploytool/app.rb', line 47

def clean_version

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

end

#cloneObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/salesforcedeploytool/app.rb', line 54

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

#commit_hashObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/salesforcedeploytool/app.rb', line 32

def commit_hash

  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.last.sha)
    file.seek(0,IO::SEEK_SET)
    file.truncate 0
    file.write content
  end if File.exists? @version_file

end

#pull(message = nil) ⇒ Object



80
81
82
83
84
85
86
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
# File 'lib/salesforcedeploytool/app.rb', line 80

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

  exit_code = myexec full_cmd, exec_options

  clean_version

  exit exit_code if exit_code != 0

end

#pushObject



115
116
117
118
119
120
121
122
123
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
164
# File 'lib/salesforcedeploytool/app.rb', line 115

def push

  # Working dir
  Dir.chdir @git_dir

  # Add the commit hash to the version file
  commit_hash

  # 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

  # Clean changes on version file
  clean_version

  # exit with exit_code
  exit exit_code if exit_code != 0

end