andrewtimberlake-interactive_editor 0.0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+
2
+ # Interactive editor
3
+
4
+ cf. my (slightly outdated) blog post: [Integrating vim and irb](http://zegoggl.es/2009/04/integrating-vim-and-irb.html) and [vimcasts #20: Running Vim within IRB](http://vimcasts.org/e/20).
5
+
6
+ ## Usage
7
+
8
+ $ gem install interactive_editor
9
+
10
+ Put the following in your .irbrc:
11
+
12
+ require 'rubygems'
13
+ require 'interactive_editor'
14
+
15
+ Then, from within irb:
16
+
17
+ $ irb
18
+ > vi # (use vi w/ temp file)
19
+ > vi 'filename.rb' # (open filename.rb in vi)
20
+ > ed # (use EDITOR env variable)
21
+ > [emacs|vim|mvim|nano|mate] # (other editors)
22
+
23
+
24
+ To try it out without installing the gem:
25
+
26
+ $ git clone git://github.com/jberkel/interactive_editor.git
27
+ $ cd interactive_editor
28
+ $ rake console
29
+
30
+ ## Credits
31
+
32
+ Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation: [Use vi or any text editor from within IRB](http://gilesbowkett.blogspot.com/2007/10/use-vi-or-any-text-editor-from-within.html).
data/Rakefile ADDED
@@ -0,0 +1,117 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ desc "Open an irb session preloaded with this library"
49
+ task :console do
50
+ sh "irb -rubygems -r ./lib/#{name}.rb"
51
+ end
52
+
53
+
54
+
55
+ #############################################################################
56
+ #
57
+ # Packaging tasks
58
+ #
59
+ #############################################################################
60
+
61
+ task :release => :build do
62
+ unless `git branch` =~ /^\* master$/
63
+ puts "You must be on the master branch to release!"
64
+ exit!
65
+ end
66
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
67
+ sh "git tag v#{version}"
68
+ sh "git push origin master --tags"
69
+ sh "gem push pkg/#{name}-#{version}.gem"
70
+ end
71
+
72
+ desc "Builds the gem"
73
+ task :build => :gemspec do
74
+ sh "mkdir -p pkg"
75
+ sh "gem build #{gemspec_file}"
76
+ sh "mv #{gem_file} pkg"
77
+ end
78
+
79
+ task :gemspec => :validate do
80
+ # read spec file and split out manifest section
81
+ spec = File.read(gemspec_file)
82
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
83
+
84
+ # replace name version and date
85
+ replace_header(head, :name)
86
+ replace_header(head, :version)
87
+ replace_header(head, :date)
88
+ #comment this out if your rubyforge_project has a different name
89
+ replace_header(head, :rubyforge_project)
90
+
91
+ # determine file list from git ls-files
92
+ files = `git ls-files`.
93
+ split("\n").
94
+ sort.
95
+ reject { |file| file =~ /^\./ }.
96
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
97
+ map { |file| " #{file}" }.
98
+ join("\n")
99
+
100
+ # piece file back together and write
101
+ manifest = " s.files = %w[\n#{files}\n ]\n"
102
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
103
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
104
+ puts "Updated #{gemspec_file}"
105
+ end
106
+
107
+ task :validate do
108
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb"]
109
+ unless libfiles.empty?
110
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
111
+ exit!
112
+ end
113
+ unless Dir['VERSION*'].empty?
114
+ puts "A `VERSION` file at root level violates Gem best practices."
115
+ exit!
116
+ end
117
+ end
@@ -0,0 +1,49 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+ s.rubygems_version = '1.3.5'
5
+
6
+ ## Leave these as is they will be modified for you by the rake gemspec task.
7
+ ## If your rubyforge_project name is different, then edit it and comment out
8
+ ## the sub! line in the Rakefile
9
+ s.name = 'andrewtimberlake-interactive_editor'
10
+ s.version = '0.0.5.1'
11
+ s.date = '2010-11-29'
12
+ s.rubyforge_project = 'interactive_editor'
13
+
14
+ ## Make sure your summary is short. The description may be as long
15
+ ## as you like.
16
+ s.summary = "Interactive editing in irb."
17
+ s.description = "Use vim (or any other text editor) from inside irb to quickly test & write new code."
18
+
19
+ ## List the primary authors. If there are a bunch of authors, it's probably
20
+ ## better to set the email to an email list or something. If you don't have
21
+ ## a custom homepage, consider using your GitHub URL or the like.
22
+ s.authors = ["Jan Berkel"]
23
+ s.email = 'jan.berkel@gmail.com'
24
+ s.homepage = 'http://github.com/andrewtimberlake/interactive_editor'
25
+
26
+ s.require_paths = %w[lib]
27
+
28
+ s.rdoc_options = ["--charset=UTF-8"]
29
+ s.extra_rdoc_files = %w[README.md LICENSE]
30
+
31
+ s.add_dependency('spoon', [">= 0.0.1"])
32
+
33
+ ## Leave this section as-is. It will be automatically generated from the
34
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
35
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
36
+ # = MANIFEST =
37
+ s.files = %w[
38
+ LICENSE
39
+ README.md
40
+ Rakefile
41
+ interactive_editor.gemspec
42
+ lib/interactive_editor.rb
43
+ ]
44
+ # = MANIFEST =
45
+
46
+ ## Test files will be grabbed from the file list. Make sure the path glob
47
+ ## matches what you actually use.
48
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
49
+ end
@@ -0,0 +1,97 @@
1
+ # vim: set syntax=ruby :
2
+ # Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation.
3
+ # http://gilesbowkett.blogspot.com/2007/10/use-vi-or-any-text-editor-from-within.html
4
+
5
+ require 'irb'
6
+ require 'fileutils'
7
+ require 'tempfile'
8
+
9
+ class InteractiveEditor
10
+ VERSION = '0.0.5.1'
11
+
12
+ def self.config
13
+ @@config ||= {}
14
+ end
15
+
16
+ attr_accessor :editor
17
+
18
+ def initialize(editor)
19
+ @editor = editor.to_s
20
+ end
21
+
22
+ def edit(file=nil)
23
+ @file = if file
24
+ FileUtils.touch(file) unless File.exist?(file)
25
+ File.new(file)
26
+ else
27
+ (@file && File.exist?(@file.path)) ? @file : if InteractiveEditor.config[:save]
28
+ File.open(File.join(InteractiveEditor.config[:save_path] || ENV['HOME'], "irb_#{Time.now.strftime('%Y%m%d%H%m')}.rb"), 'wb')
29
+ else
30
+ Tempfile.new(["irb_tempfile", ".rb"])
31
+ end
32
+ end
33
+ mtime = File.stat(@file.path).mtime
34
+
35
+ if (args = @editor.split(/\s+/)).size > 1
36
+ Exec.system(args[0], *(args[1..-1] << @file.path))
37
+ else
38
+ Exec.system(@editor, @file.path)
39
+ end
40
+
41
+ execute if mtime < File.stat(@file.path).mtime
42
+ end
43
+
44
+ def execute
45
+ eval(IO.read(@file.path), TOPLEVEL_BINDING)
46
+ end
47
+
48
+ def self.edit(editor, file=nil)
49
+ #maybe serialise last file to disk, for recovery
50
+ (IRB.conf[:interactive_editors] ||=
51
+ Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(file)
52
+ end
53
+
54
+ module Exec
55
+ module Java
56
+ def system(file, *args)
57
+ require 'spoon'
58
+ Process.waitpid(Spoon.spawnp(file, *args))
59
+ rescue Errno::ECHILD => e
60
+ raise "error exec'ing #{file}: #{e}"
61
+ end
62
+ end
63
+
64
+ module MRI
65
+ def system(file, *args)
66
+ Kernel::system(file, *args) #or raise "error exec'ing #{file}: #{$?}"
67
+ end
68
+ end
69
+
70
+ extend RUBY_PLATFORM =~ /java/ ? Java : MRI
71
+ end
72
+
73
+ module Editors
74
+ {
75
+ :vi => nil,
76
+ :vim => nil,
77
+ :emacs => nil,
78
+ :nano => nil,
79
+ :mate => 'mate -w',
80
+ :mvim => 'mvim -g -f'
81
+ }.each do |k,v|
82
+ define_method(k) do |*args|
83
+ InteractiveEditor.edit(v || k, *args)
84
+ end
85
+ end
86
+
87
+ def ed(*args)
88
+ if ENV['EDITOR'].to_s.size > 0
89
+ InteractiveEditor.edit(ENV['EDITOR'], *args)
90
+ else
91
+ raise "You need to set the EDITOR environment variable first"
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ include InteractiveEditor::Editors
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: andrewtimberlake-interactive_editor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 89
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ - 1
11
+ version: 0.0.5.1
12
+ platform: ruby
13
+ authors:
14
+ - Jan Berkel
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-11-29 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: spoon
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 29
31
+ segments:
32
+ - 0
33
+ - 0
34
+ - 1
35
+ version: 0.0.1
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: Use vim (or any other text editor) from inside irb to quickly test & write new code.
39
+ email: jan.berkel@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.md
46
+ - LICENSE
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - interactive_editor.gemspec
52
+ - lib/interactive_editor.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/andrewtimberlake/interactive_editor
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: interactive_editor
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Interactive editing in irb.
87
+ test_files: []
88
+