diff --git a/.gitea/workflows/kibot.yml b/.gitea/workflows/kibot.yml index d23c596..7e7acc5 100644 --- a/.gitea/workflows/kibot.yml +++ b/.gitea/workflows/kibot.yml @@ -5,18 +5,20 @@ name: "KiBot PCB Generation with Multi-layer Support" on: push: paths: - - 'kicad/c64psu/*.kicad_sch' - - 'kicad/c64psu/*.kicad_pcb' - - 'kicad/c64psu/*.kicad_pro' - - '.gitea/workflows/kibot_quick_start.yml' - - 'kibot.yaml' + - '**/*.kicad_sch' + - '**/*.kicad_pcb' + - '**/*.kicad_pro' + - '**/*.kibot.yaml' + - '**/kibot.yaml' + - '.gitea/workflows/*.yml' pull_request: paths: - - 'kicad/c64psu/*.kicad_sch' - - 'kicad/c64psu/*.kicad_pcb' - - 'kicad/c64psu/*.kicad_pro' - - '.gitea/workflows/kibot_quick_start.yml' - - 'kibot.yaml' + - '**/*.kicad_sch' + - '**/*.kicad_pcb' + - '**/*.kicad_pro' + - '**/*.kibot.yaml' + - '**/kibot.yaml' + - '.gitea/workflows/*.yml' repository_dispatch: types: [run_qs] workflow_dispatch: @@ -43,38 +45,68 @@ jobs: # So we can run a diff between last 2 changes fetch-depth: '0' + - name: Find KiCad project + id: find_project + run: | + # Find all .kicad_pcb files in the repository + PCB_FILES=$(find . -name "*.kicad_pcb" -type f | grep -v -E '/(backups?|backup|old|archive|deprecated)/' | head -5) + + if [ -z "$PCB_FILES" ]; then + echo "Error: No KiCad PCB files found" + exit 1 + fi + + # Use the first PCB file found + PCB_FILE=$(echo "$PCB_FILES" | head -1) + PROJECT_DIR=$(dirname "$PCB_FILE") + PROJECT_NAME=$(basename "$PCB_FILE" .kicad_pcb) + + echo "Found KiCad project: $PROJECT_NAME" + echo "Project directory: $PROJECT_DIR" + echo "PCB file: $PCB_FILE" + + # Find schematic file + SCH_FILE=$(find "$PROJECT_DIR" -name "*.kicad_sch" -o -name "*.sch" | head -1) + + echo "pcb_file=$PCB_FILE" >> $GITHUB_OUTPUT + echo "project_dir=$PROJECT_DIR" >> $GITHUB_OUTPUT + echo "project_name=$PROJECT_NAME" >> $GITHUB_OUTPUT + echo "sch_file=$SCH_FILE" >> $GITHUB_OUTPUT + + # List all found projects for debugging + echo "All PCB files found:" + echo "$PCB_FILES" + - name: Detect layer count id: layers - working-directory: kicad/c64psu run: | + PCB_FILE="${{ steps.find_project.outputs.pcb_file }}" if [ "${{ github.event.inputs.board_layers }}" != "" ] && [ "${{ github.event.inputs.board_layers }}" != "auto" ]; then echo "layers=${{ github.event.inputs.board_layers }}" >> $GITHUB_OUTPUT echo "Using manually specified ${{ github.event.inputs.board_layers }} layer configuration" else - # Auto-detect layers from PCB file - LAYERS=$(python3 -c " - import re - import sys - import glob + # Create Python script to detect layers + cat > /tmp/detect_layers.py << 'PYTHON_SCRIPT' +#!/usr/bin/env python3 +import re +import sys + +try: + pcb_file = sys.argv[1] + with open(pcb_file, 'r') as f: + content = f.read() + # Find all copper layers + copper_layers = re.findall(r'\(layer\s+"([^"]+)".*?\(type\s+"?copper"?\)', content, re.DOTALL) + # Count inner layers (In1.Cu, In2.Cu, etc) + inner_layers = [l for l in copper_layers if 'In' in l and '.Cu' in l] + total = 2 + len(inner_layers) # F.Cu + B.Cu + inner layers + print(total) +except Exception as e: + print(2) # Default to 2 layers +PYTHON_SCRIPT - try: - pcb_files = glob.glob('*.kicad_pcb') - if not pcb_files: - print(2) - sys.exit(0) - - with open(pcb_files[0], 'r') as f: - content = f.read() - # Find all copper layers - copper_layers = re.findall(r'\(layer\s+\"([^\"]+)\".*?\(type\s+\"?copper\"?\)', content, re.DOTALL) - # Count inner layers (In1.Cu, In2.Cu, etc) - inner_layers = [l for l in copper_layers if 'In' in l and '.Cu' in l] - total = 2 + len(inner_layers) # F.Cu + B.Cu + inner layers - print(total) - except Exception as e: - print(f'Error: {e}', file=sys.stderr) - print(2) # Default to 2 layers - ") + chmod +x /tmp/detect_layers.py + LAYERS=$(/tmp/detect_layers.py "$PCB_FILE") echo "layers=$LAYERS" >> $GITHUB_OUTPUT echo "Auto-detected $LAYERS layer board" fi @@ -82,66 +114,91 @@ jobs: - name: Check for KiBot config id: config_check run: | - if [ -f "kibot.yaml" ]; then + PROJECT_DIR="${{ steps.find_project.outputs.project_dir }}" + + # Search for kibot config in multiple locations + if [ -f "$PROJECT_DIR/kibot.yaml" ]; then echo "found=true" >> $GITHUB_OUTPUT - echo "Using custom kibot.yaml configuration" - elif [ -f "kicad/c64psu/kibot.yaml" ]; then + echo "config_path=$PROJECT_DIR/kibot.yaml" >> $GITHUB_OUTPUT + echo "Using project-specific kibot.yaml" + elif [ -f "$PROJECT_DIR/../kibot.yaml" ]; then echo "found=true" >> $GITHUB_OUTPUT - echo "config_path=kicad/c64psu/kibot.yaml" >> $GITHUB_OUTPUT - echo "Using project-specific kibot.yaml configuration" + echo "config_path=$PROJECT_DIR/../kibot.yaml" >> $GITHUB_OUTPUT + echo "Using parent directory kibot.yaml" + elif [ -f "kibot.yaml" ]; then + echo "found=true" >> $GITHUB_OUTPUT + echo "config_path=kibot.yaml" >> $GITHUB_OUTPUT + echo "Using root kibot.yaml" + elif [ -f ".kibot/config.yaml" ]; then + echo "found=true" >> $GITHUB_OUTPUT + echo "config_path=.kibot/config.yaml" >> $GITHUB_OUTPUT + echo "Using .kibot/config.yaml" else echo "found=false" >> $GITHUB_OUTPUT echo "Using --quick-start mode (no custom config found)" fi - name: Run ERC - working-directory: kicad/c64psu + if: steps.config_check.outputs.found == 'true' run: | - if [ "${{ steps.config_check.outputs.found }}" == "true" ]; then - CONFIG_PATH="${{ steps.config_check.outputs.config_path }}" - [ -z "$CONFIG_PATH" ] && CONFIG_PATH="../../kibot.yaml" - [ -f *.kicad_sch ] && kibot -c "$CONFIG_PATH" -s run_erc -v || true + cd "${{ steps.find_project.outputs.project_dir }}" + CONFIG_PATH="../${{ steps.config_check.outputs.config_path }}" + CONFIG_PATH=$(realpath "$CONFIG_PATH") + + if [ -n "${{ steps.find_project.outputs.sch_file }}" ]; then + echo "Running ERC on schematic files..." + kibot -c "$CONFIG_PATH" -s run_erc -v || true + else + echo "No schematic files found, skipping ERC" fi continue-on-error: true - name: Run DRC - working-directory: kicad/c64psu + if: steps.config_check.outputs.found == 'true' run: | - if [ "${{ steps.config_check.outputs.found }}" == "true" ]; then - CONFIG_PATH="${{ steps.config_check.outputs.config_path }}" - [ -z "$CONFIG_PATH" ] && CONFIG_PATH="../../kibot.yaml" - [ -f *.kicad_pcb ] && kibot -c "$CONFIG_PATH" -s run_drc -v || true - fi + cd "${{ steps.find_project.outputs.project_dir }}" + CONFIG_PATH="../${{ steps.config_check.outputs.config_path }}" + CONFIG_PATH=$(realpath "$CONFIG_PATH") + + echo "Running DRC on PCB..." + kibot -c "$CONFIG_PATH" -s run_drc -v || true continue-on-error: true - name: Generate outputs with custom config if: steps.config_check.outputs.found == 'true' - working-directory: kicad/c64psu run: | + cd "${{ steps.find_project.outputs.project_dir }}" + CONFIG_PATH="../${{ steps.config_check.outputs.config_path }}" + CONFIG_PATH=$(realpath "$CONFIG_PATH") + echo "Generating outputs for ${{ steps.layers.outputs.layers }} layer board" - CONFIG_PATH="${{ steps.config_check.outputs.config_path }}" - [ -z "$CONFIG_PATH" ] && CONFIG_PATH="../../kibot.yaml" + echo "Project: ${{ steps.find_project.outputs.project_name }}" # Run KiBot with the configuration kibot -c "$CONFIG_PATH" -d Fabrication -v - # Move outputs to match the original structure - mkdir -p ../../Generated + # Move outputs to root Generated folder + mkdir -p ../Generated if [ -d "Fabrication" ]; then - cp -r Fabrication/* ../../Generated/ + cp -r Fabrication/* ../Generated/ fi + cd .. - name: Quick Start fallback if: steps.config_check.outputs.found == 'false' - working-directory: kicad/c64psu run: | + cd "${{ steps.find_project.outputs.project_dir }}" echo "Running KiBot in quick-start mode" kibot --quick-start - # Move outputs to expected location + # Move outputs to root Generated folder if [ -d "Generated" ]; then - mv Generated ../../ + mv Generated ../ + else + mkdir -p ../Generated + echo "No Generated folder found after quick-start" fi + cd .. - name: Add layer information to outputs run: | @@ -197,7 +254,6 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Retrieve results uses: actions/download-artifact@v3 with: