Skip to content

Commit

Permalink
add output path to CLI and update all over the code
Browse files Browse the repository at this point in the history
includes relevant tests
  • Loading branch information
Dorian Karter committed Apr 8, 2015
1 parent 09b53c7 commit 2f7ffdc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
27 changes: 16 additions & 11 deletions lib/sandi_meter/cli.rb
Expand Up @@ -21,6 +21,11 @@ class CommandParser
description: "Path to folder or file to analyze",
default: "."

option :output_path,
short: "-o PATH",
long: "--output-path PATH",
description: "Path for storing generated output files (default: ./sandi_meter/)"

option :log,
short: "-l",
long: "--log",
Expand Down Expand Up @@ -79,11 +84,11 @@ def execute
cli.parse_options

if cli.config[:graph]
log_dir_path = File.join(cli.config[:path], 'sandi_meter')
FileUtils.mkdir(log_dir_path) unless Dir.exists?(log_dir_path)
cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))
FileUtils.mkdir_p(cli.config[:output_path]) unless Dir.exists?(cli.config[:output_path])

create_config_file(cli.config[:path], 'sandi_meter/.sandi_meter', %w(db vendor).join("\n"))
create_config_file(cli.config[:path], 'sandi_meter/config.yml', YAML.dump({ threshold: 90 }))
create_config_file(cli.config[:output_path], '.sandi_meter', %w(db vendor).join("\n"))
create_config_file(cli.config[:output_path], 'config.yml', YAML.dump({ threshold: 90 }))
end

if cli.config[:version]
Expand All @@ -108,16 +113,16 @@ def execute
formatter.print_data(data)

if cli.config[:graph]
if File.directory?(cli.config[:path])
if File.directory?(cli.config[:output_path])
logger = SandiMeter::Logger.new(data)
logger.log!(cli.config[:path])
logger.log!(cli.config[:output_path])

html_generator = SandiMeter::HtmlGenerator.new
html_generator.copy_assets!(cli.config[:path])
html_generator.generate_data!(cli.config[:path])
html_generator.generate_details!(cli.config[:path], data)
html_generator.copy_assets!(cli.config[:output_path])
html_generator.generate_data!(cli.config[:output_path])
html_generator.generate_details!(cli.config[:output_path], data)

index_html_path = File.join(cli.config[:path], 'sandi_meter/index.html')
index_html_path = File.join(cli.config[:output_path], 'index.html')
unless cli.config[:quiet]
open_in_browser(index_html_path)
end
Expand All @@ -126,7 +131,7 @@ def execute
end
end

config_file_path = File.join(cli.config[:path], 'sandi_meter', 'config.yml')
config_file_path = File.join(cli.config[:output_path], 'config.yml')
config = if File.exists?(config_file_path)
YAML.load(File.read(config_file_path))
else
Expand Down
14 changes: 7 additions & 7 deletions lib/sandi_meter/html_generator.rb
Expand Up @@ -4,19 +4,19 @@
module SandiMeter
class HtmlGenerator
def copy_assets!(path)
asset_dir_path = File.join(path, 'sandi_meter/assets')
asset_dir_path = File.join(path, 'assets')
FileUtils.mkdir(asset_dir_path) unless Dir.exists?(asset_dir_path)
html_dir = File.expand_path('../../html', File.dirname(__FILE__))


Dir[File.join(File.dirname(__FILE__), "../../html/*.{js,css,png}")].each do |file|
Dir[File.join(html_dir, "*.{js,css,png}")].each do |file|
FileUtils.cp file, File.join(asset_dir_path, File.basename(file))
end

FileUtils.cp File.join(File.dirname(__FILE__), "../../html", "index.html"), File.join(path, 'sandi_meter', 'index.html')
FileUtils.cp File.join(html_dir, 'index.html'), File.join(path, 'index.html')
end

def generate_data!(path)
raw_data = File.read(File.join(path, 'sandi_meter', 'sandi_meter.log')).split("\n")
raw_data = File.read(File.join(path, 'sandi_meter.log')).split("\n")
raw_data.map! { |row| row.split(';').map(&:to_i) }

data = []
Expand All @@ -31,7 +31,7 @@ def generate_data!(path)
data << hash
end

index_file = File.join(path, 'sandi_meter', 'index.html')
index_file = File.join(path, 'index.html')
index = File.read(index_file)
index.gsub!('<% plot_data %>', data.to_json)

Expand Down Expand Up @@ -91,7 +91,7 @@ def generate_details!(path, data)
)
end

index_file = File.join(path, 'sandi_meter', 'index.html')
index_file = File.join(path, 'index.html')
index = File.read(index_file)
index.gsub!('<% details %>', details)

Expand Down
2 changes: 1 addition & 1 deletion lib/sandi_meter/logger.rb
Expand Up @@ -3,7 +3,7 @@
module SandiMeter
class Logger < Struct.new(:data)
def log!(path)
File.open(File.join(path, 'sandi_meter', 'sandi_meter.log'), 'a') do |file|
File.open(File.join(path, 'sandi_meter.log'), 'a') do |file|
file.puts(log_line)
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/cli_spec.rb
Expand Up @@ -53,5 +53,34 @@
expect { cli.execute }.to raise_error(SystemExit)
end
end

context 'output path passed in' do
let(:test_path) { '/test_out_dir/test2' }
before do
ARGV.push('-q')
ARGV.push('-g')
ARGV.push('-o')
ARGV.push(test_path)
end

it 'saves output files to specified output path' do
expect { cli.execute }.to raise_error(SystemExit)
expect(File.directory?(test_path)).to eq(true)
end
end

context 'output path not specified' do
before do
ARGV.push('-q')
ARGV.push('-g')
ARGV.push('-p')
ARGV.push('/')
end

it 'saves output files in sandi_meter folder relative to scanned path' do
expect { cli.execute }.to raise_error(SystemExit)
expect(File.directory?(File.expand_path('/sandi_meter'))).to eq(true)
end
end
end
end

0 comments on commit 2f7ffdc

Please sign in to comment.