diff --git a/Rakefile b/Rakefile index 4f78fec4..19e8dc6a 100644 --- a/Rakefile +++ b/Rakefile @@ -159,6 +159,13 @@ namespace :prism do puts "Vendoring '#{file}' Prism file to #{vendored_file_path}" FileUtils.cp_r(prism_bundle_path + "/#{file}", prism_vendor_path) end + + prism_ast_header = "#{prism_vendor_path}/include/prism/ast.h" + + unless File.exist?(prism_ast_header) + puts "Generating Prism template files..." + system("ruby #{prism_vendor_path}/templates/template.rb", exception: true) + end end desc "Clean vendored Prism in vendor/prism/" diff --git a/ext/herb/extconf.rb b/ext/herb/extconf.rb index 3e07accf..08dc712c 100644 --- a/ext/herb/extconf.rb +++ b/ext/herb/extconf.rb @@ -2,10 +2,6 @@ require "mkmf" -Dir.chdir(File.expand_path("../..", __dir__)) do - system("rake templates", exception: true) -end - extension_name = "herb" include_path = File.expand_path("../../src/include", __dir__) diff --git a/lib/herb.rb b/lib/herb.rb index 9840251d..9e7ae98d 100644 --- a/lib/herb.rb +++ b/lib/herb.rb @@ -31,9 +31,36 @@ begin major, minor, _patch = RUBY_VERSION.split(".") #: [String, String, String] - require_relative "herb/#{major}.#{minor}/herb" -rescue LoadError - require_relative "herb/herb" + + if RUBY_PATCHLEVEL == -1 + require_relative "herb/herb" + else + begin + require_relative "herb/#{major}.#{minor}/herb" + rescue LoadError + require_relative "herb/herb" + end + end +rescue LoadError => e + raise LoadError, <<~MESSAGE + Failed to load the Herb native extension. + + Tried to load: #{e.message.split(" -- ").last} + + This can happen when: + 1. You're using a preview/development version of Ruby (RUBY_PATCHLEVEL=#{RUBY_PATCHLEVEL}) + 2. The native extension wasn't compiled during gem installation + 3. Required build tools (C compiler) were missing during installation + + To fix, try reinstalling with source compilation: + gem install herb --platform ruby + + If compilation fails, install a C compiler first: + - macOS: xcode-select --install + - Ubuntu/Debian: apt-get install build-essential + - Fedora/RHEL: dnf install make gcc + - Alpine: apk add build-base + MESSAGE end module Herb