Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Enhance 'rake test' tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dshevtsov committed Jan 6, 2022
1 parent 8e823a4 commit 3c177a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ desc "Same as 'rake', 'rake preview'"
task default: %w[preview]

desc "Same as 'test:report'"
task test: %w[test:md test:report]
task test: %w[test:md test:report test:unused_images test:unused_includes]

desc 'Preview the devdocs locally'
task preview: %w[install clean] do
Expand Down
49 changes: 31 additions & 18 deletions rakelib/test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ namespace :test do
puts 'No issues found'.green
end

desc 'Find unused images'
desc 'Find unused images. To exclude by regex pattern, use the "exclude_img" argument. Example of excluding a "layout" directory: "rake test:unused_includes exclude_img=/layout/"'
task :unused_images do
puts 'Running a task for finding unused images'.magenta
images = Dir['src/**/*.{png,svg,jpeg,jpg,ico}']
puts 'Running a task for finding unused images (png,svg,jpeg,jpg,ico)'.magenta
images = FileList['src/**/*.{png,svg,jpeg,jpg,ico}']

puts "The project contains a total of #{images.size} images."

exclude = ENV['exclude_img']
images.exclude(/#{exclude}/) if exclude

puts 'Checking for unlinked images...'
Dir['src/**/*.{md,html,js,css}'].each do |file|
# Exclude symmlinks
Expand All @@ -76,33 +81,41 @@ namespace :test do
images.delete_if { |image| File.read(file).include?(File.basename(image)) }
end

abort 'No unlinked images' if images.empty?

images.each do |image|
puts "No links for #{image}".yellow
if images.empty?
puts 'No unlinked images'.green
else
images.each do |image|
puts "No links for #{image}".yellow
end
puts "Found #{images.size} dangling images".red
end
puts "Found #{images.size} dangling images".red
end

desc 'Find unused includes'
desc 'Find unused includes. To exclude by regex pattern, use the "exclude_incl" argument. Example of excluding a "layout" directory: "rake test:unused_includes exclude_incl=/layout/"'
task :unused_includes do
puts 'Running a task to find unused _includes'.magenta
includes = Dir['src/_includes/**/*']
includes = FileList['src/_includes/**/*']

puts "The project contains a total of #{includes.size} includes"
puts 'The following includes are not linked:'

exclude = ENV['exclude_incl']
includes.exclude(/#{exclude}/) if exclude

Dir['src/**/*.{md,html}'].each do |file|
# Exclude symmlinks
next if File.symlink? file

includes.delete_if { |include| File.read(file).include?(File.basename(include)) }
end

abort 'No unlinked includes' if includes.empty?

includes.each do |include|
puts "No links for #{include}".yellow
if includes.empty?
puts 'No unlinked includes'.green
else
puts 'The following includes are not linked:'
includes.each do |include|
puts "No links for #{include}".yellow
end
puts "Found #{includes.size} unlinked includes".red
puts 'Be careful removing include files. Some include files, such as those in the layout/** directory, may not be linked in the project, but may be used implicitly by the doc theme.'.bold
end
puts "Found #{includes.size} unlinked includes".red
puts 'Be careful removing include files. Some include files, such as those in the layout/** directory, may not be linked in the project, but may be used implicitly by the doc theme.'.bold
end
end

0 comments on commit 3c177a3

Please sign in to comment.