console_editor 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
File without changes
@@ -0,0 +1,34 @@
1
+
2
+ # Console editor
3
+
4
+ Based almost entirely on interactive_editor, written by Jan Berkel. See [the original GitHub project](http://github.com/jberkel/interactive_editor) for details.
5
+
6
+ The only difference between this project and interactive\_editor is its behaviour: the original project is intended for use in irb, and eval()'s the contents of your editor upon exit. The purpose of console\_editor is to let console application users edit text files that are not intended for evaluation by the script.
7
+
8
+ ## Usage
9
+
10
+ $ gem install console_editor
11
+
12
+ Put the following in your console script:
13
+
14
+ require 'rubygems'
15
+ require 'console_editor'
16
+
17
+ Then, in your console application:
18
+
19
+ > nano # (use nano w/ temp file)
20
+ > nano 'filename.rb' # (open filename.rb in nano)
21
+ > ed # (use EDITOR env variable)
22
+ > [emacs|vim|mvim|nano|mate] # (other editors)
23
+
24
+ When you exit the editor, your script can continue its execution.
25
+
26
+ To try it out without installing the gem:
27
+
28
+ $ git clone git://github.com/aaronvegh/console_editor.git
29
+
30
+ Include the lib/console\_editor.rb file in your script.
31
+
32
+ ## Disclaimer
33
+
34
+ This is a quick and dirty rip from the interactive\_editor gem, so any legacy code from that project is due only to my haste and ignorance. Cheers!
@@ -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 = '0.0.1'
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 = 'console_editor'
10
+ s.version = '0.0.5'
11
+ s.date = '2010-09-26'
12
+ s.rubyforge_project = 'console_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 the console."
17
+ s.description = "Use vim (or any other text editor) from a console app."
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 = ["Aaron Vegh"]
23
+ s.email = 'aaron@innoveghtive.com'
24
+ s.homepage = 'http://github.com/aaronvegh/console_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
+ console_editor.gemspec
42
+ lib/console_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,89 @@
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'
11
+
12
+ attr_accessor :editor
13
+
14
+ def initialize(editor)
15
+ @editor = editor.to_s
16
+ end
17
+
18
+ def edit(file=nil)
19
+ @file = if file
20
+ FileUtils.touch(file) unless File.exist?(file)
21
+ File.new(file)
22
+ else
23
+ (@file && File.exist?(@file.path)) ? @file : Tempfile.new(["irb_tempfile", ".rb"])
24
+ end
25
+ mtime = File.stat(@file.path).mtime
26
+
27
+ if (args = @editor.split(/\s+/)).size > 1
28
+ Exec.system(args[0], *(args[1..-1] << @file.path))
29
+ else
30
+ Exec.system(@editor, @file.path)
31
+ end
32
+
33
+ #execute if mtime < File.stat(@file.path).mtime
34
+ end
35
+
36
+ def execute
37
+ eval(IO.read(@file.path), TOPLEVEL_BINDING)
38
+ end
39
+
40
+ def self.edit(editor, file=nil)
41
+ #maybe serialise last file to disk, for recovery
42
+ (IRB.conf[:interactive_editors] ||=
43
+ Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(file)
44
+ end
45
+
46
+ module Exec
47
+ module Java
48
+ def system(file, *args)
49
+ require 'spoon'
50
+ Process.waitpid(Spoon.spawnp(file, *args))
51
+ rescue Errno::ECHILD => e
52
+ raise "error exec'ing #{file}: #{e}"
53
+ end
54
+ end
55
+
56
+ module MRI
57
+ def system(file, *args)
58
+ Kernel::system(file, *args) #or raise "error exec'ing #{file}: #{$?}"
59
+ end
60
+ end
61
+
62
+ extend RUBY_PLATFORM =~ /java/ ? Java : MRI
63
+ end
64
+
65
+ module Editors
66
+ {
67
+ :vi => nil,
68
+ :vim => nil,
69
+ :emacs => nil,
70
+ :nano => nil,
71
+ :mate => 'mate -w',
72
+ :mvim => 'mvim -g -f'
73
+ }.each do |k,v|
74
+ define_method(k) do |*args|
75
+ InteractiveEditor.edit(v || k, *args)
76
+ end
77
+ end
78
+
79
+ def ed(*args)
80
+ if ENV['EDITOR'].to_s.size > 0
81
+ InteractiveEditor.edit(ENV['EDITOR'], *args)
82
+ else
83
+ raise "You need to set the EDITOR environment variable first"
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ include InteractiveEditor::Editors
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: console_editor
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 5
9
+ version: 0.0.5
10
+ platform: ruby
11
+ authors:
12
+ - Aaron Vegh
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-26 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: spoon
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 0
30
+ - 1
31
+ version: 0.0.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Use vim (or any other text editor) from a console app.
35
+ email: aaron@innoveghtive.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.md
42
+ - LICENSE
43
+ files:
44
+ - LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - console_editor.gemspec
48
+ - lib/console_editor.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/aaronvegh/console_editor
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: console_editor
75
+ rubygems_version: 1.3.6
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Interactive editing in the console.
79
+ test_files: []
80
+