From 05da17c4d42dd7fa3f954409e021b718051930d3 Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Wed, 10 Apr 2019 14:20:22 +0800 Subject: [PATCH 1/8] First iteration of a `gel open [gem-name]` CLI command --- lib/gel/command.rb | 1 + lib/gel/command/open.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/gel/command/open.rb diff --git a/lib/gel/command.rb b/lib/gel/command.rb index 4f49c0f..a085e8c 100644 --- a/lib/gel/command.rb +++ b/lib/gel/command.rb @@ -81,3 +81,4 @@ def self.extract_word(arguments) require_relative "command/stub" require_relative "command/config" require_relative "command/shell_setup" +require_relative "command/open" diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb new file mode 100644 index 0000000..738f524 --- /dev/null +++ b/lib/gel/command/open.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class Gel::Command::Open < Gel::Command + require "shellwords" + + def run(command_line) + raise "Please provide the name of a gem to open in your editor" if command_line.empty? + raise "Too many arguments, only 1 gem name is supported" if command_line.length > 1 + + editor = ENV.fetch("GEL_EDITOR", ENV["EDITOR"]) + raise "An editor must be set using either $GEL_EDITOR or $EDITOR" unless editor + + found_gem = Gel::Environment.find_gem(command_line.first) + raise "Can't find gem `#{command_line.first}`" unless found_gem + + command = [*Shellwords.split(editor), found_gem.root] + system(*command) + end +end From 5a8997d7e05232ffd480832104e23eb7c8577a33 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sun, 14 Apr 2019 12:21:21 +0800 Subject: [PATCH 2/8] Handle the case where we can't open an $EDITOR Co-Authored-By: keithpitt --- lib/gel/command/open.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index 738f524..af6ca99 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -14,6 +14,6 @@ def run(command_line) raise "Can't find gem `#{command_line.first}`" unless found_gem command = [*Shellwords.split(editor), found_gem.root] - system(*command) + system(*command) || exit($?.exitstatus) end end From 17674788d6802cf5da62e5ed29c92ff591d9bc9f Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Tue, 6 Aug 2019 12:26:25 +1000 Subject: [PATCH 3/8] Make sure environment is activated first --- lib/gel/command/open.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index af6ca99..19bd3d9 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -10,6 +10,8 @@ def run(command_line) editor = ENV.fetch("GEL_EDITOR", ENV["EDITOR"]) raise "An editor must be set using either $GEL_EDITOR or $EDITOR" unless editor + Gel::Environment.activate(output: $stderr) + found_gem = Gel::Environment.find_gem(command_line.first) raise "Can't find gem `#{command_line.first}`" unless found_gem From 7e6ad26748312d3ab6dd99aea305164f0f8e5372 Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Tue, 6 Aug 2019 12:26:49 +1000 Subject: [PATCH 4/8] gel process can exit in favo[u]r of editor --- lib/gel/command/open.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index 19bd3d9..af821b6 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -16,6 +16,6 @@ def run(command_line) raise "Can't find gem `#{command_line.first}`" unless found_gem command = [*Shellwords.split(editor), found_gem.root] - system(*command) || exit($?.exitstatus) + exec(*command) end end From 8e0d0a1ab12bf19e7e3557a407fdc28241b4dcec Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Tue, 6 Aug 2019 12:29:00 +1000 Subject: [PATCH 5/8] Make this look consistent --- lib/gel/command/open.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index af821b6..a305aef 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -class Gel::Command::Open < Gel::Command - require "shellwords" +require "shellwords" +class Gel::Command::Open < Gel::Command def run(command_line) raise "Please provide the name of a gem to open in your editor" if command_line.empty? raise "Too many arguments, only 1 gem name is supported" if command_line.length > 1 From 9d5496233d6b0b1ca38bc6c236700a1d9ee5aace Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Tue, 6 Aug 2019 12:30:07 +1000 Subject: [PATCH 6/8] Give a name a name --- lib/gel/command/open.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index a305aef..16043f5 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -6,14 +6,15 @@ class Gel::Command::Open < Gel::Command def run(command_line) raise "Please provide the name of a gem to open in your editor" if command_line.empty? raise "Too many arguments, only 1 gem name is supported" if command_line.length > 1 + gem_name = command_line.shift editor = ENV.fetch("GEL_EDITOR", ENV["EDITOR"]) raise "An editor must be set using either $GEL_EDITOR or $EDITOR" unless editor Gel::Environment.activate(output: $stderr) - found_gem = Gel::Environment.find_gem(command_line.first) - raise "Can't find gem `#{command_line.first}`" unless found_gem + found_gem = Gel::Environment.find_gem(gem_name) + raise "Can't find gem `#{gem_name}`" unless found_gem command = [*Shellwords.split(editor), found_gem.root] exec(*command) From 36214001428bd187adbbaccc85acd6591859c199 Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Tue, 6 Aug 2019 12:40:55 +1000 Subject: [PATCH 7/8] chdir into gem root so editor is relative --- lib/gel/command/open.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index 16043f5..8027d10 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -17,6 +17,8 @@ def run(command_line) raise "Can't find gem `#{gem_name}`" unless found_gem command = [*Shellwords.split(editor), found_gem.root] - exec(*command) + Dir.chdir(found_gem.root) do + exec(*command) + end end end From fe9c676e14e04c5b85b393664917b0453ba4eec6 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Tue, 6 Aug 2019 20:54:47 +0930 Subject: [PATCH 8/8] Defer the require until the command is run We're already not perfect on this, but try to maintain a precedent that we don't avoidably contaminate the standard runtime environment. --- lib/gel/command/open.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gel/command/open.rb b/lib/gel/command/open.rb index 8027d10..0b92b54 100644 --- a/lib/gel/command/open.rb +++ b/lib/gel/command/open.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "shellwords" - class Gel::Command::Open < Gel::Command def run(command_line) + require "shellwords" + raise "Please provide the name of a gem to open in your editor" if command_line.empty? raise "Too many arguments, only 1 gem name is supported" if command_line.length > 1 gem_name = command_line.shift