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

Commit

Permalink
Refactor command classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Aug 2, 2019
1 parent 7d824a9 commit 02f420c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 41 deletions.
42 changes: 31 additions & 11 deletions lib/svgeez/command.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
module Svgeez
class Command
def self.subclasses
@subclasses ||= []
end
class << self
def subclasses
@subclasses ||= []
end

def self.inherited(base)
subclasses << base
super(base)
end
def inherited(base)
subclasses << base
super(base)
end

def init_with_program(program)
program.command(name.downcase.to_sym) do |command|
command.description command_description
command.syntax command_syntax

add_actions(command)
add_options(command)
end
end

private

def add_actions(command)
command.action do |_, options|
command_action(options)
end
end

def self.add_build_options(command)
command.option 'source', '-s', '--source [FOLDER]', 'Source folder (defaults to ./_svgeez)'
command.option 'destination', '-d', '--destination [OUTPUT]', 'Destination file or folder (defaults to ./svgeez.svg)'
command.option 'svgo', '--with-svgo', 'Optimize source SVGs with SVGO before sprite generation (non-destructive)'
def add_options(command)
command.option 'source', '-s', '--source [FOLDER]', 'Source folder (defaults to ./_svgeez)'
command.option 'destination', '-d', '--destination [OUTPUT]', 'Destination file or folder (defaults to ./svgeez.svg)'
command.option 'svgo', '--with-svgo', 'Optimize source SVGs with SVGO before sprite generation (non-destructive)'
end
end
end
end
25 changes: 14 additions & 11 deletions lib/svgeez/commands/build.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
module Svgeez
module Commands
class Build < Command
def self.init_with_program(program)
program.command(:build) do |command|
command.description 'Builds an SVG sprite from a folder of SVG icons'
command.syntax 'build [options]'
class << self
def process(options)
Svgeez::Builder.new(options).build
end

add_build_options(command)
private

command.action do |_, options|
Build.process(options)
end
def command_action(options)
Build.process(options)
end
end

def self.process(options)
Svgeez::Builder.new(options).build
def command_description
'Builds an SVG sprite from a folder of SVG icons'
end

def command_syntax
'build [options]'
end
end
end
end
Expand Down
40 changes: 21 additions & 19 deletions lib/svgeez/commands/watch.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module Svgeez
module Commands
class Watch < Command
def self.init_with_program(program)
program.command(:watch) do |command|
command.description 'Watches a folder of SVG icons for changes'
command.syntax 'watch [options]'
class << self
def process(options)
builder = Svgeez::Builder.new(options)
folder_path = builder.source.folder_path

add_build_options(command)
listener = Listen.to(folder_path, only: /\.svg\z/) { builder.build }

command.action do |_, options|
Build.process(options)
Watch.process(options)
end
Svgeez.logger.info "Watching `#{folder_path}` for changes... Press ctrl-c to stop."

listener.start
sleep
rescue Interrupt
Svgeez.logger.info 'Quitting svgeez...'
end
end

def self.process(options)
builder = Svgeez::Builder.new(options)
private

listener = Listen.to(builder.source.folder_path, only: /\.svg\z/) do
builder.build
def command_action(options)
Build.process(options)
Watch.process(options)
end

Svgeez.logger.info "Watching `#{builder.source.folder_path}` for changes... Press ctrl-c to stop."
def command_description
'Watches a folder of SVG icons for changes'
end

listener.start
sleep
rescue Interrupt
Svgeez.logger.info 'Quitting svgeez...'
def command_syntax
'watch [options]'
end
end
end
end
Expand Down

0 comments on commit 02f420c

Please sign in to comment.