|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to prepare WIT files by copying and making modifications |
| 4 | +# Usage: ./scripts/prepare-wit-files.sh -i <input_dir> -o <output_dir> -s <search_string> |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +BLUE='\033[0;34m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +# Default values |
| 16 | +INPUT_DIR="" |
| 17 | +OUTPUT_DIR="" |
| 18 | +SEARCH_STRING="" |
| 19 | +SCRIPT_NAME=$(basename "$0") |
| 20 | + |
| 21 | +# Function to show usage |
| 22 | +show_usage() { |
| 23 | + echo -e "${BLUE}Usage:${NC}" |
| 24 | + echo -e " $SCRIPT_NAME -i <input_dir> -o <output_dir> -s <search_string>" |
| 25 | + echo "" |
| 26 | + echo -e "${BLUE}Options:${NC}" |
| 27 | + echo -e " -i Input directory containing .wit files" |
| 28 | + echo -e " -o Output directory for processed .wit files" |
| 29 | + echo -e " -s Search string to uncomment (e.g., 'SPECIFIC TinyGo')" |
| 30 | + echo "" |
| 31 | + echo -e "${BLUE}Examples:${NC}" |
| 32 | + echo -e " $SCRIPT_NAME -i ./crates/pluginlab/wit -o ./go_modules/wit -s 'SPECIFIC TinyGo'" |
| 33 | + echo -e " $SCRIPT_NAME -i ./src/wit -o ./dist/wit -s 'SPECIFIC Rust'" |
| 34 | + echo "" |
| 35 | + echo -e "${BLUE}Description:${NC}" |
| 36 | + echo -e " Copies .wit files from input directory to output directory and:" |
| 37 | + echo -e " 1. Adds a header comment indicating the file is generated" |
| 38 | + echo -e " 2. Uncomments lines containing the specified search string" |
| 39 | +} |
| 40 | + |
| 41 | +# Parse command line arguments |
| 42 | +while getopts "i:o:s:h" opt; do |
| 43 | + case $opt in |
| 44 | + i) INPUT_DIR="$OPTARG" ;; |
| 45 | + o) OUTPUT_DIR="$OPTARG" ;; |
| 46 | + s) SEARCH_STRING="$OPTARG" ;; |
| 47 | + h) show_usage; exit 0 ;; |
| 48 | + \?) echo -e "${RED}Invalid option: -$OPTARG${NC}" >&2; show_usage; exit 1 ;; |
| 49 | + :) echo -e "${RED}Option -$OPTARG requires an argument.${NC}" >&2; show_usage; exit 1 ;; |
| 50 | + esac |
| 51 | +done |
| 52 | + |
| 53 | +# Check if required arguments are provided |
| 54 | +if [ -z "$INPUT_DIR" ] || [ -z "$OUTPUT_DIR" ] || [ -z "$SEARCH_STRING" ]; then |
| 55 | + echo -e "${RED}Error: Missing required arguments${NC}" |
| 56 | + show_usage |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +# Check if input directory exists |
| 61 | +if [ ! -d "$INPUT_DIR" ]; then |
| 62 | + echo -e "${RED}Error: Input directory '$INPUT_DIR' does not exist${NC}" |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +# Create output directory if it doesn't exist |
| 67 | +mkdir -p "$OUTPUT_DIR" |
| 68 | + |
| 69 | +echo -e "${GREEN}Preparing WIT files...${NC}" |
| 70 | +echo -e "${BLUE}Input:${NC} $INPUT_DIR" |
| 71 | +echo -e "${BLUE}Output:${NC} $OUTPUT_DIR" |
| 72 | +echo -e "${BLUE}Search:${NC} $SEARCH_STRING" |
| 73 | +echo "" |
| 74 | + |
| 75 | +# Copy all .wit files from input to output |
| 76 | +echo -e "${YELLOW}Copying WIT files...${NC}" |
| 77 | +cp "$INPUT_DIR"/*.wit "$OUTPUT_DIR/" |
| 78 | + |
| 79 | +# Process each .wit file |
| 80 | +for wit_file in "$OUTPUT_DIR"/*.wit; do |
| 81 | + if [ -f "$wit_file" ]; then |
| 82 | + filename=$(basename "$wit_file") |
| 83 | + echo -e "${YELLOW}Processing $filename...${NC}" |
| 84 | + |
| 85 | + # Create a temporary file |
| 86 | + temp_file=$(mktemp) |
| 87 | + |
| 88 | + # Add the header comment |
| 89 | + echo "// Code generated by \`$SCRIPT_NAME\`, from \`$INPUT_DIR/*.wit\`. DO NOT EDIT!" > "$temp_file" |
| 90 | + echo "" >> "$temp_file" |
| 91 | + |
| 92 | + # Add the rest of the file content, uncommenting lines with search string |
| 93 | + sed "s|^\\([[:space:]]*\\)//[[:space:]]*\\(.*$SEARCH_STRING.*\\)|\\1\\2|" "$wit_file" >> "$temp_file" |
| 94 | + |
| 95 | + # Replace the original file with the modified content |
| 96 | + mv "$temp_file" "$wit_file" |
| 97 | + |
| 98 | + echo -e "${GREEN}✓ Processed $filename${NC}" |
| 99 | + fi |
| 100 | +done |
| 101 | + |
| 102 | +echo "" |
| 103 | +echo -e "${GREEN}✓ All WIT files prepared successfully!${NC}" |
| 104 | +echo -e "${YELLOW}Files copied to: $OUTPUT_DIR${NC}" |
0 commit comments