Skip to content

Commit 5b61343

Browse files
authored
Merge pull request magento-commerce/devdocs#3231
Add rake update:migrated_links_at
2 parents 39d85da + c928b9e commit 5b61343

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Rakefile

Lines changed: 2 additions & 0 deletions
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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,66 @@ namespace :update do
4848
update_dir subrepo['directory']
4949
end
5050
end
51+
52+
desc "Find and replace links from 'tmp/migrated-from-to.csv' in files at the provided directory.
53+
Arguments:
54+
- 'dir' is an absolute path to the directory to process the links. Required.
55+
- 'exclude' is an fnmatch pattern for paths to exclude from processing. For fnmatch format, see https://ruby-doc.org/core-2.7.5/Dir.html#method-c-glob. Optional.
56+
Examples:
57+
rake update:migrated_links_at dir=path/to/codebase.
58+
rake update:migrated_links_at dir=path/to/codebase exclude='**/Test/**'"
59+
task :migrated_links_at do
60+
# check if 'tmp/migrated-from-to.csv' exists
61+
links_file = 'tmp/migrated-from-to.csv'
62+
unless File.exist? links_file
63+
abort 'FAILED. Missing "tmp/migrated-from-to.csv" file. Make sure that your _config.local.yml file contains the "migrated_log: generate_file" parameter.'
64+
end
65+
# check if the provided directory ('dir') exist
66+
dir = File.expand_path(ENV['dir'])
67+
unless dir
68+
abort 'FAILED. Missing argument "dir". Provide a directory to check the links. Example: rake update:migrated_links_at dir=path/to/codebase'
69+
end
70+
unless Dir.exist?(dir)
71+
abort "FAILED. Check the path provided through the 'dir' argument. The provide directory does not exist: #{dir}"
72+
end
73+
exclude = ENV['exclude']
74+
# parse 'tmp/migrated-from-to.csv'
75+
links = CSV.read links_file
76+
# for each file in dir, find and replace all links
77+
puts 'Work in progress...'.magenta
78+
dir_glob_pattern = File.join(dir, '**', '*')
79+
full_file_list = Dir[dir_glob_pattern]
80+
# exclude paths by pattern from the file list if the 'exclude' argument was added
81+
if exclude
82+
exclude_glob_pattern = File.join(dir, exclude)
83+
excluded_file_list = Dir[exclude_glob_pattern]
84+
final_file_list = full_file_list - excluded_file_list
85+
else
86+
final_file_list = full_file_list
87+
end
88+
89+
final_file_list.each do |file|
90+
# ignore directory paths
91+
next if File.directory? file
92+
# ignore symlinks
93+
next if File.symlink? file
94+
# ignore empty files
95+
next if File.zero? file
96+
# ignore binary files
97+
next if RDoc::Parser.binary? file
98+
99+
# read the file
100+
content = File.read file
101+
# iterate through the array of links
102+
links.each do |redirect|
103+
# replace first link from the array with the second links
104+
content.gsub!(redirect[0], redirect[1])
105+
end
106+
# write the update content back to the file
107+
File.write(file, content)
108+
end
109+
puts 'Done!'.green
110+
end
51111
end
52112

53113
def update_dir(dir)

0 commit comments

Comments
 (0)