lib/pdd/sources.rb in pdd-0.20.8 vs lib/pdd/sources.rb in pdd-0.21.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2014-2021 Yegor Bugayenko +# Copyright (c) 2014-2022 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -17,12 +17,12 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'rainbow' -require 'shellwords' require 'English' +require 'filemagic' require_relative 'source' require_relative '../../utils/glob' module PDD # Code base abstraction @@ -75,22 +75,22 @@ self end private - # @todo #98:30min Change the implementation of this method - # to also work in Windows machines. Investigate the possibility - # of use a gem for this. After that, remove the skip of the test - # `test_ignores_binary_files` in `test_sources.rb`. def binary?(file) - return false if Gem.win_platform? - - `grep -qI '.' #{Shellwords.escape(file)}` - if $CHILD_STATUS.success? + if text_file?(file) false else PDD.log.info "#{file} is a binary file (#{File.size(file)} bytes)" true end + end + + def text_file?(file) + fm = FileMagic.new(FileMagic::MAGIC_MIME) + fm.file(file) =~ %r{^text/} + ensure + fm.close end end end