Skip to content

Commit

Permalink
feat: Content traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier-IV committed Sep 8, 2023
1 parent 047c579 commit 7985bf6
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem 'dry-cli', '~> 1.0'

gem 'bump', '~> 0.10.0'

gem 'awesome_print', '~> 1.9'

group :development, :test do
gem 'minitest', '~> 5.19'
gem 'mocha', '~> 2.1'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
awesome_print (1.9.2)
base64 (0.1.1)
bump (0.10.0)
colorize (1.1.0)
Expand Down Expand Up @@ -45,6 +46,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
awesome_print (~> 1.9)
bump (~> 0.10.0)
colorize (~> 1.1)
dry-cli (~> 1.0)
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ task default: :test

desc 'Run the Windclutter executable'
task :windclutter, [:args] do
args = ARGV[1..] # Capture all arguments after the task name
ARGV.delete('--')
args = ARGV[1..]

begin
cmd = "ruby -Ilib ./bin/windclutter #{args.join(' ')}"
sh cmd
Expand Down
23 changes: 18 additions & 5 deletions lib/windclutter/analyser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require 'windclutter/util/collector'
require 'windclutter/util/config'
require 'windclutter/util/sorter'
require 'windclutter/util/file_handler'

module WindClutter
# Analyser for windclutter
Expand All @@ -21,12 +24,22 @@ def self.init(content)
content.scan(regex).each do |occurrence|
cls = occurrence.to_s.match(occurrence_regex)[1].split(' ')

cls.each do |c|
total = collections[c].nil? ? 0 : collections[c]
collections[c] = total + 1
end
cls.each { |k| collect(collections, k, 1) }
end
collections.sort_by { |_, v| -v }.to_h
sorter(collections).to_h
end

def self.traverse(suffix, limit)
collection = {}
scanned = FileHandler.scanners(suffix)
scanned.each do |file|
init(File.open(file).read).each { |k, v| collect(collection, k, v) }
end

sorted = sorter(collection)
result = limit.positive? ? sorted.to_h : sorted.first(limit).to_h

[sorted.count, result, scanned.count]
end
end
end
40 changes: 35 additions & 5 deletions lib/windclutter/cli/commands/analysis.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require 'awesome_print'
require 'dry/cli'
require 'windclutter/analyser'
require 'windclutter/util/file_handler'
require 'windclutter/util/collector'
require 'windclutter/util/sorter'
require 'windclutter/cli/commands/project'
require 'windclutter/cli/commands/generate'

Expand All @@ -12,17 +15,44 @@ module Commands
module Analysis
# Initiate setup for specified project
class FilePath < Dry::CLI::Command
include WindClutter
include WindClutter::Util

desc 'Perform CSS analysis of the path'
argument :path, aliases: ['-p'], required: true, desc: 'Path of your CSS file to be dump.'
argument :file, aliases: ['-f'], required: true, desc: 'Path of your CSS file to be dump.'

def call(path:, **)
puts "Analysing #{path}...".green
def call(file:, **)
return puts "No file found #{file}".red unless File.file?(file)

file = File.open(FileHandler.scan_one(path))
puts "Analysing #{file}...".yellow

content = File.open(FileHandler.scan_one(file))
puts 'Done!'.green
puts WindClutter::Analyser.init(file.read)
ap Analyser.init(content.read)
end
end

# Perform full traversal analysis
class Traverse < Dry::CLI::Command
include WindClutter
include WindClutter::Util

desc 'Perform full traversal analysis'

argument :suffix, aliases: ['-s'], required: true, desc: 'Suffix of all files to be traversed.'

option :full, type: :boolean, alias: '-f', default: false, desc: 'Print out whole classes.'

option :collect, type: :integer, alias: '-c', default: 5, desc: 'Specify how many of result to collect.'

def call(suffix:, **options)
collect_count = options.fetch(:collect).to_i
puts "Analysing #{suffix}...".yellow

total, scanned, file_count = Analyser.traverse(suffix, collect_count)
puts "Traversed #{file_count} #{suffix} file(s)... 🎉".green
ap scanned
puts "...and #{total - collect_count} more".yellow if total > collect_count
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/windclutter/cli/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def call(*)

register 'analysis', aliases: %w[a -a] do |prefix|
prefix.register 'file', Commands::Analysis::FilePath, aliases: ['-p']
prefix.register 'traverse', Commands::Analysis::Traverse, aliases: ['-t']
end

register 'generate', aliases: ['g'] do |prefix|
Expand Down
6 changes: 6 additions & 0 deletions lib/windclutter/util/collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

def collect(collection, key, value)
collection[key] ||= 0
collection[key] = collection[key] + value
end
2 changes: 1 addition & 1 deletion lib/windclutter/util/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.list_projects
end

def self.scanners(extension)
Dir["#{File.dirname(__FILE__)}/**/*#{extension}"]
Dir["#{Dir.pwd}/**/*#{extension}"]
end

def self.scan_one(path)
Expand Down
5 changes: 5 additions & 0 deletions lib/windclutter/util/sorter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

def sorter(collection)
collection.sort_by { |_, v| -v }
end

0 comments on commit 7985bf6

Please sign in to comment.