class BigcommerceTool::Deployer

Public Class Methods

new(options) click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 3
def initialize(options)
  raise 'Missing config file' unless File.exists?('bigcommerce.yml') 
  @branch = options[:branch] || ENV['CIRCLE_BRANCH']
  @config = YAML.load_file(options[:config_path] || 'bigcommerce.yml')
end

Public Instance Methods

deploy() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 9
def deploy
  return unless env_name

  Net::DAV.start(URI("https://#{url}/dav/")) do |dav|
    dav.credentials(ENV['USERNAME'], password)
    puts "push to: #{url}"
    changed_files.each do |file|
      begin
        if File.exists?(file)
          if file.include?(' ')
            puts "skipping: #{file}, file has a space in the name."
            next
          else
            create_path_if_missing(dav, file)
            dav.put_string(file, File.open(file, 'r').read)
            puts "add/update #{file}"
          end
        else
          dav.delete(file) if dav.exists?(file)
          puts "remove: #{file}"
        end
      rescue Exception => e
        puts "file: #{file}"
        puts e.message
        puts e.backtrace
        raise e
      end
    end
  end
end

Private Instance Methods

changed_files() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 74
def changed_files
  all_files = Dir.glob('template/**/*.*')
  return all_files if !ENV['CIRCLE_COMPARE_URL'] || ENV['CIRCLE_COMPARE_URL'].empty?
  puts ENV['CIRCLE_COMPARE_URL']

  range = ENV['CIRCLE_COMPARE_URL'].split('/').last

  `git diff --name-only #{range}`.split("\n").reject{|file| file !~ /^template\//}
end
create_directory(conn, path, arr) click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 60
def create_directory(conn, path, arr)
  @current_folders ||= {}
  create_path = (path.empty?) ? arr.shift : "#{path}/#{arr.shift}"
  unless @current_folders.has_key?(create_path)
    conn.mkdir(create_path) unless conn.exists?(create_path)
    @current_folders[create_path] = 1
  end
  create_directory(conn, create_path, arr) if arr.length > 0
end
create_path_if_missing(conn, file) click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 70
def create_path_if_missing(conn, file)
  create_directory(conn, '', File.dirname(file).split(/\//))
end
env_name() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 46
def env_name
  environments_mapping[@branch].or_if_nil? do
    puts "Skipping deploy, no mapping set up for branch: #{@branch}"
  end
end
environments_mapping() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 42
def environments_mapping
  @config['deploy']['environments_mapping']
end
password() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 56
def password
  ENV["#{env_name}_PASSWORD"]
end
url() click to toggle source
# File lib/bigcommerce_tool/deployer.rb, line 52
def url
  ENV["#{env_name}_URL"]
end