Skip to content

Commit

Permalink
Escape CLI Command Strings
Browse files Browse the repository at this point in the history
Closes [#156].

Use `Cocaine` to generate properly escaped CLI commands.

[#156]: #156
  • Loading branch information
seanpdoyle committed Nov 20, 2015
1 parent 1477ccb commit e4acc18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
master
------

* Escape generated CLI strings. Adds support for paths with spaces.

0.5.3
-----

Expand Down
1 change: 1 addition & 0 deletions ember-cli-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Gem::Specification.new do |spec|
spec.add_dependency "railties", ">= 3.2", "< 5"
spec.add_dependency "non-stupid-digest-assets", "~> 1.0.0"
spec.add_dependency "sprockets", ">= 2.0"
spec.add_dependency "cocaine", "~> 0.5.0"
end
58 changes: 30 additions & 28 deletions lib/ember_cli/command.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "cocaine"

module EmberCli
class Command
def initialize(paths:, options: {})
Expand All @@ -6,50 +8,50 @@ def initialize(paths:, options: {})
end

def test
"#{paths.ember} test --environment test"
line = Cocaine::CommandLine.new(paths.ember, "test --environment test")

line.command
end

def build(watch: false)
[
"#{paths.ember} build",
"#{watch_flag(watch)}",
"--environment #{build_environment}",
"--output-path #{paths.dist}",
pipe_errors_to_file,
pipe_to_log_files,
].join(" ")
build_command(watch: watch),
pipe_to_logs_command,
].compact.join(" | ")
end

private

attr_reader :options, :paths

def process_watcher
options.fetch(:watcher) { EmberCli.configuration.watcher }
end

def watch_flag(watch)
watch_flag = ""
def pipe_to_logs_command
unless paths.tee.nil?
line = Cocaine::CommandLine.new(paths.tee, "-a :log_file")

if watch
watch_flag = "--watch"

if process_watcher
watch_flag += " --watcher #{process_watcher}"
end
line.command(log_file: paths.log)
end

watch_flag
end

def pipe_errors_to_file
"2> #{paths.build_error_file}"
def build_command(watch: false)
line = Cocaine::CommandLine.new(paths.ember, [
"build",
("--watch" if watch),
("--watcher :watcher" if process_watcher),
"--environment :environment",
"--output-path :output_path",
"2> :error_file",
].compact.join(" "))

line.command(
environment: build_environment,
output_path: paths.dist,
watcher: process_watcher,
error_file: paths.build_error_file,
)
end

def pipe_to_log_files
if paths.tee
"| #{paths.tee} -a #{paths.log}"
end
def process_watcher
options.fetch(:watcher) { EmberCli.configuration.watcher }
end

def build_environment
Expand Down
18 changes: 9 additions & 9 deletions spec/lib/ember_cli/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
paths = build_paths(ember: "path/to/ember")
command = build_command(paths: paths)

expect(command.test).to eq("path/to/ember test --environment test")
expect(command.test).to eq("path\/to\/ember test --environment test")
end
end

Expand All @@ -15,7 +15,7 @@
paths = build_paths(ember: "path/to/ember")
command = build_command(paths: paths)

expect(command.build).to match(%{path/to/ember build})
expect(command.build).to match(%{path\/to\/ember build})
end

context "when building in production" do
Expand All @@ -24,7 +24,7 @@
command = build_command(paths: paths)
allow(EmberCli).to receive(:env).and_return("production")

expect(command.build).to match(/--environment production/)
expect(command.build).to match(/--environment 'production'/)
end
end

Expand All @@ -34,30 +34,30 @@
command = build_command(paths: paths)
allow(EmberCli).to receive(:env).and_return("test")

expect(command.build).to match(/--environment development/)
expect(command.build).to match(/--environment 'development'/)
end
end

it "includes the `--output-path` flag" do
paths = build_paths(dist: "path/to/dist")
command = build_command(paths: paths)

expect(command.build).to match(%r{--output-path path/to/dist})
expect(command.build).to match(%r{--output-path 'path\/to\/dist'})
end

it "redirects STDERR to the build error file" do
paths = build_paths(build_error_file: "path/to/errors.txt")
command = build_command(paths: paths)

expect(command.build).to match(%r{2> path/to/errors\.txt})
expect(command.build).to match(%r{2> 'path/to/errors\.txt'})
end

context "when `tee` command exists" do
it "uses `tee` to pipe to log files" do
paths = build_paths(tee: "path/to/tee", log: "path/to/logs")
command = build_command(paths: paths)

expect(command.build).to match(%r{| path/to/tee -a path/to/logs})
expect(command.build).to match(%r{\| path/to/tee -a 'path/to/logs'})
end
end

Expand Down Expand Up @@ -93,15 +93,15 @@
to receive(:configuration).
and_return(build_paths(watcher: "bar"))

expect(command.build(watch: true)).to match(/--watcher bar/)
expect(command.build(watch: true)).to match(/--watcher 'bar'/)
end

context "when a watcher is configured" do
it "configures the build with the value" do
paths = build_paths
command = build_command(paths: paths, options: { watcher: "foo" })

expect(command.build(watch: true)).to match(/--watcher foo/)
expect(command.build(watch: true)).to match(/--watcher 'foo'/)
end
end
end
Expand Down

0 comments on commit e4acc18

Please sign in to comment.