lib/salesforcedeploytool/app.rb in salesforce-deploy-tool-0.7.3 vs lib/salesforcedeploytool/app.rb in salesforce-deploy-tool-0.8.0

- old
+ new

@@ -1,26 +1,58 @@ module SalesforceDeployTool class App - def initialize config + attr_accessor :build_number - @git_repo = config[:git_repo] - @git_dir = config[:git_dir] - @sandbox = config[:sandbox] - @username = @sandbox == 'prod' ? config[:username] : config[:username] + '.' + @sandbox - @password = config[:password] + def initialize config + @debug = config[:debug] @test = config[:test] - @deploy_ignore_files = config[:deploy_ignore_files] + @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 + 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 + + def clean_version + + g = Git.open(@git_dir) + g.checkout @version_file + + end + def clone if Dir.exists? File.join(@git_dir,'.git') return end @@ -72,16 +104,24 @@ exec_options[:failmsg] = nil end exit_code = myexec full_cmd, exec_options + clean_version + exit exit_code if exit_code != 0 end 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 @@ -105,18 +145,21 @@ exec_options[:message] += "\n\n" if @debug cmd = @test ? " ant deployAndTestCode" : " ant deployCode" full_cmd = env_vars + cmd - Dir.chdir @git_dir - # 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 end