From 47f1a8e530053b81e5978f36f71763fd4e822228 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 27 Dec 2022 17:42:00 -0300 Subject: [PATCH] feat: leer datos de las plantillas directamente #1849 usando el lockfile de cada repositorio, acceder directamente al directorio de datos usando jekyll-data. --- app/models/site.rb | 22 +++--------- config/initializers/core_extensions.rb | 47 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/app/models/site.rb b/app/models/site.rb index 638b3f47..5dee0b85 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -335,7 +335,10 @@ class Site < ApplicationRecord def jekyll run_in_path do - @jekyll ||= Jekyll::Site.new(configuration) + @jekyll ||= + Jekyll::Site.new(configuration).tap do |site| + site.reader = JekyllData::Reader.new(site) if site.theme + end end end @@ -382,9 +385,6 @@ class Site < ApplicationRecord @configuration[unneeded] = [] if @configuration.key? unneeded end - # Eliminar el theme si no es una gema válida - @configuration.delete('theme') unless theme_available? - # Si estamos usando nuestro propio plugin de i18n, los posts están # en "colecciones" locales.map(&:to_s).each do |i| @@ -394,20 +394,6 @@ class Site < ApplicationRecord @configuration end - # Lista los nombres de las plantillas disponibles como gemas, - # tomándolas dinámicamente de las que agreguemos en el grupo :themes - # del Gemfile. - def available_themes - @available_themes ||= Bundler.load.current_dependencies.select do |gem| - gem.groups.include? :themes - end.map(&:name) - end - - # Detecta si el tema actual es una gema - def theme_available? - available_themes.include? design&.gem - end - # Devuelve el dominio actual def self.domain ENV.fetch('SUTTY', 'sutty.nl') diff --git a/config/initializers/core_extensions.rb b/config/initializers/core_extensions.rb index 66d2c92b..1516a43a 100644 --- a/config/initializers/core_extensions.rb +++ b/config/initializers/core_extensions.rb @@ -37,6 +37,13 @@ end # # TODO: Aplicar monkey patches en otro lado... module Jekyll + Site.class_eval do + def configure_theme + self.theme = nil + self.theme = Jekyll::Theme.new(config['theme'], self) unless config['theme'].nil? + end + end + Reader.class_eval do # No necesitamos otros posts def retrieve_posts(_); end @@ -69,6 +76,46 @@ module Jekyll end end + Theme.class_eval do + attr_reader :site + + def initialize(name, site) + @name = name.downcase.strip + @site = site + end + + def root + @root ||= begin + lockfile = Bundler::LockfileParser.new(File.read(site.in_source_dir('Gemfile.lock'))) + spec = lockfile.specs.find do |spec| + spec.name == name + end + + ruby_version = Gem::Version.new(RUBY_VERSION) + ruby_version.canonical_segments[2] = 0 + base_path = Rails.root.join('_storage', 'gems', File.basename(site.source), 'ruby', + ruby_version.canonical_segments.join('.')) + + File.realpath( + case spec.source + when Bundler::Source::Git + File.join(base_path, 'bundler', 'gems', spec.source.extension_dir_name) + when Bundler::Source::Rubygems + File.join(base_path, 'gems', spec.full_name) + end + ) + end + end + + def runtime_dependencies + [] + end + + private + + def gemspec; end + end + # No necesitamos los archivos de la plantilla ThemeAssetsReader.class_eval do def read; end