Class: Nanoc::Deploying::Deployers::NeocitiesDeployer

Inherits:
Nanoc::Deploying::Deployer
  • Object
show all
Defined in:
lib/nanoc-neocities.rb

Overview

A deployer that deploys a site to [Neocities](neocities.org/).

Examples:

A deployment configuration for Neocities:


deploy:
  default:
    kind:         neocities
    api_key:      1234abcd
    sitename:     mysite
    prune_remote: true

Defined Under Namespace

Modules: Errors

Instance Method Summary collapse

Instance Method Details

#runObject



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
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nanoc-neocities.rb', line 58

def run
  unless File.exist?(source_path)
    raise Errors::OutputEmptyOrGone.new(source_path)
  end

  api_key  = config.fetch(:api_key,  '')
  sitename = config.fetch(:sitename, '')
  prune    = config.fetch(:prune_remote, true)

  client = Neocities::Client.new({api_key: api_key, sitename: sitename})

  remote_files = client.list
  
  unless remote_files[:result] == 'success'
    if remote_files[:error_type] == 'invalid_auth'
puts remote_files
      raise Errors::AuthenticationFailed
    else
      raise Errors::APIerror.new(remote_files)
    end
  end

  puts "Deploying to Neocities site “#{sitename}”…"

  Dir.chdir(source_path) do
    Dir.glob("**/*") {|path|
      unless File.directory?(path)
        upload = client.upload(path, path)
        if upload[:result] == 'success'
          puts "Uploaded file “#{path}”."
        elsif upload[:error_type] == 'file_exists'
          puts "Existing file “#{path}” is identical. Skipping upload."
        else 
          raise Errors::UploadFailed.new(path, upload[:message] || 'unknown problem')
        end
      end
    }

    puts "Neocities site “#{sitename}” deployed."

    if prune
      remote_files = client.list
      puts "Deleting orphaned files from Neocities site “#{sitename}”…"

      orphans = client.list[:files].map{|file| file[:path]} - Dir.glob("**/*")

      for path in orphans
        puts "Deleting “#{path}”."
        unless client.delete(orphan)[:result] == 'success'
          raise Errors::DeleteOrphanFailed.new(path)
        end
      end
    end

  end
end