lib/gorails/commands/railsbytes.rb in gorails-0.1.1 vs lib/gorails/commands/railsbytes.rb in gorails-0.1.2
- old
+ new
@@ -3,11 +3,28 @@
require "json"
module Gorails
module Commands
class Railsbytes < Gorails::Command
- def call(_args, _name)
+ def call(args, _name)
+ if args.none?
+ list
+ else
+ apply(args.first)
+ end
+ end
+
+ def self.help
+ <<~EOF
+ View the latest Railsbytes templates or load a template by ID.
+ Usage:
+ {{command:#{Gorails::TOOL_NAME} railsbytes}}
+ {{command:#{Gorails::TOOL_NAME} railsbytes x7msKX}}
+ EOF
+ end
+
+ def list
bytes = JSON.parse Net::HTTP.get(URI("https://railsbytes.com/public/templates.json"))
CLI::UI::Frame.open("Railsbytes") do
bytes.each do |byte|
puts CLI::UI.fmt "{{green:#{byte["name"]}}} by #{byte["created_by"]}"
@@ -16,11 +33,35 @@
puts
end
end
end
- def self.help
- "View the latest Railsbytes templates.\nUsage: {{command:#{Gorails::TOOL_NAME} railsbytes}}"
+ def apply(id)
+ byte = JSON.parse Net::HTTP.get(URI("https://railsbytes.com/public/templates/#{id}.json"))
+
+ CLI::UI::Frame.open("Railsbyte") do
+ puts CLI::UI.fmt "{{green:#{byte["name"]}}} by #{byte["created_by"]}"
+ puts byte["short_description"]
+ puts
+
+ CLI::UI::Prompt.ask("What would you like to do?") do |handler|
+ handler.option("View source") do |selection|
+ puts
+ puts byte["script"]
+ end
+
+ handler.option("Apply Railsbyte") do |selection|
+ puts
+ puts "Running Railsbyte..."
+
+ Bundler.with_original_env do
+ system "rails app:template LOCATION=\"https://railsbytes.com/script/#{id}\""
+ end
+ end
+
+ handler.option("exit") { |selection| exit 0 }
+ end
+ end
end
end
end
end