Sha256: 52e49d80e184c84d47d20788a85d80172b1009771023493bb9eca2cadcdf1653

Contents?: true

Size: 1.09 KB

Versions: 36

Compression:

Stored size: 1.09 KB

Contents

require "open3"

class Terraspace::Compiler::Strategy::Mod
  class TextFile
    include Terraspace::Util::Logging

    def initialize(filename)
      @filename = filename
    end

    def check
      return true if Gem.win_platform? # assume text file if on windows
      return true unless file_installed?

      # Thanks: https://stackoverflow.com/questions/2355866/ruby-how-to-determine-if-file-being-read-is-binary-or-text
      file_type, status = Open3.capture2e("file", @filename)
      status.success? && file_type.include?("text")
    end

  private
    @@has_file = nil
    def file_installed?
      return @@has_file unless @@has_file.nil?
      @@has_file = system("type file > /dev/null 2>&1")
      unless @@has_file
        logger.warn <<~EOL.color(:yellow)
          WARN: The command 'file' is not installed.
          Unable to check if files are text or binary files as a part of the Terraspace compile processing.
          Assuming all files are not binary file.
          Please install the file command to remove this warning message.
        EOL
      end
     @@has_file
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.16 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.15 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.14 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.13 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.12 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.11 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.10 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.9 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.8 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.7 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.6 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.5 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.4 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.3 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.2 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.1 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.2.0 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.1.7 lib/terraspace/compiler/strategy/mod/text_file.rb
terraspace-2.1.6 lib/terraspace/compiler/strategy/mod/text_file.rb