|
| 1 | +# frozen_string_literal: true |
| 2 | +# typed: ignore |
| 3 | + |
| 4 | +require "fileutils" |
| 5 | + |
| 6 | +module Herb |
| 7 | + module Bootstrap |
| 8 | + ROOT_PATH = File.expand_path("../..", __dir__) |
| 9 | + PRISM_VENDOR_DIR = File.join(ROOT_PATH, "vendor", "prism") |
| 10 | + |
| 11 | + PRISM_ENTRIES = [ |
| 12 | + "config.yml", |
| 13 | + "Rakefile", |
| 14 | + "src/", |
| 15 | + "include/", |
| 16 | + "templates/" |
| 17 | + ].freeze |
| 18 | + |
| 19 | + def self.generate_templates |
| 20 | + require "pathname" |
| 21 | + require "set" |
| 22 | + require_relative "../../templates/template" |
| 23 | + |
| 24 | + Dir.chdir(ROOT_PATH) do |
| 25 | + Dir.glob("#{ROOT_PATH}/templates/**/*.erb").each do |template| |
| 26 | + Herb::Template.render(template) |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def self.git_source? |
| 32 | + File.directory?(File.join(ROOT_PATH, ".git")) |
| 33 | + end |
| 34 | + |
| 35 | + def self.templates_generated? |
| 36 | + File.exist?(File.join(ROOT_PATH, "ext", "herb", "nodes.c")) |
| 37 | + end |
| 38 | + |
| 39 | + def self.vendor_prism(prism_gem_path:) |
| 40 | + FileUtils.mkdir_p(PRISM_VENDOR_DIR) |
| 41 | + |
| 42 | + PRISM_ENTRIES.each do |entry| |
| 43 | + source = File.join(prism_gem_path, entry) |
| 44 | + next unless File.exist?(source) |
| 45 | + |
| 46 | + puts "Vendoring '#{entry}' Prism file to #{PRISM_VENDOR_DIR}/#{entry}" |
| 47 | + FileUtils.cp_r(source, PRISM_VENDOR_DIR) |
| 48 | + end |
| 49 | + |
| 50 | + generate_prism_templates unless prism_ast_header_exists? |
| 51 | + end |
| 52 | + |
| 53 | + def self.prism_vendored? |
| 54 | + File.directory?(File.join(PRISM_VENDOR_DIR, "include")) |
| 55 | + end |
| 56 | + |
| 57 | + def self.prism_ast_header_exists? |
| 58 | + File.exist?(File.join(PRISM_VENDOR_DIR, "include", "prism", "ast.h")) |
| 59 | + end |
| 60 | + |
| 61 | + def self.find_prism_gem_path |
| 62 | + find_prism_as_bundler_sibling || find_prism_from_gem_spec |
| 63 | + end |
| 64 | + |
| 65 | + def self.generate_prism_templates |
| 66 | + puts "Generating Prism template files..." |
| 67 | + system("ruby", "#{PRISM_VENDOR_DIR}/templates/template.rb", exception: true) |
| 68 | + end |
| 69 | + |
| 70 | + def self.find_prism_as_bundler_sibling |
| 71 | + bundler_gems_dir = File.expand_path("..", ROOT_PATH) |
| 72 | + candidates = Dir.glob(File.join(bundler_gems_dir, "prism-*")) |
| 73 | + |
| 74 | + candidates.find { |path| File.directory?(File.join(path, "src")) } |
| 75 | + end |
| 76 | + |
| 77 | + def self.find_prism_from_gem_spec |
| 78 | + path = Gem::Specification.find_by_name("prism").full_gem_path |
| 79 | + |
| 80 | + return path if File.directory?(File.join(path, "src")) |
| 81 | + |
| 82 | + nil |
| 83 | + rescue Gem::MissingSpecError |
| 84 | + nil |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments