Class: Bloggit::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/bloggit/publisher.rb

Overview

Publisher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, verbose = false) ⇒ Publisher

Returns a new instance of Publisher.



9
10
11
12
13
# File 'lib/bloggit/publisher.rb', line 9

def initialize(site, verbose=false)
  @site = site
  @verbose = verbose
  @settings = site.settings.publish
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



7
8
9
# File 'lib/bloggit/publisher.rb', line 7

def settings
  @settings
end

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'lib/bloggit/publisher.rb', line 7

def site
  @site
end

#verboseObject (readonly)

Returns the value of attribute verbose.



7
8
9
# File 'lib/bloggit/publisher.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#local_path(path) ⇒ Object



107
108
109
# File 'lib/bloggit/publisher.rb', line 107

def local_path(path)
  File.join(site.base_path, 'cache', path)
end

#puts_if_verbose(msg = '') ⇒ Object



111
112
113
# File 'lib/bloggit/publisher.rb', line 111

def puts_if_verbose(msg='')
  puts(msg) if @verbose
end

#server_path(path) ⇒ Object Also known as: remote_path



102
103
104
# File 'lib/bloggit/publisher.rb', line 102

def server_path(path)
  [settings['path'], path].flatten.join('/')
end

#uploadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bloggit/publisher.rb', line 15

def upload
  puts_if_verbose "Connecting to #{settings['host']}..."
  
  Net::SFTP.start(settings['host'], settings['user'], settings['pass'], :timeout=>settings.fetch('timeout', 30)) do |sftp|
    
    puts_if_verbose "Fetching checksums..."
    local_checksums = YAML::load(File.open( local_path("checksum.yml") ))
    remote_checksums = {}
    begin
      checksums_src = ''
      sftp.open_handle( server_path('checksum.yml')) do |handle|
        checksums_src = sftp.read(handle)
      end
      remote_checksums = YAML::load(checksums_src) unless checksums_src.nil? or checksums_src==''
    rescue Net::SFTP::Operations::StatusException=>se
      # server's checksum.yml is missing 
    end
    
    puts_if_verbose "Comparing checksum data..."
    to_upload, to_remove = Bloggit::Checksum.diff(local_checksums, remote_checksums)
    
    if to_upload.length > 0 or to_remove.length > 0
      puts_if_verbose "Differences found:"
      to_upload.each {|f| puts_if_verbose " - (Upload) #{f}" }
      to_remove.each {|f| puts_if_verbose " - (Delete) #{f}"}
      puts_if_verbose "Beginning sync..."
      
      to_upload.each do |filename|
        begin
          puts_if_verbose " - #{remote_path(filename)}"

          dir_name = File.dirname(filename)
          dir_segs = dir_name.split('/')
          #puts "Checking path: #{dir_segs.join( '/' )}"
          prog_path = []
          dir_segs.each do |partial_dir|
            begin
              prog_path << partial_dir
              sftp.mkdir( remote_path( prog_path ), :permissions=>0755 )
              puts_if_verbose " + #{remote_path( prog_path )}"
            rescue Net::SFTP::Operations::StatusException=>se
              # don't worry about it
            end
          end

          sftp.put_file local_path(filename), remote_path(filename)
          sftp.open_handle( remote_path(filename) ) do |handle|
            sftp.fsetstat( handle, :permissions=>0644 )
          end
        rescue  Net::SFTP::Operations::StatusException=>se
          puts_if_verbose " ! Error uploading '#{filename}': #{se}"
          puts_if_verbose; puts_if_verbose "Halted execution of upload."
          exit(1)
        end
      end
      
      to_remove.each do |filename|
        begin
          sftp.remove remote_path(filename)
          puts_if_verbose " x #{remote_path(filename)}"
        rescue
          puts_if_verbose " ! Error removing '#{filename}': #{$!}"
        end
      end

      begin
        sftp.put_file local_path('checksum.yml'), remote_path('checksum.yml')
        sftp.open_handle( remote_path('checksum.yml') ) do |handle|
          sftp.fsetstat( handle, :permissions=>0644 )
        end
      rescue
        puts_if_verbose " ! Error uploading 'checksum.yml': #{$!}"
      end

      
      summary = "#{to_upload.length} file(s) uploaded"
      summary += ", #{to_remove.length} files(s) deleted" if to_remove.length > 0
      
      puts summary
    else
      puts "No changes made. The server is up to date!"
    end
    
    puts "Done."
  end
end