This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathupdate.rake
126 lines (112 loc) · 3.99 KB
/
update.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
# frozen_string_literal: true
namespace :update do
desc 'Update the devdocs theme'
task :theme do
print 'Updating the devdocs theme gem dependency: $ '.magenta
sh 'bundle update devdocs --conservative'
print "Let's see the changes in Gemfile.lock: $ ".blue
sh 'git diff Gemfile.lock'
end
desc 'Update MBI docs'
task :mbi do
puts 'Updating MBI docs:'.magenta
update_dir 'src/mbi'
end
desc 'Update Page Builder docs'
task :pb do
puts 'Updating Page Builder docs:'.magenta
update_dir 'src/page-builder'
end
desc 'Update MFTF docs'
task :mftf do
puts 'Updating MFTF docs:'.magenta
update_dir 'src/mftf'
end
desc 'Update devdocs master'
task :devdocs do
puts 'Updating devdocs:'.magenta
sh 'git remote -v'
sh 'git checkout master'
sh 'git pull'
sh 'git status -sb'
end
desc 'Update devdocs and subrepositories'
task all: %w[devdocs subrepos]
desc 'Update subrepositories only'
task :subrepos do
@content_map.each do |subrepo|
update_dir subrepo['directory']
end
end
desc "Find and replace links from 'tmp/migrated-from-to.csv' in files at the provided directory.
Arguments:
- 'dir' is an absolute path to the directory to process the links. Required.
- '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.
Examples:
rake update:migrated_links_at dir=path/to/codebase.
rake update:migrated_links_at dir=path/to/codebase exclude='**/Test/**'"
task :migrated_links_at do
# check if 'tmp/migrated-from-to.csv' exists
links_file = 'tmp/migrated-from-to.csv'
unless File.exist? links_file
abort 'FAILED. Missing "tmp/migrated-from-to.csv" file. Make sure that your _config.local.yml file contains the "migrated_log: generate_file" parameter.'
end
# check if the provided directory ('dir') exist
dir = File.expand_path(ENV['dir'])
unless dir
abort 'FAILED. Missing argument "dir". Provide a directory to check the links. Example: rake update:migrated_links_at dir=path/to/codebase'
end
unless Dir.exist?(dir)
abort "FAILED. Check the path provided through the 'dir' argument. The provide directory does not exist: #{dir}"
end
exclude = ENV['exclude']
# parse 'tmp/migrated-from-to.csv'
links = CSV.read links_file
# for each file in dir, find and replace all links
puts 'Work in progress...'.magenta
dir_glob_pattern = File.join(dir, '**', '*')
full_file_list = Dir[dir_glob_pattern]
# exclude paths by pattern from the file list if the 'exclude' argument was added
if exclude
exclude_glob_pattern = File.join(dir, exclude)
excluded_file_list = Dir[exclude_glob_pattern]
final_file_list = full_file_list - excluded_file_list
else
final_file_list = full_file_list
end
final_file_list.each do |file|
# ignore directory paths
next if File.directory? file
# ignore symlinks
next if File.symlink? file
# ignore empty files
next if File.zero? file
# ignore binary files
next if RDoc::Parser.binary? file
# read the file
content = File.read file
# iterate through the array of links
links.each do |redirect|
# replace first link from the array with the second links
content.gsub!(redirect[0], redirect[1])
end
# write the update content back to the file
File.write(file, content)
end
puts 'Done!'.green
end
end
def update_dir(dir)
unless Dir.exist? dir
abort "Cannot find the #{dir} directory. You can run 'rake init' to create it and rerun 'rake update:all' again.".red
end
Dir.chdir dir do
puts "Updating #{dir}:".magenta
next warn 'No branch to update' if `git status -sb`.include? 'no branch'
sh 'git remote -v'
sh 'git pull --no-recurse-submodules'
sh 'git status -sb'
end
end