Module: Deploy

Defined in:
lib/deploy.rb

Defined Under Namespace

Modules: Compress

Class Method Summary collapse

Class Method Details

.create_config(config = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deploy.rb', line 18

def self.create_config(config = {})  
  config = {
    'aws_key' =>  nil,
    'aws_secret' =>  nil, 
    'aws_bucket' => nil, 
    'deploy_folder' =>  nil,
    'compress' => true
  }.merge(config)
  config['aws_key'] = ENV['AWS_KEY'] if config['aws_key'].empty?
  config['aws_secret'] = ENV['AWS_SECRET'] if config['aws_secret'].empty?
  File.open("#{Dir.pwd}/.deploy", 'w+') { |file| file.write(config.to_yaml) }
end

.get_bucketObject



31
32
33
34
35
# File 'lib/deploy.rb', line 31

def self.get_bucket
  ::AWS.config(access_key_id: @config['aws_key'], secret_access_key: @config['aws_secret'], region: @config['aws_region'])
  service = ::AWS::S3.new(s3_endpoint: "s3-#{@config['aws_region']}.amazonaws.com")
  service.buckets[@config['aws_bucket']]
end

.load_configObject



9
10
11
12
13
14
15
16
# File 'lib/deploy.rb', line 9

def self.load_config
  
  # We need to account for AWS Keys stored in 
  # ENV Variables here.
  defaults = {'aws_region' => 'us-west-2', 'compress' => true}
  @config = YAML.load_file("#{Dir.pwd}/.deploy")
  @config = defaults.merge(@config)
end

.syncObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/deploy.rb', line 37

def self.sync
  bucket = get_bucket
  Dir.glob("#{@config['deploy_folder']}/**/*").each do |file|

    if File.file?(file)
      remote_file = file.sub("#{@config['deploy_folder']}/", "")
        
        remote = bucket.objects[remote_file]
        
        content_type = case file.split('.').last
        when 'css'
          Deploy::Compress.compress(file)
          'text/css'
        when 'js'
          Deploy::Compress.compile(file)
          'application/javascript'
        when 'otf'
          'font/opentype'
        when 'svg'
          'image/svg+xml'
        when 'xml'
          'text/xml'
        when 'html', 'htm'
          'text/html'
        when 'gz'
          'skip'
        end 
        
        unless content_type == 'skip'
          if File.exist?("#{file}.gz")
            puts "+ #{file} (compressed)"
            remote.write(file: "#{file}.gz", content_encoding: 'gzip',  content_type: content_type)
          else
            puts "+ #{file}"
            remote.write(file: file,  content_type: content_type)
          end
          remote.acl = :public_read 
        end
    end
  end
end