class StaticExe

Public Instance Methods

s3_deploy(directory = '.') click to toggle source
# File lib/staticmatic/deployers/aws-s3.rb, line 9
def s3_deploy(directory = '.')
  return if needs_setup? directory
  
  invoke :build, directory unless options[:skip_build]
  
  if build_files(directory).empty?
    say 'Your build/ folder is empty', :red
    say 'Nothing to do.'
    return
  end
  
  amazon_config = YAML::load File.read(amazon_config_path directory)
  env = options[:env] || 'development'
  bucket_name = amazon_config['Buckets'][env]
  
  connect_to_amazon_s3(amazon_config,env,bucket_name)

  build_files(directory).each do |file|
    contents = File.read file
    s3path = file.sub(%r{^\./build/},'')
    
    say "      upload ", :cyan; say s3path
    AWS::S3::S3Object.store(s3path, contents, bucket_name, :access => :public_read)
  end
  say 'Done.'
end
s3_upload(*files) click to toggle source
# File lib/staticmatic/deployers/aws-s3.rb, line 40
def s3_upload(*files)
  return if needs_setup? options[:directory]
  
  if files.length == 0
    say 'No files selected.', :red
    say 'Nothing to do.'
    return
  end
  
  amazon_config = YAML::load File.read(amazon_config_path '.')
  env = options[:env] || 'development'
  bucket_name = amazon_config['Buckets'][env]
  
  connect_to_amazon_s3(amazon_config,env,bucket_name)
  
  files.each do |file|
    
    unless File.exists? file
      say "      Does not exist ", :red; say file; next
    end
    
    contents = File.read file
    s3path = file.sub(%r{^(\./)?build/},'')
  
    say "      upload ", :cyan; say s3path
    AWS::S3::S3Object.store(s3path, contents, bucket_name, :access => :public_read)
  end
end

Private Instance Methods

amazon_config_path(directory) click to toggle source
# File lib/staticmatic/deployers/aws-s3.rb, line 87
def amazon_config_path(directory)
  File.join(directory, 'config','amazon.yml')
end
connect_to_amazon_s3(config,env,bucket_name) click to toggle source
# File lib/staticmatic/deployers/aws-s3.rb, line 71
def connect_to_amazon_s3(config,env,bucket_name)
  say 'Uploading to Amazon S3...', :cyan
  say 'Establishing connection to Amazon S3...'
  AWS::S3::Base.establish_connection!(config['AccessKeys'])
  
  say "Grabbing #{env} bucket '#{bucket_name}'"
  buckets = AWS::S3::Service.buckets
  unless buckets.map {|b| b.name}.include? bucket_name
    say 'The bucket ', :red
    say "'#{bucket_name}' "
    say 'does not exist!', :red
    say 'Nothing to do.'
    return
  end
end
needs_setup?(directory) click to toggle source
# File lib/staticmatic/deployers/aws-s3.rb, line 91
def needs_setup?(directory)
  config_file = amazon_config_path(directory)
  
  unless File.exists? config_file
    say 'You need an Amazon S3 config file.', :red
    response = ask 'Would you like to generate one now? [Y/n]'

    if response == '' || response.downcase == 'y'
      copy_file lib_path('deployers/config/amazon.yml'), config_file
      say 'Done.'
      say 'Edit this config file to specify your Amazon S3 access keys, ' +
          'and then try again.'
      say 'For more information, visit http://aws.amazon.com/s3/'
    end
    return true
  end
end