Sha256: 5ab119631a86e973dc374a8800d21d53d320fdd74027e5d8db094e358b8a08b5

Contents?: true

Size: 1.9 KB

Versions: 94

Compression:

Stored size: 1.9 KB

Contents

require 'yaml'
desc 'Move content from one location to another. (Be careful with this one)'
task move: :environment do
  ARGV.each { |a| task a.to_sym }

  from = ARGV[1]
  to = ARGV[2]

  raise 'Usage: rake move <from> <to>' unless from && to

  to_dir, = File.split(to)

  # Make sure it starts with _documentation
  documentation_folder = "#{Rails.configuration.docs_base_path}/_documentation/"
  raise "'from' must start with '#{documentation_folder}'" unless from[0..documentation_folder.length - 1] == documentation_folder
  raise "'to' must start with '#{documentation_folder}'" unless to[0..documentation_folder.length - 1] == documentation_folder

  raise "You tried to move files from a location that doesn't exist (#{from})" unless File.exist? from
  raise "You tried to move files to a location that doesn't exist (#{to})" unless File.exist? to_dir
  raise "You tried to move files to a location that already exists (#{to})" if File.exist? to

  # Load up our redirect file
  path = "#{Rails.root}/config/automatic-redirects.yml"
  document = File.read(path)
  redirects = YAML.safe_load(document) || {}

  # Add a top level redirect
  add_redirect(documentation_folder, from, to, redirects)

  # Iterate over every file in there and build up a list
  Dir.glob("#{from}/**/*").each do |filename|
    target = filename.gsub(from, to)
    add_redirect(documentation_folder, filename, target, redirects)
  end

  # Actually move the files
  FileUtils.mv(from, to)

  File.write(path, redirects.to_yaml)
rescue StandardError => e
  puts e
  exit(1)
end

def add_redirect(documentation_folder, from, to, redirects)
  # Strip off the leading _documentation as that never shows in the URL
  from = from.gsub(documentation_folder, '')
  to = to.gsub(documentation_folder, '')

  puts "#{from} => #{to}"

  ext = File.extname(from)
  from = from.gsub(ext, '')
  to = to.gsub(ext, '')

  # Also strip off any file types
  redirects[from] = to
end

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
station-0.5.16 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.15 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.14 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.13 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.12 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.11 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.10 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.9 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.8 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.7 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.6 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.5 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.4 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.3 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.2 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.1 lib/nexmo_developer/lib/tasks/move.rake
station-0.5.0 lib/nexmo_developer/lib/tasks/move.rake
station-0.4.9 lib/nexmo_developer/lib/tasks/move.rake
station-0.4.8 lib/nexmo_developer/lib/tasks/move.rake
station-0.4.7 lib/nexmo_developer/lib/tasks/move.rake