Last active
September 4, 2019 08:14
-
-
Save Ji-Yuhang/ac9f4fadaa7694a2393daa532196772f to your computer and use it in GitHub Desktop.
批量拷贝src/assets下文件到temp下,文件名加上[hash:8]
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
#!/usr/bin/env ruby | |
require "fileutils" | |
require "digest" | |
def images | |
Dir.glob("src/assets/**/*").select { |f| !File.directory?(f) } | |
end | |
def unhashed_images | |
images.select { |f| f.slice(-8, 8) != sha1(f) } | |
end | |
def sha1(file) | |
sha = Digest::SHA1.file file | |
hash = sha.hexdigest | |
hash_8 = hash.slice(0, 8) | |
hash_8 | |
end | |
data = unhashed_images.map do |f| | |
hash = sha1(f) | |
extname = File.extname(f) | |
basename = File.basename(f) | |
hash_name = f.gsub(extname, ".#{hash}#{extname}") | |
base_hash_name = basename.gsub(extname, ".#{hash}#{extname}") | |
#qiniu_url = "http://cdn.improver-article.joinuscn.com/assets/#{basename}.#{hash}.#{extname}" | |
# qiniu_url = "https://cdn.improver-article.joinuscn.com/assets/#{base_hash_name}" | |
qiniu_url = hash_name.gsub("src/assets/", "https://cdn.improver-article.joinuscn.com/assets/") | |
qiniu_url = URI.encode(qiniu_url) | |
{ | |
file: f, | |
size: File.size(f), | |
sha: hash, | |
hash_name: hash_name, | |
qiniu_url: qiniu_url, | |
} | |
end | |
data.sort { |a, b| b[:size] <=> a[:size] }.each { |t| | |
puts "size: #{t[:size]} , file: #{t[:file]}, sha: #{t[:sha]}, hash_name: #{t[:hash_name]}, qiniu_url: #{t[:qiniu_url]}" | |
} | |
puts data.reduce(0) { |s, f| f[:size] + s } | |
def copy_asset(data) | |
FileUtils.rm_rf("temp/") | |
Dir.glob("src/assets/**/*/").each do |path| | |
puts path | |
target_dir = path.gsub("src/assets/", "temp/assets/") | |
puts target_dir | |
FileUtils.mkdir_p(target_dir) | |
end | |
File.open "temp/url.txt", "w" do |io| | |
data.each do |obj| | |
f = obj[:file] | |
target_hash_path = obj[:hash_name].gsub("src/assets", "temp/assets") | |
target_path = obj[:file].gsub("src/assets", "temp/assets") | |
FileUtils.cp(f, target_path) | |
FileUtils.cp(f, target_hash_path) | |
io.puts obj[:qiniu_url] | |
end | |
end | |
end | |
def upload_qiniu(data) | |
copy_asset(data) | |
end | |
# upload_qiniu(data) | |
def js_files | |
Dir.glob(["src/components/**/*", "src/pages/**/*"]).select { |f| !File.directory?(f) } | |
end | |
def js_files_with_image | |
file_assets = {} | |
js_files.each do |f| | |
assets = File.read(f).scan /'[^']*?assets\/.*?'/ | |
if !assets.empty? | |
# puts assets | |
file_assets[f] = assets | |
end | |
# !assets.empty? | |
end | |
file_assets | |
end | |
def replace_require(f) | |
end | |
def global_replace(data) | |
file_assets = js_files_with_image | |
puts file_assets.count | |
puts file_assets.values.flatten.count | |
# require("../../assets/***.png") | |
file_assets.each do |f, assets| | |
# puts "#{f} #{assets}" | |
content = File.read(f) | |
lines = content.lines.map do |line| | |
assets.each do |asset| | |
if line.include? asset | |
# puts line | |
if line.include? "import" | |
caputers = line.scan /import\s(\w+)\sfrom\s'(.*?)'/ | |
variable, origin_image_path = caputers.first | |
similar_image_obj = data.select do |t| | |
t_file = t[:file] | |
t_qiniu_url = t[:qiniu_url] | |
if origin_image_path.nil? | |
puts line | |
puts variable, origin_image_path | |
end | |
origin_image_path.include?(t_file.gsub("src/", "")) | |
end | |
raise "查找图片文件错误" if similar_image_obj.size != 1 | |
puts variable, origin_image_path, similar_image_obj | |
line = "const #{variable} = '#{similar_image_obj.first[:qiniu_url]}'\n" | |
end | |
if line.include? "require" | |
caputers = line.scan /(require.*?\('.*?'\))/ | |
require_sentences = caputers.first | |
similar_image_obj = data.select do |t| | |
t_file = t[:file] | |
t_qiniu_url = t[:qiniu_url] | |
if require_sentences.nil? | |
puts line | |
puts caputers | |
end | |
require_sentences.any?{|sen| sen.include?(t_file.gsub("src/", ""))} | |
end | |
raise "查找图片文件错误" if similar_image_obj.size != 1 | |
require_sentences.each do |require_sentence| | |
line.gsub!(require_sentence, "\"#{similar_image_obj.first[:qiniu_url]}\"") | |
end | |
end | |
end | |
end | |
line | |
end | |
File.open(f, 'w') do |io| | |
lines.each do |line| | |
io.write line | |
end | |
end | |
end | |
end | |
# 如果需要上传图片到七牛云,通知 @冀宇航 | |
# global_replace(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment