# frozen_string_literal: true module Bridgetown module Commands class Build < Thor::Group extend BuildOptions extend Summarizable include ConfigurationOverridable Registrations.register do register(Build, "build", "build", Build.summary) end def self.banner "bridgetown build [options]" end summary "Build your site and save to destination folder" class_option :watch, type: :boolean, aliases: "-w", desc: "Watch for changes and rebuild" def self.print_startup_message Bridgetown.logger.info "Starting:", "Bridgetown v#{Bridgetown::VERSION.magenta} " \ "(codename \"#{Bridgetown::CODE_NAME.yellow}\")" end # Build your bridgetown site # Continuously watch if `watch` is set to true in the config. def build Bridgetown.logger.adjust_verbosity(options) # @type [Bridgetown::Configuration] config_options = configuration_with_overrides( options, Bridgetown::Current.preloaded_configuration ) self.class.print_startup_message unless config_options["start_command"] Process.setproctitle( "bridgetown #{Bridgetown::VERSION} " \ "(#{config_options["start_command"] ? "start" : "build"}) [#{File.basename(Dir.pwd)}]" ) config_options.run_initializers! context: :static if !Bridgetown.env.production? && !config_options[:skip_frontend] && config_options["start_command"] if Bridgetown::Utils.frontend_bundler_type(config_options[:root_dir]) == :esbuild Bridgetown::Utils.update_esbuild_autogenerated_config config_options end require "rake" Rake.with_application do |rake| rake.load_rakefile rake["frontend:watcher"].invoke(true) end end @site = Bridgetown::Site.new(config_options) if config_options.fetch("skip_initial_build", false) Bridgetown.logger.warn "Build Warning:", "Skipping the initial build. " \ "This may result in an out-of-date site." else build_site(config_options) end if config_options.fetch("watch", false) watch_site(config_options) else Bridgetown.logger.info "Auto-regeneration:", "disabled. Use --watch to enable." end end protected # Build your Bridgetown site # # @param options [Bridgetown::Configuration] options loaded from config and/or CLI def build_site(config_options) t = Time.now display_folder_paths(config_options) if config_options["unpublished"] Bridgetown.logger.info "Unpublished mode:", "enabled. Processing documents marked unpublished" end Bridgetown.logger.info "Generating…" @site.process Bridgetown.logger.info "Done! 🎉", "#{"Completed".bold.green} in less than " \ "#{(Time.now - t).ceil(2)} seconds." end # Watch for file changes and rebuild the site # # @param options [Bridgetown::Configuration] options loaded from config and/or CLI def watch_site(config_options) Bridgetown::Watcher.watch(@site, config_options) end # Display the source and destination folder paths # # @param options [Bridgetown::Configuration] options loaded from config and/or CLI def display_folder_paths(config_options) source = File.expand_path(config_options["source"]) destination = File.expand_path(config_options["destination"]) Bridgetown.logger.info "Environment:", Bridgetown.environment.cyan Bridgetown.logger.info "Source:", source Bridgetown.logger.info "Destination:", destination return unless config_options["plugins_dir"].is_a?(String) plugins_dir = File.expand_path(config_options["plugins_dir"]) Bridgetown.logger.info "Custom Plugins:", plugins_dir if Dir.exist?(plugins_dir) end end end end