bin/sf in salesforce-deploy-tool-1.3.0 vs bin/sf in salesforce-deploy-tool-2.0.0

- old
+ new

@@ -1,9 +1,7 @@ #!/usr/bin/env ruby -require 'pp' -require 'yaml' -require 'fileutils' +require 'salesforcedeploytool' # Disable stdout buffer STDOUT.sync = true # Static files @@ -27,246 +25,46 @@ end end # Read sandbox environment begin - sandbox = File.open(File.expand_path(SANDBOX_CONFIG_FILE)).read + config[:sandbox] = File.open(File.expand_path(SANDBOX_CONFIG_FILE)).read rescue - sandbox = nil + config[:sandbox] = nil end -# Grab variables from env if not in the config files -config[:git_dir] = ENV["SFDT_GIT_DIR"] || config[:git_dir] -config[:tmp_dir] = ENV["SFDT_TMP_DIR"] || config[:tmp_dir] -config[:version_file] = ENV["SFDT_VERSION_FILE"] || config[:version_file] -config[:build_number_pattern] = ENV["SFDT_BUILD_NUMBER_PATTERN"] || config[:build_number_pattern] -config[:commit_hash_pattern] = ENV["SFDT_COMMIT_HASH_PATTERN"] || config[:commit_hash_pattern] +# Configuration variables firrst from ENV , if not config file + +# Git configs: config[:git_repo] = ENV["SFDT_GIT_REPO"] || config[:git_repo] +config[:git_dir] = ENV["SFDT_GIT_DIR"] || config[:git_dir] +config[:src_dir] = ENV["SFDT_SRC_DIR"] || config[:src_dir] + +# Salesforce credential configs config[:username] = ENV["SFDT_USERNAME"] || config[:username] config[:password] = ENV["SFDT_PASSWORD"] || config[:password] config[:salesforce_url] = ENV["SFDT_SALESFORCE_URL"] || config[:salesforce_url] + +# Project configs +config[:version_file] = ENV["SFDT_VERSION_FILE"] || config[:version_file] +config[:build_number_pattern] = ENV["SFDT_BUILD_NUMBER_PATTERN"] || config[:build_number_pattern] +config[:commit_hash_pattern] = ENV["SFDT_COMMIT_HASH_PATTERN"] || config[:commit_hash_pattern] config[:deploy_ignore_files] = ENV["SFDT_DEPLOY_IGNORE_FILES"].nil? ? config[:deploy_ignore_files] : ENV["SFDT_DEPLOY_IGNORE_FILES"].split(',') -# Default values -config[:buildxml_dir] = ENV["SFDT_BUILDXML_DIR"] || config[:buildxml_dir] || '' - # Minimal config validation +abort "Config error: src_dir not found in #{GLOBAL_CONFIG_FILE} or through SFDT_SRC_DIR" if config[:src_dir].nil? abort "Config error: git_dir not found in #{GLOBAL_CONFIG_FILE} or through SFDT_GIT_DIR" if config[:git_dir].nil? -abort "Config error: tmp_dir not found in #{GLOBAL_CONFIG_FILE} or through SFDT_TMP_DIR" if config[:tmp_dir].nil? +abort "Config error: git_repo not found in #{GLOBAL_CONFIG_FILE} or through SFDT_GIT_DIR" if config[:git_repo].nil? abort "Config error: username not found in #{GLOBAL_CONFIG_FILE} or through SFDT_USERNAME" if config[:username].nil? && ARGV[0] != 'config' abort "Config error: password not found in #{GLOBAL_CONFIG_FILE} or through SFDT_PASSWORD" if config[:password].nil? && ARGV[0] != 'config' -# If the repository is not cloned then fail: -if !File.exists?(config[:git_dir]) && ['pull','push'].include?(ARGV[0]) - abort "ERROR: The environment is not properly configured, please run sf config to clone the repo and setup the credentials" -end +# Create a temporary directory +config[:tmp_dir] = Dir.mktmpdir 'sfdt-' -# Normalize file paths: -config[:git_dir] = File.expand_path config[:git_dir] -config[:tmp_dir] = File.expand_path config[:tmp_dir] - -require 'salesforcedeploytool' - -program :version, SalesforceDeployTool::VERSION -program :description, 'A cli tool to help manage and deploy salesforce sandboxes with git' - -command :pull do |c| - c.syntax = 'sf pull' - c.summary = 'Pull code from the sandbox' - c.description = "Pull code from sandbox and update #{config[:git_dir]}" - c.example 'usage:', 'sf pull' - c.option "--append", "Pull code appending it to the local repository" - c.option "--debug", "Verbose output" - c.option "--sandbox NAME", "-s NAME", "use 'prod' to deploy production or sandbox name" - c.action do |args, options| - - # short flag mapping - options.sandbox = options.s if options.s - - # Parameter validation: - if options.sandbox.nil? and sandbox.nil? - puts "error: please specify sandbox using --sandbox or sf sandbox" - exit 1 - end - config[:sandbox] = options.sandbox || sandbox - config[:debug] = options.debug.nil? ? false : true - - # The salesforce URL - config[:salesforce_url] = - ENV["SFDT_SALESFORCE_URL"] || # First from environment variables - config[:salesforce_url] || # Second from the configuration files - ( config[:sandbox] == 'prod' ? 'https://login.salesforce.com' : 'https://test.salesforce.com' ) # If not defined anywhere, use defaults - - # Initialize - sfdt = SalesforceDeployTool::App.new config - - # Clean all files from repo - sfdt.clean_git_dir unless options.append - - # Pull the changes - print "INFO: Pulling changes from #{config[:sandbox]} using url #{config[:salesforce_url]} " - print "\n\n" if options.debug - sfdt.pull - sfdt.clean_version - - end - +begin + SalesforceDeployTool::CLI.new.run config +rescue => e + puts "ERROR: #{e}" + exit 1 +ensure + FileUtils.rm_rf config[:tmp_dir] if Dir.exists? config[:tmp_dir] end - -command :push do |c| - c.syntax = 'sf push [options]' - c.summary = 'Push code into a sandbox' - c.description = '' - c.example 'description', "Push the code that is located into #{config[:git_dir]} into the active sandbox" - c.option "--sandbox NAME", "-s NAME", "use 'prod' to deploy production or sandbox name" - c.option "--debug", "Verbose output" - c.option "--test", "-T", "Deploy and test" - c.option "--exclude LIST", "-x LIST", "a CSV list of metadata to exclude when creating destructiveChange.xml" - c.option "--append", "Disable destructive change and do an append deploy" - c.option "--build_number NUMBER","Record build number on version file" - c.action do |args, options| - - # short flag mapping - options.test = true if options.T - options.exclude = options.x if options.x - options.sandbox = options.s if options.s - - # Parameter validation: - if options.sandbox.nil? and sandbox.nil? - puts "error: please specify the sandbox to pull from using --sandbox" - exit 1 - end - config[:sandbox] = options.sandbox || sandbox - config[:test] = options.test.nil? ? false : true - config[:debug] = options.debug.nil? ? false : true - - # The salesforce URL - config[:salesforce_url] = - ENV["SFDT_SALESFORCE_URL"] || # First from environment variables - config[:salesforce_url] || # Second from the configuration files - ( config[:sandbox] == 'prod' ? 'https://login.salesforce.com' : 'https://test.salesforce.com' ) # If not defined anywhere, use defaults - - # Initialize - sfdt = SalesforceDeployTool::App.new config.clone - - # Remove destructive change if there is one - DESTRUCTIVE_CHANGE_FILE = File.join(config[:git_dir],config[:buildxml_dir],'src','destructiveChanges.xml') - FileUtils.rm DESTRUCTIVE_CHANGE_FILE if File.exists? DESTRUCTIVE_CHANGE_FILE - - if ! options.append - # Pull changes from sandbox to temporary directory: - config_tmp = config.clone - config_tmp[:git_dir] = config_tmp[:tmp_dir] - FileUtils.rm_rf config_tmp[:git_dir] if File.exists? config_tmp[:git_dir] - FileUtils.cp_r config[:git_dir],config_tmp[:git_dir] - sfdt_tmp = SalesforceDeployTool::App.new config_tmp - print "INFO: Pulling changes from #{config[:sandbox]} using url #{config[:salesforce_url]} to temporary directory #{config[:tmp_dir]} to generate destructiveChanges.xml " - sfdt_tmp.pull - - # Create destructiveChanges.xml - puts "INFO: Creating destructive changes xml" - dc_gen = Dcgen::App.new - dc_gen.master = File.join(config[:git_dir],config[:buildxml_dir],'src') - dc_gen.destination = File.join(config[:tmp_dir],config[:buildxml_dir],'src') - dc_gen.output = DESTRUCTIVE_CHANGE_FILE - dc_gen.exclude = options.exclude.split(',') unless options.exclude.nil? - dc_gen.verbose = false if not options.debug - dc_gen.generate_destructive_changes - - end - - # Push code to sandbox - begin - # Set version - sfdt.build_number = options.build_number if not options.build_number.nil? - sfdt.set_version - - # Enable test if option enabled - sfdt.test = options.test.nil? ? false : true - - # Push - print(options.test.nil? ? "INFO: Deploying code to #{config[:sandbox]}: ": "INFO: Deploying and Testing code to #{config[:sandbox]}: \n\n") - sfdt.push - ensure - sfdt.clean_version - end - - end -end - -command :sandbox do |c| - c.syntax = 'sf sandbox SANDBOX_NAME' - c.summary = 'Set sandbox to work on, this can be overriden by --sandbox ' - c.description = 'Set the sandbox to work with pull and push. If no parameter defined, it will print the current sandbox selected.' - c.action do |args, options| - - if args.size > 1 - puts "error: Wrong number of arguments" - end - - if args.size == 0 - if ! sandbox.nil? - puts "sandbox: " + sandbox - exit 0 - else - puts "WARN: Sandbox has not been set yet" - exit 1 - end - end - File.open(File.expand_path(SANDBOX_CONFIG_FILE),'w').write args.first - - end -end - -command :config do |c| - c.syntax = 'sf config' - c.summary = 'Go through the configuration wizard to config your environment' - c.action do |args, options| - - if args.size != 0 - puts "error: Wrong number of arguments" - end - - config_new = {} - - config_new[:username] = ask "Please enter your SalesForce production login user name" do |q| - q.validate = /^\S+@\S+$/ - end.to_s - - config_new[:password] = ask "Please enter your Salesforce production password" do |q| - q.echo = "x" - end.to_s - - git_name = ask "Please enter your Full name to be used as commit owner on GIT" do |q| - q.validate = /^[a-zA-Z\s]+$/ - end - - git_email = ask "Please enter your email to be used as commit email on GIT" do |q| - q.validate = /^\S+@\S+$/ - end - - sandbox = ask "Please enter your sandbox so to be used as default when push and pull" do |q| - q.validate = /^[a-zA-Z]+$/ - end - - %x[git config --global user.email "#{git_email}"] - %x[git config --global user.name "#{git_name}"] - - config[:username] = config_new[:username] - config[:password] = config_new[:password] - config[:sandbox] = sandbox - - File.open(File.expand_path(SANDBOX_CONFIG_FILE),'w').write sandbox - File.open(File.expand_path(CONFIG_FILE),'w').write config_new.to_yaml - - # Initialize - sfdt = SalesforceDeployTool::App.new config - - # Clone - sfdt.clone - - end -end - -default_command :help - -