Sha256: 5362bb117deb6fb98ebcae98f47fdbce7df6708ee8b370112096e89c118181c7

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

# This is a redundancy check for the rust compiled library needed for this gem.
# If the asset is not available and we can't compile it from this code then FAIL
# on require of 'faster_path' with a very clear message as to why."

module FasterPath
  module AssetResolution # BREAK IN CASE OF EMERGENCY ;-)
    class << self
      def verify!
        return lib_file if file?

        if rust?
          compile!
          raise "Rust failed to compile asset! The dynamic library for this package was not found." unless file?
          return lib_file
        end

        raise "The dynamic library for this package was not found nor was Rust's cargo executable found. This package will not work without it!"
      end

      private

      def compile!
        require 'open3'
        Dir.chdir(File.expand_path('../../', __dir__)) do
          Open3.popen3("rake build_lib") do |stdin, stdout, stderr, wait_thr|
            stdin.close

            wait_thr && wait_thr.value.exitstatus
            out = Thread.new { stdout.read }.value.strip
            Thread.new { stderr.read }.value
            out
          end
        end
        File.exist? lib_file
      end

      def rust?
        require 'mkmf'
        MakeMakefile::Logging.instance_variable_set(:@log, File.open(File::NULL, 'w'))
        MakeMakefile.instance_eval "undef :message; def message(*); end"
        MakeMakefile.find_executable('cargo')
      end

      def file?
        File.exist? lib_file
      end

      def lib_dir
        File.expand_path("../../target/release/", __dir__)
      end

      def lib_file
        prefix = Gem.win_platform? ? "" : "lib"
        "#{lib_dir}/#{prefix}faster_path.#{FFI::Platform::LIBSUFFIX}"
      end
    end
  end
end
FasterPath::AssetResolution.verify! unless ENV['TEST']

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
faster_path-0.2.3 lib/faster_path/asset_resolution.rb
faster_path-0.2.2 lib/faster_path/asset_resolution.rb
faster_path-0.2.1 lib/faster_path/asset_resolution.rb
faster_path-0.2.0 lib/faster_path/asset_resolution.rb
faster_path-0.1.13 lib/faster_path/asset_resolution.rb
faster_path-0.1.12 lib/faster_path/asset_resolution.rb
faster_path-0.1.11 lib/faster_path/asset_resolution.rb