2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Un sitio es un directorio dentro del directorio de sitios de cada
|
|
|
|
# Usuaria
|
2019-07-03 22:04:50 +00:00
|
|
|
class Site < ApplicationRecord
|
2019-07-03 23:40:24 +00:00
|
|
|
include FriendlyId
|
2020-05-30 19:43:25 +00:00
|
|
|
include Site::Forms
|
2020-06-09 14:22:13 +00:00
|
|
|
include Site::FindAndReplace
|
2020-12-07 16:21:46 +00:00
|
|
|
include Site::Api
|
2023-03-18 00:38:31 +00:00
|
|
|
include Site::DeployDependencies
|
2023-03-31 22:32:36 +00:00
|
|
|
include Site::BuildStats
|
2023-04-17 21:30:21 +00:00
|
|
|
include Site::LayoutOrdering
|
2023-08-29 20:43:19 +00:00
|
|
|
include Site::SocialDistributedPress
|
2020-11-11 21:15:58 +00:00
|
|
|
include Tienda
|
2019-07-03 23:40:24 +00:00
|
|
|
|
2020-08-10 23:40:12 +00:00
|
|
|
# Cifrar la llave privada que cifra y decifra campos ocultos. Sutty
|
|
|
|
# tiene acceso pero los datos se guardan cifrados en el sitio. Esto
|
|
|
|
# protege información privada en repositorios públicos, pero no la
|
|
|
|
# protege de acceso al panel de Sutty!
|
|
|
|
encrypts :private_key
|
|
|
|
|
2019-11-18 17:08:30 +00:00
|
|
|
validates :name, uniqueness: true, hostname: {
|
|
|
|
allow_root_label: true
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:18:48 +00:00
|
|
|
validates :design_id, presence: true
|
2019-07-26 01:11:01 +00:00
|
|
|
validates_inclusion_of :status, in: %w[waiting enqueued building]
|
2019-07-31 20:55:34 +00:00
|
|
|
validates_presence_of :title
|
2021-11-04 13:42:57 +00:00
|
|
|
validates :description, length: { in: 10..160 }
|
2020-08-03 18:58:08 +00:00
|
|
|
validate :deploy_local_presence
|
|
|
|
validate :compatible_layouts, on: :update
|
|
|
|
|
|
|
|
attr_reader :incompatible_layouts
|
2019-07-25 18:51:32 +00:00
|
|
|
|
2019-07-03 23:40:24 +00:00
|
|
|
friendly_id :name, use: %i[finders]
|
|
|
|
|
2019-07-17 22:18:48 +00:00
|
|
|
belongs_to :design
|
2019-07-19 22:37:53 +00:00
|
|
|
belongs_to :licencia
|
2019-07-17 22:18:48 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
has_many :stats
|
2020-11-26 17:40:03 +00:00
|
|
|
has_many :log_entries, dependent: :destroy
|
|
|
|
has_many :deploys, dependent: :destroy
|
2019-08-02 00:20:42 +00:00
|
|
|
has_many :build_stats, through: :deploys
|
2020-11-26 17:40:03 +00:00
|
|
|
has_many :roles, dependent: :destroy
|
2019-07-08 17:08:06 +00:00
|
|
|
has_many :usuaries, -> { where('roles.rol = ?', 'usuarie') },
|
|
|
|
through: :roles
|
|
|
|
has_many :invitades, -> { where('roles.rol = ?', 'invitade') },
|
|
|
|
through: :roles, source: :usuarie
|
2019-07-03 22:04:50 +00:00
|
|
|
|
2019-08-22 01:09:29 +00:00
|
|
|
# Mantenemos el nombre que les da Jekyll
|
|
|
|
has_many_attached :static_files
|
|
|
|
|
2019-07-12 18:22:37 +00:00
|
|
|
# Clonar el directorio de esqueleto antes de crear el sitio
|
|
|
|
before_create :clone_skel!
|
2019-07-12 18:34:16 +00:00
|
|
|
# Elimina el directorio al destruir un sitio
|
|
|
|
before_destroy :remove_directories!
|
2019-07-13 00:20:36 +00:00
|
|
|
# Cambiar el nombre del directorio
|
|
|
|
before_update :update_name!
|
2020-08-20 23:38:31 +00:00
|
|
|
before_save :add_private_key_if_missing!
|
2019-07-31 20:55:34 +00:00
|
|
|
# Guardar la configuración si hubo cambios
|
|
|
|
after_save :sync_attributes_with_config!
|
2019-07-03 23:25:23 +00:00
|
|
|
|
2019-07-25 18:51:32 +00:00
|
|
|
accepts_nested_attributes_for :deploys, allow_destroy: true
|
|
|
|
|
2021-05-14 19:59:47 +00:00
|
|
|
# XXX: Es importante incluir luego de los callbacks de :load_jekyll
|
|
|
|
include Site::Index
|
|
|
|
|
2019-08-01 23:13:38 +00:00
|
|
|
# No permitir HTML en estos atributos
|
|
|
|
def title=(title)
|
|
|
|
super(title.strip_tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
def description=(description)
|
|
|
|
super(description.strip_tags)
|
|
|
|
end
|
|
|
|
|
2019-07-16 19:47:44 +00:00
|
|
|
# El repositorio git para este sitio
|
|
|
|
def repository
|
|
|
|
@repository ||= Site::Repository.new path
|
|
|
|
end
|
|
|
|
|
2019-09-12 18:10:01 +00:00
|
|
|
def hostname
|
2019-09-24 22:50:20 +00:00
|
|
|
sub = name || I18n.t('deploys.deploy_local.ejemplo')
|
2020-03-25 18:49:36 +00:00
|
|
|
|
|
|
|
if sub.ends_with? '.'
|
|
|
|
sub.gsub(/\.\Z/, '')
|
|
|
|
else
|
|
|
|
"#{sub}.#{Site.domain}"
|
|
|
|
end
|
2019-09-12 18:10:01 +00:00
|
|
|
end
|
|
|
|
|
2020-05-30 19:43:25 +00:00
|
|
|
# Devuelve la URL siempre actualizada a través del hostname
|
|
|
|
#
|
|
|
|
# @param slash Boolean Agregar / al final o no
|
|
|
|
# @return String La URL con o sin / al final
|
|
|
|
def url(slash: true)
|
2021-05-06 20:54:49 +00:00
|
|
|
"https://#{hostname}#{slash ? '/' : ''}"
|
2020-06-02 19:39:54 +00:00
|
|
|
end
|
|
|
|
|
2022-04-26 19:46:48 +00:00
|
|
|
# TODO: Cambiar al mergear origin-referer
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
def hostnames
|
|
|
|
@hostnames ||= deploys.map do |deploy|
|
|
|
|
case deploy
|
|
|
|
when DeployLocal
|
|
|
|
hostname
|
|
|
|
when DeployWww
|
|
|
|
deploy.fqdn
|
|
|
|
when DeployAlternativeDomain
|
|
|
|
deploy.hostname.dup.tap do |h|
|
|
|
|
h.replace(h.end_with?('.') ? h[0..-2] : "#{h}.#{Site.domain}")
|
|
|
|
end
|
|
|
|
when DeployHiddenService
|
|
|
|
deploy.onion
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2020-06-02 19:39:54 +00:00
|
|
|
# Obtiene los dominios alternativos
|
|
|
|
#
|
|
|
|
# @return Array
|
|
|
|
def alternative_hostnames
|
|
|
|
deploys.where(type: 'DeployAlternativeDomain').map(&:hostname).map do |h|
|
2021-05-06 20:54:49 +00:00
|
|
|
h.end_with?('.') ? h[0..-2] : "#{h}.#{Site.domain}"
|
2020-06-02 19:39:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Obtiene todas las URLs alternativas para este sitio
|
|
|
|
#
|
|
|
|
# @return Array
|
|
|
|
def alternative_urls(slash: true)
|
|
|
|
alternative_hostnames.map do |h|
|
2021-05-06 20:54:49 +00:00
|
|
|
"https://#{h}#{slash ? '/' : ''}"
|
2020-05-30 19:43:25 +00:00
|
|
|
end
|
2019-09-12 18:10:01 +00:00
|
|
|
end
|
|
|
|
|
2020-06-02 19:39:54 +00:00
|
|
|
# Todas las URLs posibles para este sitio
|
|
|
|
#
|
|
|
|
# @return Array
|
|
|
|
def urls(slash: true)
|
2022-04-26 19:46:48 +00:00
|
|
|
@urls ||= hostnames.map do |h|
|
|
|
|
"https://#{h}#{slash ? '/' : ''}"
|
|
|
|
end
|
2020-06-02 19:39:54 +00:00
|
|
|
end
|
|
|
|
|
2019-07-04 00:47:59 +00:00
|
|
|
def invitade?(usuarie)
|
2020-06-25 19:33:36 +00:00
|
|
|
!invitades.find_by(id: usuarie.id).nil?
|
2019-07-04 00:47:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def usuarie?(usuarie)
|
2020-06-25 19:33:36 +00:00
|
|
|
!usuaries.find_by(id: usuarie.id).nil?
|
2019-07-03 23:25:23 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Este sitio acepta invitades?
|
|
|
|
def invitades?
|
2020-08-22 20:56:37 +00:00
|
|
|
acepta_invitades || config.fetch('invitades', false)
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2019-07-03 23:25:23 +00:00
|
|
|
# Traer la ruta del sitio
|
|
|
|
def path
|
2019-07-13 00:20:36 +00:00
|
|
|
File.join(Site.site_path, name)
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# La ruta anterior
|
|
|
|
def path_was
|
2019-07-13 00:20:36 +00:00
|
|
|
File.join(Site.site_path, name_was)
|
2019-07-03 23:25:23 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2020-10-30 21:57:46 +00:00
|
|
|
# Limpiar la ruta y unirla con el separador de directorios del
|
|
|
|
# sistema operativo. Como si algún día fuera a cambiar o
|
|
|
|
# soportáramos Windows :P
|
|
|
|
def relative_path(suspicious_path)
|
|
|
|
File.join(path, *suspicious_path.gsub('..', '/').gsub('./', '').squeeze('/').split('/'))
|
|
|
|
end
|
|
|
|
|
2018-05-08 21:14:00 +00:00
|
|
|
# Obtiene la lista de traducciones actuales
|
|
|
|
#
|
2019-08-06 23:17:29 +00:00
|
|
|
# Siempre tiene que tener algo porque las traducciones están
|
|
|
|
# incorporadas a los sitios de Sutty, aunque les usuaries no traduzcan
|
|
|
|
# sus sitios.
|
2023-03-16 21:26:17 +00:00
|
|
|
#
|
|
|
|
# @return [Array]
|
2019-09-20 13:19:04 +00:00
|
|
|
def locales
|
2021-03-03 12:43:15 +00:00
|
|
|
@locales ||= config.fetch('locales', I18n.available_locales).map(&:to_sym)
|
2018-05-11 16:24:30 +00:00
|
|
|
end
|
|
|
|
|
2023-03-16 21:26:17 +00:00
|
|
|
# Modificar los locales disponibles
|
|
|
|
#
|
|
|
|
# @param :new_locales [Array]
|
|
|
|
# @return [Array]
|
|
|
|
def locales=(new_locales)
|
|
|
|
@locales = new_locales.map(&:to_sym).uniq
|
|
|
|
end
|
|
|
|
|
2020-03-31 20:20:34 +00:00
|
|
|
# Similar a site.i18n en jekyll-locales
|
2022-06-29 21:17:23 +00:00
|
|
|
#
|
|
|
|
# @return [Hash]
|
2020-03-31 20:20:34 +00:00
|
|
|
def i18n
|
2022-06-29 21:17:23 +00:00
|
|
|
data[I18n.locale.to_s] || {}
|
2020-03-31 20:20:34 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Devuelve el idioma por defecto del sitio, el primero de la lista.
|
2019-09-20 13:19:04 +00:00
|
|
|
def default_locale
|
|
|
|
locales.first
|
2018-02-22 19:01:11 +00:00
|
|
|
end
|
2019-09-20 13:19:04 +00:00
|
|
|
alias default_lang default_locale
|
2018-02-22 19:01:11 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Trae los datos del directorio _data dentro del sitio
|
2018-02-09 21:28:27 +00:00
|
|
|
def data
|
2021-09-15 22:50:24 +00:00
|
|
|
unless jekyll.data.present?
|
|
|
|
run_in_path do
|
|
|
|
jekyll.reader.read_data
|
|
|
|
jekyll.data['layouts'] ||= {}
|
2021-05-07 22:02:15 +00:00
|
|
|
end
|
2020-11-27 23:53:14 +00:00
|
|
|
end
|
|
|
|
|
2021-09-15 22:50:24 +00:00
|
|
|
jekyll.data
|
2018-02-09 21:28:27 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Traer las colecciones. Todos los artículos van a estar dentro de
|
|
|
|
# colecciones.
|
|
|
|
def collections
|
2021-05-07 22:02:15 +00:00
|
|
|
unless @read
|
2021-09-15 22:50:24 +00:00
|
|
|
run_in_path do
|
|
|
|
jekyll.reader.read_collections
|
|
|
|
end
|
|
|
|
|
2021-05-07 22:02:15 +00:00
|
|
|
@read = true
|
|
|
|
end
|
2018-05-14 17:17:13 +00:00
|
|
|
|
2021-09-15 22:50:24 +00:00
|
|
|
jekyll.collections
|
2018-02-22 19:01:11 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Traer la configuración de forma modificable
|
|
|
|
def config
|
|
|
|
@config ||= Site::Config.new(self)
|
2018-05-08 21:57:11 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Los posts en el idioma actual o en uno en particular
|
2018-02-22 19:01:11 +00:00
|
|
|
#
|
2019-08-06 23:17:29 +00:00
|
|
|
# @param lang: [String|Symbol] traer los artículos de este idioma
|
|
|
|
def posts(lang: nil)
|
2021-04-22 22:30:47 +00:00
|
|
|
# Traemos los posts del idioma actual por defecto o el que haya
|
|
|
|
lang ||= locales.include?(I18n.locale) ? I18n.locale : default_locale
|
2021-02-17 21:46:00 +00:00
|
|
|
lang = lang.to_sym
|
2020-02-13 15:38:53 +00:00
|
|
|
|
|
|
|
# Crea un Struct dinámico con los valores de los locales, si
|
|
|
|
# llegamos a pasar un idioma que no existe vamos a tener una
|
|
|
|
# excepción NoMethodError
|
2021-03-03 12:43:15 +00:00
|
|
|
@posts ||= Struct.new(*locales).new
|
2020-02-13 15:38:53 +00:00
|
|
|
|
|
|
|
return @posts[lang] unless @posts[lang].blank?
|
2018-04-27 18:48:26 +00:00
|
|
|
|
2021-02-17 21:46:00 +00:00
|
|
|
@posts[lang] = PostRelation.new site: self, lang: lang
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# No fallar si no existe colección para este idioma
|
|
|
|
# XXX: queremos fallar silenciosamente?
|
2020-10-04 00:32:32 +00:00
|
|
|
(collections[lang.to_s]&.docs || []).each do |doc|
|
|
|
|
layout = layouts[Post.find_layout(doc.path)]
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
@posts[lang].build(document: doc, layout: layout, lang: lang)
|
2022-05-05 22:37:14 +00:00
|
|
|
rescue TypeError => e
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { site: name, site_id: id, path: doc.path })
|
2018-02-23 19:20:51 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
@posts[lang]
|
2018-02-03 23:41:02 +00:00
|
|
|
end
|
|
|
|
|
2020-05-23 15:38:03 +00:00
|
|
|
# Todos los Post del sitio para poder buscar en todos.
|
|
|
|
#
|
|
|
|
# @return PostRelation
|
|
|
|
def docs
|
2021-02-17 21:46:00 +00:00
|
|
|
@docs ||= PostRelation.new(site: self, lang: :docs).push(locales.flat_map do |locale|
|
2020-05-23 15:38:03 +00:00
|
|
|
posts(lang: locale)
|
|
|
|
end).flatten!
|
|
|
|
end
|
|
|
|
|
2020-02-12 21:24:54 +00:00
|
|
|
# Elimina un artículo de la colección
|
|
|
|
def delete_post(post)
|
|
|
|
lang = post.lang.value
|
|
|
|
|
|
|
|
collections[lang.to_s].docs.delete(post.document) &&
|
|
|
|
posts(lang: lang).delete(post)
|
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Obtiene todas las plantillas de artículos
|
|
|
|
#
|
2020-11-27 23:53:14 +00:00
|
|
|
# @return [Hash] { post: Layout }
|
2019-08-06 23:17:29 +00:00
|
|
|
def layouts
|
2020-02-13 15:38:53 +00:00
|
|
|
# Crea un Struct dinámico cuyas llaves son los nombres de todos los
|
|
|
|
# layouts. Si pasamos un layout que no existe, obtenemos un
|
|
|
|
# NoMethodError
|
2020-06-16 22:10:54 +00:00
|
|
|
@layouts_struct ||= Struct.new(*layout_keys, keyword_init: true)
|
2020-11-27 23:53:14 +00:00
|
|
|
@layouts ||= @layouts_struct.new(**data['layouts'].map do |name, metadata|
|
2021-05-06 20:54:49 +00:00
|
|
|
[name.to_sym,
|
|
|
|
Layout.new(site: self, name: name.to_sym, meta: metadata.delete('meta')&.with_indifferent_access,
|
|
|
|
metadata: metadata.with_indifferent_access)]
|
2020-11-07 23:51:00 +00:00
|
|
|
end.to_h)
|
2018-02-08 14:05:05 +00:00
|
|
|
end
|
|
|
|
|
2020-11-27 23:53:14 +00:00
|
|
|
# TODO: Si la estructura de datos no existe, vamos a producir una
|
|
|
|
# excepción.
|
2020-06-16 22:10:54 +00:00
|
|
|
def layout_keys
|
2020-11-27 23:53:14 +00:00
|
|
|
@layout_keys ||= data['layouts'].keys.map(&:to_sym)
|
2020-06-16 22:10:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Consulta si el Layout existe
|
|
|
|
#
|
|
|
|
# @param [String,Symbol] El nombre del Layout
|
|
|
|
# @return [Boolean]
|
|
|
|
def layout?(layout)
|
|
|
|
layout_keys.include? layout.to_sym
|
|
|
|
end
|
|
|
|
|
2021-06-07 16:37:18 +00:00
|
|
|
# Lee los layouts en HTML desde el sitio
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def theme_layouts
|
2021-09-15 22:50:24 +00:00
|
|
|
run_in_path do
|
|
|
|
jekyll.reader.read_layouts
|
|
|
|
end
|
2021-06-07 16:37:18 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Trae todos los valores disponibles para un campo
|
|
|
|
#
|
|
|
|
# TODO: Traer recursivamente, si el campo contiene Hash
|
|
|
|
#
|
2020-05-23 16:52:58 +00:00
|
|
|
# TODO: Mover a PostRelation#pluck
|
|
|
|
#
|
2020-05-23 15:38:03 +00:00
|
|
|
# @param attr [Symbol|String] El atributo a buscar
|
2019-08-06 23:17:29 +00:00
|
|
|
# @return Array
|
2018-02-27 23:32:10 +00:00
|
|
|
def everything_of(attr, lang: nil)
|
2020-11-16 22:17:40 +00:00
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/everything_of/#{lang}/#{attr}", expires_in: 1.hour) do
|
|
|
|
attr = attr.to_sym
|
2020-05-23 15:38:03 +00:00
|
|
|
|
2020-11-16 22:17:40 +00:00
|
|
|
posts(lang: lang).flat_map do |p|
|
|
|
|
p[attr].value if p.attribute? attr
|
|
|
|
end.uniq.compact
|
|
|
|
end
|
2018-02-03 23:41:02 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Poner en la cola de compilación
|
2019-07-26 01:11:01 +00:00
|
|
|
def enqueue!
|
2021-05-10 16:55:36 +00:00
|
|
|
update(status: 'enqueued') if waiting?
|
2018-02-20 17:47:11 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Está en la cola de compilación?
|
2021-05-10 16:14:02 +00:00
|
|
|
#
|
|
|
|
# TODO: definir todos estos métodos dinámicamente, aunque todavía no
|
|
|
|
# tenemos una máquina de estados propiamente dicha.
|
2018-02-20 17:47:11 +00:00
|
|
|
def enqueued?
|
2019-07-26 01:11:01 +00:00
|
|
|
status == 'enqueued'
|
2018-02-20 17:47:11 +00:00
|
|
|
end
|
|
|
|
|
2021-05-10 16:14:02 +00:00
|
|
|
def waiting?
|
|
|
|
status == 'waiting'
|
|
|
|
end
|
|
|
|
|
|
|
|
def building?
|
|
|
|
status == 'building'
|
|
|
|
end
|
|
|
|
|
2022-12-30 13:55:10 +00:00
|
|
|
def jekyll?
|
|
|
|
File.directory? path
|
|
|
|
end
|
|
|
|
|
2021-09-15 22:50:24 +00:00
|
|
|
def jekyll
|
2022-12-30 13:56:00 +00:00
|
|
|
@jekyll ||=
|
|
|
|
begin
|
|
|
|
install_gems
|
|
|
|
|
2022-12-27 20:42:00 +00:00
|
|
|
Jekyll::Site.new(configuration).tap do |site|
|
|
|
|
site.reader = JekyllData::Reader.new(site) if site.theme
|
|
|
|
end
|
2022-12-30 13:56:00 +00:00
|
|
|
end
|
2021-09-15 22:50:24 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Cargar el sitio Jekyll
|
|
|
|
#
|
2019-07-30 20:05:46 +00:00
|
|
|
# TODO: En lugar de leer todo junto de una vez, extraer la carga de
|
|
|
|
# documentos de Jekyll hacia Sutty para que podamos leer los datos que
|
|
|
|
# necesitamos.
|
2019-08-06 23:17:29 +00:00
|
|
|
def load_jekyll
|
|
|
|
return unless name.present? && File.directory?(path)
|
|
|
|
|
2019-09-17 23:26:59 +00:00
|
|
|
reload_jekyll!
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload_jekyll!
|
2020-03-24 17:06:33 +00:00
|
|
|
reset
|
2021-09-15 22:50:24 +00:00
|
|
|
jekyll
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2019-09-17 23:26:59 +00:00
|
|
|
def reload
|
|
|
|
super
|
|
|
|
reload_jekyll!
|
|
|
|
end
|
|
|
|
|
2020-08-03 18:58:08 +00:00
|
|
|
def configuration
|
|
|
|
return @configuration if @configuration
|
|
|
|
|
2018-05-08 20:51:14 +00:00
|
|
|
# Pasamos destination porque configuration() toma el directorio
|
2019-08-06 23:17:29 +00:00
|
|
|
# actual
|
2018-05-08 20:51:14 +00:00
|
|
|
#
|
|
|
|
# Especificamos `safe` para no cargar los _plugins, que interfieren
|
2019-08-06 23:17:29 +00:00
|
|
|
# entre sitios incluso.
|
|
|
|
#
|
|
|
|
# excerpt_separator está vacío para no incorporar el Excerpt en los
|
|
|
|
# metadatos de Document
|
2020-08-03 18:58:08 +00:00
|
|
|
@configuration =
|
2019-08-06 23:17:29 +00:00
|
|
|
::Jekyll.configuration('source' => path,
|
|
|
|
'destination' => File.join(path, '_site'),
|
|
|
|
'safe' => true, 'watch' => false,
|
|
|
|
'quiet' => true, 'excerpt_separator' => '')
|
2018-02-19 19:33:28 +00:00
|
|
|
|
|
|
|
# No necesitamos cargar plugins en este momento
|
2020-03-24 17:06:33 +00:00
|
|
|
%w[plugins gems].each do |unneeded|
|
2020-08-03 18:58:08 +00:00
|
|
|
@configuration[unneeded] = [] if @configuration.key? unneeded
|
2018-02-19 19:33:28 +00:00
|
|
|
end
|
|
|
|
|
2018-02-22 19:01:11 +00:00
|
|
|
# Si estamos usando nuestro propio plugin de i18n, los posts están
|
|
|
|
# en "colecciones"
|
2021-03-03 12:43:15 +00:00
|
|
|
locales.map(&:to_s).each do |i|
|
2020-08-03 18:58:08 +00:00
|
|
|
@configuration['collections'][i] = {}
|
2018-02-22 19:01:11 +00:00
|
|
|
end
|
|
|
|
|
2020-08-03 18:58:08 +00:00
|
|
|
@configuration
|
2018-02-19 19:33:28 +00:00
|
|
|
end
|
2019-07-04 00:47:59 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Devuelve el dominio actual
|
2019-07-25 18:51:32 +00:00
|
|
|
def self.domain
|
|
|
|
ENV.fetch('SUTTY', 'sutty.nl')
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# El directorio donde se almacenan los sitios
|
|
|
|
def self.site_path
|
2022-04-11 16:29:42 +00:00
|
|
|
@site_path ||= File.realpath(ENV.fetch('SITE_PATH', Rails.root.join('_sites')))
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2020-08-29 23:21:45 +00:00
|
|
|
def self.default
|
2021-05-06 20:54:49 +00:00
|
|
|
find_by(name: "#{Site.domain}.")
|
2020-08-29 23:21:45 +00:00
|
|
|
end
|
|
|
|
|
2020-03-24 17:06:33 +00:00
|
|
|
def reset
|
|
|
|
@read = false
|
|
|
|
@layouts = nil
|
|
|
|
@layouts_struct = nil
|
2020-09-29 21:22:28 +00:00
|
|
|
@layout_keys = nil
|
|
|
|
@configuration = nil
|
|
|
|
@repository = nil
|
|
|
|
@incompatible_layouts = nil
|
|
|
|
@jekyll = nil
|
|
|
|
@config = nil
|
|
|
|
@posts = nil
|
|
|
|
@docs = nil
|
2020-03-24 17:06:33 +00:00
|
|
|
end
|
|
|
|
|
2019-07-04 00:47:59 +00:00
|
|
|
private
|
|
|
|
|
2020-08-20 23:38:31 +00:00
|
|
|
# Asegurarse que el sitio tenga una llave privada
|
|
|
|
def add_private_key_if_missing!
|
|
|
|
self.private_key ||= Lockbox.generate_key
|
|
|
|
end
|
|
|
|
|
2019-07-12 18:22:37 +00:00
|
|
|
# Clona el esqueleto de Sutty para crear el sitio nuevo, no pasa nada
|
|
|
|
# si el sitio ya existe
|
|
|
|
def clone_skel!
|
2022-12-30 13:55:10 +00:00
|
|
|
return if jekyll?
|
2019-07-12 18:22:37 +00:00
|
|
|
|
|
|
|
Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path
|
|
|
|
end
|
|
|
|
|
2019-07-12 19:11:07 +00:00
|
|
|
# Elimina el directorio del sitio
|
2019-07-12 18:34:16 +00:00
|
|
|
def remove_directories!
|
|
|
|
FileUtils.rm_rf path
|
|
|
|
end
|
2019-07-13 00:20:36 +00:00
|
|
|
|
|
|
|
def update_name!
|
|
|
|
return unless name_changed?
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
FileUtils.mv path_was, path
|
2019-09-17 23:26:59 +00:00
|
|
|
reload_jekyll!
|
2019-07-13 00:20:36 +00:00
|
|
|
end
|
2019-07-25 18:51:32 +00:00
|
|
|
|
2019-07-31 20:55:34 +00:00
|
|
|
# Sincroniza algunos atributos del sitio con su configuración y
|
|
|
|
# guarda los cambios
|
2019-08-06 23:17:29 +00:00
|
|
|
#
|
|
|
|
# TODO: Guardar la configuración también, quizás aprovechando algún
|
|
|
|
# método de ActiveRecord para que lance un salvado recursivo.
|
2019-07-31 20:55:34 +00:00
|
|
|
def sync_attributes_with_config!
|
2020-06-25 19:37:34 +00:00
|
|
|
config.theme = design.gem unless design.no_theme?
|
2019-08-09 19:49:25 +00:00
|
|
|
config.description = description
|
|
|
|
config.title = title
|
2022-04-28 15:06:27 +00:00
|
|
|
config.url = url(slash: false)
|
2020-03-23 20:46:19 +00:00
|
|
|
config.hostname = hostname
|
2023-03-16 21:26:17 +00:00
|
|
|
config.locales = locales.map(&:to_s)
|
2019-07-31 20:55:34 +00:00
|
|
|
end
|
|
|
|
|
2019-07-25 18:51:32 +00:00
|
|
|
# Valida si el sitio tiene al menos una forma de alojamiento asociada
|
|
|
|
# y es la local
|
|
|
|
#
|
|
|
|
# TODO: Volver opcional el alojamiento local, pero ahora mismo está
|
|
|
|
# atado a la generación del sitio así que no puede faltar
|
|
|
|
def deploy_local_presence
|
|
|
|
# Usamos size porque queremos saber la cantidad de deploys sin
|
|
|
|
# guardar también
|
2020-05-30 19:43:25 +00:00
|
|
|
return if deploys.size.positive? && deploys.map(&:type).include?('DeployLocal')
|
2019-07-25 18:51:32 +00:00
|
|
|
|
|
|
|
errors.add(:deploys, I18n.t('activerecord.errors.models.site.attributes.deploys.deploy_local_presence'))
|
|
|
|
end
|
2020-08-03 18:58:08 +00:00
|
|
|
|
|
|
|
# Valida que al cambiar de plantilla no tengamos artículos en layouts
|
|
|
|
# inexistentes.
|
|
|
|
def compatible_layouts
|
|
|
|
return unless design_id_changed?
|
|
|
|
|
|
|
|
new_configuration = configuration.dup
|
2020-09-12 15:29:43 +00:00
|
|
|
|
|
|
|
if design.no_theme?
|
|
|
|
new_configuration.delete 'theme'
|
2020-09-25 00:16:15 +00:00
|
|
|
else
|
|
|
|
new_configuration['theme'] = design.gem
|
2020-09-12 15:29:43 +00:00
|
|
|
end
|
|
|
|
|
2020-08-03 18:58:08 +00:00
|
|
|
new_site = Jekyll::Site.new(new_configuration)
|
|
|
|
new_site.read
|
2020-09-28 22:28:57 +00:00
|
|
|
|
|
|
|
# No vale la pena seguir procesando si no hay artículos!
|
|
|
|
return if new_site.documents.empty?
|
|
|
|
|
2020-08-03 18:58:08 +00:00
|
|
|
new_site.documents.map(&:read!)
|
2020-09-28 22:28:57 +00:00
|
|
|
|
2020-08-03 18:58:08 +00:00
|
|
|
old_layouts = new_site.documents.map do |doc|
|
|
|
|
doc.data['layout']
|
|
|
|
end.uniq.compact
|
|
|
|
|
2020-09-28 22:28:57 +00:00
|
|
|
@incompatible_layouts = old_layouts - new_site.layouts.keys
|
2020-08-03 18:58:08 +00:00
|
|
|
|
|
|
|
return if @incompatible_layouts.empty?
|
|
|
|
|
|
|
|
@incompatible_layouts.map! do |layout|
|
|
|
|
i18n.dig('layouts', layout) || layout
|
|
|
|
end
|
|
|
|
|
|
|
|
errors.add(:design_id,
|
|
|
|
I18n.t('activerecord.errors.models.site.attributes.design_id.layout_incompatible.error'))
|
|
|
|
end
|
2021-09-15 22:50:24 +00:00
|
|
|
|
|
|
|
def run_in_path(&block)
|
|
|
|
Dir.chdir path, &block
|
|
|
|
end
|
2022-12-27 21:41:36 +00:00
|
|
|
|
2023-03-27 14:45:23 +00:00
|
|
|
# Instala las gemas cuando es necesario:
|
|
|
|
#
|
|
|
|
# * El sitio existe
|
|
|
|
# * No están instaladas
|
|
|
|
# * El archivo Gemfile se modificó
|
|
|
|
# * El archivo Gemfile.lock se modificó
|
2022-12-27 21:41:36 +00:00
|
|
|
def install_gems
|
|
|
|
return unless persisted?
|
|
|
|
|
2023-04-11 13:48:00 +00:00
|
|
|
deploys.find_by_type('DeployLocal').send(:git_lfs)
|
|
|
|
|
2023-03-27 14:45:23 +00:00
|
|
|
if !gem_dir? || gemfile_updated? || gemfile_lock_updated?
|
|
|
|
deploys.find_by_type('DeployLocal').send(:bundle)
|
|
|
|
touch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si el repositorio de gemas existe
|
|
|
|
def gem_dir?
|
|
|
|
Rails.root.join('_storage', 'gems', name).directory?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si el Gemfile fue modificado
|
|
|
|
def gemfile_updated?
|
2023-03-31 13:56:09 +00:00
|
|
|
updated_at < File.mtime(File.join(path, 'Gemfile'))
|
2023-03-27 14:45:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si el Gemfile.lock fue modificado
|
|
|
|
def gemfile_lock_updated?
|
2023-03-31 13:56:09 +00:00
|
|
|
updated_at < File.mtime(File.join(path, 'Gemfile.lock'))
|
2022-12-27 21:41:36 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|