require "tailwindcss-rails" require "shellwords" require "panda_cms/engine" ENV["TAILWIND_PATH"] ||= Tailwindcss::Engine.root.join("exe/tailwindcss").to_s namespace :panda_cms do desc "Generate missing blocks from template files" task generate_missing_blocks: [:environment] do # Loop through all templates in app/views/layouts/*.html.erb Dir.glob("app/views/layouts/*.html.erb").each do |file| File.open(file).each_line do |line| # Matches: # PandaCms::RichTextComponent.new(key: :value) # PandaCms::RichTextComponent.new key: :value, key: value line.match(/PandaCms::([a-zA-Z]+)Component\.new[ \(]+([^\)]+)[\)]*/) do |match| # Extract the hash values template_path = file.gsub("app/views/", "").gsub(".html.erb", "") template_name = template_path.gsub("layouts/", "").titleize # Create the template if it doesn't exist template = PandaCms::Template.find_or_create_by!(file_path: template_path) do |template| template.name = template_name end block_kind = match[1].underscore match[2].split(",").map do |keyvar| key, value = keyvar.split(":", 2) next if key != "key" block_name = value.to_s.strip.tr(":", "") # Create the block if it doesn't exist block = PandaCms::Block.find_or_create_by!(template: template, kind: block_kind, key: block_name) do |block| block.name = block_name.titleize end # For the given block, create the block_content for each page using the template template.pages.each do |page| PandaCms::BlockContent.find_or_create_by!(block: block, page: page) do |block_content| block_content.content = "" end end end end end end end namespace :assets do desc "Compile assets for release" task :compile do # Copy all the JS files into public admin_js_path = PandaCms::Engine.root.join("app/javascript/panda_cms") FileUtils.cp_r "#{admin_js_path}/.", PandaCms::Engine.root.join("public/panda-cms-assets/javascripts") end desc "Build admin assets for Panda CMS" task :admin do # This is enough for development run_tailwind( root_path: PandaCms::Engine.root, input_path: "app/assets/stylesheets/panda_cms/application.tailwind.css", output_path: "app/assets/builds/panda_cms.css" ) end desc "Build dummy assets for Panda CMS" # We only care about this in development task :dummy do run_tailwind( root_path: Rails.application.root, input_path: "app/assets/stylesheets/application.tailwind.css", output_path: "app/assets/builds/application.css", config_path: "config/tailwind.config.js" ) end desc "Watch admin assets for Panda CMS" # We only care about this in development task :watch_admin do run_tailwind( root_path: PandaCms::Engine.root, input_path: "app/assets/stylesheets/panda_cms/application.tailwind.css", output_path: "app/assets/builds/panda_cms.css", watch: true ) end desc "Watch dummy assets for Panda CMS" # We only care about this in development task :watch_dummy do run_tailwind( root_path: Rails.application.root, input_path: "app/assets/stylesheets/application.tailwind.css", output_path: "app/assets/builds/application.css", config_path: "config/tailwind.config.js", watch: true ) end end end task default: [:spec, :panda_cms] def run_tailwind(root_path:, input_path: nil, output_path: nil, config_path: nil, watch: false) Rails.logger = Logger.new($stdout) config_path ||= root_path.join("config/tailwind.config.js") command = [ ENV["TAILWIND_PATH"], "-i #{root_path.join(input_path)}", "-o #{root_path.join(output_path)}", "-c #{root_path.join(config_path)}", "-m" ] command << "-w" if watch exec command.join(" ") end