#!/usr/bin/env ruby require 'optparse' require 'fileutils' options = { prefix: nil, inline: false } # Parse command line options option_parser = OptionParser.new do |opts| opts.banner = "Usage: #{$0} [options] FOLDER_NAME" opts.on("--prefix PREFIX", "Prefix for asset names") do |prefix| options[:prefix] = prefix end opts.on("--inline", "Set $inline to true (default: false)") do options[:inline] = true end opts.on("-h", "--help", "Display this help message") do puts opts exit end end begin option_parser.parse! # Check if folder name is provided if ARGV.empty? STDERR.puts "Error: Folder name is required." STDERR.puts option_parser exit 1 end # Get the folder name from remaining arguments folder_name = ARGV[0] # Check if prefix is provided if options[:prefix].nil? STDERR.puts "Error: Prefix is required. Use --prefix option." STDERR.puts option_parser exit 1 end # Verify the folder exists unless Dir.exist?(folder_name) STDERR.puts "Error: Folder '#{folder_name}' does not exist." exit 1 end # Get all files in the folder (non-recursively) files = Dir.entries(folder_name).reject { |f| File.directory?(File.join(folder_name, f)) } # Process each file and create the content content = files.map do |file| # Skip hidden files (those starting with a dot) next if file.start_with?('.') # Get the base name and extension base_name = File.basename(file, ".*") # Convert hyphens to underscores underscored_name = base_name.gsub('-', '_') <<~ASSET @asset #{options[:prefix]}_#{underscored_name} { $inline: #{options[:inline]} file_name: "#{file}" } ASSET end.compact.join("\n") # Output the content to stdout puts content # Print summary information to stderr total_assets = files.count { |f| !f.start_with?('.') } STDERR.puts "Generated #{total_assets} asset entries for folder '#{folder_name}'." rescue OptionParser::InvalidOption => e STDERR.puts e.message STDERR.puts option_parser exit 1 end