Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: { ruby: ['3.1', '3.2', '3.3', '3.4'] }
matrix: { ruby: ['3.2', '3.3', '3.4'] }

steps:
- name: Check out code
Expand Down
5 changes: 3 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require:
plugins:
- rubocop-rspec
- rubocop-performance

Expand All @@ -9,10 +9,11 @@ inherit_gem:

AllCops:
SuggestExtensions: false
TargetRubyVersion: 3.1
TargetRubyVersion: 3.2
Exclude:
- debug.rb
- 'dev/**/*'
- 'tmp/**/*'
- 'spec/approvals/**/*'
- 'spec/tmp/**/*'
# This file contains errors on purpose
Expand Down
1 change: 1 addition & 0 deletions bin/victor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require 'victor/cli'
require 'pretty_trace/enable-trim'

include Colsole

PrettyTrace.filter [/gem/, /lib/, %r{bin/victor}, %r{bin/bundle}]
PrettyTrace.debug_tip

Expand Down
25 changes: 15 additions & 10 deletions lib/victor/cli/commands/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Commands
class Convert < Base
summary 'Convert SVG to Ruby code'

usage 'victor convert SVG_FILE [RUBY_FILE --template NAME]'
usage 'victor convert SVG_FILE [--save RUBY_FILE --template NAME]'
usage 'victor convert (-h|--help)'

option '-t, --template NAME', <<~USAGE
Expand All @@ -14,21 +14,15 @@ class Convert < Base
cli a Victor CLI compatible DSL script (default)
USAGE

param 'SVG_FILE', 'Input SVG file'
option '-o, --save RUBY_FILE', 'Save to RUBY file instead of printing to stdout'

param 'RUBY_FILE', 'Output Ruby file. Leave empty to write to stdout'

example 'victor convert example.svg example.rb'
example 'victor convert example.svg --save example.rb'

def run
svg_file = args['SVG_FILE']
template = args['--template']
svg_node = SVGNode.load_file svg_file, layout: template

validate_template template if template

code = svg_node.render
ruby_file = args['RUBY_FILE']

if ruby_file
File.write ruby_file, code
say "Saved #{ruby_file}"
Expand All @@ -37,6 +31,17 @@ def run
end
end

protected

def svg_file = args['SVG_FILE']
def template = args['--template']
def ruby_file = args['--save']

def svg_node = @svg_node ||= SVGNode.load_file(svg_file, layout: template)
def code = @code ||= svg_node.render

private

def validate_template(template)
allowed = %w[cli dsl standalone]
return if allowed.include? template
Expand Down
39 changes: 17 additions & 22 deletions lib/victor/cli/commands/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ module Victor
module CLI
module Commands
class Render < Base
using PairSplit

summary 'Render Ruby code to SVG'

usage 'victor render RUBY_FILE [SVG_FILE] [options]'
usage 'victor render RUBY_FILE [options] [PARAMS...]'
usage 'victor render (-h|--help)'

option '-t, --template TEMPLATE', <<~USAGE
Expand All @@ -15,28 +17,33 @@ class Render < Base
USAGE

option '-w, --watch', 'Watch the source file and regenerate on change'
option '-o, --save SVG_FILE', 'Save to SVG file instead of printing to stdout'

param 'RUBY_FILE', 'Input Ruby file'
param 'SVG_FILE', 'Output SVG file. Leave empty to write to stdout'
param 'PARAMS', 'One or more key=value pairs that will be available in the `params` hash for the Ruby script'

example 'victor render input.rb output.svg'
example 'victor render input.rb output.svg --watch'
example 'victor render input.rb -o output.svg'
example 'victor render input.rb --save output.svg --watch'
example 'victor render input.rb --template minimal'
example 'victor render input.rb color=black "text=Hello World"'

def run
if args['--watch']
watch_and_generate
else
generate
end
args['--watch'] ? watch_and_generate : generate
end

protected

def ruby_file = args['RUBY_FILE']
def svg_file = args['--save']
def template = args['--template']
def params = @params ||= args['PARAMS'].pair_split

private

def generate
code = File.read ruby_file

ruby_source = RubySource.new code, ruby_file
ruby_source = RubySource.new code, filename: ruby_file, params: params
ruby_source.evaluate
ruby_source.template template if template

Expand Down Expand Up @@ -68,18 +75,6 @@ def watch_and_generate
def file_watcher
@file_watcher ||= Filewatcher.new(ruby_file, immediate: true)
end

def ruby_file
args['RUBY_FILE']
end

def svg_file
args['SVG_FILE']
end

def template
args['--template']
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/victor/cli/refinements/pair_split.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Victor
module CLI
module PairSplit
refine Array do
def pair_split(operator = '=')
return {} if empty?

to_h { |pair| pair.split(operator, 2) }.transform_keys(&:to_sym)
end
end
end
end
end
10 changes: 7 additions & 3 deletions lib/victor/cli/ruby_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ module Victor
module CLI
class RubySource
include Victor::DSL
attr_reader :code, :filename

def initialize(code, filename = nil)
attr_reader :code, :filename, :global_params, :params

def initialize(code, filename: nil, params: nil)
@code = code
@filename = filename
@global_params = params || {}
@params = global_params
end

def evaluate
def evaluate(local_params = nil)
@params = local_params || global_params
if filename
instance_eval code, filename
else
Expand Down
10 changes: 5 additions & 5 deletions spec/approvals/cli/convert/help
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Convert SVG to Ruby code

Usage:
victor convert SVG_FILE [RUBY_FILE --template NAME]
victor convert SVG_FILE [--save RUBY_FILE --template NAME]
victor convert (-h|--help)

Options:
Expand All @@ -11,15 +11,15 @@ Options:
dsl a Victor DSL script
cli a Victor CLI compatible DSL script (default)

-o, --save RUBY_FILE
Save to RUBY file instead of printing to stdout

-h --help
Show this help

Parameters:
SVG_FILE
Input SVG file

RUBY_FILE
Output Ruby file. Leave empty to write to stdout

Examples:
victor convert example.svg example.rb
victor convert example.svg --save example.rb
2 changes: 1 addition & 1 deletion spec/approvals/cli/convert/usage
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Usage:
victor convert SVG_FILE [RUBY_FILE --template NAME]
victor convert SVG_FILE [--save RUBY_FILE --template NAME]
victor convert (-h|--help)
15 changes: 10 additions & 5 deletions spec/approvals/cli/render/help
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Render Ruby code to SVG

Usage:
victor render RUBY_FILE [SVG_FILE] [options]
victor render RUBY_FILE [options] [PARAMS...]
victor render (-h|--help)

Options:
Expand All @@ -12,17 +12,22 @@ Options:
-w, --watch
Watch the source file and regenerate on change

-o, --save SVG_FILE
Save to SVG file instead of printing to stdout

-h --help
Show this help

Parameters:
RUBY_FILE
Input Ruby file

SVG_FILE
Output SVG file. Leave empty to write to stdout
PARAMS
One or more key=value pairs that will be available in the `params` hash for
the Ruby script

Examples:
victor render input.rb output.svg
victor render input.rb output.svg --watch
victor render input.rb -o output.svg
victor render input.rb --save output.svg --watch
victor render input.rb --template minimal
victor render input.rb color=black "text=Hello World"
10 changes: 10 additions & 0 deletions spec/approvals/cli/render/svg-code-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion spec/approvals/cli/render/usage
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Usage:
victor render RUBY_FILE [SVG_FILE] [options]
victor render RUBY_FILE [options] [PARAMS...]
victor render (-h|--help)
10 changes: 6 additions & 4 deletions spec/fixtures/render/pacman_dsl.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
color = params[:color] || :yellow

setup width: '140', height: '100', style: 'background:#ddd'

build do
rect x: '10', y: '10', width: '120', height: '80', rx: '10', fill: '#666'
circle cx: '50', cy: '50', r: '30', fill: 'yellow'
circle cx: '50', cy: '50', r: '30', fill: color
circle cx: '58', cy: '32', r: '4', fill: 'black'
polygon points: '45,50 80,30 80,70', fill: '#666'
circle cx: '80', cy: '50', r: '4', fill: 'yellow'
circle cx: '98', cy: '50', r: '4', fill: 'yellow'
circle cx: '116', cy: '50', r: '4', fill: 'yellow'
circle cx: '80', cy: '50', r: '4', fill: color
circle cx: '98', cy: '50', r: '4', fill: color
circle cx: '116', cy: '50', r: '4', fill: color
end
6 changes: 3 additions & 3 deletions spec/victor-cli/commands/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

context 'with SVG_FILE' do
it 'outputs the converted ruby code to stdout' do
expect { subject.run ['convert', svg_file] }.to output_approval('cli/convert/ruby-code.rb')
expect { subject.run %W[convert #{svg_file}] }.to output_approval('cli/convert/ruby-code.rb')
end
end

context 'with SVG_FILE RUBY_FILE' do
context 'with SVG_FILE --save RUBY_FILE' do
let(:ruby_file) { 'spec/tmp/code.rb' }

before { File.unlink ruby_file if File.exist? ruby_file }

it 'saves the converted ruby code' do
expect { subject.run ['convert', svg_file, ruby_file] }.to output_approval('cli/convert/save')
expect { subject.run %W[convert #{svg_file} --save #{ruby_file}] }.to output_approval('cli/convert/save')
expect(File.read ruby_file).to match_approval('cli/convert/ruby-code.rb')
end
end
Expand Down
16 changes: 13 additions & 3 deletions spec/victor-cli/commands/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

context 'with RUBY_FILE' do
it 'outputs the converted SVG code to stdout' do
expect { subject.execute ['render', ruby_file] }.to output_approval('cli/render/svg-code.svg')
expect { subject.execute %W[render #{ruby_file}] }
.to output_approval('cli/render/svg-code.svg')
end
end

context 'with RUBY_FILE PARAMS...' do
it 'passes param pairs to the DSL' do
expect { subject.execute %W[render #{ruby_file} color=blue] }
.to output_approval('cli/render/svg-code-blue.svg')
end
end

Expand Down Expand Up @@ -46,13 +54,15 @@
end
end

context 'with RUBY_FILE SVG_FILE' do
context 'with RUBY_FILE --save SVG_FILE' do
let(:svg_file) { 'spec/tmp/svg.svg' }

before { File.unlink svg_file if File.exist? svg_file }

it 'saves the converted SVG code' do
expect { subject.execute ['render', ruby_file, svg_file] }.to output_approval('cli/render/save')
expect { subject.execute %W[render #{ruby_file} --save #{svg_file}] }
.to output_approval('cli/render/save')

expect(File.read(svg_file)).to match_approval('cli/render/svg-code.svg')
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/victor-cli/refinements/pair_split_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe PairSplit do
using described_class

subject { ['color=blue', 'text=Hello World'] }

describe Array do
describe '#pair_split' do
it 'splits key=value pairs and returns a hash' do
expect(subject.pair_split).to eq({ color: 'blue', text: 'Hello World' })
end
end
end
end
2 changes: 1 addition & 1 deletion spec/victor-cli/ruby_source_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe RubySource do
subject { described_class.new code, filename }
subject { described_class.new code, filename: filename }

let(:code) { "puts 'hello'" }
let(:filename) { nil }
Expand Down
4 changes: 2 additions & 2 deletions victor-cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.executables = ['victor']
s.homepage = 'https://github.com/dannyben/victor-cli'
s.license = 'MIT'
s.required_ruby_version = '>= 3.1'
s.required_ruby_version = '>= 3.2'

s.add_dependency 'colsole', '~> 1.0'
s.add_dependency 'css_parser', '~> 1.7'
Expand All @@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.add_dependency 'requires', '~> 1.0'
s.add_dependency 'rufo', '~> 0.12'
s.add_dependency 'victor', '~> 0.4'

# FIXME: Remove when resolved.
# This is a sub-dependency of filewatcher which does not bundle logger.
# ref: https://github.com/filewatcher/filewatcher/pull/272
Expand Down