sass 3.7.4 → 4.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +13 -5
  2. data/.yardopts +1 -1
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/CONTRIBUTING.md +1 -146
  5. data/MIT-LICENSE +1 -1
  6. data/README.md +25 -39
  7. data/Rakefile +274 -0
  8. data/VERSION +1 -1
  9. data/VERSION_DATE +1 -1
  10. data/lib/sass.rb +3 -3
  11. data/lib/sass/cache_stores/filesystem.rb +2 -2
  12. data/lib/sass/cache_stores/memory.rb +5 -4
  13. data/lib/sass/callbacks.rb +2 -2
  14. data/lib/sass/css.rb +12 -12
  15. data/lib/sass/engine.rb +44 -62
  16. data/lib/sass/environment.rb +7 -35
  17. data/lib/sass/error.rb +14 -14
  18. data/lib/sass/exec/base.rb +14 -3
  19. data/lib/sass/exec/sass_convert.rb +6 -20
  20. data/lib/sass/exec/sass_scss.rb +29 -5
  21. data/lib/sass/features.rb +2 -3
  22. data/lib/sass/importers/filesystem.rb +6 -11
  23. data/lib/sass/logger.rb +3 -8
  24. data/lib/sass/logger/base.rb +2 -19
  25. data/lib/sass/plugin.rb +2 -3
  26. data/lib/sass/plugin/compiler.rb +67 -48
  27. data/lib/sass/plugin/configuration.rb +3 -3
  28. data/lib/sass/plugin/merb.rb +1 -1
  29. data/lib/sass/plugin/rack.rb +3 -3
  30. data/lib/sass/plugin/staleness_checker.rb +3 -3
  31. data/lib/sass/railtie.rb +1 -1
  32. data/lib/sass/script.rb +3 -3
  33. data/lib/sass/script/css_parser.rb +15 -5
  34. data/lib/sass/script/functions.rb +121 -337
  35. data/lib/sass/script/lexer.rb +36 -102
  36. data/lib/sass/script/parser.rb +153 -529
  37. data/lib/sass/script/tree/funcall.rb +34 -42
  38. data/lib/sass/script/tree/interpolation.rb +26 -171
  39. data/lib/sass/script/tree/list_literal.rb +8 -23
  40. data/lib/sass/script/tree/map_literal.rb +2 -2
  41. data/lib/sass/script/tree/node.rb +3 -3
  42. data/lib/sass/script/tree/operation.rb +16 -43
  43. data/lib/sass/script/tree/string_interpolation.rb +43 -64
  44. data/lib/sass/script/tree/variable.rb +1 -1
  45. data/lib/sass/script/value.rb +0 -2
  46. data/lib/sass/script/value/arg_list.rb +1 -1
  47. data/lib/sass/script/value/base.rb +9 -27
  48. data/lib/sass/script/value/color.rb +18 -26
  49. data/lib/sass/script/value/helpers.rb +18 -44
  50. data/lib/sass/script/value/list.rb +14 -35
  51. data/lib/sass/script/value/map.rb +2 -2
  52. data/lib/sass/script/value/number.rb +16 -26
  53. data/lib/sass/script/value/string.rb +1 -30
  54. data/lib/sass/scss.rb +2 -0
  55. data/lib/sass/scss/css_parser.rb +3 -7
  56. data/lib/sass/scss/parser.rb +78 -196
  57. data/lib/sass/scss/rx.rb +14 -7
  58. data/lib/sass/scss/script_lexer.rb +15 -0
  59. data/lib/sass/scss/script_parser.rb +25 -0
  60. data/lib/sass/scss/static_parser.rb +55 -38
  61. data/lib/sass/selector.rb +10 -7
  62. data/lib/sass/selector/abstract_sequence.rb +12 -15
  63. data/lib/sass/selector/comma_sequence.rb +6 -24
  64. data/lib/sass/selector/pseudo.rb +6 -19
  65. data/lib/sass/selector/sequence.rb +16 -14
  66. data/lib/sass/selector/simple.rb +7 -9
  67. data/lib/sass/selector/simple_sequence.rb +12 -16
  68. data/lib/sass/shared.rb +1 -1
  69. data/lib/sass/source/map.rb +9 -7
  70. data/lib/sass/source/position.rb +4 -4
  71. data/lib/sass/stack.rb +3 -23
  72. data/lib/sass/tree/charset_node.rb +1 -1
  73. data/lib/sass/tree/comment_node.rb +1 -1
  74. data/lib/sass/tree/function_node.rb +3 -2
  75. data/lib/sass/tree/node.rb +3 -5
  76. data/lib/sass/tree/prop_node.rb +58 -49
  77. data/lib/sass/tree/rule_node.rb +8 -15
  78. data/lib/sass/tree/visitors/check_nesting.rb +23 -19
  79. data/lib/sass/tree/visitors/convert.rb +13 -15
  80. data/lib/sass/tree/visitors/cssize.rb +15 -4
  81. data/lib/sass/tree/visitors/deep_copy.rb +2 -2
  82. data/lib/sass/tree/visitors/extend.rb +14 -10
  83. data/lib/sass/tree/visitors/perform.rb +18 -29
  84. data/lib/sass/tree/visitors/set_options.rb +2 -2
  85. data/lib/sass/tree/visitors/to_css.rb +47 -77
  86. data/lib/sass/util.rb +311 -98
  87. data/lib/sass/util/cross_platform_random.rb +19 -0
  88. data/lib/sass/util/multibyte_string_scanner.rb +133 -127
  89. data/lib/sass/util/normalized_map.rb +8 -1
  90. data/lib/sass/util/ordered_hash.rb +192 -0
  91. data/lib/sass/version.rb +6 -2
  92. data/test/sass/cache_test.rb +131 -0
  93. data/test/sass/callbacks_test.rb +61 -0
  94. data/test/sass/compiler_test.rb +236 -0
  95. data/test/sass/conversion_test.rb +2171 -0
  96. data/test/sass/css2sass_test.rb +526 -0
  97. data/test/sass/data/hsl-rgb.txt +319 -0
  98. data/test/sass/encoding_test.rb +219 -0
  99. data/test/sass/engine_test.rb +3400 -0
  100. data/test/sass/exec_test.rb +86 -0
  101. data/test/sass/extend_test.rb +1719 -0
  102. data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
  103. data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
  104. data/test/sass/functions_test.rb +1984 -0
  105. data/test/sass/importer_test.rb +421 -0
  106. data/test/sass/logger_test.rb +58 -0
  107. data/test/sass/mock_importer.rb +49 -0
  108. data/test/sass/more_results/more1.css +9 -0
  109. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  110. data/test/sass/more_results/more_import.css +29 -0
  111. data/test/sass/more_templates/_more_partial.sass +2 -0
  112. data/test/sass/more_templates/more1.sass +23 -0
  113. data/test/sass/more_templates/more_import.sass +11 -0
  114. data/test/sass/plugin_test.rb +556 -0
  115. data/test/sass/results/alt.css +4 -0
  116. data/test/sass/results/basic.css +9 -0
  117. data/test/sass/results/cached_import_option.css +3 -0
  118. data/test/sass/results/compact.css +5 -0
  119. data/test/sass/results/complex.css +86 -0
  120. data/test/sass/results/compressed.css +1 -0
  121. data/test/sass/results/expanded.css +19 -0
  122. data/test/sass/results/filename_fn.css +3 -0
  123. data/test/sass/results/if.css +3 -0
  124. data/test/sass/results/import.css +31 -0
  125. data/test/sass/results/import_charset.css +5 -0
  126. data/test/sass/results/import_charset_1_8.css +5 -0
  127. data/test/sass/results/import_charset_ibm866.css +5 -0
  128. data/test/sass/results/import_content.css +1 -0
  129. data/test/sass/results/line_numbers.css +49 -0
  130. data/test/sass/results/mixins.css +95 -0
  131. data/test/sass/results/multiline.css +24 -0
  132. data/test/sass/results/nested.css +22 -0
  133. data/test/sass/results/options.css +1 -0
  134. data/test/sass/results/parent_ref.css +13 -0
  135. data/test/sass/results/script.css +16 -0
  136. data/test/sass/results/scss_import.css +31 -0
  137. data/test/sass/results/scss_importee.css +2 -0
  138. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  139. data/test/sass/results/subdir/subdir.css +3 -0
  140. data/test/sass/results/units.css +11 -0
  141. data/test/sass/results/warn.css +0 -0
  142. data/test/sass/results/warn_imported.css +0 -0
  143. data/test/sass/script_conversion_test.rb +306 -0
  144. data/test/sass/script_test.rb +1206 -0
  145. data/test/sass/scss/css_test.rb +1281 -0
  146. data/test/sass/scss/rx_test.rb +160 -0
  147. data/test/sass/scss/scss_test.rb +4147 -0
  148. data/test/sass/scss/test_helper.rb +37 -0
  149. data/test/sass/source_map_test.rb +1055 -0
  150. data/test/sass/superselector_test.rb +210 -0
  151. data/test/sass/templates/_cached_import_option_partial.scss +1 -0
  152. data/test/sass/templates/_double_import_loop2.sass +1 -0
  153. data/test/sass/templates/_filename_fn_import.scss +11 -0
  154. data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  155. data/test/sass/templates/_imported_charset_utf8.sass +4 -0
  156. data/test/sass/templates/_imported_content.sass +3 -0
  157. data/test/sass/templates/_partial.sass +2 -0
  158. data/test/sass/templates/_same_name_different_partiality.scss +1 -0
  159. data/test/sass/templates/alt.sass +16 -0
  160. data/test/sass/templates/basic.sass +23 -0
  161. data/test/sass/templates/bork1.sass +2 -0
  162. data/test/sass/templates/bork2.sass +2 -0
  163. data/test/sass/templates/bork3.sass +2 -0
  164. data/test/sass/templates/bork4.sass +2 -0
  165. data/test/sass/templates/bork5.sass +3 -0
  166. data/test/sass/templates/cached_import_option.scss +3 -0
  167. data/test/sass/templates/compact.sass +17 -0
  168. data/test/sass/templates/complex.sass +305 -0
  169. data/test/sass/templates/compressed.sass +15 -0
  170. data/test/sass/templates/double_import_loop1.sass +1 -0
  171. data/test/sass/templates/expanded.sass +17 -0
  172. data/test/sass/templates/filename_fn.scss +18 -0
  173. data/test/sass/templates/if.sass +11 -0
  174. data/test/sass/templates/import.sass +12 -0
  175. data/test/sass/templates/import_charset.sass +9 -0
  176. data/test/sass/templates/import_charset_1_8.sass +6 -0
  177. data/test/sass/templates/import_charset_ibm866.sass +11 -0
  178. data/test/sass/templates/import_content.sass +4 -0
  179. data/test/sass/templates/importee.less +2 -0
  180. data/test/sass/templates/importee.sass +19 -0
  181. data/test/sass/templates/line_numbers.sass +13 -0
  182. data/test/sass/templates/mixin_bork.sass +5 -0
  183. data/test/sass/templates/mixins.sass +76 -0
  184. data/test/sass/templates/multiline.sass +20 -0
  185. data/test/sass/templates/nested.sass +25 -0
  186. data/test/sass/templates/nested_bork1.sass +2 -0
  187. data/test/sass/templates/nested_bork2.sass +2 -0
  188. data/test/sass/templates/nested_bork3.sass +2 -0
  189. data/test/sass/templates/nested_bork4.sass +2 -0
  190. data/test/sass/templates/nested_import.sass +2 -0
  191. data/test/sass/templates/nested_mixin_bork.sass +6 -0
  192. data/test/sass/templates/options.sass +2 -0
  193. data/test/sass/templates/parent_ref.sass +25 -0
  194. data/test/sass/templates/same_name_different_ext.sass +2 -0
  195. data/test/sass/templates/same_name_different_ext.scss +1 -0
  196. data/test/sass/templates/same_name_different_partiality.scss +1 -0
  197. data/test/sass/templates/script.sass +101 -0
  198. data/test/sass/templates/scss_import.scss +12 -0
  199. data/test/sass/templates/scss_importee.scss +1 -0
  200. data/test/sass/templates/single_import_loop.sass +1 -0
  201. data/test/sass/templates/subdir/import_up1.scss +1 -0
  202. data/test/sass/templates/subdir/import_up2.scss +1 -0
  203. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  204. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  205. data/test/sass/templates/subdir/subdir.sass +6 -0
  206. data/test/sass/templates/units.sass +11 -0
  207. data/test/sass/templates/warn.sass +3 -0
  208. data/test/sass/templates/warn_imported.sass +4 -0
  209. data/test/sass/test_helper.rb +8 -0
  210. data/test/sass/util/multibyte_string_scanner_test.rb +147 -0
  211. data/test/sass/util/normalized_map_test.rb +51 -0
  212. data/test/sass/util/subset_map_test.rb +91 -0
  213. data/test/sass/util_test.rb +438 -0
  214. data/test/sass/value_helpers_test.rb +179 -0
  215. data/test/test_helper.rb +110 -0
  216. data/vendor/listen/CHANGELOG.md +1 -0
  217. data/vendor/listen/CONTRIBUTING.md +38 -0
  218. data/vendor/listen/Gemfile +20 -0
  219. data/vendor/listen/Guardfile +8 -0
  220. data/vendor/listen/LICENSE +20 -0
  221. data/vendor/listen/README.md +349 -0
  222. data/vendor/listen/Rakefile +5 -0
  223. data/vendor/listen/Vagrantfile +96 -0
  224. data/vendor/listen/lib/listen.rb +54 -0
  225. data/vendor/listen/lib/listen/adapter.rb +327 -0
  226. data/vendor/listen/lib/listen/adapters/bsd.rb +75 -0
  227. data/vendor/listen/lib/listen/adapters/darwin.rb +48 -0
  228. data/vendor/listen/lib/listen/adapters/linux.rb +81 -0
  229. data/vendor/listen/lib/listen/adapters/polling.rb +58 -0
  230. data/vendor/listen/lib/listen/adapters/windows.rb +91 -0
  231. data/vendor/listen/lib/listen/directory_record.rb +406 -0
  232. data/vendor/listen/lib/listen/listener.rb +323 -0
  233. data/vendor/listen/lib/listen/turnstile.rb +32 -0
  234. data/vendor/listen/lib/listen/version.rb +3 -0
  235. data/vendor/listen/listen.gemspec +28 -0
  236. data/vendor/listen/spec/listen/adapter_spec.rb +149 -0
  237. data/vendor/listen/spec/listen/adapters/bsd_spec.rb +36 -0
  238. data/vendor/listen/spec/listen/adapters/darwin_spec.rb +37 -0
  239. data/vendor/listen/spec/listen/adapters/linux_spec.rb +47 -0
  240. data/vendor/listen/spec/listen/adapters/polling_spec.rb +68 -0
  241. data/vendor/listen/spec/listen/adapters/windows_spec.rb +30 -0
  242. data/vendor/listen/spec/listen/directory_record_spec.rb +1250 -0
  243. data/vendor/listen/spec/listen/listener_spec.rb +258 -0
  244. data/vendor/listen/spec/listen/turnstile_spec.rb +56 -0
  245. data/vendor/listen/spec/listen_spec.rb +67 -0
  246. data/vendor/listen/spec/spec_helper.rb +25 -0
  247. data/vendor/listen/spec/support/adapter_helper.rb +666 -0
  248. data/vendor/listen/spec/support/directory_record_helper.rb +57 -0
  249. data/vendor/listen/spec/support/fixtures_helper.rb +29 -0
  250. data/vendor/listen/spec/support/listeners_helper.rb +179 -0
  251. data/vendor/listen/spec/support/platform_helper.rb +15 -0
  252. metadata +217 -76
  253. data/extra/sass-spec-ref.sh +0 -40
  254. data/lib/sass/deprecation.rb +0 -55
  255. data/lib/sass/logger/delayed.rb +0 -50
  256. data/lib/sass/script/value/callable.rb +0 -25
  257. data/lib/sass/script/value/function.rb +0 -19
@@ -0,0 +1,75 @@
1
+ module Listen
2
+ module Adapters
3
+
4
+ # Listener implementation for BSD's `kqueue`.
5
+ #
6
+ class BSD < Adapter
7
+ # Watched kqueue events
8
+ #
9
+ # @see http://www.freebsd.org/cgi/man.cgi?query=kqueue
10
+ # @see https://github.com/nex3/rb-kqueue/blob/master/lib/rb-kqueue/queue.rb
11
+ #
12
+ EVENTS = [:delete, :write, :extend, :attrib, :link, :rename, :revoke]
13
+
14
+ def self.target_os_regex; /freebsd/i; end
15
+ def self.adapter_gem; 'rb-kqueue'; end
16
+
17
+ private
18
+
19
+ # Initializes a kqueue Queue and adds a watcher for each files in
20
+ # the directories passed to the adapter.
21
+ #
22
+ # @return [INotify::Notifier] initialized kqueue
23
+ #
24
+ # @see Listen::Adapter#initialize_worker
25
+ #
26
+ def initialize_worker
27
+ require 'find'
28
+
29
+ callback = lambda do |event|
30
+ path = event.watcher.path
31
+ mutex.synchronize do
32
+ # kqueue watches everything, but Listen only needs the
33
+ # directory where stuffs happens.
34
+ @changed_directories << (File.directory?(path) ? path : File.dirname(path))
35
+
36
+ # If it is a directory, and it has a write flag, it means a
37
+ # file has been added so find out which and deal with it.
38
+ # No need to check for removed files, kqueue will forget them
39
+ # when the vfs does.
40
+ if File.directory?(path) && event.flags.include?(:write)
41
+ queue = event.watcher.queue
42
+ Find.find(path) do |file|
43
+ unless queue.watchers.detect { |k,v| v.path == file.to_s }
44
+ queue.watch_file(file, *EVENTS, &callback)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ KQueue::Queue.new.tap do |queue|
52
+ directories.each do |directory|
53
+ Find.find(directory) do |path|
54
+ queue.watch_file(path, *EVENTS, &callback)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ # Starts the worker in a new thread.
61
+ #
62
+ # @see Listen::Adapter#start_worker
63
+ #
64
+ def start_worker
65
+ @worker_thread = Thread.new do
66
+ until stopped
67
+ worker.poll
68
+ sleep(latency)
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,48 @@
1
+ module Listen
2
+ module Adapters
3
+
4
+ # Adapter implementation for Mac OS X `FSEvents`.
5
+ #
6
+ class Darwin < Adapter
7
+ LAST_SEPARATOR_REGEX = /\/$/
8
+
9
+
10
+ def self.target_os_regex; /darwin(1.+)?$/i; end
11
+ def self.adapter_gem; 'rb-fsevent'; end
12
+
13
+ private
14
+
15
+ # Initializes a FSEvent worker and adds a watcher for
16
+ # each directory passed to the adapter.
17
+ #
18
+ # @return [FSEvent] initialized worker
19
+ #
20
+ # @see Listen::Adapter#initialize_worker
21
+ #
22
+ def initialize_worker
23
+ FSEvent.new.tap do |worker|
24
+ worker.watch(directories.dup, :latency => latency) do |changes|
25
+ next if paused
26
+
27
+ mutex.synchronize do
28
+ changes.each { |path| @changed_directories << path.sub(LAST_SEPARATOR_REGEX, '') }
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # Starts the worker in a new thread and sleep 0.1 second.
35
+ #
36
+ # @see Listen::Adapter#start_worker
37
+ #
38
+ def start_worker
39
+ @worker_thread = Thread.new { worker.run }
40
+ # The FSEvent worker needs some time to start up. Turnstiles can't
41
+ # be used to wait for it as it runs in a loop.
42
+ # TODO: Find a better way to block until the worker starts.
43
+ sleep 0.1
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,81 @@
1
+ module Listen
2
+ module Adapters
3
+
4
+ # Listener implementation for Linux `inotify`.
5
+ #
6
+ class Linux < Adapter
7
+ # Watched inotify events
8
+ #
9
+ # @see http://www.tin.org/bin/man.cgi?section=7&topic=inotify
10
+ # @see https://github.com/nex3/rb-inotify/blob/master/lib/rb-inotify/notifier.rb#L99-L177
11
+ #
12
+ EVENTS = [:recursive, :attrib, :create, :delete, :move, :close_write]
13
+
14
+ # The message to show when the limit of inotify watchers is not enough
15
+ #
16
+ INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '')
17
+ Listen error: unable to monitor directories for changes.
18
+
19
+ Please head to https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
20
+ for information on how to solve this issue.
21
+ EOS
22
+
23
+ def self.target_os_regex; /linux/i; end
24
+ def self.adapter_gem; 'rb-inotify'; end
25
+
26
+ # Initializes the Adapter.
27
+ #
28
+ # @see Listen::Adapter#initialize
29
+ #
30
+ def initialize(directories, options = {}, &callback)
31
+ super
32
+ rescue Errno::ENOSPC
33
+ abort(INOTIFY_LIMIT_MESSAGE)
34
+ end
35
+
36
+ private
37
+
38
+ # Initializes a INotify worker and adds a watcher for
39
+ # each directory passed to the adapter.
40
+ #
41
+ # @return [INotify::Notifier] initialized worker
42
+ #
43
+ # @see Listen::Adapter#initialize_worker
44
+ #
45
+ def initialize_worker
46
+ callback = lambda do |event|
47
+ if paused || (
48
+ # Event on root directory
49
+ event.name == ""
50
+ ) || (
51
+ # INotify reports changes to files inside directories as events
52
+ # on the directories themselves too.
53
+ #
54
+ # @see http://linux.die.net/man/7/inotify
55
+ event.flags.include?(:isdir) and (event.flags & [:close, :modify]).any?
56
+ )
57
+ # Skip all of these!
58
+ next
59
+ end
60
+
61
+ mutex.synchronize do
62
+ @changed_directories << File.dirname(event.absolute_name)
63
+ end
64
+ end
65
+
66
+ INotify::Notifier.new.tap do |worker|
67
+ directories.each { |dir| worker.watch(dir, *EVENTS, &callback) }
68
+ end
69
+ end
70
+
71
+ # Starts the worker in a new thread.
72
+ #
73
+ # @see Listen::Adapter#start_worker
74
+ #
75
+ def start_worker
76
+ @worker_thread = Thread.new { worker.run }
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,58 @@
1
+ module Listen
2
+ module Adapters
3
+
4
+ DEFAULT_POLLING_LATENCY = 1.0
5
+
6
+ # Polling Adapter that works cross-platform and
7
+ # has no dependencies. This is the adapter that
8
+ # uses the most CPU processing power and has higher
9
+ # file IO than the other implementations.
10
+ #
11
+ class Polling < Adapter
12
+ private
13
+
14
+ # The default delay between checking for changes.
15
+ #
16
+ # @see Listen::Adapter#default_latency
17
+ #
18
+ def default_latency
19
+ 1.0
20
+ end
21
+
22
+ # The thread on which the main thread should wait
23
+ # when the adapter has been started in blocking mode.
24
+ #
25
+ # @see Listen::Adapter#blocking_thread
26
+ #
27
+ def blocking_thread
28
+ poller_thread
29
+ end
30
+
31
+ # @see Listen::Adapter#start_worker
32
+ #
33
+ # @see Listen::Adapter#start_worker
34
+ #
35
+ def start_worker
36
+ # The polling adapter has no worker! Sad panda! :'(
37
+ end
38
+
39
+ # Poll listener directory for file system changes.
40
+ #
41
+ # @see Listen::Adapter#poll_changed_directories
42
+ #
43
+ def poll_changed_directories
44
+ until stopped
45
+ next if paused
46
+
47
+ start = Time.now.to_f
48
+ callback.call(directories.dup, :recursive => true)
49
+ turnstile.signal
50
+ nap_time = latency - (Time.now.to_f - start)
51
+ sleep(nap_time) if nap_time > 0
52
+ end
53
+ rescue Interrupt
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,91 @@
1
+ require 'set'
2
+ require 'rubygems'
3
+
4
+ module Listen
5
+ module Adapters
6
+
7
+ # Adapter implementation for Windows `wdm`.
8
+ #
9
+ class Windows < Adapter
10
+
11
+ BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
12
+ Please add the following to your Gemfile to avoid polling for changes:
13
+ require 'rbconfig'
14
+ gem 'wdm', '>= 0.1.0' if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
15
+ EOS
16
+
17
+ def self.target_os_regex; /mswin|mingw/i; end
18
+ def self.adapter_gem; 'wdm'; end
19
+
20
+ # Checks if the adapter is usable on target OS.
21
+ #
22
+ # @return [Boolean] whether usable or not
23
+ #
24
+ def self.usable?
25
+ super if mri? && at_least_ruby_1_9?
26
+ end
27
+
28
+ # Load the adapter gem
29
+ #
30
+ # @return [Boolean] whether required or not
31
+ #
32
+ def self.load_dependent_adapter
33
+ super
34
+ rescue Gem::LoadError
35
+ Kernel.warn BUNDLER_DECLARE_GEM
36
+ end
37
+
38
+ private
39
+
40
+ # Checks if Ruby engine is MRI.
41
+ #
42
+ # @return [Boolean]
43
+ #
44
+ def self.mri?
45
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby'
46
+ end
47
+
48
+ # Checks if Ruby engine is MRI.
49
+ #
50
+ # @return [Boolean]
51
+ #
52
+ def self.at_least_ruby_1_9?
53
+ Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('1.9.2')
54
+ end
55
+
56
+ # Initializes a WDM monitor and adds a watcher for
57
+ # each directory passed to the adapter.
58
+ #
59
+ # @return [WDM::Monitor] initialized worker
60
+ #
61
+ # @see Listen::Adapter#initialize_worker
62
+ #
63
+ def initialize_worker
64
+ callback = Proc.new do |change|
65
+ next if paused
66
+
67
+ mutex.synchronize do
68
+ @changed_directories << File.dirname(change.path)
69
+ end
70
+ end
71
+
72
+ WDM::Monitor.new.tap do |worker|
73
+ directories.each { |dir| worker.watch_recursively(dir, &callback) }
74
+ end
75
+ end
76
+
77
+ # Start the worker in a new thread and sleep 0.1 second.
78
+ #
79
+ # @see Listen::Adapter#start_worker
80
+ #
81
+ def start_worker
82
+ @worker_thread = Thread.new { worker.run! }
83
+ # Wait for the worker to start. This is needed to avoid a deadlock
84
+ # when stopping immediately after starting.
85
+ sleep 0.1
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,406 @@
1
+ require 'set'
2
+ require 'find'
3
+ require 'digest/sha1'
4
+
5
+ module Listen
6
+
7
+ # The directory record stores information about
8
+ # a directory and keeps track of changes to
9
+ # the structure of its childs.
10
+ #
11
+ class DirectoryRecord
12
+ attr_reader :directory, :paths, :sha1_checksums
13
+
14
+ # The default list of directories that get ignored by the listener.
15
+ DEFAULT_IGNORED_DIRECTORIES = %w[.rbx .bundle .git .svn bundle log tmp vendor]
16
+
17
+ # The default list of files that get ignored by the listener.
18
+ DEFAULT_IGNORED_EXTENSIONS = %w[.DS_Store]
19
+
20
+ # Defines the used precision based on the type of mtime returned by the
21
+ # system (whether its in milliseconds or just seconds)
22
+ #
23
+ begin
24
+ HIGH_PRECISION_SUPPORTED = File.mtime(__FILE__).to_f.to_s[-2..-1] != '.0'
25
+ rescue
26
+ HIGH_PRECISION_SUPPORTED = false
27
+ end
28
+
29
+ # Data structure used to save meta data about a path
30
+ #
31
+ MetaData = Struct.new(:type, :mtime)
32
+
33
+ # Class methods
34
+ #
35
+ class << self
36
+
37
+ # Creates the ignoring patterns from the default ignored
38
+ # directories and extensions. It memoizes the generated patterns
39
+ # to avoid unnecessary computation.
40
+ #
41
+ def generate_default_ignoring_patterns
42
+ @@default_ignoring_patterns ||= Array.new.tap do |default_patterns|
43
+ # Add directories
44
+ ignored_directories = DEFAULT_IGNORED_DIRECTORIES.map { |d| Regexp.escape(d) }
45
+ default_patterns << %r{^(?:#{ignored_directories.join('|')})/}
46
+
47
+ # Add extensions
48
+ ignored_extensions = DEFAULT_IGNORED_EXTENSIONS.map { |e| Regexp.escape(e) }
49
+ default_patterns << %r{(?:#{ignored_extensions.join('|')})$}
50
+ end
51
+ end
52
+ end
53
+
54
+ # Initializes a directory record.
55
+ #
56
+ # @option [String] directory the directory to keep track of
57
+ #
58
+ def initialize(directory)
59
+ raise ArgumentError, "The path '#{directory}' is not a directory!" unless File.directory?(directory)
60
+
61
+ @directory, @sha1_checksums = File.expand_path(directory), Hash.new
62
+ @ignoring_patterns, @filtering_patterns = Set.new, Set.new
63
+
64
+ @ignoring_patterns.merge(DirectoryRecord.generate_default_ignoring_patterns)
65
+ end
66
+
67
+ # Returns the ignoring patterns in the record to know
68
+ # which paths should be ignored.
69
+ #
70
+ # @return [Array<Regexp>] the ignoring patterns
71
+ #
72
+ def ignoring_patterns
73
+ @ignoring_patterns.to_a
74
+ end
75
+
76
+ # Returns the filtering patterns in the record to know
77
+ # which paths should be stored.
78
+ #
79
+ # @return [Array<Regexp>] the filtering patterns
80
+ #
81
+ def filtering_patterns
82
+ @filtering_patterns.to_a
83
+ end
84
+
85
+ # Adds ignoring patterns to the record.
86
+ #
87
+ # @example Ignore some paths
88
+ # ignore %r{^ignored/path/}, /man/
89
+ #
90
+ # @param [Regexp] regexps a list of patterns for ignoring paths
91
+ #
92
+ def ignore(*regexps)
93
+ @ignoring_patterns.merge(regexps).reject! { |r| r.nil? }
94
+ end
95
+
96
+ # Replaces ignoring patterns in the record.
97
+ #
98
+ # @example Ignore only these paths
99
+ # ignore! %r{^ignored/path/}, /man/
100
+ #
101
+ # @param [Regexp] regexps a list of patterns for ignoring paths
102
+ #
103
+ def ignore!(*regexps)
104
+ @ignoring_patterns.replace(regexps).reject! { |r| r.nil? }
105
+ end
106
+
107
+ # Adds filtering patterns to the record.
108
+ #
109
+ # @example Filter some files
110
+ # filter /\.txt$/, /.*\.zip/
111
+ #
112
+ # @param [Regexp] regexps a list of patterns for filtering files
113
+ #
114
+ def filter(*regexps)
115
+ @filtering_patterns.merge(regexps).reject! { |r| r.nil? }
116
+ end
117
+
118
+ # Replaces filtering patterns in the record.
119
+ #
120
+ # @example Filter only these files
121
+ # filter! /\.txt$/, /.*\.zip/
122
+ #
123
+ # @param [Regexp] regexps a list of patterns for filtering files
124
+ #
125
+ def filter!(*regexps)
126
+ @filtering_patterns.replace(regexps).reject! { |r| r.nil? }
127
+ end
128
+
129
+ # Returns whether a path should be ignored or not.
130
+ #
131
+ # @param [String] path the path to test
132
+ #
133
+ # @return [Boolean]
134
+ #
135
+ def ignored?(path)
136
+ path = relative_to_base(path)
137
+ @ignoring_patterns.any? { |pattern| pattern =~ path }
138
+ end
139
+
140
+ # Returns whether a path should be filtered or not.
141
+ #
142
+ # @param [String] path the path to test
143
+ #
144
+ # @return [Boolean]
145
+ #
146
+ def filtered?(path)
147
+ # When no filtering patterns are set, ALL files are stored.
148
+ return true if @filtering_patterns.empty?
149
+
150
+ path = relative_to_base(path)
151
+ @filtering_patterns.any? { |pattern| pattern =~ path }
152
+ end
153
+
154
+ # Finds the paths that should be stored and adds them
155
+ # to the paths' hash.
156
+ #
157
+ def build
158
+ @paths = Hash.new { |h, k| h[k] = Hash.new }
159
+ important_paths { |path| insert_path(path) }
160
+ end
161
+
162
+ # Detects changes in the passed directories, updates
163
+ # the record with the new changes and returns the changes.
164
+ #
165
+ # @param [Array] directories the list of directories to scan for changes
166
+ # @param [Hash] options
167
+ # @option options [Boolean] recursive scan all sub-directories recursively
168
+ # @option options [Boolean] relative_paths whether or not to use relative paths for changes
169
+ #
170
+ # @return [Hash<Array>] the changes
171
+ #
172
+ def fetch_changes(directories, options = {})
173
+ @changes = { :modified => [], :added => [], :removed => [] }
174
+ directories = directories.sort_by { |el| el.length }.reverse # diff sub-dir first
175
+
176
+ directories.each do |directory|
177
+ next unless directory[@directory] # Path is or inside directory
178
+
179
+ detect_modifications_and_removals(directory, options)
180
+ detect_additions(directory, options)
181
+ end
182
+
183
+ @changes
184
+ end
185
+
186
+ # Converts an absolute path to a path that's relative to the base directory.
187
+ #
188
+ # @param [String] path the path to convert
189
+ #
190
+ # @return [String] the relative path
191
+ #
192
+ def relative_to_base(path)
193
+ path = path.dup
194
+ regexp = "\\A#{Regexp.quote directory}(#{File::SEPARATOR}|\\z)"
195
+ if path.respond_to?(:force_encoding)
196
+ path.force_encoding("BINARY")
197
+ regexp.force_encoding("BINARY")
198
+ end
199
+ if path.sub!(Regexp.new(regexp), '')
200
+ path
201
+ end
202
+ end
203
+
204
+ private
205
+
206
+ # Detects modifications and removals recursively in a directory.
207
+ #
208
+ # @note Modifications detection begins by checking the modification time (mtime)
209
+ # of files and then by checking content changes (using SHA1-checksum)
210
+ # when the mtime of files is not changed.
211
+ #
212
+ # @param [String] directory the path to analyze
213
+ # @param [Hash] options
214
+ # @option options [Boolean] recursive scan all sub-directories recursively
215
+ # @option options [Boolean] relative_paths whether or not to use relative paths for changes
216
+ #
217
+ def detect_modifications_and_removals(directory, options = {})
218
+ paths[directory].each do |basename, meta_data|
219
+ path = File.join(directory, basename)
220
+ case meta_data.type
221
+ when 'Dir'
222
+ detect_modification_or_removal_for_dir(path, options)
223
+ when 'File'
224
+ detect_modification_or_removal_for_file(path, meta_data, options)
225
+ end
226
+ end
227
+ end
228
+
229
+ def detect_modification_or_removal_for_dir(path, options)
230
+
231
+ # Directory still exists
232
+ if File.directory?(path)
233
+ detect_modifications_and_removals(path, options) if options[:recursive]
234
+
235
+ # Directory has been removed
236
+ else
237
+ detect_modifications_and_removals(path, options)
238
+ @paths[File.dirname(path)].delete(File.basename(path))
239
+ @paths.delete("#{File.dirname(path)}/#{File.basename(path)}")
240
+ end
241
+ end
242
+
243
+ def detect_modification_or_removal_for_file(path, meta_data, options)
244
+ # File still exists
245
+ if File.exist?(path)
246
+ detect_modification(path, meta_data, options)
247
+
248
+ # File has been removed
249
+ else
250
+ removal_detected(path, meta_data, options)
251
+ end
252
+ end
253
+
254
+ def detect_modification(path, meta_data, options)
255
+ new_mtime = mtime_of(path)
256
+
257
+ # First check if we are in the same second (to update checksums)
258
+ # before checking the time difference
259
+ if (meta_data.mtime.to_i == new_mtime.to_i && content_modified?(path)) || meta_data.mtime < new_mtime
260
+ modification_detected(path, meta_data, new_mtime, options)
261
+ end
262
+ end
263
+
264
+ def modification_detected(path, meta_data, new_mtime, options)
265
+ # Update the sha1 checksum of the file
266
+ update_sha1_checksum(path)
267
+
268
+ # Update the meta data of the file
269
+ meta_data.mtime = new_mtime
270
+ @paths[File.dirname(path)][File.basename(path)] = meta_data
271
+
272
+ @changes[:modified] << (options[:relative_paths] ? relative_to_base(path) : path)
273
+ end
274
+
275
+ def removal_detected(path, meta_data, options)
276
+ @paths[File.dirname(path)].delete(File.basename(path))
277
+ @sha1_checksums.delete(path)
278
+ @changes[:removed] << (options[:relative_paths] ? relative_to_base(path) : path)
279
+ end
280
+
281
+ # Detects additions in a directory.
282
+ #
283
+ # @param [String] directory the path to analyze
284
+ # @param [Hash] options
285
+ # @option options [Boolean] recursive scan all sub-directories recursively
286
+ # @option options [Boolean] relative_paths whether or not to use relative paths for changes
287
+ #
288
+ def detect_additions(directory, options = {})
289
+ # Don't process removed directories
290
+ return unless File.exist?(directory)
291
+
292
+ Find.find(directory) do |path|
293
+ next if path == @directory
294
+
295
+ if File.directory?(path)
296
+ # Add a trailing slash to directories when checking if a directory is
297
+ # ignored to optimize finding them as Find.find doesn't.
298
+ if ignored?(path + File::SEPARATOR) || (directory != path && (!options[:recursive] && existing_path?(path)))
299
+ Find.prune # Don't look any further into this directory.
300
+ else
301
+ insert_path(path)
302
+ end
303
+ elsif !ignored?(path) && filtered?(path) && !existing_path?(path)
304
+ if File.file?(path)
305
+ @changes[:added] << (options[:relative_paths] ? relative_to_base(path) : path)
306
+ insert_path(path)
307
+ end
308
+ end
309
+ end
310
+ end
311
+
312
+ # Returns whether or not a file's content has been modified by
313
+ # comparing the SHA1-checksum to a stored one.
314
+ # Ensure that the SHA1-checksum is inserted to the sha1_checksums
315
+ # array for later comparaison if false.
316
+ #
317
+ # @param [String] path the file path
318
+ #
319
+ def content_modified?(path)
320
+ return false unless File.ftype(path) == 'file'
321
+ @sha1_checksum = sha1_checksum(path)
322
+ if sha1_checksums[path] == @sha1_checksum || !sha1_checksums.key?(path)
323
+ update_sha1_checksum(path)
324
+ false
325
+ else
326
+ true
327
+ end
328
+ end
329
+
330
+ # Inserts a SHA1-checksum path in @SHA1-checksums hash.
331
+ #
332
+ # @param [String] path the SHA1-checksum path to insert in @sha1_checksums.
333
+ #
334
+ def update_sha1_checksum(path)
335
+ if @sha1_checksum ||= sha1_checksum(path)
336
+ @sha1_checksums[path] = @sha1_checksum
337
+ @sha1_checksum = nil
338
+ end
339
+ end
340
+
341
+ # Returns the SHA1-checksum for the file path.
342
+ #
343
+ # @param [String] path the file path
344
+ #
345
+ def sha1_checksum(path)
346
+ Digest::SHA1.file(path).to_s
347
+ rescue
348
+ nil
349
+ end
350
+
351
+ # Traverses the base directory looking for paths that should
352
+ # be stored; thus paths that are filtered or not ignored.
353
+ #
354
+ # @yield [path] an important path
355
+ #
356
+ def important_paths
357
+ Find.find(directory) do |path|
358
+ next if path == directory
359
+
360
+ if File.directory?(path)
361
+ # Add a trailing slash to directories when checking if a directory is
362
+ # ignored to optimize finding them as Find.find doesn't.
363
+ if ignored?(path + File::SEPARATOR)
364
+ Find.prune # Don't look any further into this directory.
365
+ else
366
+ yield(path)
367
+ end
368
+ elsif !ignored?(path) && filtered?(path)
369
+ yield(path)
370
+ end
371
+ end
372
+ end
373
+
374
+ # Inserts a path with its type (Dir or File) in paths hash.
375
+ #
376
+ # @param [String] path the path to insert in @paths.
377
+ #
378
+ def insert_path(path)
379
+ meta_data = MetaData.new
380
+ meta_data.type = File.directory?(path) ? 'Dir' : 'File'
381
+ meta_data.mtime = mtime_of(path) unless meta_data.type == 'Dir' # mtimes of dirs are not used yet
382
+ @paths[File.dirname(path)][File.basename(path)] = meta_data
383
+ rescue Errno::ENOENT
384
+ end
385
+
386
+ # Returns whether or not a path exists in the paths hash.
387
+ #
388
+ # @param [String] path the path to check
389
+ #
390
+ # @return [Boolean]
391
+ #
392
+ def existing_path?(path)
393
+ paths[File.dirname(path)][File.basename(path)] != nil
394
+ end
395
+
396
+ # Returns the modification time of a file based on the precision defined by the system
397
+ #
398
+ # @param [String] file the file for which the mtime must be returned
399
+ #
400
+ # @return [Fixnum, Float] the mtime of the file
401
+ #
402
+ def mtime_of(file)
403
+ File.lstat(file).mtime.send(HIGH_PRECISION_SUPPORTED ? :to_f : :to_i)
404
+ end
405
+ end
406
+ end