# frozen_string_literal: true # Alojamiento local, genera el sitio como si corriéramos `jekyll build`. class DeployLocal < Deploy # Asegurarse que el hostname es el permitido. before_validation :reset_hostname!, :default_hostname! # Actualiza el hostname con www si cambiamos el hostname before_update :update_deploy_www!, if: :hostname_changed? # Realizamos la construcción del sitio usando Jekyll y un entorno # limpio para no pasarle secretos # # Pasamos variables de entorno mínimas para no filtrar secretos de # Sutty def deploy return false unless mkdir return false unless yarn return false unless bundle jekyll_build end # Obtener el tamaño de todos los archivos y directorios (los # directorios son archivos :) # # @return [Integer] def size paths = [destination, File.join(destination, '**', '**')] Dir.glob(paths).map do |file| File.symlink?(file) ? 0 : File.size(file) end.inject(:+) end # El hostname es el nombre del sitio más el dominio principal. # # @return [String] def default_hostname "#{site.name}.#{Site.domain}" end private def reset_hostname! self.hostname = nil end # XXX: En realidad el DeployWww debería regenerar su propio hostname. def update_deploy_www! site.deploys.where(type: 'DeployWww').map do |www| www.update hostname: www.default_hostname end end # Crea el directorio destino si no existe. def mkdir FileUtils.mkdir_p destination end # Un entorno que solo tiene lo que necesitamos # # @return [Hash] def env # XXX: This doesn't support Windows paths :B paths = [File.dirname(`which bundle`), '/usr/bin', '/bin'] { 'HOME' => site.path, 'PATH' => paths.join(':'), 'SPREE_API_KEY' => site.tienda_api_key, 'SPREE_URL' => site.tienda_url, 'AIRBRAKE_PROJECT_ID' => site.id.to_s, 'AIRBRAKE_PROJECT_KEY' => site.airbrake_api_key, 'JEKYLL_ENV' => Rails.env, 'LANG' => ENV['LANG'] } end # @return [String] def yarn_lock File.join(site.path, 'yarn.lock') end # Determina si este proyecto se gestiona con Yarn, buscando si el # archivo yarn.lock existe. # # @return [Boolean] def yarn_lock? File.exist? yarn_lock end def gem run %(gem install bundler --no-document) end # Corre yarn dentro del repositorio # # @return [Boolean,Nil] def yarn return true unless yarn_lock? run 'yarn' end # Instala las dependencias. # # @return [Boolean] def bundle if Rails.env.production? run %(bundle install --no-cache --path="#{gems_dir}") else run %(bundle install) end end # Genera el sitio. # # @return [Boolean] def jekyll_build run %(bundle exec jekyll build --trace --profile --destination "#{escaped_destination}") end # no debería haber espacios ni caracteres especiales, pero por si # acaso... def escaped_destination Shellwords.escape destination end # Eliminar el destino si se elimina el deploy def remove_destination! FileUtils.rm_rf destination end end