#!/usr/bin/env ruby # require "byebug" PWD = Dir.pwd GEM_DIR = Gem::Specification.find_by_name("jekyll-octopod").gem_dir # HAS20170501 disgusting hack for development # GEM_DIR = "/home/user/jekyll/jekyll-octopod/" require 'rubygems' require 'optparse' require 'yaml' require 'fileutils' octopod_help = < ./_site octopod build # . -> octopod build # -> octopod import # imports posts using named import script octopod setup # Setup blog to become podcast-aware, copy assets and default config octopod s[erver] # Starts the server Additional Octopod commands: octopod episode # adds a template for a new episode octopod deploy # deploys your site See 'octopod --help' for more information on a specific command. Configuration is read from '/_config.yml' but can be overridden using the following options: HELP episode_help = </_config.yml' but can be overridden using the following options: EPISODE_HELP deploy_help = </_config.yml' but can be overridden using the following options: DEPLOY_HELP DEFAULT_EXT = '.md' class OptionParser alias_method :_octopod_banner=, :banner= def banner=(_) return '' end end def ok_failed(condition) if (condition) puts "OK" else puts "FAILED" end end def write_file?(filename) if File.exists?(filename) unless ["Y","N"].include?(@answer) puts "" @answer = ask("File '#{filename}' already exists. Overwrite? Capitalized answer for ALL files.", ['y', 'Y', 'n', 'N']) end ["Y", "y"].include?(@answer) else true end end def copy_file_or_create_dir(file) filename = file.split(GEM_DIR + "/assets")[1] if File.directory?(file) FileUtils.mkdir(PWD + filename, {verbose: true}) unless File.exists?(PWD + filename) else if write_file?(PWD + filename) FileUtils.cp(file, PWD + filename, {verbose: true}) end end end def ask(message, valid_options = nil) if valid_options answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer) else answer = get_stdin(message) end answer end def get_stdin(message) print message STDIN.gets.chomp end if ARGV.size > 0 && (ARGV[0] == 'episode' || ARGV[0] == 'deploy' || ARGV[0] == 'setup') options = YAML.load_file(File.join(PWD, '_config.yml')) options['posts_dir'] = File.join(PWD, '_posts') case ARGV[0] when 'episode' post_header = { 'title' => 'A Title', 'subtitle' => "A Subtle Subtitle", 'datum' => 'May 22nd, 2016', 'layout' => 'post', 'author' => options['author'], 'explicit' => options['explicit'] || 'no', 'duration' => "0:01:00", 'audio' => { 'm4a' => 'name.m4a', 'mp3' => 'name.mp3', 'ogg' => 'name.ogg'}, 'filesize' => { 'm4a' => 1234567, 'mp3' => 2345678, 'ogg' => 3456789}, 'chapters' => [ '00:00:00.000 Intro.', '00:00:30.000 Outro.' ] } opts = OptionParser.new do |opts| opts._octopod_banner = episode_help opts.on("-a", "--author [AUTHOR]", "Name of post author") do |episode_author| post_header['author'] = options['author'] = episode_author end opts.on("-c", "--chapters [chapter1,chapter2,...]", Array, "The episodes chapters.") do |episode_chapters| warn episode_chapters.inspect post_header['chapters'] = options['chapters'] = episode_chapters end opts.on("-d", "--duration [DURATION]", "Duration of the episode") do |episode_duration| post_header['duration'] = options['duration'] = episode_duration.to_i end opts.on("-e", "--explicit [yes/no]", "Explicit rating for this episode (default: 'no')") do |episode_explicit| post_header['explicit'] = options['explicit'] = episode_explicit end opts.on("-l", "--layout [LAYOUT]", "Layout to use (default: 'post')") do |episode_layout| post_header['layout'] = options['layout'] = episode_layout end opts.on("-p", "--posts-dir [PATH]", "Path to the post directory (default: '_posts')") do |episode_posts_dir| options['posts_dir'] = episode_posts_dir end opts.on("-s", "--subtitle [TEXT]", "Subtitle of the episode") do |episode_subtitle| post_header['subtitle'] = options['subtitle'] = episode_subtitle end opts.on("--summary [TEXT]", "Summary of the episode") do |episode_summary| post_header['summary'] = options['summary'] = episode_summary end opts.on("--tags [tag1,tag2,...]", Array, "Tags for the episode") do |episode_tags| post_header['tags'] = options['tags'] = episode_tags end opts.on("-t", "--title [TEXT]", "Title of the episode") do |episode_title| post_header['title'] = options['title'] = episode_title end end opts.parse! unless FileTest.directory?(options['posts_dir']) abort("rake aborted: '#{options['posts_dir']}' directory not found.") end post_header['title'] = post_header['title'] || 'untitled' slug = post_header['title'].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '_') date = Time.now.strftime('%Y-%m-%d') filename = File.join(options['posts_dir'], "#{date}-#{slug}#{DEFAULT_EXT}") abort() unless write_file?(filename) open(filename, 'w') do |post| post.puts post_header.to_yaml post.puts '---' post.puts post.puts '' post.puts post.puts "{% podigee_player page %}\n\n" post.puts "## Shownotes and Links\n\n* Note" end puts "Created new episode: #{filename}" if File.exists?(filename) when 'deploy' opts = OptionParser.new do |opts| opts._octopod_banner = deploy_help opts.on("--document-root [PATH]", "Path to the documwrite_fileent root on the remote machine") do |deploy_document_root| options['document_root'] = deploy_document_root end opts.on("--rsync-delete", "Delete extraneous files from destination dir") do |deploy_rsync_delete| options['rsync_delete'] = deploy_rsync_delete end opts.on("--ssh-host [USER@]", "[User and] host of the destination machine") do |deploy_ssh_host| options['ssh_host'] = deploy_ssh_host =~ /\A\w*@.*/ ? deploy_ssh_host : "#{ENV['USER']}@#{deploy_ssh_host}" end opts.on("--ssh-port [PORT]", "SSH port on the remote machine") do |deploy_ssh_port| options['ssh_port'] = deploy_ssh_port.to_i end end opts.parse! options['public_dir'] = ARGV[1] ? ARGV[1] : File.join(PWD, '_site') exclude = "" if File.exists?('./rsync-exclude') exclude = "--exclude-from '#{File.expand_path('./rsync-exclude')}'" end puts "## Deploying website via Rsync" rsync_command = "rsync --progress -avze 'ssh -p #{options['ssh_port']}' #{exclude} \ #{"--delete" unless options['rsync_delete'] == false} \ #{options['public_dir']}/ #{options['ssh_host']}:#{options['document_root']}" ok_failed system(rsync_command) when 'setup' unless File.exists?(PWD + '/_config.dist_backup.yml') puts "", "===== Creating a backup of the configuration file ... =====" FileUtils.cp(PWD + '/_config.yml', PWD + '/_config.dist_backup.yml', {verbose: true}) puts "===== ... done ===== ", "" end puts "", "If you are creating a brand new podcasting site, you can safely overwrite and answer with 'Y'." puts "If this is an old site, it's better to answer with 'N'." , "" puts "", "===== Copying assets ... =====" Dir.glob(GEM_DIR + "/assets/**/*").each do |file| copy_file_or_create_dir(file) end Dir.glob(GEM_DIR + "/assets/**/.*").each do |file| copy_file_or_create_dir(file) end if write_file?(PWD + "/_config.yaml") FileUtils.cp(GEM_DIR + "/assets/_config.yml.sample", PWD + "/_config.yml", {verbose: true}) end puts "===== ... done ===== ", "" end else puts octopod_help if ARGV.size > 0 && ARGV[0] == '--help' load Gem.bin_path('jekyll', 'jekyll') end