sass 3.4.25 → 3.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CODE_OF_CONDUCT.md +1 -1
  3. data/CONTRIBUTING.md +3 -3
  4. data/README.md +17 -9
  5. data/VERSION +1 -1
  6. data/VERSION_DATE +1 -1
  7. data/VERSION_NAME +1 -1
  8. data/extra/sass-spec-ref.sh +9 -1
  9. data/lib/sass.rb +0 -7
  10. data/lib/sass/cache_stores/filesystem.rb +1 -1
  11. data/lib/sass/css.rb +1 -2
  12. data/lib/sass/engine.rb +39 -29
  13. data/lib/sass/environment.rb +26 -5
  14. data/lib/sass/error.rb +2 -2
  15. data/lib/sass/exec/base.rb +2 -13
  16. data/lib/sass/exec/sass_scss.rb +1 -5
  17. data/lib/sass/features.rb +1 -0
  18. data/lib/sass/importers/filesystem.rb +6 -4
  19. data/lib/sass/logger/base.rb +11 -0
  20. data/lib/sass/plugin/compiler.rb +20 -50
  21. data/lib/sass/plugin/configuration.rb +2 -2
  22. data/lib/sass/railtie.rb +1 -1
  23. data/lib/sass/script/css_parser.rb +4 -1
  24. data/lib/sass/script/functions.rb +308 -81
  25. data/lib/sass/script/lexer.rb +63 -9
  26. data/lib/sass/script/parser.rb +287 -118
  27. data/lib/sass/script/tree/funcall.rb +35 -34
  28. data/lib/sass/script/tree/interpolation.rb +0 -3
  29. data/lib/sass/script/tree/list_literal.rb +23 -8
  30. data/lib/sass/script/tree/map_literal.rb +2 -2
  31. data/lib/sass/script/tree/node.rb +0 -8
  32. data/lib/sass/script/tree/operation.rb +1 -8
  33. data/lib/sass/script/value.rb +2 -0
  34. data/lib/sass/script/value/arg_list.rb +1 -1
  35. data/lib/sass/script/value/base.rb +17 -0
  36. data/lib/sass/script/value/callable.rb +25 -0
  37. data/lib/sass/script/value/color.rb +8 -2
  38. data/lib/sass/script/value/function.rb +19 -0
  39. data/lib/sass/script/value/helpers.rb +37 -11
  40. data/lib/sass/script/value/list.rb +35 -14
  41. data/lib/sass/script/value/map.rb +2 -2
  42. data/lib/sass/script/value/number.rb +3 -2
  43. data/lib/sass/scss/css_parser.rb +6 -1
  44. data/lib/sass/scss/parser.rb +145 -56
  45. data/lib/sass/scss/rx.rb +5 -11
  46. data/lib/sass/scss/static_parser.rb +20 -42
  47. data/lib/sass/selector.rb +4 -0
  48. data/lib/sass/selector/abstract_sequence.rb +7 -6
  49. data/lib/sass/selector/comma_sequence.rb +9 -5
  50. data/lib/sass/selector/pseudo.rb +20 -3
  51. data/lib/sass/selector/sequence.rb +35 -10
  52. data/lib/sass/selector/simple.rb +9 -2
  53. data/lib/sass/selector/simple_sequence.rb +8 -4
  54. data/lib/sass/source/map.rb +2 -6
  55. data/lib/sass/stack.rb +21 -1
  56. data/lib/sass/tree/charset_node.rb +1 -1
  57. data/lib/sass/tree/prop_node.rb +45 -53
  58. data/lib/sass/tree/rule_node.rb +6 -8
  59. data/lib/sass/tree/visitors/check_nesting.rb +1 -1
  60. data/lib/sass/tree/visitors/convert.rb +2 -3
  61. data/lib/sass/tree/visitors/cssize.rb +4 -15
  62. data/lib/sass/tree/visitors/deep_copy.rb +1 -1
  63. data/lib/sass/tree/visitors/extend.rb +2 -8
  64. data/lib/sass/tree/visitors/perform.rb +23 -15
  65. data/lib/sass/tree/visitors/set_options.rb +1 -1
  66. data/lib/sass/tree/visitors/to_css.rb +49 -22
  67. data/lib/sass/util.rb +72 -310
  68. data/lib/sass/util/multibyte_string_scanner.rb +127 -131
  69. data/lib/sass/util/normalized_map.rb +1 -8
  70. data/lib/sass/version.rb +0 -4
  71. metadata +55 -202
  72. data/Rakefile +0 -453
  73. data/lib/sass/script/css_variable_warning.rb +0 -52
  74. data/lib/sass/util/cross_platform_random.rb +0 -19
  75. data/lib/sass/util/ordered_hash.rb +0 -192
  76. data/test/sass-spec.yml +0 -3
  77. data/test/sass/cache_test.rb +0 -131
  78. data/test/sass/callbacks_test.rb +0 -61
  79. data/test/sass/compiler_test.rb +0 -236
  80. data/test/sass/conversion_test.rb +0 -2188
  81. data/test/sass/css2sass_test.rb +0 -526
  82. data/test/sass/css_variable_test.rb +0 -132
  83. data/test/sass/data/hsl-rgb.txt +0 -319
  84. data/test/sass/encoding_test.rb +0 -219
  85. data/test/sass/engine_test.rb +0 -3447
  86. data/test/sass/exec_test.rb +0 -96
  87. data/test/sass/extend_test.rb +0 -1733
  88. data/test/sass/fixtures/test_staleness_check_across_importers.css +0 -1
  89. data/test/sass/fixtures/test_staleness_check_across_importers.scss +0 -1
  90. data/test/sass/functions_test.rb +0 -1977
  91. data/test/sass/importer_test.rb +0 -421
  92. data/test/sass/logger_test.rb +0 -58
  93. data/test/sass/mock_importer.rb +0 -49
  94. data/test/sass/more_results/more1.css +0 -9
  95. data/test/sass/more_results/more1_with_line_comments.css +0 -26
  96. data/test/sass/more_results/more_import.css +0 -29
  97. data/test/sass/more_templates/_more_partial.sass +0 -2
  98. data/test/sass/more_templates/more1.sass +0 -23
  99. data/test/sass/more_templates/more_import.sass +0 -11
  100. data/test/sass/plugin_test.rb +0 -556
  101. data/test/sass/results/alt.css +0 -4
  102. data/test/sass/results/basic.css +0 -9
  103. data/test/sass/results/cached_import_option.css +0 -3
  104. data/test/sass/results/compact.css +0 -5
  105. data/test/sass/results/complex.css +0 -86
  106. data/test/sass/results/compressed.css +0 -1
  107. data/test/sass/results/expanded.css +0 -19
  108. data/test/sass/results/filename_fn.css +0 -3
  109. data/test/sass/results/if.css +0 -3
  110. data/test/sass/results/import.css +0 -31
  111. data/test/sass/results/import_charset.css +0 -5
  112. data/test/sass/results/import_charset_1_8.css +0 -5
  113. data/test/sass/results/import_charset_ibm866.css +0 -5
  114. data/test/sass/results/import_content.css +0 -1
  115. data/test/sass/results/line_numbers.css +0 -49
  116. data/test/sass/results/mixins.css +0 -95
  117. data/test/sass/results/multiline.css +0 -24
  118. data/test/sass/results/nested.css +0 -22
  119. data/test/sass/results/options.css +0 -1
  120. data/test/sass/results/parent_ref.css +0 -13
  121. data/test/sass/results/script.css +0 -16
  122. data/test/sass/results/scss_import.css +0 -31
  123. data/test/sass/results/scss_importee.css +0 -2
  124. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +0 -1
  125. data/test/sass/results/subdir/subdir.css +0 -3
  126. data/test/sass/results/units.css +0 -11
  127. data/test/sass/results/warn.css +0 -0
  128. data/test/sass/results/warn_imported.css +0 -0
  129. data/test/sass/script_conversion_test.rb +0 -357
  130. data/test/sass/script_test.rb +0 -1431
  131. data/test/sass/scss/css_test.rb +0 -1281
  132. data/test/sass/scss/rx_test.rb +0 -160
  133. data/test/sass/scss/scss_test.rb +0 -4205
  134. data/test/sass/scss/test_helper.rb +0 -37
  135. data/test/sass/source_map_test.rb +0 -1055
  136. data/test/sass/superselector_test.rb +0 -210
  137. data/test/sass/templates/_cached_import_option_partial.scss +0 -1
  138. data/test/sass/templates/_double_import_loop2.sass +0 -1
  139. data/test/sass/templates/_filename_fn_import.scss +0 -11
  140. data/test/sass/templates/_imported_charset_ibm866.sass +0 -4
  141. data/test/sass/templates/_imported_charset_utf8.sass +0 -4
  142. data/test/sass/templates/_imported_content.sass +0 -3
  143. data/test/sass/templates/_partial.sass +0 -2
  144. data/test/sass/templates/_same_name_different_partiality.scss +0 -1
  145. data/test/sass/templates/alt.sass +0 -16
  146. data/test/sass/templates/basic.sass +0 -23
  147. data/test/sass/templates/bork1.sass +0 -2
  148. data/test/sass/templates/bork2.sass +0 -2
  149. data/test/sass/templates/bork3.sass +0 -2
  150. data/test/sass/templates/bork4.sass +0 -2
  151. data/test/sass/templates/bork5.sass +0 -3
  152. data/test/sass/templates/cached_import_option.scss +0 -3
  153. data/test/sass/templates/compact.sass +0 -17
  154. data/test/sass/templates/complex.sass +0 -305
  155. data/test/sass/templates/compressed.sass +0 -15
  156. data/test/sass/templates/double_import_loop1.sass +0 -1
  157. data/test/sass/templates/expanded.sass +0 -17
  158. data/test/sass/templates/filename_fn.scss +0 -18
  159. data/test/sass/templates/if.sass +0 -11
  160. data/test/sass/templates/import.sass +0 -12
  161. data/test/sass/templates/import_charset.sass +0 -9
  162. data/test/sass/templates/import_charset_1_8.sass +0 -6
  163. data/test/sass/templates/import_charset_ibm866.sass +0 -11
  164. data/test/sass/templates/import_content.sass +0 -4
  165. data/test/sass/templates/importee.less +0 -2
  166. data/test/sass/templates/importee.sass +0 -19
  167. data/test/sass/templates/line_numbers.sass +0 -13
  168. data/test/sass/templates/mixin_bork.sass +0 -5
  169. data/test/sass/templates/mixins.sass +0 -76
  170. data/test/sass/templates/multiline.sass +0 -20
  171. data/test/sass/templates/nested.sass +0 -25
  172. data/test/sass/templates/nested_bork1.sass +0 -2
  173. data/test/sass/templates/nested_bork2.sass +0 -2
  174. data/test/sass/templates/nested_bork3.sass +0 -2
  175. data/test/sass/templates/nested_bork4.sass +0 -2
  176. data/test/sass/templates/nested_import.sass +0 -2
  177. data/test/sass/templates/nested_mixin_bork.sass +0 -6
  178. data/test/sass/templates/options.sass +0 -2
  179. data/test/sass/templates/parent_ref.sass +0 -25
  180. data/test/sass/templates/same_name_different_ext.sass +0 -2
  181. data/test/sass/templates/same_name_different_ext.scss +0 -1
  182. data/test/sass/templates/same_name_different_partiality.scss +0 -1
  183. data/test/sass/templates/script.sass +0 -101
  184. data/test/sass/templates/scss_import.scss +0 -12
  185. data/test/sass/templates/scss_importee.scss +0 -1
  186. data/test/sass/templates/single_import_loop.sass +0 -1
  187. data/test/sass/templates/subdir/import_up1.scss +0 -1
  188. data/test/sass/templates/subdir/import_up2.scss +0 -1
  189. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +0 -2
  190. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +0 -3
  191. data/test/sass/templates/subdir/subdir.sass +0 -6
  192. data/test/sass/templates/units.sass +0 -11
  193. data/test/sass/templates/warn.sass +0 -3
  194. data/test/sass/templates/warn_imported.sass +0 -4
  195. data/test/sass/test_helper.rb +0 -8
  196. data/test/sass/util/multibyte_string_scanner_test.rb +0 -155
  197. data/test/sass/util/normalized_map_test.rb +0 -51
  198. data/test/sass/util/subset_map_test.rb +0 -91
  199. data/test/sass/util_test.rb +0 -438
  200. data/test/sass/value_helpers_test.rb +0 -179
  201. data/test/test_helper.rb +0 -110
  202. data/vendor/listen/CHANGELOG.md +0 -1
  203. data/vendor/listen/CONTRIBUTING.md +0 -38
  204. data/vendor/listen/Gemfile +0 -20
  205. data/vendor/listen/Guardfile +0 -8
  206. data/vendor/listen/LICENSE +0 -20
  207. data/vendor/listen/README.md +0 -349
  208. data/vendor/listen/Rakefile +0 -5
  209. data/vendor/listen/Vagrantfile +0 -96
  210. data/vendor/listen/lib/listen.rb +0 -54
  211. data/vendor/listen/lib/listen/adapter.rb +0 -327
  212. data/vendor/listen/lib/listen/adapters/bsd.rb +0 -75
  213. data/vendor/listen/lib/listen/adapters/darwin.rb +0 -48
  214. data/vendor/listen/lib/listen/adapters/linux.rb +0 -81
  215. data/vendor/listen/lib/listen/adapters/polling.rb +0 -58
  216. data/vendor/listen/lib/listen/adapters/windows.rb +0 -91
  217. data/vendor/listen/lib/listen/directory_record.rb +0 -406
  218. data/vendor/listen/lib/listen/listener.rb +0 -323
  219. data/vendor/listen/lib/listen/turnstile.rb +0 -32
  220. data/vendor/listen/lib/listen/version.rb +0 -3
  221. data/vendor/listen/listen.gemspec +0 -28
  222. data/vendor/listen/spec/listen/adapter_spec.rb +0 -149
  223. data/vendor/listen/spec/listen/adapters/bsd_spec.rb +0 -36
  224. data/vendor/listen/spec/listen/adapters/darwin_spec.rb +0 -37
  225. data/vendor/listen/spec/listen/adapters/linux_spec.rb +0 -47
  226. data/vendor/listen/spec/listen/adapters/polling_spec.rb +0 -68
  227. data/vendor/listen/spec/listen/adapters/windows_spec.rb +0 -30
  228. data/vendor/listen/spec/listen/directory_record_spec.rb +0 -1250
  229. data/vendor/listen/spec/listen/listener_spec.rb +0 -258
  230. data/vendor/listen/spec/listen/turnstile_spec.rb +0 -56
  231. data/vendor/listen/spec/listen_spec.rb +0 -67
  232. data/vendor/listen/spec/spec_helper.rb +0 -25
  233. data/vendor/listen/spec/support/adapter_helper.rb +0 -666
  234. data/vendor/listen/spec/support/directory_record_helper.rb +0 -57
  235. data/vendor/listen/spec/support/fixtures_helper.rb +0 -29
  236. data/vendor/listen/spec/support/listeners_helper.rb +0 -179
  237. data/vendor/listen/spec/support/platform_helper.rb +0 -15
@@ -1,5 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
- task :default => :spec
@@ -1,96 +0,0 @@
1
- # -*- mode: ruby -*-
2
- # vi: set ft=ruby :
3
-
4
- Vagrant::Config.run do |config|
5
- # All Vagrant configuration is done here. The most common configuration
6
- # options are documented and commented below. For a complete reference,
7
- # please see the online documentation at vagrantup.com.
8
-
9
- # Every Vagrant virtual environment requires a box to build off of.
10
- config.vm.box = "lucid32"
11
-
12
- # The url from where the 'config.vm.box' box will be fetched if it
13
- # doesn't already exist on the user's system.
14
- # config.vm.box_url = "http://domain.com/path/to/above.box"
15
-
16
- # Boot with a GUI so you can see the screen. (Default is headless)
17
- # config.vm.boot_mode = :gui
18
-
19
- # Assign this VM to a host-only network IP, allowing you to access it
20
- # via the IP. Host-only networks can talk to the host machine as well as
21
- # any other machines on the same network, but cannot be accessed (through this
22
- # network interface) by any external networks.
23
- # config.vm.network :hostonly, "33.33.33.10"
24
-
25
- # Assign this VM to a bridged network, allowing you to connect directly to a
26
- # network using the host's network device. This makes the VM appear as another
27
- # physical device on your network.
28
- # config.vm.network :bridged
29
-
30
- # Forward a port from the guest to the host, which allows for outside
31
- # computers to access the VM, whereas host only networking does not.
32
- # config.vm.forward_port 80, 8080
33
-
34
- # Share an additional folder to the guest VM. The first argument is
35
- # an identifier, the second is the path on the guest to mount the
36
- # folder, and the third is the path on the host to the actual folder.
37
- # config.vm.share_folder "v-data", "/vagrant_data", "../data"
38
-
39
- # Enable provisioning with Puppet stand alone. Puppet manifests
40
- # are contained in a directory path relative to this Vagrantfile.
41
- # You will need to create the manifests directory and a manifest in
42
- # the file lucid32.pp in the manifests_path directory.
43
- #
44
- # An example Puppet manifest to provision the message of the day:
45
- #
46
- # # group { "puppet":
47
- # # ensure => "present",
48
- # # }
49
- # #
50
- # # File { owner => 0, group => 0, mode => 0644 }
51
- # #
52
- # # file { '/etc/motd':
53
- # # content => "Welcome to your Vagrant-built virtual machine!
54
- # # Managed by Puppet.\n"
55
- # # }
56
- #
57
- # config.vm.provision :puppet do |puppet|
58
- # puppet.manifests_path = "manifests"
59
- # puppet.manifest_file = "lucid32.pp"
60
- # end
61
-
62
- # Enable provisioning with chef solo, specifying a cookbooks path (relative
63
- # to this Vagrantfile), and adding some recipes and/or roles.
64
- #
65
- # config.vm.provision :chef_solo do |chef|
66
- # chef.cookbooks_path = "cookbooks"
67
- # chef.add_recipe "mysql"
68
- # chef.add_role "web"
69
- #
70
- # # You may also specify custom JSON attributes:
71
- # chef.json = { :mysql_password => "foo" }
72
- # end
73
-
74
- # Enable provisioning with chef server, specifying the chef server URL,
75
- # and the path to the validation key (relative to this Vagrantfile).
76
- #
77
- # The Opscode Platform uses HTTPS. Substitute your organization for
78
- # ORGNAME in the URL and validation key.
79
- #
80
- # If you have your own Chef Server, use the appropriate URL, which may be
81
- # HTTP instead of HTTPS depending on your configuration. Also change the
82
- # validation key to validation.pem.
83
- #
84
- # config.vm.provision :chef_client do |chef|
85
- # chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
86
- # chef.validation_key_path = "ORGNAME-validator.pem"
87
- # end
88
- #
89
- # If you're using the Opscode platform, your validator client is
90
- # ORGNAME-validator, replacing ORGNAME with your organization name.
91
- #
92
- # IF you have your own Chef Server, the default validation client name is
93
- # chef-validator, unless you changed the configuration.
94
- #
95
- # chef.validation_client_name = "ORGNAME-validator"
96
- end
@@ -1,54 +0,0 @@
1
- require 'listen/turnstile'
2
- require 'listen/listener'
3
- require 'listen/directory_record'
4
- require 'listen/adapter'
5
-
6
- module Listen
7
-
8
- module Adapters
9
- Adapter::ADAPTERS.each do |adapter|
10
- require "listen/adapters/#{adapter.downcase}"
11
- end
12
- end
13
-
14
- # Listens to file system modifications on a either single directory or multiple directories.
15
- # When calling this method, the current thread is not blocked.
16
- #
17
- # @param (see Listen::Listener#new)
18
- #
19
- # @yield [modified, added, removed] the changed files
20
- # @yieldparam [Array<String>] modified the list of modified files
21
- # @yieldparam [Array<String>] added the list of added files
22
- # @yieldparam [Array<String>] removed the list of removed files
23
- #
24
- # @return [Listen::Listener] the file listener if no block given
25
- #
26
- def self.to(*args, &block)
27
- listener = _init_listener(*args, &block)
28
-
29
- block ? listener.start : listener
30
- end
31
-
32
- # Listens to file system modifications on a either single directory or multiple directories.
33
- # When calling this method, the current thread is blocked.
34
- #
35
- # @param (see Listen::Listener#new)
36
- #
37
- # @yield [modified, added, removed] the changed files
38
- # @yieldparam [Array<String>] modified the list of modified files
39
- # @yieldparam [Array<String>] added the list of added files
40
- # @yieldparam [Array<String>] removed the list of removed files
41
- #
42
- # @since 1.0.0
43
- #
44
- def self.to!(*args, &block)
45
- _init_listener(*args, &block).start!
46
- end
47
-
48
- # @private
49
- #
50
- def self._init_listener(*args, &block)
51
- Listener.new(*args, &block)
52
- end
53
-
54
- end
@@ -1,327 +0,0 @@
1
- require 'rbconfig'
2
- require 'thread'
3
- require 'set'
4
- require 'fileutils'
5
-
6
- module Listen
7
- class Adapter
8
- attr_accessor :directories, :callback, :stopped, :paused,
9
- :mutex, :changed_directories, :turnstile, :latency,
10
- :worker, :worker_thread, :poller_thread
11
-
12
- # The list of existing optimized adapters.
13
- OPTIMIZED_ADAPTERS = %w[Darwin Linux BSD Windows]
14
-
15
- # The list of existing fallback adapters.
16
- FALLBACK_ADAPTERS = %w[Polling]
17
-
18
- # The list of all existing adapters.
19
- ADAPTERS = OPTIMIZED_ADAPTERS + FALLBACK_ADAPTERS
20
-
21
- # The default delay between checking for changes.
22
- DEFAULT_LATENCY = 0.25
23
-
24
- # The default warning message when falling back to polling adapter.
25
- POLLING_FALLBACK_MESSAGE = <<-EOS.gsub(/^\s*/, '')
26
- Listen will be polling for changes. Learn more at https://github.com/guard/listen#polling-fallback.
27
- EOS
28
-
29
- # Selects the appropriate adapter implementation for the
30
- # current OS and initializes it.
31
- #
32
- # @param [String, Array<String>] directories the directories to watch
33
- # @param [Hash] options the adapter options
34
- # @option options [Boolean] force_polling to force polling or not
35
- # @option options [String, Boolean] polling_fallback_message to change polling fallback message or remove it
36
- # @option options [Float] latency the delay between checking for changes in seconds
37
- #
38
- # @yield [changed_directories, options] callback the callback called when a change happens
39
- # @yieldparam [Array<String>] changed_directories the changed directories
40
- # @yieldparam [Hash] options callback options (like recursive: true)
41
- #
42
- # @return [Listen::Adapter] the chosen adapter
43
- #
44
- def self.select_and_initialize(directories, options = {}, &callback)
45
- forced_adapter_class = options.delete(:force_adapter)
46
- force_polling = options.delete(:force_polling)
47
-
48
- if forced_adapter_class
49
- forced_adapter_class.load_dependent_adapter
50
- return forced_adapter_class.new(directories, options, &callback)
51
- end
52
-
53
- return Adapters::Polling.new(directories, options, &callback) if force_polling
54
-
55
- OPTIMIZED_ADAPTERS.each do |adapter|
56
- namespaced_adapter = Adapters.const_get(adapter)
57
- if namespaced_adapter.send(:usable_and_works?, directories, options)
58
- return namespaced_adapter.new(directories, options, &callback)
59
- end
60
- end
61
-
62
- self.warn_polling_fallback(options)
63
- Adapters::Polling.new(directories, options, &callback)
64
- end
65
-
66
- # Initializes the adapter.
67
- #
68
- # @param [String, Array<String>] directories the directories to watch
69
- # @param [Hash] options the adapter options
70
- # @option options [Float] latency the delay between checking for changes in seconds
71
- #
72
- # @yield [changed_directories, options] callback Callback called when a change happens
73
- # @yieldparam [Array<String>] changed_directories the changed directories
74
- # @yieldparam [Hash] options callback options (like recursive: true)
75
- #
76
- # @return [Listen::Adapter] the adapter
77
- #
78
- def initialize(directories, options = {}, &callback)
79
- @directories = Array(directories)
80
- @callback = callback
81
- @stopped = true
82
- @paused = false
83
- @mutex = Mutex.new
84
- @changed_directories = Set.new
85
- @turnstile = Turnstile.new
86
- @latency = options.fetch(:latency, default_latency)
87
- @worker = initialize_worker
88
- end
89
-
90
- # Starts the adapter and don't block the current thread.
91
- #
92
- # @param [Boolean] blocking whether or not to block the current thread after starting
93
- #
94
- def start
95
- mutex.synchronize do
96
- return unless stopped
97
- @stopped = false
98
- end
99
-
100
- start_worker
101
- start_poller
102
- end
103
-
104
- # Starts the adapter and block the current thread.
105
- #
106
- # @since 1.0.0
107
- #
108
- def start!
109
- start
110
- blocking_thread.join
111
- end
112
-
113
- # Stops the adapter.
114
- #
115
- def stop
116
- mutex.synchronize do
117
- return if stopped
118
- @stopped = true
119
- turnstile.signal # ensure no thread is blocked
120
- end
121
-
122
- worker.stop if worker
123
- Thread.kill(worker_thread) if worker_thread
124
- if poller_thread
125
- poller_thread.kill
126
- poller_thread.join
127
- end
128
- end
129
-
130
- # Pauses the adapter.
131
- #
132
- def pause
133
- @paused = true
134
- end
135
-
136
- # Unpauses the adapter.
137
- #
138
- def unpause
139
- @paused = false
140
- end
141
-
142
- # Returns whether the adapter is started or not.
143
- #
144
- # @return [Boolean] whether the adapter is started or not
145
- #
146
- def started?
147
- !stopped
148
- end
149
-
150
- # Returns whether the adapter is paused or not.
151
- #
152
- # @return [Boolean] whether the adapter is paused or not
153
- #
154
- def paused?
155
- paused
156
- end
157
-
158
- # Blocks the main thread until the poll thread
159
- # runs the callback.
160
- #
161
- def wait_for_callback
162
- turnstile.wait unless paused
163
- end
164
-
165
- # Blocks the main thread until N changes are
166
- # detected.
167
- #
168
- def wait_for_changes(threshold = 0)
169
- changes = 0
170
-
171
- loop do
172
- mutex.synchronize { changes = changed_directories.size }
173
-
174
- return if paused || stopped
175
- return if changes >= threshold
176
-
177
- sleep(latency)
178
- end
179
- end
180
-
181
- # Checks if the adapter is usable and works on the current OS.
182
- #
183
- # @param [String, Array<String>] directories the directories to watch
184
- # @param [Hash] options the adapter options
185
- # @option options [Float] latency the delay between checking for changes in seconds
186
- #
187
- # @return [Boolean] whether the adapter is usable and work or not
188
- #
189
- def self.usable_and_works?(directories, options = {})
190
- usable? && Array(directories).all? { |d| works?(d, options) }
191
- end
192
-
193
- # Checks if the adapter is usable on target OS.
194
- #
195
- # @return [Boolean] whether usable or not
196
- #
197
- def self.usable?
198
- load_dependent_adapter if RbConfig::CONFIG['target_os'] =~ target_os_regex
199
- end
200
-
201
- # Load the adapter gem
202
- #
203
- # @return [Boolean] whether loaded or not
204
- #
205
- def self.load_dependent_adapter
206
- return true if @loaded
207
- require adapter_gem
208
- return @loaded = true
209
- rescue LoadError
210
- false
211
- end
212
-
213
- # Runs a tests to determine if the adapter can actually pick up
214
- # changes in a given directory and returns the result.
215
- #
216
- # @note This test takes some time depending on the adapter latency.
217
- #
218
- # @param [String, Pathname] directory the directory to watch
219
- # @param [Hash] options the adapter options
220
- # @option options [Float] latency the delay between checking for changes in seconds
221
- #
222
- # @return [Boolean] whether the adapter works or not
223
- #
224
- def self.works?(directory, options = {})
225
- work = false
226
- test_file = "#{directory}/.listen_test"
227
- callback = lambda { |*| work = true }
228
- adapter = self.new(directory, options, &callback)
229
- adapter.start
230
-
231
- FileUtils.touch(test_file)
232
-
233
- t = Thread.new { sleep(adapter.latency * 5); adapter.stop }
234
-
235
- adapter.wait_for_callback
236
- work
237
- ensure
238
- Thread.kill(t) if t
239
- FileUtils.rm(test_file, :force => true)
240
- adapter.stop if adapter && adapter.started?
241
- end
242
-
243
- # Runs the callback and passes it the changes if there are any.
244
- #
245
- def report_changes
246
- changed_dirs = nil
247
-
248
- mutex.synchronize do
249
- return if @changed_directories.empty?
250
- changed_dirs = @changed_directories.to_a
251
- @changed_directories.clear
252
- end
253
-
254
- callback.call(changed_dirs, {})
255
- turnstile.signal
256
- end
257
-
258
- private
259
-
260
- # The default delay between checking for changes.
261
- #
262
- # @note This method can be overriden on a per-adapter basis.
263
- #
264
- def default_latency
265
- DEFAULT_LATENCY
266
- end
267
-
268
- # The thread on which the main thread should wait
269
- # when the adapter has been started in blocking mode.
270
- #
271
- # @note This method can be overriden on a per-adapter basis.
272
- #
273
- def blocking_thread
274
- worker_thread
275
- end
276
-
277
- # Initialize the adpater' specific worker.
278
- #
279
- # @note Each adapter must override this method
280
- # to initialize its own @worker.
281
- #
282
- def initialize_worker
283
- nil
284
- end
285
-
286
- # Should start the worker in a new thread.
287
- #
288
- # @note Each adapter must override this method
289
- # to start its worker on a new @worker_thread thread.
290
- #
291
- def start_worker
292
- raise NotImplementedError, "#{self.class} cannot respond to: #{__method__}"
293
- end
294
-
295
- # This method starts a new thread which poll for changes detected by
296
- # the adapter and report them back to the user.
297
- #
298
- def start_poller
299
- @poller_thread = Thread.new { poll_changed_directories }
300
- end
301
-
302
- # Warn of polling fallback unless the :polling_fallback_message
303
- # has been set to false.
304
- #
305
- # @param [String] warning an existing warning message
306
- # @param [Hash] options the adapter options
307
- # @option options [Boolean] polling_fallback_message to change polling fallback message or remove it
308
- #
309
- def self.warn_polling_fallback(options)
310
- return if options[:polling_fallback_message] == false
311
-
312
- warning = options[:polling_fallback_message] || POLLING_FALLBACK_MESSAGE
313
- Kernel.warn "[Listen warning]:\n#{warning.gsub(/^(.*)/, ' \1')}"
314
- end
315
-
316
- # Polls changed directories and reports them back when there are changes.
317
- #
318
- # @note This method can be overriden on a per-adapter basis.
319
- #
320
- def poll_changed_directories
321
- until stopped
322
- sleep(latency)
323
- report_changes
324
- end
325
- end
326
- end
327
- end