Skip to content

Commit

Permalink
Merge pull request #62 from dkarter/feature/specify_output_path
Browse files Browse the repository at this point in the history
Feature/specify output path
  • Loading branch information
makaroni4 committed May 5, 2015
2 parents 73abf8c + 2074fc7 commit 1751ea8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ gemspec

group :test do
gem 'rspec'
gem 'pry' # standard debugger
gem 'fakefs', require: 'fakefs/safe' # stubs out FS to avoid creating files during testing
end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sandi_meter --help
--json Output as JSON
-l, --log Show syntax error and indentation log output
-p, --path PATH Path to folder or file to analyze (default is ".")
-o, --output_path PATH Path to save output files (default is "./sandi_meter")
-r, --rules Show rules
-h, --help Help
Expand Down
28 changes: 17 additions & 11 deletions lib/sandi_meter/cli.rb
Original file line number Diff line number Diff line change
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 @@ -77,13 +82,14 @@ class << self
def execute
cli = CommandParser.new
cli.parse_options

cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))

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)
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 +114,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 +132,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
42 changes: 42 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
require_relative '../lib/sandi_meter/cli'

describe SandiMeter::CLI do
include FakeFS::SpecHelpers

let(:cli) { SandiMeter::CLI }
let(:gem_root) { File.expand_path('../', File.dirname(__FILE__)) }

before do
FakeFS.activate!

FakeFS::FileSystem.clone(gem_root)
end

after do
FakeFS.deactivate!
end

describe '#execute' do
before do
@original_argv = ARGV
Expand Down Expand Up @@ -40,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
17 changes: 17 additions & 0 deletions spec/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
require 'pry'
require 'rspec/autorun'
require 'ripper'
require 'fakefs/spec_helpers'

# silence CLI output
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do
# Redirect stderr and stdout
$stderr = File.open(File::NULL, "w")
$stdout = File.open(File::NULL, "w")
end
config.after(:all) do
$stderr = original_stderr
$stdout = original_stdout
end
end

Dir["#{Dir.pwd}/spec/support/**/*.rb"].each { |f| require f }

0 comments on commit 1751ea8

Please sign in to comment.