Skip to content

Commit

Permalink
feat: Added specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier-IV committed Sep 7, 2023
1 parent 7301424 commit 7f8321d
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 26 deletions.
Empty file added .rubocop.yml
Empty file.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ group :development, :test do
end

gem 'dry-cli', '~> 1.0'

gem 'mocha', '~> 2.1'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ GEM
json (2.6.3)
language_server-protocol (3.17.0.3)
minitest (5.20.0)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand All @@ -33,6 +35,7 @@ GEM
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
unicode-display_width (2.4.2)
yaml (0.2.1)

Expand All @@ -45,6 +48,7 @@ DEPENDENCIES
dry-cli (~> 1.0)
fileutils (~> 1.7)
minitest (~> 5.19)
mocha (~> 2.1)
rake (~> 13.0)
rubocop (~> 1.56)
yaml (~> 0.2.1)
Expand Down
25 changes: 25 additions & 0 deletions lib/windclutter/analyser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
# frozen_string_literal: true

require 'windclutter/util/config'

module WindClutter
# Analyser for windclutter
class Analyser
include WindClutter::Util

def self.init(content)
active_project = Config.read('active_project')

class_key = Config.read_project(active_project, 'class_key')
class_start = Config.read_project(active_project, 'class_start')
class_end = Config.read_project(active_project, 'class_end')

regex = /#{class_key}=#{class_start}(?:(?!windclutter:)[^#{class_end}])*#{class_end}/
occurrence_regex = /#{class_key}=#{class_start}([^#{class_end}]*)#{class_end}/

collections = {}
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
end
collections.sort_by { |_, v| -v }.to_h
end
end
end
31 changes: 31 additions & 0 deletions lib/windclutter/cli/commands/analysis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

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

module WindClutter
module CLI
module Commands
module Analysis
# Initiate setup for specified project
class FilePath < Dry::CLI::Command
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.'

def call(path:, **)
puts "Analysing #{path}...".green

file = File.open(FileHandler.scan_one(path))
puts 'Done!'.green
puts WindClutter::Analyser.init(file.read)
end
end
end
end
end
end
21 changes: 20 additions & 1 deletion lib/windclutter/cli/commands/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Current < Dry::CLI::Command
def call(**)
project_name = Config.read('active_project')

print "Currently using: ".green
print 'Currently using: '.green
puts project_name
end
end
Expand All @@ -86,6 +86,25 @@ def call(path:, **)
Config.update_project(active_project, 'dump_path', dump_path)
end
end

# Update config
class ConfigUpdate < Dry::CLI::Command
include WindClutter::Util

desc 'Update the config for your project'

argument :key, required: true, desc: 'Key of your config.'
argument :value, required: true, desc: 'Value of your config.'

def call(key:, value:, **)
active_project = Config.read('active_project')
return puts 'No project specified. Select a project with `use` command.'.yellow if active_project.nil?

puts "Updating #{key}:#{value}".green

Config.update_project(active_project, key, value)
end
end
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/windclutter/cli/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'windclutter/util/file_handler'
require 'windclutter/cli/commands/project'
require 'windclutter/cli/commands/generate'
require 'windclutter/cli/commands/analysis'

module WindClutter
module CLI
Expand Down Expand Up @@ -49,7 +50,7 @@ class Debug < Dry::CLI::Command
desc 'Debug the configuration of windclutter'

def call(*)
Config.wtf?
puts Config.wtf?
end
end

Expand All @@ -64,6 +65,11 @@ def call(*)
prefix.register 'use', Commands::Project::Use
prefix.register 'current', Commands::Project::Current
prefix.register 'dump-path', Commands::Project::DumpPath, aliases: ['-d']
prefix.register 'config', Commands::Project::ConfigUpdate, aliases: ['-c']
end

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

register 'generate', aliases: ['g'] do |prefix|
Expand Down
27 changes: 18 additions & 9 deletions lib/windclutter/util/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Util
# Config handler
class Config
def self.update(key, value)
return unless config_exists?
return unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')
config[key] = value
Expand All @@ -18,20 +18,23 @@ def self.update(key, value)
end

def self.setup_project(name)
return unless config_exists?
return unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')

config['projects'][name] = {
'project_path' => nil,
'dump_path' => nil
'dump_path' => nil,
'class_key' => 'class',
'class_start' => '"',
'class_end' => '"'
}

File.open('/tmp/windclutter/config.yml', 'w') { |f| YAML.dump(config, f) }
end

def self.update_project(name, key, value)
return unless config_exists?
return unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')
if config['projects'][name].nil?
Expand All @@ -45,21 +48,27 @@ def self.update_project(name, key, value)
File.open('/tmp/windclutter/config.yml', 'w') { |f| YAML.dump(config, f) }
end

def self.read_project(name, key)
return unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')
config['projects'][name][key]
end

def self.read(key)
return unless config_exists?
return unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')
config[key]
end

def self.wtf?
return puts 'No config found.'.red unless config_exists?
return puts 'No config found.'.red unless exists?

config = YAML.load_file('/tmp/windclutter/config.yml')
puts config
YAML.load_file('/tmp/windclutter/config.yml')
end

def self.config_exists?
def self.exists?
unless File.file?('/tmp/windclutter/config.yml')
puts 'You have not install windclutter yet'.yellow
puts 'To install, run:'
Expand Down
6 changes: 5 additions & 1 deletion lib/windclutter/util/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.init_config
FileUtils.cp(file, '/tmp/windclutter')
end

Dir["/tmp/windclutter/*"].each do |file|
Dir['/tmp/windclutter/*'].each do |file|
puts "\t#{file}"
end

Expand Down Expand Up @@ -49,6 +49,10 @@ def self.scanners(extension)
Dir["#{File.dirname(__FILE__)}/**/*#{extension}"]
end

def self.scan_one(path)
File.expand_path(path, Dir.pwd)
end

def self.overwrite(file, content)
File.open(file, 'w') { |t| t.puts content }
end
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/config_fixture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

def config_fixture
{ 'projects' => { 'test_project' => { 'class_key' => 'class', 'class_start' => '"', 'class_end' => '"' } } }
end
3 changes: 3 additions & 0 deletions test/test_windclutter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'minitest/autorun'
require 'minitest/mock'
require 'mocha/minitest'

Dir['test/fixtures/**/*.rb'].sort.each { |f| require f.split('/')[1..].join('/') }
Dir['test/windclutter/**/*.rb'].sort.each { |f| require f.split('/')[1..].join('/') }
32 changes: 32 additions & 0 deletions test/windclutter/test_analyser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'windclutter/analyser'
require 'windclutter/util/config'

class WindClutterAnalyserTest < Minitest::Test
include WindClutter
include WindClutter::Util
def test_able_to_analyse_content
Config.expects(:read).returns('test_project')
File.stubs(:file?).with('/tmp/windclutter/config.yml').returns(true)
Config.any_instance.stubs(:exists?).returns(true)
YAML.stubs(:load_file).with('/tmp/windclutter/config.yml').returns(config_fixture)

# Config.expects(:read_project).with('test_project', 'class_key').returns('class')
assert_equal Analyser.init(dummy_content), expected_output
end

private

def dummy_content
<<~CONTENT
<div class="flex flex-col items-center gap-6 px-10 py-40 overflow-y-scroll">
</div>
CONTENT
end

def expected_output
{ 'flex' => 1, 'flex-col' => 1, 'items-center' => 1, 'gap-6' => 1, 'px-10' => 1, 'py-40' => 1,
'overflow-y-scroll' => 1 }
end
end
25 changes: 13 additions & 12 deletions test/windclutter/test_processor.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# frozen_string_literal: true

require 'minitest/autorun'
require 'windclutter/processor'

class WindClutterProcessorTest < Minitest::Test
include WindClutter::Util
include WindClutter

def test_able_to_generate_block
result = WindClutter::Processor.build_single('class', %w[container mx-auto])
result = Processor.build_single('class', %w[container mx-auto])

assert_equal ".class {\n @apply container mx-auto;\n}\n",
result
end

def test_able_to_auto_process
WindClutter::Util::Generator.stub(:random_class, 'windclutter:mocked_value') do
collections = []
result = WindClutter::Processor.auto_process(dummy_content, collections)

assert_equal result, expected_output
assert_equal collections[0][:generated_name], 'windclutter:mocked_value'
assert_equal collections[0][:class], 'flex flex-col items-center gap-6 px-10 py-40 overflow-y-scroll'
assert_nil collections[0][:provided_name]
assert_equal collections[0][:named], false
end
Generator.stubs(:random_class).returns('windclutter:mocked_value')
collections = []
result = Processor.auto_process(dummy_content, collections)

assert_equal result, expected_output
assert_equal collections[0][:generated_name], 'windclutter:mocked_value'
assert_equal collections[0][:class], 'flex flex-col items-center gap-6 px-10 py-40 overflow-y-scroll'
assert_nil collections[0][:provided_name]
assert_equal collections[0][:named], false
end

private
Expand Down
Loading

0 comments on commit 7f8321d

Please sign in to comment.