Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit a1a29ee4602d73166b28611912d0ecdbcbe4548f @jberkel committed May 15, 2010
Showing with 241 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. 0 LICENSE
  3. 0 README.md
  4. +117 −0 Rakefile
  5. +45 −0 interactive_editor.gemspec
  6. +78 −0 lib/interactive_editor.rb
1 .gitignore
@@ -0,0 +1 @@
+pkg
0 LICENSE
No changes.
0 README.md
No changes.
117 Rakefile
@@ -0,0 +1,117 @@
+require 'rubygems'
+require 'rake'
+require 'date'
+
+#############################################################################
+#
+# Helper functions
+#
+#############################################################################
+
+def name
+ @name ||= Dir['*.gemspec'].first.split('.').first
+end
+
+def version
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
+end
+
+def date
+ Date.today.to_s
+end
+
+def rubyforge_project
+ name
+end
+
+def gemspec_file
+ "#{name}.gemspec"
+end
+
+def gem_file
+ "#{name}-#{version}.gem"
+end
+
+def replace_header(head, header_name)
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
+end
+
+#############################################################################
+#
+# Standard tasks
+#
+#############################################################################
+
+task :default => :test
+
+desc "Open an irb session preloaded with this library"
+task :console do
+ sh "irb -rubygems -r ./lib/#{name}.rb"
+end
+
+
+
+#############################################################################
+#
+# Packaging tasks
+#
+#############################################################################
+
+task :release => :build do
+ unless `git branch` =~ /^\* master$/
+ puts "You must be on the master branch to release!"
+ exit!
+ end
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
+ sh "git tag v#{version}"
+ sh "git push origin master"
+ sh "git push v#{version}"
+ sh "gem push pkg/#{name}-#{version}.gem"
+end
+
+task :build => :gemspec do
+ sh "mkdir -p pkg"
+ sh "gem build #{gemspec_file}"
+ sh "mv #{gem_file} pkg"
+end
+
+task :gemspec => :validate do
+ # read spec file and split out manifest section
+ spec = File.read(gemspec_file)
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
+
+ # replace name version and date
+ replace_header(head, :name)
+ replace_header(head, :version)
+ replace_header(head, :date)
+ #comment this out if your rubyforge_project has a different name
+ replace_header(head, :rubyforge_project)
+
+ # determine file list from git ls-files
+ files = `git ls-files`.
+ split("\n").
+ sort.
+ reject { |file| file =~ /^\./ }.
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
+ map { |file| " #{file}" }.
+ join("\n")
+
+ # piece file back together and write
+ manifest = " s.files = %w[\n#{files}\n ]\n"
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
+ puts "Updated #{gemspec_file}"
+end
+
+task :validate do
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb"]
+ unless libfiles.empty?
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
+ exit!
+ end
+ unless Dir['VERSION*'].empty?
+ puts "A `VERSION` file at root level violates Gem best practices."
+ exit!
+ end
+end
45 interactive_editor.gemspec
@@ -0,0 +1,45 @@
+Gem::Specification.new do |s|
+ s.specification_version = 2 if s.respond_to? :specification_version=
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.rubygems_version = '1.3.5'
+
+ ## Leave these as is they will be modified for you by the rake gemspec task.
+ ## If your rubyforge_project name is different, then edit it and comment out
+ ## the sub! line in the Rakefile
+ s.name = 'interactive_editor'
+ s.version = '0.0.1'
+ s.date = '2010-05-16'
+ s.rubyforge_project = 'interactive_editor'
+
+ ## Make sure your summary is short. The description may be as long
+ ## as you like.
+ s.summary = "Interactive editing in irb."
+ s.description = "Use vim (or any other text editor) from inside irb to quickly test & write new code."
+
+ ## List the primary authors. If there are a bunch of authors, it's probably
+ ## better to set the email to an email list or something. If you don't have
+ ## a custom homepage, consider using your GitHub URL or the like.
+ s.authors = ["Jan Berkel"]
+ s.email = 'jan.berkel@gmail.com'
+ s.homepage = 'http://github.com/jberkel/interactive_editor'
+
+ s.require_paths = %w[lib]
+
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.extra_rdoc_files = %w[README.md LICENSE]
+
+ s.add_dependency('spoon', [">= 0.0.1"])
+
+ ## Leave this section as-is. It will be automatically generated from the
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
+ # = MANIFEST =
+ s.files = %w[
+
+ ]
+ # = MANIFEST =
+
+ ## Test files will be grabbed from the file list. Make sure the path glob
+ ## matches what you actually use.
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
+end
78 lib/interactive_editor.rb
@@ -0,0 +1,78 @@
+# vim: set syntax=ruby :
+# Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation.
+# http://gilesbowkett.blogspot.com/2007/10/use-vi-or-any-text-editor-from-within.html
+
+require 'irb'
+require 'fileutils'
+require 'tempfile'
+
+class InteractiveEditor
+ VERSION = '0.0.1'
+
+ attr_accessor :editor
+
+ def initialize(editor)
+ @editor = editor.to_s
+ end
+
+ def edit(file=nil)
+ @file = if file
+ FileUtils.touch(file) unless File.exist?(file)
+ File.new(file)
+ else
+ (@file && File.exist?(@file.path)) ? @file : Tempfile.new(["irb_tempfile", ".rb"])
+ end
+ mtime = File.stat(@file.path).mtime
+ Exec.system(@editor, @file.path)
+
+ execute if mtime < File.stat(@file.path).mtime
+ end
+
+ def execute
+ Object.class_eval(IO.read(@file.path))
+ end
+
+ def self.edit(editor, file=nil)
+ #idea serialise last file to disk, for recovery
+ unless IRB.conf[:interactive_editors] && IRB.conf[:interactive_editors][editor]
+ IRB.conf[:interactive_editors] ||= {}
+ IRB.conf[:interactive_editors][editor] = InteractiveEditor.new(editor)
+ end
+ IRB.conf[:interactive_editors][editor].edit(file)
+ end
+
+ module Exec
+ extend self
+
+ if RUBY_PLATFORM =~ /java/
+ #http://github.com/headius/spoon
+ require 'rubygems'
+ require 'spoon'
+
+ def system(*args)
+ Process.waitpid(Spoon.spawnp(*args))
+ end
+ else
+ def system(file, *args)
+ Kernel::system(file, *args)
+ end
+ end
+ end
+
+ module Editors
+ {
+ :vi => nil,
+ :vim => nil,
+ :emacs => nil,
+ :nano => nil,
+ :mate => 'mate -w'
+ }.each do |k,v|
+ define_method(k) do |*args|
+ InteractiveEditor.edit(v || k, *args)
+ end
+ end
+ end
+end
+
+Object.send(:include, InteractiveEditor::Editors)
+

0 comments on commit a1a29ee

Please sign in to comment.