Class: Awestruct::Deploy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/deploy/base_deploy.rb

Direct Known Subclasses

GitHubPagesDeploy, RSyncDeploy, S3Deploy

Constant Summary collapse

UNCOMMITTED_CHANGES =
'You have uncommitted changes in the working branch. Please commit or stash them.'

Instance Method Summary collapse

Constructor Details

#initialize(site_config, deploy_config) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
25
26
27
# File 'lib/awestruct/deploy/base_deploy.rb', line 19

def initialize(site_config, deploy_config)
  # Add a single front slash at the end of output dir
  @site_path = File.join( site_config.output_dir, '/' ).gsub(/^\w:\//, '/')
  @gzip = deploy_config['gzip']
  @gzip_level = deploy_config['gzip_level'] || Zlib::BEST_COMPRESSION
  @source_dir = deploy_config['source_dir'] || site_config.dir
  @ignore_uncommitted = deploy_config['uncommitted']
  init_scm(deploy_config['scm'] || 'git')
end

Instance Method Details

#compress_siteObject



56
57
58
59
60
# File 'lib/awestruct/deploy/base_deploy.rb', line 56

def compress_site
  if @gzip
    gzip_site @site_path
  end
end

#existing_changesObject



52
53
54
# File 'lib/awestruct/deploy/base_deploy.rb', line 52

def existing_changes
  $LOG.error UNCOMMITTED_CHANGES
end

#gzip_file(filename, level) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/awestruct/deploy/base_deploy.rb', line 79

def gzip_file(filename, level)
  $LOG.debug "Gzipping File #{filename}"
  Zlib::GzipWriter.open("#{filename}.gz", level) do |gz|
    gz.mtime = File.mtime(filename)
    gz.orig_name = filename
    gz.write File.binread(filename)
  end
  File.rename("#{filename}.gz", "#{filename}")
end

#gzip_site(site_path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/awestruct/deploy/base_deploy.rb', line 62

def gzip_site(site_path)
  Dir.glob("#{site_path}/**/*") do |item|
    next if item == '.' or item == '..'
    ext = File.extname(item)
    if !ext.empty?
      ext_sym = ext[1..-1].to_sym
      case ext_sym
      when :css, :js, :html
        require 'zlib'
        if !is_gzipped item
          gzip_file(item, @gzip_level)
        end
      end
    end
  end
end

#init_scm(type) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/awestruct/deploy/base_deploy.rb', line 100

def init_scm type
  begin
    clazz = Object.const_get('Awestruct').const_get('Scm').const_get(type.capitalize)
    @scm = clazz.new
  rescue
    ExceptionHelper.log_message( "Could not resolve class for scm type: #{type}" )
    ExceptionHelper.mark_failed
  end
end

#is_gzipped(filename) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/awestruct/deploy/base_deploy.rb', line 89

def is_gzipped(filename)
  begin
    File.open("#{filename}") do |f|
      Zlib::GzipReader.new(f)
      true
    end
  rescue
    false
  end
end

#publish_siteObject



48
49
50
# File 'lib/awestruct/deploy/base_deploy.rb', line 48

def publish_site
  $LOG.error( "#{self.class.name}#publish_site not implemented." )
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/awestruct/deploy/base_deploy.rb', line 29

def run
  if ExceptionHelper.build_failed?
    ExceptionHelper.log_message 'Not running deploy due to build failure'
    return
  end

  if @ignore_uncommitted == true
    compress_site
    publish_site
  else
    if @scm.uncommitted_changes? @source_dir
      existing_changes
    else
      compress_site
      publish_site
    end
  end
end