sketches 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ === 0.1.0 / 2009-04-29
2
+
3
+ * Initial release:
4
+ * Spawn an editor of your choosing from IRB.
5
+ * Automatically reload your code when it changes.
6
+ * Use a custom editor command.
7
+ * Use a custom temp directory to store sketches in.
8
+
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Manifest.txt
3
+ Rakefile
4
+ README.txt
5
+ lib/sketches.rb
6
+ lib/sketches/exceptions.rb
7
+ lib/sketches/exceptions/unknown_sketch.rb
8
+ lib/sketches/exceptions/editor_not_defined.rb
9
+ lib/sketches/config.rb
10
+ lib/sketches/temp_sketch.rb
11
+ lib/sketches/sketch.rb
12
+ lib/sketches/extensions.rb
13
+ lib/sketches/extensions/kernel.rb
14
+ lib/sketches/cache.rb
15
+ lib/sketches/sketches.rb
16
+ lib/sketches/version.rb
@@ -0,0 +1,86 @@
1
+ = Sketches
2
+
3
+ * http://sketches.rubyforge.org/
4
+ * http://github.com/postmodern/sketches/
5
+ * Postmodern (postmodern.mod3 at gmail.com)
6
+
7
+ == DESCRIPTION:
8
+
9
+ Sketches allows you to create and edit Ruby code from the comfort of your
10
+ editor, while having it safely reloaded in IRB whenever changes to the
11
+ code are saved.
12
+
13
+ == FEATURES:
14
+
15
+ * Spawn an editor of your choosing from IRB.
16
+ * Automatically reload your code when it changes.
17
+ * Use a custom editor command.
18
+ * Use a custom temp directory to store sketches in.
19
+
20
+ == INSTALL:
21
+
22
+ $ sudo gem install sketches
23
+
24
+ Then require sketches in your <tt>.irbrc</tt> file:
25
+
26
+ require 'sketches'
27
+
28
+ Sketches can be configured to use a custom editor command:
29
+
30
+ Sketches.config :editor => 'gvim'
31
+
32
+ Sketches.config :editor => lambda { |path|
33
+ "xterm -fg gray -bg black -e vim #{path} &"
34
+ }
35
+
36
+ == EXAMPLES:
37
+
38
+ * Open a new sketch:
39
+
40
+ sketch
41
+
42
+ * Open a new named sketch:
43
+
44
+ sketch :foo
45
+
46
+ * Open a sketch from an existing file:
47
+
48
+ sketch_from 'path/to/bar.rb'
49
+
50
+ * Reopen an existing sketch:
51
+
52
+ sketch 2
53
+
54
+ sketch :foo
55
+
56
+ * List all sketches:
57
+
58
+ sketches
59
+
60
+ * Name a sketch:
61
+
62
+ name_sketch 2, :foo
63
+
64
+ * Save a sketch to an alternant location:
65
+
66
+ save_sketch :foo, 'path/to/foo.rb'
67
+
68
+ == LICENSE:
69
+
70
+ Sketches - A Ruby library for live programming and code reloading.
71
+
72
+ Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
73
+
74
+ This program is free software; you can redistribute it and/or modify
75
+ it under the terms of the GNU General Public License as published by
76
+ the Free Software Foundation; either version 2 of the License, or
77
+ (at your option) any later version.
78
+
79
+ This program is distributed in the hope that it will be useful,
80
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
81
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82
+ GNU General Public License for more details.
83
+
84
+ You should have received a copy of the GNU General Public License
85
+ along with this program; if not, write to the Free Software
86
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/sketches/version.rb'
6
+
7
+ Hoe.new('sketches', Sketches::VERSION) do |p|
8
+ p.rubyforge_name = 'sketches'
9
+ p.developer('Postmodern','postmodern.mod3@gmail.com')
10
+ end
11
+
12
+ # vim: syntax=Ruby
@@ -0,0 +1,25 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/extensions'
24
+ require 'sketches/sketches'
25
+ require 'sketches/version'
@@ -0,0 +1,164 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/exceptions/unknown_sketch'
24
+ require 'sketches/config'
25
+ require 'sketches/sketch'
26
+
27
+ require 'thread'
28
+
29
+ module Sketches
30
+ class Cache < Hash
31
+
32
+ #
33
+ # Creates a new Sketches cache.
34
+ #
35
+ def initialize
36
+ @mutex = Mutex.new
37
+
38
+ super()
39
+
40
+ @thread = Thread.new(self) do |cache|
41
+ loop do
42
+ cache.synchronize do
43
+ cache.each_sketch do |sketch|
44
+ sketch.synchronize do
45
+ sketch.reload! if sketch.stale?
46
+ end
47
+ end
48
+ end
49
+
50
+ sleep(Config.pause)
51
+ end
52
+ end
53
+ end
54
+
55
+ #
56
+ # Returns +true+ if the cache is still checking if any sketches have
57
+ # been modified, returns +false+ otherwise.
58
+ #
59
+ def running?
60
+ @thread.alive?
61
+ end
62
+
63
+ #
64
+ # Provides thread-safe access to the cache.
65
+ #
66
+ def synchronize(&block)
67
+ @mutex.synchronize(&block)
68
+ return nil
69
+ end
70
+
71
+ #
72
+ # Creates a new Sketch with the given _id_or_name_.
73
+ #
74
+ # cache.new_sketch 2
75
+ #
76
+ # cache.new_sketch :foobar
77
+ #
78
+ def new_sketch(id_or_name=nil)
79
+ id_or_name ||= next_id
80
+
81
+ if id_or_name.kind_of?(Integer)
82
+ return self[id_or_name] = Sketch.new(id_or_name)
83
+ else
84
+ id = next_id
85
+ name = id_or_name
86
+
87
+ return self[id] = Sketch.new(id,:name => name)
88
+ end
89
+ end
90
+
91
+ #
92
+ # Creates a new sketch using the existing _path_.
93
+ #
94
+ # reuse_sketch 'path/to/foo.rb'
95
+ #
96
+ def reuse_sketch(path)
97
+ id = next_id
98
+
99
+ return self[id] = Sketch.new(id,:path => path)
100
+ end
101
+
102
+ #
103
+ # Finds the sketch with the specified _id_ and gives it then
104
+ # specified _name_.
105
+ #
106
+ # cache.name_sketch 1, :foobar
107
+ #
108
+ def name_sketch(id,name)
109
+ id = id.to_i
110
+
111
+ unless has_key?(id)
112
+ raise(UnknownSketch,"cannot find the sketch with id: #{id}",caller)
113
+ end
114
+
115
+ sketch = self[id]
116
+ sketch.synchronize { sketch.name = name.to_sym }
117
+
118
+ return sketch
119
+ end
120
+
121
+ #
122
+ # Returns the sketch with the specified _name_.
123
+ #
124
+ # cache.find_by_name :foobar
125
+ #
126
+ def find_by_name(name)
127
+ name = name.to_sym
128
+
129
+ each_value do |sketch|
130
+ return sketch if sketch.name == name
131
+ end
132
+
133
+ return nil
134
+ end
135
+
136
+ #
137
+ # Returns the sketch with the specified _id_or_name_.
138
+ #
139
+ def [](id_or_name)
140
+ if id_or_name.kind_of?(Integer)
141
+ return super(id_or_name)
142
+ elsif (id_or_name.kind_of?(String) || id_or_name.kind_of?(Symbol))
143
+ return find_by_name(id_or_name)
144
+ end
145
+ end
146
+
147
+ #
148
+ # Returns the next available sketch id.
149
+ #
150
+ def next_id
151
+ size + 1
152
+ end
153
+
154
+ alias each_sketch each_value
155
+
156
+ #
157
+ # Returns the String representation of the cache.
158
+ #
159
+ def to_s(verbose=false)
160
+ values.inject('') { |str,sketch| str << sketch.to_s(verbose) }
161
+ end
162
+
163
+ end
164
+ end
@@ -0,0 +1,104 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'tmpdir'
24
+
25
+ module Sketches
26
+ module Config
27
+ # Directory to store temporary sketches in
28
+ TMPDIR = Dir.tmpdir
29
+
30
+ # Default editor to use
31
+ EDITOR = ENV['EDITOR']
32
+
33
+ # Default pause between checking if sketches were modified
34
+ PAUSE = 3
35
+
36
+ @@sketches_editor = EDITOR
37
+ @@sketches_tmpdir = TMPDIR
38
+ @@sketches_pause = PAUSE
39
+
40
+ #
41
+ # Returns the directory to store temporary sketches in.
42
+ #
43
+ # Config.tmpdir
44
+ # # => "/tmp"
45
+ #
46
+ def Config.tmpdir
47
+ @@sketches_tmpdir
48
+ end
49
+
50
+ #
51
+ # Sets the directory to store temporary sketches in to the specified
52
+ # _directory_.
53
+ #
54
+ def Config.tmpdir=(directory)
55
+ @@sketches_tmpdir = File.expand_path(dir)
56
+ end
57
+
58
+ #
59
+ # Returns the current editor to use for editing sketches.
60
+ #
61
+ # Config.editor
62
+ # # => 'pico'
63
+ #
64
+ def Config.editor
65
+ @@sketches_editor
66
+ end
67
+
68
+ #
69
+ # Use the specified _new_editor_ to edit sketches. _new_editor_ may
70
+ # be either a String or a lambda which accepts the +path+ of the sketch
71
+ # and returns the command to run.
72
+ #
73
+ # Config.editor = 'gvim'
74
+ #
75
+ # Config.editor = lambda { |path|
76
+ # "xterm -fg gray -bg black -e vim #{path} &"
77
+ # }
78
+ #
79
+ def Config.editor=(new_editor)
80
+ @@sketches_editor = new_editor
81
+ end
82
+
83
+ #
84
+ # Returns the current number of seconds to pause in between checking
85
+ # if any sketches were modified.
86
+ #
87
+ # Config.pause
88
+ # # => 3
89
+ #
90
+ def Config.pause
91
+ @@sketches_pause
92
+ end
93
+
94
+ #
95
+ # Use the specified number of _seconds_ to pause in between checking
96
+ # if any sketches were modified.
97
+ #
98
+ # Config.pause = 2
99
+ #
100
+ def Config.pause=(seconds)
101
+ @@sketches_pause = seconds
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,24 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/exceptions/unknown_sketch'
24
+ require 'sketches/exceptions/editor_not_defined'
@@ -0,0 +1,26 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ module Sketches
24
+ class EditorNotDefined < RuntimeError
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ module Sketches
24
+ class UnknownSketch < StandardError
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/extensions/kernel'
@@ -0,0 +1,74 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/sketches'
24
+
25
+ module Kernel
26
+ #
27
+ # Edits the sketch with the specified _id_or_name_. If no sketch exists
28
+ # with the specified _id_or_name_, one will be created.
29
+ #
30
+ # sketch 2
31
+ #
32
+ # sketch :foo
33
+ #
34
+ def sketch(id_or_name=nil)
35
+ Sketches.sketch(id_or_name)
36
+ end
37
+
38
+ #
39
+ # Creates a new sketch using the specified _path_.
40
+ #
41
+ # sketch_from 'path/to/foo.rb'
42
+ #
43
+ def sketch_from(path)
44
+ Sketches.from(path)
45
+ end
46
+
47
+ #
48
+ # Names the sketch with the specified _id_ with the specified _name_.
49
+ #
50
+ # name_sketch 2, :foo
51
+ #
52
+ def name_sketch(id,name)
53
+ Sketches.name(id,name)
54
+ end
55
+
56
+ #
57
+ # Saves the sketch with the specified _id_or_name_ to the specified
58
+ # _path_.
59
+ #
60
+ # save_sketch 2, 'path/to/example.rb'
61
+ #
62
+ # save_sketch :foo, 'path/to/foo.rb'
63
+ #
64
+ def save_sketch(id_or_name,path)
65
+ Sketches.save(id_or_name,path)
66
+ end
67
+
68
+ #
69
+ # Print out all of the sketches.
70
+ #
71
+ def sketches
72
+ Sketches.print
73
+ end
74
+ end
@@ -0,0 +1,185 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/exceptions/editor_not_defined'
24
+ require 'sketches/config'
25
+ require 'sketches/temp_sketch'
26
+
27
+ require 'thread'
28
+ require 'fileutils'
29
+
30
+ module Sketches
31
+ class Sketch
32
+
33
+ # ID of the sketch
34
+ attr_reader :id
35
+
36
+ # Optional name of the sketch
37
+ attr_accessor :name
38
+
39
+ # Last modification time of the sketch
40
+ attr_reader :mtime
41
+
42
+ #
43
+ # Creates a new sketch object with the specified _id_ and the given
44
+ # _options_.
45
+ #
46
+ # _options_ may contain the following keys:
47
+ # <tt>:name</tt>:: The name of the sketch.
48
+ # <tt>:path</tt>:: The path to an existing sketch.
49
+ #
50
+ def initialize(id,options={})
51
+ @id = id
52
+ @name = options[:name]
53
+
54
+ @mtime = Time.now
55
+ @checksum = 0
56
+ @mutex = Mutex.new
57
+
58
+ if options[:path]
59
+ @path = options[:path]
60
+ @name ||= File.basename(@path)
61
+ else
62
+ TempSketch.open { |file| @path = file.path }
63
+ end
64
+
65
+ reload!
66
+ end
67
+
68
+ #
69
+ # Provides thread-safe access to the sketch.
70
+ #
71
+ def synchronize(&block)
72
+ @mutex.synchronize(&block)
73
+ return nil
74
+ end
75
+
76
+ #
77
+ # Spawns the Sketches.editor with the path of the sketch.
78
+ #
79
+ def edit
80
+ if Config.editor.nil?
81
+ raise(EditorNotDefined,"no editor has defined via ENV['EDITOR'] or Sketches.config",caller)
82
+ elsif Config.editor.kind_of?(Proc)
83
+ cmd = Config.editor.call(@path)
84
+ else
85
+ cmd = "#{Config.editor} #{@path}"
86
+ end
87
+
88
+ system(cmd)
89
+ end
90
+
91
+ #
92
+ # Returns +true+ if the sketch has become stale and needs reloading,
93
+ # returns +false+ otherwise.
94
+ #
95
+ def stale?
96
+ if File.file?(@path)
97
+ if File.mtime(@path) > @mtime
98
+ return crc32 != @checksum
99
+ end
100
+ end
101
+
102
+ return false
103
+ end
104
+
105
+ #
106
+ # Reloads the sketch.
107
+ #
108
+ def reload!
109
+ if File.file?(@path)
110
+ @mtime = File.mtime(@path)
111
+ @checksum = crc32
112
+
113
+ begin
114
+ return load(@path)
115
+ rescue LoadError => e
116
+ STDERR.puts "#{e.class}: #{e.message}"
117
+ end
118
+ end
119
+
120
+ return false
121
+ end
122
+
123
+ #
124
+ # Saves the sketch to the specified _path_.
125
+ #
126
+ def save(path)
127
+ if (File.file?(@path) && @path != path)
128
+ FileUtils.cp(@path,path)
129
+ return true
130
+ end
131
+
132
+ return false
133
+ end
134
+
135
+ #
136
+ # Returns the String representation of the sketch.
137
+ #
138
+ def to_s(verbose=false)
139
+ str = "##{@id}"
140
+ str << ": #{@name}" if @name
141
+ str << "\n\n"
142
+
143
+ if File.file?(@path)
144
+ File.open(@path) do |file|
145
+ unless verbose
146
+ 4.times do
147
+ if file.eof?
148
+ str << "\n" unless str[-1..-1] == "\n"
149
+ str << " ..."
150
+ break
151
+ end
152
+
153
+ str << " #{file.readline}"
154
+ end
155
+ else
156
+ file.each_line { |line| str << " #{line}" }
157
+ end
158
+
159
+ str << "\n"
160
+ end
161
+ end
162
+
163
+ return str
164
+ end
165
+
166
+ protected
167
+
168
+ #
169
+ # Returns the CRC32 checksum of the sketch file.
170
+ #
171
+ def crc32
172
+ r = 0xffffffff
173
+
174
+ File.open(@path) do |file|
175
+ file.each_byte do |b|
176
+ r ^= b
177
+ 8.times { r = ((r >> 1) ^ (0xEDB88320 * (r & 1))) }
178
+ end
179
+ end
180
+
181
+ return r ^ 0xffffffff
182
+ end
183
+
184
+ end
185
+ end
@@ -0,0 +1,135 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/config'
24
+ require 'sketches/cache'
25
+
26
+ module Sketches
27
+ #
28
+ # Configure Sketches with the given _options_.
29
+ #
30
+ # _options_ may contain the following keys:
31
+ # <tt>:tmpdir</tt>:: Directory to store temporary sketches in.
32
+ # Defaults to Dir.tmpdir if unspecified.
33
+ # <tt>:editor</tt>:: The editor to use to edit sketches.
34
+ # <tt>:pause</tt>:: The number of seconds to pause in-between
35
+ # checking if any sketches were modified.
36
+ #
37
+ # Sketches.config :editor => 'gvim', :pause => 2
38
+ #
39
+ # Sketches.config :editor => lambda { |path|
40
+ # "xterm -fg gray -bg black -e vim #{path} &"
41
+ # }
42
+ #
43
+ def Sketches.config(options={})
44
+ if options[:tmpdir]
45
+ Config.tmpdir = options[:tmpdir]
46
+ end
47
+
48
+ if options[:editor]
49
+ Config.editor = options[:editor]
50
+ end
51
+
52
+ if options[:pause]
53
+ Config.pause = options[:pause]
54
+ end
55
+
56
+ return nil
57
+ end
58
+
59
+ @@sketches_cache = nil
60
+
61
+ #
62
+ # The cache of sketches.
63
+ #
64
+ def Sketches.cache
65
+ unless @@sketches_cache
66
+ @@sketches_cache = Cache.new
67
+ end
68
+
69
+ return @@sketches_cache
70
+ end
71
+
72
+ #
73
+ # Edits the sketch with the specified _id_or_name_. If no sketch exists
74
+ # with the specified _id_or_name_, one will be created.
75
+ #
76
+ # Sketches.sketch 2
77
+ #
78
+ # Sketches.sketch :foo
79
+ #
80
+ def Sketches.sketch(id_or_name)
81
+ Sketches.cache.synchronize do
82
+ sketch = Sketches.cache[id_or_name]
83
+ sketch ||= Sketches.cache.new_sketch(id_or_name)
84
+
85
+ sketch.synchronize { sketch.edit }
86
+ end
87
+ end
88
+
89
+ #
90
+ # Creates a new sketch using the specified _path_.
91
+ #
92
+ # Sketches.from 'path/to/foo.rb'
93
+ #
94
+ def Sketches.from(path)
95
+ Sketches.cache.synchronize do
96
+ sketch = Sketches.cache.reuse_sketch(path)
97
+
98
+ sketch.synchronize { sketch.edit }
99
+ end
100
+ end
101
+
102
+ #
103
+ # Names the sketch with the specified _id_ with the specified _name_.
104
+ #
105
+ # Sketches.name 2, :foo
106
+ #
107
+ def Sketches.name(id,name)
108
+ Sketches.cache.synchronize do
109
+ Sketches.cache.name_sketch(id,name)
110
+ end
111
+ end
112
+
113
+ #
114
+ # Saves the sketch with the specified _id_or_name_ to the specified
115
+ # _path_.
116
+ #
117
+ # Sketches.save 2, 'path/to/example.rb'
118
+ #
119
+ # Sketches.save :foo, 'path/to/foo.rb'
120
+ #
121
+ def Sketches.save(id_or_name,path)
122
+ Sketches.cache.synchronize do
123
+ if (sketch = Sketches.cache[id_or_name])
124
+ sketch.save(path)
125
+ end
126
+ end
127
+ end
128
+
129
+ #
130
+ # Print out all of the sketches.
131
+ #
132
+ def Sketches.print
133
+ Sketches.cache.synchronize { puts Sketches.cache }
134
+ end
135
+ end
@@ -0,0 +1,52 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ require 'sketches/config'
24
+
25
+ require 'tempfile'
26
+
27
+ module Sketches
28
+ class TempSketch < Tempfile
29
+
30
+ # Basename to use for temp sketches
31
+ BASENAME = 'sketch'
32
+
33
+ # Extension to use for temp sketches
34
+ EXT = '.rb'
35
+
36
+ #
37
+ # Create a new TempSketch object.
38
+ #
39
+ def initialize
40
+ super(BASENAME,Config.tmpdir)
41
+ end
42
+
43
+ private
44
+
45
+ def make_tmpname(basename,n)
46
+ prefix, suffix = basename, EXT
47
+
48
+ t = Time.now.strftime("%Y%m%d")
49
+ return "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ #--
3
+ # Sketches - A Ruby library for live programming and code reloading.
4
+ #
5
+ # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #++
21
+ #
22
+
23
+ module Sketches
24
+ # Sketches version
25
+ VERSION = '0.1.0'
26
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sketches
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Postmodern
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.12.2
24
+ version:
25
+ description: Sketches allows you to create and edit Ruby code from the comfort of your editor, while having it safely reloaded in IRB whenever changes to the code are saved.
26
+ email:
27
+ - postmodern.mod3@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - Rakefile
40
+ - README.txt
41
+ - lib/sketches.rb
42
+ - lib/sketches/exceptions.rb
43
+ - lib/sketches/exceptions/unknown_sketch.rb
44
+ - lib/sketches/exceptions/editor_not_defined.rb
45
+ - lib/sketches/config.rb
46
+ - lib/sketches/temp_sketch.rb
47
+ - lib/sketches/sketch.rb
48
+ - lib/sketches/extensions.rb
49
+ - lib/sketches/extensions/kernel.rb
50
+ - lib/sketches/cache.rb
51
+ - lib/sketches/sketches.rb
52
+ - lib/sketches/version.rb
53
+ has_rdoc: true
54
+ homepage: http://sketches.rubyforge.org/
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: sketches
76
+ rubygems_version: 1.3.1
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: Sketches allows you to create and edit Ruby code from the comfort of your editor, while having it safely reloaded in IRB whenever changes to the code are saved.
80
+ test_files: []
81
+