autoreload 0.3.1 → 1.0.0

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.
@@ -1,43 +1,22 @@
1
- require 'fileutils'
2
- require 'pathname'
3
- require 'autoreload'
4
-
5
- class Pathname
6
- def write(str)
7
- self.open('wb') {|out|
8
- out.print str
9
- }
10
- end
11
- end
12
-
13
- module AutoReload
14
- class Reloader
15
- public :warn
16
- end
17
- end
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
18
2
 
19
3
  describe "AutoReload" do
20
4
 
21
- before :all do
22
- FileUtils.mkdir('tmp') unless File.exist?('tmp')
23
- end
24
-
25
5
  it "should autoreload" do
26
6
  # create a library
27
7
  library = Pathname.new('tmp/library.rb')
28
8
  library.write 'def foo; 1; end'
29
9
 
30
10
  # setup the autoreload
31
- autoreload(library.to_s, :interval => 1) #, :verbose=>true)
32
-
33
- # require it
34
- require "#{library}"
11
+ autoreload(:interval => 1) do #, :verbose=>true)
12
+ require "library"
13
+ end
35
14
 
36
15
  # check the number
37
- foo.should == 1
16
+ foo.must_equal 1
38
17
 
39
18
  # wait is needed for time stamp to not be same with the next file.
40
- sleep 1
19
+ sleep 2
41
20
 
42
21
  # recreate the file
43
22
  library.write 'def foo; 2; end'
@@ -46,10 +25,10 @@ describe "AutoReload" do
46
25
  sleep 2
47
26
 
48
27
  # check the number again
49
- foo.should == 2
28
+ foo.must_equal 2
50
29
 
51
30
  # clean up
52
- library.unlink
31
+ library.delete
53
32
  end
54
33
 
55
34
  end
@@ -0,0 +1,29 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ require 'fileutils'
5
+ require 'pathname'
6
+ require 'autoreload'
7
+
8
+ # Some helper stuff....
9
+
10
+ # create a tmp directory
11
+ FileUtils.mkdir('tmp') unless File.exist?('tmp')
12
+
13
+ $LOAD_PATH.unshift 'lib'
14
+ $LOAD_PATH.unshift 'tmp'
15
+
16
+ class Pathname
17
+ def write(str)
18
+ self.open('w') {|out|
19
+ out.puts str
20
+ }
21
+ end
22
+ end
23
+
24
+ module AutoReload
25
+ class Reloader
26
+ public :warn
27
+ end
28
+ end
29
+
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ describe "AutoReload" do
4
+
5
+ it "should autoreload a require of a require" do
6
+ library1 = Pathname.new('tmp/library1.rb')
7
+ library2 = Pathname.new('tmp/library2.rb')
8
+
9
+ library1.write "require 'library2'"
10
+ library2.write "def foo; 1; end"
11
+
12
+ # setup the autoreload
13
+ autoreload(:interval => 1) do #, :verbose=>true)
14
+ require "library1"
15
+ end
16
+
17
+ # check the number
18
+ foo.must_equal 1
19
+
20
+ # wait is needed for time stamp to not be same with the next file.
21
+ sleep 2
22
+
23
+ # recreate the file
24
+ library2.write "def foo; 2; end"
25
+
26
+ # wait again for the autoreload loop to repeat.
27
+ sleep 2
28
+
29
+ # check the number again
30
+ foo.must_equal 2
31
+
32
+ # clean up
33
+ library2.delete
34
+ library1.delete
35
+ end
36
+
37
+ end
@@ -0,0 +1,5 @@
1
+ # Change this file during 'sample1.rb' is running.
2
+
3
+ def message
4
+ "Change Me!"
5
+ end
@@ -0,0 +1,20 @@
1
+ # sample1.rb
2
+
3
+ # Run this script and change 'foo.rb' while this script is running.
4
+
5
+ __dir__ = File.dirname(__FILE__)
6
+
7
+ $LOAD_PATH.unshift(File.join(__dir__, '../lib'))
8
+
9
+ library = './' + __dir__ + '/changeme.rb'
10
+
11
+ require 'autoreload'
12
+
13
+ autoreload(:interval=>1, :verbose=>true)
14
+ require library
15
+ end
16
+
17
+ loop {
18
+ puts message
19
+ sleep 1
20
+ }
@@ -0,0 +1,115 @@
1
+ module Kernel
2
+
3
+ # Autoreload feature files.
4
+ #
5
+ # Automatically reload, at regular intervals, any previously loaded features,
6
+ # and/or other files not already loaded, if they have been modified since the last
7
+ # interval check. A numeric parameter sets the reload interval in seconds
8
+ # and the file parameter can either be a glob string or an array
9
+ # of file paths. If a glob string, it is expanded only once on the initial
10
+ # method call. Supplying a boolean parameter of 'false' will force autreload to
11
+ # skip previously loaded features and only reload the specified files.
12
+ # Also keeps a "dirty" flag.
13
+ #
14
+ # Copyright (c) 2003 Michael Neumann
15
+
16
+ def autoreload( *args )
17
+
18
+ check_interval=10
19
+ include_features = true
20
+ files = nil
21
+
22
+ args.each do |arg|
23
+ case arg
24
+ when Numeric
25
+ check_interval = arg
26
+ when String
27
+ files = Dir.glob( arg )
28
+ when Array
29
+ files = arg
30
+ when TrueClass, FalseClass
31
+ include_features = arg
32
+ end
33
+ end
34
+
35
+ file_mtime = {}
36
+
37
+ Thread.new(Time.now) do |start_time|
38
+ loop do
39
+ sleep check_interval
40
+
41
+ if include_features
42
+ feature_files = $LOADED_FEATURES.collect { |feature|
43
+ $LOAD_PATH.each { |lp| file = File.join(lp, feature) }
44
+ }.flatten
45
+
46
+ feature_files.each { |file|
47
+ if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
48
+ $autoreload_dirty = true
49
+ file_mtime[file] = mtime
50
+ STDERR.puts "File '#{ file }' reloaded"
51
+ begin
52
+ load(file)
53
+ rescue Exception => e
54
+ STDERR.puts e.inspect
55
+ end
56
+ end
57
+ }
58
+ end
59
+
60
+ if files
61
+ files.each do |file|
62
+ if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
63
+ $autoreload_dirty = true
64
+ file_mtime[file] = mtime
65
+ STDERR.puts "File '#{ file }' changed"
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ # Same as #autoreload, but does not include previously loaded features.
76
+ # This is equivalent to as adding a 'false' parameter to #autoreload.
77
+ #
78
+ def autoreload_files(*args)
79
+ autoreload(false, *args)
80
+ end
81
+
82
+ # deprecated
83
+ #def autoreload_glob(*args)
84
+ # warn "autoreload_glob will be deprecated. Use autoreload_files instead."
85
+ # autoreload_files(*args)
86
+ #end
87
+ end
88
+
89
+ #--
90
+ # # OLD VERSION
91
+ # def autoreload(check_interval=10)
92
+ # Thread.new(Time.now) { |start_time|
93
+ # file_mtime = {}
94
+ # loop {
95
+ # sleep check_interval
96
+ # $LOADED_FEATURES.each { |feature|
97
+ # $LOAD_PATH.each { |lp|
98
+ # file = File.join(lp, feature)
99
+ # if (File.exists?(file) and
100
+ # File.stat(file).mtime > (file_mtime[file] || start_time))
101
+ # file_mtime[file] = File.stat(file).mtime
102
+ # STDERR.puts "reload #{ file }"
103
+ # begin
104
+ # load(file)
105
+ # rescue Exception => e
106
+ # STDERR.puts e.inspect
107
+ # end
108
+ # end
109
+ # }
110
+ # }
111
+ # }
112
+ # }
113
+ # end
114
+ #++
115
+
@@ -0,0 +1,115 @@
1
+ module Kernel
2
+
3
+ # Autoreload feature files.
4
+ #
5
+ # Automatically reload, at regular intervals, any previously loaded features,
6
+ # and/or other files not already loaded, if they have been modified since the last
7
+ # interval check. A numeric parameter sets the reload interval in seconds
8
+ # and the file parameter can either be a glob string or an array
9
+ # of file paths. If a glob string, it is expanded only once on the initial
10
+ # method call. Supplying a boolean parameter of 'false' will force autreload to
11
+ # skip previously loaded features and only reload the specified files.
12
+ # Also keeps a "dirty" flag.
13
+ #
14
+ # Copyright (c) 2003 Michael Neumann
15
+
16
+ def autoreload( *args )
17
+
18
+ check_interval=10
19
+ include_features = true
20
+ files = nil
21
+
22
+ args.each do |arg|
23
+ case arg
24
+ when Numeric
25
+ check_interval = arg
26
+ when String
27
+ files = Dir.glob( arg )
28
+ when Array
29
+ files = arg
30
+ when TrueClass, FalseClass
31
+ include_features = arg
32
+ end
33
+ end
34
+
35
+ file_mtime = {}
36
+
37
+ Thread.new(Time.now) do |start_time|
38
+ loop do
39
+ sleep check_interval
40
+
41
+ if include_features
42
+ feature_files = $LOADED_FEATURES.collect { |feature|
43
+ $LOAD_PATH.each { |lp| file = File.join(lp, feature) }
44
+ }.flatten
45
+
46
+ feature_files.each { |file|
47
+ if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
48
+ $autoreload_dirty = true
49
+ file_mtime[file] = mtime
50
+ STDERR.puts "File '#{ file }' reloaded"
51
+ begin
52
+ load(file)
53
+ rescue Exception => e
54
+ STDERR.puts e.inspect
55
+ end
56
+ end
57
+ }
58
+ end
59
+
60
+ if files
61
+ files.each do |file|
62
+ if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
63
+ $autoreload_dirty = true
64
+ file_mtime[file] = mtime
65
+ STDERR.puts "File '#{ file }' changed"
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ # Same as #autoreload, but does not include previously loaded features.
76
+ # This is equivalent to as adding a 'false' parameter to #autoreload.
77
+ #
78
+ def autoreload_files(*args)
79
+ autoreload(false, *args)
80
+ end
81
+
82
+ # deprecated
83
+ #def autoreload_glob(*args)
84
+ # warn "autoreload_glob will be deprecated. Use autoreload_files instead."
85
+ # autoreload_files(*args)
86
+ #end
87
+ end
88
+
89
+ #--
90
+ # # OLD VERSION
91
+ # def autoreload(check_interval=10)
92
+ # Thread.new(Time.now) { |start_time|
93
+ # file_mtime = {}
94
+ # loop {
95
+ # sleep check_interval
96
+ # $LOADED_FEATURES.each { |feature|
97
+ # $LOAD_PATH.each { |lp|
98
+ # file = File.join(lp, feature)
99
+ # if (File.exists?(file) and
100
+ # File.stat(file).mtime > (file_mtime[file] || start_time))
101
+ # file_mtime[file] = File.stat(file).mtime
102
+ # STDERR.puts "reload #{ file }"
103
+ # begin
104
+ # load(file)
105
+ # rescue Exception => e
106
+ # STDERR.puts e.inspect
107
+ # end
108
+ # end
109
+ # }
110
+ # }
111
+ # }
112
+ # }
113
+ # end
114
+ #++
115
+
@@ -0,0 +1,56 @@
1
+ == 2007-07-07
2
+
3
+ * lib/autoreload.rb: Do not use @thread.
4
+
5
+ == 2007-07-06
6
+
7
+ * Rakefile: To use Chages.txt
8
+
9
+ == 2007-07-05
10
+
11
+ * Changes.txt: New file. Moved from ChangeLog.
12
+ * ChangeLog: Deleted.
13
+
14
+ * bin: Deleted.
15
+
16
+ * lib/autoreload.rb: Make it start from class method.
17
+
18
+ * lib/autoreload/version.rb: Removed.
19
+
20
+ * lib/autoreload.rb: Reformat.
21
+
22
+ * examples/sample1.rb: New file.
23
+ * examples/foo.rb: New file.
24
+
25
+ * default_task.rb: New file. Splited from Rakefile.
26
+ * Rakefile: Follow.
27
+ * script/lib-txt2html.rb: New file.
28
+ * script/makemanifest.rb: New file.
29
+
30
+ * License.txt: Update license to Ruby license.
31
+
32
+ * test/test_autoreload.rb: Sometimes test is failed.
33
+
34
+ * default_task.rb: Check file exist in chmod task.
35
+
36
+ * default_task.rb: Specify version and download.
37
+ * scripts/lib-txt2html.rb: Remove dependency.
38
+ * scripts/txt2html: Follow.
39
+
40
+ == 2007-07-04
41
+
42
+ * License.txt: created.
43
+ * README.txt: created.
44
+
45
+ * lib/autoreload/version.rb: Truncated.
46
+ * Rakefile: Ditto.
47
+
48
+ * lib/autoreload.rb: Created.
49
+ * test/test_autoreload.rb: Ditto.
50
+
51
+ * lib/autoreload.rb: Refactoring.
52
+
53
+ == 2007-07-04
54
+
55
+ * Init.
56
+