2012-06-23 15:56:54 -04:00
|
|
|
class Pry
|
2012-12-07 00:14:04 -05:00
|
|
|
module Gist
|
|
|
|
DESCRIPTION = 'Upload code, docs, history to https://gist.github.com/'
|
|
|
|
INVOCATIONS = {
|
|
|
|
:method => ['gist -m my_method', 'gist the method my_method' ],
|
|
|
|
:doc => ['gist -d my_method', 'gist the docs for my_method' ],
|
|
|
|
:input => ['gist -i 1..2', 'gist the input expressions from 1 to 2' ],
|
|
|
|
:kommand => ['gist -k show-method', 'gists pry command show-method' ],
|
2012-12-09 23:27:04 -05:00
|
|
|
:class => ['gist -c Pry::Method', 'gist the Pry::Method class' ],
|
|
|
|
:jist => ['jist -c Pry::Method', 'alias for the above' ],
|
2012-12-07 00:14:04 -05:00
|
|
|
:lines => ['gist -m my_method --lines 2..-2', 'limit by range' ],
|
|
|
|
:cliponly => ['gist -m my_method --clip', 'copy (but do not gist)' ],
|
|
|
|
:clipit => ['clipit -m my_method', 'alias for the above' ],
|
|
|
|
# TODO:
|
|
|
|
# :var =>
|
|
|
|
# :hist =>
|
|
|
|
# :file =>
|
|
|
|
}
|
|
|
|
class << self
|
|
|
|
def example_code sym; INVOCATIONS[sym][0] end
|
|
|
|
def example_description sym; INVOCATIONS[sym][1] end
|
|
|
|
def examples_docs
|
|
|
|
INVOCATIONS.keys.map do |e|
|
|
|
|
"e.g.: %-33s # %s " % [example_code(e), example_description(e)]
|
|
|
|
end.join "\n"
|
|
|
|
end
|
2012-12-10 00:21:18 -05:00
|
|
|
def require_jist; require 'jist' end
|
2012-12-07 00:14:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::Gist < Pry::ClassCommand
|
2012-08-11 20:22:29 -04:00
|
|
|
include Pry::Helpers::DocumentationHelpers
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
match 'gist'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Misc'
|
2012-12-07 00:14:04 -05:00
|
|
|
description Pry::Gist::DESCRIPTION
|
2012-08-11 21:26:59 -04:00
|
|
|
command_options :requires_gem => 'jist', :shellwords => false
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
banner <<-USAGE
|
2012-12-07 00:14:04 -05:00
|
|
|
Usage: gist [options]
|
|
|
|
#{Pry::Gist::DESCRIPTION}
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-12-07 00:14:04 -05:00
|
|
|
If you'd like to associate your gists with your GitHub account, run:
|
|
|
|
gist --login
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
USAGE
|
2012-12-07 00:14:04 -05:00
|
|
|
banner << Pry::Gist.examples_docs << "\n"
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-12-06 23:46:01 -05:00
|
|
|
attr_accessor :content, :filename
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def setup
|
2012-12-10 00:21:18 -05:00
|
|
|
Pry::Gist.require_jist # extracted so test can stub out
|
2012-12-06 23:46:01 -05:00
|
|
|
@content = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_pry_api api_obj
|
|
|
|
@content << api_obj.source << "\n"
|
2012-12-07 09:35:25 -05:00
|
|
|
@filename = api_obj.source_file
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def options(opt)
|
2012-12-06 23:46:01 -05:00
|
|
|
ext ='ruby'
|
2012-08-11 20:22:29 -04:00
|
|
|
opt.on :login, "Authenticate the jist gem with GitHub"
|
|
|
|
opt.on :d, :doc, "Gist a method's documentation.", :argument => true do |meth_name|
|
|
|
|
meth = get_method_or_raise(meth_name, target, {})
|
|
|
|
text.no_color do
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << process_comment_markup(meth.doc) << "\n"
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
2012-12-06 23:46:01 -05:00
|
|
|
@filename = meth.source_file + ".doc"
|
|
|
|
end
|
|
|
|
opt.on :m, :method, "Gist a method's source.", :argument => true do |meth_name|
|
|
|
|
from_pry_api get_method_or_raise(meth_name, target, {})
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :k, :command, "Gist a command's source.", :argument => true do |command_name|
|
|
|
|
command = find_command(command_name)
|
2012-12-06 23:46:01 -05:00
|
|
|
from_pry_api Pry::Method.new(command.block)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :c, :class, "Gist a class or module's source.", :argument => true do |class_name|
|
2012-12-06 23:46:01 -05:00
|
|
|
from_pry_api Pry::WrappedModule.from_str(class_name, target)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :var, "Gist a variable's content.", :argument => true do |variable_name|
|
|
|
|
begin
|
|
|
|
obj = target.eval(variable_name)
|
|
|
|
rescue Pry::RescuableException
|
|
|
|
raise CommandError, "Gist failed: Invalid variable name: #{variable_name}"
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
|
|
|
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << Pry.config.gist.inspecter.call(obj) << "\n"
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :hist, "Gist a range of Readline history lines.", :optional_argument => true, :as => Range, :default => -20..-1 do |range|
|
|
|
|
h = Pry.history.to_a
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << h[one_index_range(convert_to_range(range))].join("\n") << "\n"
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
opt.on :f, :file, "Gist a file.", :argument => true do |file|
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << File.read(File.expand_path(file)) << "\n"
|
|
|
|
@filename = file
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :o, :out, "Gist entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
|
|
|
|
:as => Range, :default => -1 do |range|
|
|
|
|
range = convert_to_range(range)
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
range.each do |v|
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
|
|
|
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << "\n"
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it.", :default => false
|
|
|
|
opt.on :p, :public, "Create a public gist (default: false)", :default => false
|
|
|
|
opt.on :l, :lines, "Only gist a subset of lines from the gistable content.", :optional_argument => true, :as => Range, :default => 1..-1
|
|
|
|
opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional_argument => true,
|
|
|
|
:as => Range, :default => -1 do |range|
|
|
|
|
range = convert_to_range(range)
|
|
|
|
input_expressions = _pry_.input_array[range] || []
|
|
|
|
Array(input_expressions).each_with_index do |code, index|
|
|
|
|
corrected_index = index + range.first
|
|
|
|
if code && code != ""
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << code
|
2012-08-11 20:22:29 -04:00
|
|
|
if code !~ /;\Z/
|
2012-12-06 23:46:01 -05:00
|
|
|
@content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def process
|
|
|
|
return Jist.login! if opts.present?(:login)
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-12-06 23:46:01 -05:00
|
|
|
if @content =~ /\A\s*\z/
|
2012-08-11 20:22:29 -04:00
|
|
|
raise CommandError, "Found no code to gist."
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.present?(:clip)
|
|
|
|
perform_clipboard
|
|
|
|
else
|
|
|
|
perform_gist
|
|
|
|
end
|
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
# copy content to clipboard instead (only used with --clip flag)
|
|
|
|
def perform_clipboard
|
2012-12-07 09:35:25 -05:00
|
|
|
Jist.copy(@content)
|
2012-08-11 20:22:29 -04:00
|
|
|
output.puts "Copied content to clipboard!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_gist
|
|
|
|
if opts.present?(:lines)
|
2012-12-06 23:46:01 -05:00
|
|
|
@content = restrict_to_lines(content, opts[:l])
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
2012-12-06 23:46:01 -05:00
|
|
|
response = Jist.gist(content, :filename => filename_or_fake,
|
2012-08-11 20:22:29 -04:00
|
|
|
:public => !!opts[:p])
|
|
|
|
|
|
|
|
if response
|
2012-12-06 23:46:01 -05:00
|
|
|
url = response['html_url']
|
2012-12-07 09:35:25 -05:00
|
|
|
Jist.copy(url)
|
2012-12-06 23:46:01 -05:00
|
|
|
output.puts 'Gist created at URL, which is now in the clipboard: ', url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename_or_fake
|
|
|
|
case @filename
|
|
|
|
when nil
|
|
|
|
'anonymous.rb' # not sure what triggers this condition
|
|
|
|
when '(pry)'
|
|
|
|
'repl.rb'
|
|
|
|
else
|
|
|
|
File.basename(@filename)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_range(n)
|
|
|
|
if !n.is_a?(Range)
|
|
|
|
(n..n)
|
|
|
|
else
|
|
|
|
n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def comment_expression_result_for_gist(result)
|
|
|
|
content = ""
|
|
|
|
result.lines.each_with_index do |line, index|
|
|
|
|
if index == 0
|
|
|
|
content << "# => #{line}"
|
|
|
|
else
|
|
|
|
content << "# #{line}"
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
content
|
|
|
|
end
|
|
|
|
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
Pry::Commands.add_command(Pry::Command::Gist)
|
|
|
|
Pry::Commands.alias_command 'clipit', 'gist --clip'
|
|
|
|
Pry::Commands.alias_command 'jist', 'gist'
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|