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
30
# 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?
  config['aws_region'] = ENV['AWS_REGION'] if config['aws_region'].empty?
  File.open("#{Dir.pwd}/.deploy", 'w+') { |file| file.write(config.to_yaml) }
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



32
33
34
35
36
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
# File 'lib/deploy.rb', line 32

def self.sync
  Aws.config[:region] = @config['aws_region']
  Aws.config[:credentials] = Aws::Credentials.new(@config['aws_key'], @config['aws_secret'])
  s3 = Aws.s3
  
  Dir.glob("#{@config['deploy_folder']}/**/*").each do |file|

    if File.file?(file)
      remote_file = file.sub("#{@config['deploy_folder']}/", "")
        
        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)"
            s3.put_object(bucket: @config['aws_bucket'], key: remote_file, body: File.read("#{file}.gz"),
                               content_type: content_type, content_encoding: 'gzip', acl: 'public-read')
          else
            puts "+ #{file}"
            s3.put_object(bucket: @config['aws_bucket'], key: remote_file, body: File.read(file),
                               content_type: content_type, acl: 'public-read')
          end

        end
    end
  end
end