diff --git a/Gemfile b/Gemfile index 5b086971..ac81b570 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,6 @@ gem 'inline_svg' gem 'httparty' gem 'safe_yaml' gem 'jekyll', '~> 4.2.0' -gem 'jekyll-data' gem 'jekyll-commonmark' gem 'jekyll-images' gem 'jekyll-include-cache' diff --git a/Gemfile.lock b/Gemfile.lock index 2e741f0b..74e4775f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -276,8 +276,6 @@ GEM terminal-table (~> 2.0) jekyll-commonmark (1.4.0) commonmarker (~> 0.22) - jekyll-data (1.1.2) - jekyll (>= 3.3, < 5.0.0) jekyll-images (0.4.0) jekyll (~> 4) ruby-filemagic (~> 0.7) @@ -591,7 +589,6 @@ DEPENDENCIES jbuilder (~> 2.5) jekyll (~> 4.2.0) jekyll-commonmark - jekyll-data jekyll-images jekyll-include-cache kaminari diff --git a/app/jobs/gitlab_notifier_job.rb b/app/jobs/gitlab_notifier_job.rb index 7308a4ef..308adfc7 100644 --- a/app/jobs/gitlab_notifier_job.rb +++ b/app/jobs/gitlab_notifier_job.rb @@ -30,7 +30,7 @@ class GitlabNotifierJob < ApplicationJob count: 1, issue: @issue['iid'], user_agents: [user_agent].compact, - params: [request&.filtered_parameters].compact, + params: request&.filtered_parameters&.as_json, urls: [url].compact } end @@ -192,7 +192,7 @@ class GitlabNotifierJob < ApplicationJob ``` #{request.request_method} #{url} - #{pp request.filtered_parameters} + #{pp request.filtered_parameters.as_json} ``` REQUEST diff --git a/app/lib/jekyll/readers/data_reader_decorator.rb b/app/lib/jekyll/readers/data_reader_decorator.rb new file mode 100644 index 00000000..9fed7ac7 --- /dev/null +++ b/app/lib/jekyll/readers/data_reader_decorator.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Jekyll + module Readers + # Permite leer datos utilizando rutas absolutas. + # + # {Jekyll::DataReader} usa {Dir.chdir} con rutas relativas, lo que + # en nuestro uso provoca confusiones en el lector de datos. + # + # Con este módulo, podemos leer todos los archivos usando rutas + # absolutas, lo que nos permite reemplazar jekyll-data, que agregaba + # código duplicado. + module DataReaderDecorator + extend ActiveSupport::Concern + + included do + def read_data_to(dir, data) + return unless File.directory?(dir) && !@entry_filter.symlink?(dir) + + Dir.glob(File.join(dir, '*')).each do |path| + next if @entry_filter.symlink?(path) + + entry = Pathname.new(path).relative_path_from(dir).to_s + + if File.directory?(path) + read_data_to(path, data[sanitize_filename(entry)] = {}) + else + key = sanitize_filename(File.basename(entry, ".*")) + data[key] = read_data_file(path) + end + end + end + end + end + end +end diff --git a/app/models/deploy.rb b/app/models/deploy.rb index a92708c0..573fe2f0 100644 --- a/app/models/deploy.rb +++ b/app/models/deploy.rb @@ -50,11 +50,6 @@ class Deploy < ApplicationRecord site.path end - # XXX: Ver DeployLocal#bundle - def gems_dir - @gems_dir ||= Rails.root.join('_storage', 'gems', site.name) - end - # Corre un comando, lo registra en la base de datos y devuelve el # estado. # diff --git a/app/models/deploy_local.rb b/app/models/deploy_local.rb index d3d81d9a..4f8744f3 100644 --- a/app/models/deploy_local.rb +++ b/app/models/deploy_local.rb @@ -7,6 +7,18 @@ class DeployLocal < Deploy before_destroy :remove_destination! + def git_lfs(output: false) + run %(git lfs fetch), output: output + run %(git lfs checkout), output: output + end + + def bundle(output: false) + # XXX: Desde que ya no compartimos el directorio de gemas, tenemos + # que hacer limpieza después de instalar. + + run %(bundle install #{'--deployment' if site.gemfile_lock_path?} --no-cache --path="#{site.bundle_path}" --clean --without test development), output: output + end + # Realizamos la construcción del sitio usando Jekyll y un entorno # limpio para no pasarle secretos # @@ -55,7 +67,7 @@ class DeployLocal < Deploy # # @return [nil] def cleanup! - FileUtils.rm_rf(gems_dir) + FileUtils.rm_rf(site.bundle_path) FileUtils.rm_rf(yarn_cache_dir) FileUtils.rm_rf(File.join(site.path, 'node_modules')) FileUtils.rm_rf(File.join(site.path, '.sass-cache')) @@ -121,11 +133,6 @@ class DeployLocal < Deploy run 'pnpm install --production', output: output end - def git_lfs(output: false) - run %(git lfs fetch), output: output - run %(git lfs checkout), output: output - end - def gem(output: false) run %(gem install bundler --no-document), output: output end @@ -137,13 +144,6 @@ class DeployLocal < Deploy run 'yarn install --production', output: output end - def bundle(output: false) - # XXX: Desde que ya no compartimos el directorio de gemas, tenemos - # que hacer limpieza después de instalar. - - run %(bundle install --deployment --no-cache --path="#{gems_dir}" --clean --without test development), output: output - end - def jekyll_build(output: false) run %(bundle exec jekyll build --trace --profile --destination "#{escaped_destination}"), output: output end diff --git a/app/models/site.rb b/app/models/site.rb index 4ae5e2a7..b0edc08b 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -212,10 +212,8 @@ class Site < ApplicationRecord # Trae los datos del directorio _data dentro del sitio def data unless jekyll.data.present? - run_in_path do - jekyll.reader.read_data - jekyll.data['layouts'] ||= {} - end + jekyll.reader.read_data + jekyll.data['layouts'] ||= {} end jekyll.data @@ -225,9 +223,7 @@ class Site < ApplicationRecord # colecciones. def collections unless @read - run_in_path do - jekyll.reader.read_collections - end + jekyll.reader.read_collections @read = true end @@ -322,9 +318,7 @@ class Site < ApplicationRecord # # @return [Hash] def theme_layouts - run_in_path do - jekyll.reader.read_layouts - end + jekyll.reader.read_layouts end # Trae todos los valores disponibles para un campo @@ -375,9 +369,7 @@ class Site < ApplicationRecord begin install_gems - Jekyll::Site.new(configuration).tap do |site| - site.reader = JekyllData::Reader.new(site) if site.theme - end + Jekyll::Site.new(configuration) end end @@ -461,6 +453,15 @@ class Site < ApplicationRecord @docs = nil end + # @return [Pathname] + def bundle_path + @bundle_path ||= Rails.root.join('_storage', 'gems', name) + end + + def gemfile_lock_path? + File.exist? gemfile_lock_path + end + private # Asegurarse que el sitio tenga una llave privada @@ -552,10 +553,6 @@ class Site < ApplicationRecord I18n.t('activerecord.errors.models.site.attributes.design_id.layout_incompatible.error')) end - def run_in_path(&block) - Dir.chdir path, &block - end - # Instala las gemas cuando es necesario: # # * El sitio existe @@ -565,17 +562,28 @@ class Site < ApplicationRecord def install_gems return unless persisted? - deploys.find_by_type('DeployLocal').send(:git_lfs) + deploy_local = deploys.find_by_type('DeployLocal') + deploy_local.git_lfs - if !gem_dir? || gemfile_updated? || gemfile_lock_updated? - deploys.find_by_type('DeployLocal').send(:bundle) + if !gems_installed? || gemfile_updated? || gemfile_lock_updated? + deploy_local.bundle touch end end + def gem_path + @gem_path ||= + begin + ruby_version = Gem::Version.new(RUBY_VERSION) + ruby_version.canonical_segments[2] = 0 + + bundle_path.join('ruby', ruby_version.canonical_segments.join('.')) + end + end + # Detecta si el repositorio de gemas existe - def gem_dir? - Rails.root.join('_storage', 'gems', name).directory? + def gems_installed? + gem_path.directory? && !gem_path.empty? end # Detecta si el Gemfile fue modificado @@ -583,8 +591,15 @@ class Site < ApplicationRecord updated_at < File.mtime(File.join(path, 'Gemfile')) end + # @return [String] + def gemfile_lock_path + @gemfile_lock_path ||= File.join(path, 'Gemfile.lock') + end + # Detecta si el Gemfile.lock fue modificado def gemfile_lock_updated? - updated_at < File.mtime(File.join(path, 'Gemfile.lock')) + return false unless gemfile_lock_path? + + updated_at < File.mtime(gemfile_lock_path) end end diff --git a/config/initializers/core_extensions.rb b/config/initializers/core_extensions.rb index 1516a43a..861491ae 100644 --- a/config/initializers/core_extensions.rb +++ b/config/initializers/core_extensions.rb @@ -2,6 +2,7 @@ String.include CoreExtensions::String::StripTags Jekyll::Document.include CoreExtensions::Jekyll::Document::Path +Jekyll::DataReader.include Jekyll::Readers::DataReaderDecorator # Definir tags de Liquid que provienen de complementos para que siempre # devuelvan contenido vacío. @@ -37,6 +38,19 @@ end # # TODO: Aplicar monkey patches en otro lado... module Jekyll + Configuration.class_eval do + # No agregar colecciones por defecto, solo las que digamos en base a + # los idiomas. Esto remueve la colección "posts". + # + # Las colecciones de idiomas son agregadas por Site. + # + # @see Site#configuration + # @return [Jekyll::Configuration] + def add_default_collections + self + end + end + Site.class_eval do def configure_theme self.theme = nil @@ -57,9 +71,27 @@ module Jekyll # No necesitamos los archivos estáticos def retrieve_static_files(_, _); end - # Solo lee los datos + # Solo lee los datos, desde la plantilla y luego desde el sitio, + # usando rutas absolutas, para evitar el uso confuso de Dir.chdir. + # + # Reemplaza jekyll-data también! + # + # @return [Hash] def read_data - @site.data = DataReader.new(site).read(site.config['data_dir']) + @site.data = + begin + reader = DataReader.new(site) + theme_dir = site.in_theme_dir('_data') + data_dir = site.in_source_dir(site.config['data_dir']) + + if theme_dir + reader.read_data_to(theme_dir, reader.content) + reader.read_data_to(data_dir, reader.content) + reader.content + else + reader.read data_dir + end + end end # Lee los layouts @@ -161,14 +193,3 @@ module PgSearch end end end - -# JekyllData::Reader del plugin jekyll-data modifica Jekyll::Site#reader -# para también leer los datos que vienen en el theme. -module JekyllData - Reader.class_eval do - def read_data - super - read_theme_data - end - end -end