-
-
Save two7sclash-zz/248806999812911b13d0 to your computer and use it in GitHub Desktop.
methods for converting filenames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
require 'securerandom' | |
# | |
# all files must match cengage file names | |
# | |
def cengagify_files(folder_path) | |
Dir.glob("#{folder_path}/**/*").each do |f| | |
# skip directories and simple names | |
next if File.directory?(f) | |
orig_name = File.basename(f) | |
basename = File.basename(f, '.*') | |
extension = File.extname(f) | |
# move html to index for cengage | |
if orig_name == 'main.html' | |
new_name = 'index.html' | |
new_path = f.gsub(orig_name, new_name) | |
# skip if the name is otherwise ok | |
elsif /^[a-z][a-z_]*$/.match(basename) && basename.size < 20 | |
next | |
# "funny" filenames need to be renamed | |
else | |
puts "Bad name #{basename}" | |
new_name = "#{SecureRandom.hex(10)}#{extension.downcase}" | |
new_path = f.gsub(orig_name, new_name) | |
end | |
FileUtils.mv(f, new_path) | |
puts "moving file file: #{f}" | |
referencing_files(folder_path).each do |rf| | |
puts "overriting file: #{rf}" | |
content = File.open(rf, 'r').read | |
content.gsub!(orig_name, new_name) | |
File.open(rf, 'w') do |of| | |
of.write(content) | |
end | |
end | |
# move main.html | |
FileUtils.mv(f, new_path) | |
end | |
end | |
# return all files that might reference a path | |
def referencing_files(folder_path) | |
Dir.glob("#{folder_path}/**/*.js") + | |
Dir.glob("#{folder_path}/**/*.css") + | |
Dir.glob("#{folder_path}/**/*.json") + | |
Dir.glob("#{folder_path}/**/*.html") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rubocopped