#!/usr/bin/env ruby # frozen_string_literal: true VERSION = '8.6.1' APPS = %w[repro rails demo calendar].freeze LINUX = RbConfig::CONFIG['host_os'].include?('linux') HOST = '0.0.0.0' PORT = '8000' require_relative '../lib/optimist' opts = Optimist.options do text <<~HEAD Pagy #{VERSION} (https://ddnexus.github.io/pagy/playground) Playground to showcase, clone and develop pagy APPs APPs repro Reproduce generic/simple issues rails Reproduce rails related issues demo Showcase all the helpers and styles calendar Showcase the calendar; reproduce related issues USAGE pagy APP [options] Showcase APP from the installed gem pagy clone APP Clone APP to the current dir pagy APPFILE [options] Develop APPFILE from local path EXAMPLES pagy demo Showcase demo at http://#{HOST}:#{PORT} pagy clone repro Clone repro to ./repro.ru pagy ~/my-repro.ru Develop ~/my-repro.ru at#{HOST}:#{PORT} HEAD text 'Rackup options' opt :env, 'Environment', default: 'development' opt :host, 'Host', default: HOST, short: :o opt :port, 'Port', default: PORT opt :install, 'Install bundle for users', default: true if LINUX text 'Rerun options' opt :rerun, 'Enable rerun for development', default: true opt :clear, 'Clear screen before each rerun' end text 'Other options' opt :quiet, 'Quiet mode for development' version VERSION end Optimist.educate if ARGV.empty? run_from_repo = File.exist?(File.expand_path('../pagy.gemspec', __dir__)) # Handles gems require 'bundler/inline' require 'bundler' Bundler.configure gemfile(opts[:install]) do source 'https://rubygems.org' gem 'rackup' gem 'rerun' if LINUX end path = ->(app) { File.expand_path("../apps/#{app}.ru", __dir__) } arg = ARGV.shift if arg.eql?('clone') arg = ARGV.shift Optimist.die("Expected APP to be in [#{APPS.join(', ')}]; got #{arg.inspect}") unless APPS.include?(arg) file = path.(arg) name = File.basename(file) if File.exist?(name) print "Do you want to overwrite the #{name.inspect} file? (y/n)> " answer = gets.chomp Optimist.die("#{name.inspect} file already present") unless answer.start_with?(/y/i) end require 'fileutils' FileUtils.cp(file, '.', verbose: true) else if APPS.include?(arg) # showcase env opts[:env] = 'showcase' opts[:rerun] = false opts[:quiet] = true # Avoid the creation of './tmp/local_secret.txt' for showcase env ENV['SECRET_KEY_BASE'] = 'absolute secret!' if arg.eql?('rails') file = path.(arg) else # development env file = arg end Optimist.die("#{file.inspect} app not found") unless File.exist?(file) # Run command gem_dir = File.expand_path('..', __dir__) rackup = "rackup -I #{gem_dir}/lib -r pagy -o #{opts[:host]} -p #{opts[:port]} -E #{opts[:env]} #{file}" rackup << ' -q' if opts[:quiet] if opts[:rerun] name = File.basename(file) dir = File.dirname(file) rerun = if run_from_repo # rerun app also when gem dir files change (for pagy devs) "rerun --name #{name} -d #{dir},#{gem_dir} -p **/*.{rb,js,css,scss,ru,yml}" else "rerun --name #{name} -d #{dir} -p #{name}" # rerun only when app.ru changes end rerun << ' -q' if opts[:quiet] rerun << ' -c' if opts[:clear] rerun << " -- #{rackup}" end exec("PAGY_INSTALL_BUNDLE='#{opts[:install]}' #{rerun || rackup}") end