module Hoe::Publish

def activate_publish_deps

def activate_publish_deps
  dependency "rdoc", [">= 4.0", "< 7"], :developer if need_rdoc
end

def announcement # :nodoc:

:nodoc:
def announcement # :nodoc:
  changes = self.changes.rdoc_to_markdown
  subject = "#{name} #{version} Released"
  title   = "#{name} version #{version} has been released!"
  body    = "#{description}\n\nChanges:\n\n#{changes}".rdoc_to_markdown
  urls =
    case self.urls
    when Hash then
      self.urls.map { |k, v| "* #{k}: <#{v.strip.rdoc_to_markdown}>" }
    when Array then
      self.urls.map { |s| "* <#{s.strip.rdoc_to_markdown}>" }
    else
      raise "unknown urls format: #{urls.inspect}"
    end
  return subject, title, body, urls.join("\n")
end

def define_publish_tasks

def define_publish_tasks
  if need_rdoc then
    task :isolate # ensure it exists
    desc "Generate rdoc"
    task :docs => [:clobber_docs, :isolate] do
      sh(*make_rdoc_cmd)
    end
    desc "Generate rdoc coverage report"
    task :dcov => :isolate do
      sh(*make_rdoc_cmd("-C"))
    end
    desc "Remove RDoc files"
    task :clobber_docs do
      rm_rf local_rdoc_dir
    end
    task :clobber => :clobber_docs
    desc "Generate ri locally for testing."
    task :ridocs => [:clean, :isolate] do
      sh(*make_rdoc_cmd("--ri", "-o", "ri"))
    end
  end
  desc "Publish RDoc to wherever you want."
  task :publish_docs => [:clean, :docs] do
    publish_docs_task
  end
  # no doco for this one
  task :publish_on_announce do
    publish_on_announce_task
  end
  desc "Generate email announcement file."
  task :debug_email do
    puts generate_email ENV["FULL"]
  end
  desc 'Post announcement to blog. Uses the "blogs" array in your hoerc.'
  task :post_blog do
    post_blog_task
  end
  desc "Announce your release."
  task :announce => [:post_blog, :publish_on_announce ]
end

def generate_email full = nil # :nodoc:

:nodoc:
def generate_email full = nil # :nodoc:
  require "time"
  abort "No email 'to' entry. Run `rake config_hoe` to fix." unless
    !full || email_to
  from_name, from_email      = author.first, email.first
  subject, title, body, urls = announcement
  [
   full && "From: #{from_name} <#{from_email}>",
   full && "To: #{email_to.join(", ")}",
   full && "Date: #{Time.now.rfc2822}",
   "Subject: [ANN] #{subject}",
   "",
   title,
   "",
   urls,
   "",
   body,
  ].compact.join("\n")
end

def initialize_publish

def initialize_publish
  self.blog_categories ||= [self.name]
  self.local_rdoc_dir  ||= "doc"
  self.need_rdoc       ||= true
  self.rdoc_locations  ||= []
  self.remote_rdoc_dir ||= self.name
  self.rsync_args      ||= "-av -O --delete"
end

def make_rdoc_cmd(*extra_args) # :nodoc:

:nodoc:
def make_rdoc_cmd(*extra_args) # :nodoc:
  title = "#{name}-#{version} Documentation"
  title = "#{group_name}'s #{title}" if group_name != name
  (
   %W[#{Gem.ruby} -S rdoc
      --title #{title}
      -o #{local_rdoc_dir}
     ] +
     spec.rdoc_options +
     extra_args +
     spec.require_paths +
     spec.extra_rdoc_files
  ).reject(&:empty?)
end

def post_blog_task # :nodoc:

:nodoc:
def post_blog_task # :nodoc:
  with_config do |config, _path|
    break unless config["blogs"]
    config["blogs"].each do |site|
      if site["path"] then
        msg = "post_blog_#{site["type"]}"
        send msg, site
        system site["cmd"] if site["cmd"]
      else
        require "xmlrpc/client"
        _, title, body, urls = announcement
        body += "\n\n#{urls}"
        server = XMLRPC::Client.new2(site["url"])
        content = site["extra_headers"].merge(:title => title,
                                              :description => body,
                                              :categories => blog_categories)
        server.call("metaWeblog.newPost",
                    site["blog_id"],
                    site["user"],
                    site["password"],
                    content,
                    true)
      end
    end
  end
end

def post_blog_zenweb site # :nodoc:

:nodoc:
def post_blog_zenweb site # :nodoc:
  dir = site["path"]
  _, title, body, urls = announcement
  body += "\n\n#{urls}"
  Dir.chdir File.expand_path dir do
    time = Time.at Time.now.to_i # nukes fractions
    path = [time.strftime("%Y-%m-%d-"),
            title.sub(/\W+$/, "").gsub(/\W+/, "-"),
            ".html.md"].join
    header = {
      "title"      => title,
      "categories" => blog_categories,
      "date"       => time,
    }
    File.open path, "w" do |f|
      f.puts header.to_yaml.gsub(/\s$/, "")
      f.puts "..."
      f.puts
      f.puts body
    end
  end
end

def publish_docs_task # :nodoc:

:nodoc:
def publish_docs_task # :nodoc:
  warn "no rdoc_location values" if rdoc_locations.empty?
  self.rdoc_locations.each do |dest|
    sh %(rsync #{rsync_args} #{local_rdoc_dir}/ #{dest})
  end
end

def publish_on_announce_task # :nodoc:

:nodoc:
def publish_on_announce_task # :nodoc:
  with_config do |config, _|
    Rake::Task["publish_docs"].invoke if config["publish_on_announce"]
  end
end