Bladeren bron

Merge pull request #128 from Shopify/add_pry_to_cli

Add option to use Pry for the CLI
Ben Cox 11 jaren geleden
bovenliggende
commit
f7bc5b9b64
6 gewijzigde bestanden met toevoegingen van 60 en 17 verwijderingen
  1. 4 0
      CHANGELOG
  2. 9 1
      Gemfile.lock
  3. 19 4
      lib/shopify_api/cli.rb
  4. 1 1
      lib/shopify_api/version.rb
  5. 1 0
      shopify_api.gemspec
  6. 26 11
      test/cli_test.rb

+ 4 - 0
CHANGELOG

@@ -1,3 +1,7 @@
+== Version 3.2.3 (Unreleased)
+
+* Added pry to the CLI
+
 == Version 3.2.2
 
 * Temporary fix for the CLI

+ 9 - 1
Gemfile.lock

@@ -1,8 +1,9 @@
 PATH
   remote: .
   specs:
-    shopify_api (3.2.2)
+    shopify_api (3.2.3)
       activeresource (>= 3.0.0)
+      pry
       thor (~> 0.18.1)
 
 GEM
@@ -23,16 +24,23 @@ GEM
       tzinfo (~> 0.3.37)
     atomic (1.1.14)
     builder (3.1.4)
+    coderay (1.1.0)
     fakeweb (1.3.0)
     i18n (0.6.9)
     metaclass (0.0.1)
+    method_source (0.8.2)
     minitest (4.7.5)
     mocha (0.14.0)
       metaclass (~> 0.0.1)
     multi_json (1.8.4)
+    pry (0.9.12.6)
+      coderay (~> 1.0)
+      method_source (~> 0.8)
+      slop (~> 3.4)
     rails-observers (0.1.2)
       activemodel (~> 4.0)
     rake (10.1.0)
+    slop (3.5.0)
     thor (0.18.1)
     thread_safe (0.1.3)
       atomic

+ 19 - 4
lib/shopify_api/cli.rb

@@ -30,6 +30,11 @@ module ShopifyAPI
         puts "\nopen https://#{config['domain']}/admin/api in your browser to get API credentials\n"
         config['api_key']  = ask("API key?")
         config['password'] = ask("Password?")
+        if ask("Would you like to use pry as your shell? (y/n)") === "y"
+          config["shell"] = "pry"
+        else
+          config["shell"] = "irb"
+        end
         create_file(file, config.to_yaml)
       end
       if available_connections.one?
@@ -100,10 +105,7 @@ module ShopifyAPI
       puts "using #{config['domain']}"
       ShopifyAPI::Base.site = site_from_config(config)
 
-      require 'irb'
-      require 'irb/completion'
-      ARGV.clear
-      IRB.start
+      launch_shell(config)
     end
 
     tasks.keys.abbrev.each do |shortcut, command|
@@ -137,6 +139,19 @@ module ShopifyAPI
       ShopifyAPI::Base.site = "#{protocol}://#{api_key}:#{password}@#{domain}/admin"
     end
 
+    def launch_shell(config)
+      if config["shell"] === "pry"
+        require 'pry'
+        ARGV.clear
+        Pry.start
+      else
+        require 'irb'
+        require 'irb/completion'
+        ARGV.clear
+        IRB.start
+      end
+    end
+
     def available_connections
       @available_connections ||= begin
         pattern = File.join(shop_config_dir, "*.yml")

+ 1 - 1
lib/shopify_api/version.rb

@@ -1,3 +1,3 @@
 module ShopifyAPI
-  VERSION = "3.2.2"
+  VERSION = "3.2.3"
 end

+ 1 - 0
shopify_api.gemspec

@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
 
   s.add_dependency("activeresource", [">= 3.0.0"])
   s.add_dependency("thor", ["~> 0.18.1"])
+  s.add_dependency("pry", [">= 0.9.12.6"])
 
   if s.respond_to?(:add_development_dependency)
     s.add_development_dependency("mocha", ">= 0.9.8")

+ 26 - 11
test/cli_test.rb

@@ -26,11 +26,8 @@ class CliTest < Test::Unit::TestCase
   end
 
   test "add with blank domain" do
-    `rm -rf #{@shop_config_dir}/*`
-    $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
-    $stdout.expects(:print).with("API key? ")
-    $stdout.expects(:print).with("Password? ")
-    $stdin.expects(:gets).times(3).returns("", "key", "pass")
+    standard_add_shop_prompts
+    $stdin.expects(:gets).times(4).returns("", "key", "pass", "y")
     @cli.expects(:puts).with("\nopen https://foo.myshopify.com/admin/api in your browser to get API credentials\n")
     @cli.expects(:puts).with("Default connection is foo")
 
@@ -40,16 +37,14 @@ class CliTest < Test::Unit::TestCase
     assert_equal 'foo.myshopify.com', config['domain']
     assert_equal 'key', config['api_key']
     assert_equal 'pass', config['password']
+    assert_equal 'pry', config['shell']
     assert_equal 'https', config['protocol']
     assert_equal config_file('foo'), File.readlink(@default_symlink)
   end
 
   test "add with explicit domain" do
-    `rm -rf #{@shop_config_dir}/*`
-    $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
-    $stdout.expects(:print).with("API key? ")
-    $stdout.expects(:print).with("Password? ")
-    $stdin.expects(:gets).times(3).returns("bar.myshopify.com", "key", "pass")
+    standard_add_shop_prompts
+    $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "y")
     @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
     @cli.expects(:puts).with("Default connection is foo")
 
@@ -59,6 +54,18 @@ class CliTest < Test::Unit::TestCase
     assert_equal 'bar.myshopify.com', config['domain']
   end
 
+  test "add with irb as shell" do
+    standard_add_shop_prompts
+    $stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "fuuuuuuu")
+    @cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/api in your browser to get API credentials\n")
+    @cli.expects(:puts).with("Default connection is foo")
+
+    @cli.add('foo')
+
+    config = YAML.load(File.read(config_file('foo')))
+    assert_equal 'irb', config['shell']
+  end
+
   test "list" do
     @cli.expects(:puts).with("   bar")
     @cli.expects(:puts).with(" * foo")
@@ -99,11 +106,19 @@ class CliTest < Test::Unit::TestCase
   private
 
   def valid_options
-    {'domain' => 'snowdevil.myshopify.com', 'api_key' => 'key', 'password' => 'pass', 'protocol' => 'https'}
+    {'domain' => 'snowdevil.myshopify.com', 'api_key' => 'key', 'password' => 'pass', 'shell' => 'pry', 'protocol' => 'https'}
   end
 
   def config_file(connection)
     File.join(@shop_config_dir, "#{connection}.yml")
   end
 
+  def standard_add_shop_prompts
+    `rm -rf #{@shop_config_dir}/*`
+    $stdout.expects(:print).with("Domain? (leave blank for foo.myshopify.com) ")
+    $stdout.expects(:print).with("API key? ")
+    $stdout.expects(:print).with("Password? ")
+    $stdout.expects(:print).with("Would you like to use pry as your shell? (y/n) ")
+  end
+
 end