Skip to content

Commit f621d4b

Browse files
committed
Add rake update:migrated_links
1 parent 39d85da commit f621d4b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ require 'html-proofer'
1313
require 'kramdown'
1414
require 'launchy'
1515
require 'colorator'
16+
require 'csv'
17+
require 'rdoc'
1618

1719
# Require helper methods from the 'lib' directory
1820
Dir.glob('lib/**/*.rb') { |file| require_relative(file) }

rakelib/update.rake

+34
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ namespace :update do
4848
update_dir subrepo['directory']
4949
end
5050
end
51+
desc 'Find and replace links from "tmp/migrated-from-to.csv" in files at the provided directory. Example: rake update:migrated_links dir=path/to/codebase'
52+
task :migrated_links do
53+
#
54+
# check if 'tmp/migrated-from-to.csv' exists
55+
links_file = 'tmp/migrated-from-to.csv'
56+
abort 'FAILED. Missing "tmp/migrated-from-to.csv" file. Make sure that your _config.local.yml file contains the "migrated_log: generate_file" parameter.' unless File.exist? links_file
57+
# check if the provided directory ('dir') exist
58+
dir = ENV['dir']
59+
abort 'FAILED. Missing argument "dir". Provide a directory to check the links. Example: rake update:migrated_links dir=path/to/codebase' unless dir
60+
abort "FAILED. Check the path provided through the 'dir' argument. The provide directory does not exist: #{dir}" unless Dir.exist?(dir)
61+
# parse 'tmp/migrated-from-to.csv'
62+
links = CSV.read links_file
63+
# for each file in dir, find and replace all links
64+
Dir[File.join(dir, '**', '*')].each do |file|
65+
# ignore directory paths
66+
next if File.directory? file
67+
# ignore symlinks
68+
next if File.symlink? file
69+
# ignore empty files
70+
next if File.zero? file
71+
# ignore binary files
72+
next if RDoc::Parser.binary? file
73+
74+
# read the file
75+
content = File.read file
76+
# iterate through the array of links
77+
links.each do |redirect|
78+
# replace first link from the array with the second links
79+
content.gsub!(redirect[0], redirect[1])
80+
end
81+
# write the update content back to the file
82+
File.write(file, content)
83+
end
84+
end
5185
end
5286

5387
def update_dir(dir)

0 commit comments

Comments
 (0)