|
| 1 | +// Copyright (c) 2025 by Developer Network. |
| 2 | +// |
| 3 | +// For more details, see the LICENSE file in the root directory of this |
| 4 | +// source code repository or contact Developer Network at info@devnw.com. |
| 5 | + |
| 6 | +package project |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "text/template" |
| 13 | + |
| 14 | + "go.devnw.com/canary/cli/internal/utils" |
| 15 | +) |
| 16 | + |
| 17 | +// CANARY: REQ=CBIN-148; FEATURE="CopilotInstructionCreator"; ASPECT=CLI; STATUS=BENCHED; TEST=TestCreateCopilotInstructions; BENCH=BenchmarkCreateCopilotInstructions; UPDATED=2025-10-19 |
| 18 | +// createCopilotInstructions generates GitHub Copilot instruction files for the project |
| 19 | +func createCopilotInstructions(projectPath, projectKey string) error { |
| 20 | + instructionsDir := filepath.Join(projectPath, ".github", "instructions") |
| 21 | + |
| 22 | + // Create .github/instructions/ directory structure |
| 23 | + if err := os.MkdirAll(instructionsDir, 0755); err != nil { |
| 24 | + return fmt.Errorf("create .github/instructions: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + // Define instruction files to create |
| 28 | + instructionFiles := map[string]string{ |
| 29 | + // Repository-wide instruction |
| 30 | + "repository.md": "base/copilot/repository.md", |
| 31 | + |
| 32 | + // Path-specific instructions (nested directories) |
| 33 | + ".canary/specs/instruction.md": "base/copilot/specs.md", |
| 34 | + ".canary/instruction.md": "base/copilot/canary.md", |
| 35 | + "tests/instruction.md": "base/copilot/tests.md", |
| 36 | + } |
| 37 | + |
| 38 | + // Template data for variable substitution |
| 39 | + type TemplateData struct { |
| 40 | + ProjectKey string |
| 41 | + } |
| 42 | + data := TemplateData{ProjectKey: projectKey} |
| 43 | + |
| 44 | + for targetPath, templatePath := range instructionFiles { |
| 45 | + fullTargetPath := filepath.Join(instructionsDir, targetPath) |
| 46 | + |
| 47 | + // Check if file already exists (preserve user customizations) |
| 48 | + if _, err := os.Stat(fullTargetPath); err == nil { |
| 49 | + // Silently skip existing files to avoid noise in tests |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + // Create parent directories for path-specific instructions |
| 54 | + if err := os.MkdirAll(filepath.Dir(fullTargetPath), 0755); err != nil { |
| 55 | + return fmt.Errorf("create directory for %s: %w", targetPath, err) |
| 56 | + } |
| 57 | + |
| 58 | + // Read template from embedded filesystem |
| 59 | + templateContent, err := utils.ReadEmbeddedFile(templatePath) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("read template %s: %w", templatePath, err) |
| 62 | + } |
| 63 | + |
| 64 | + // Parse and execute template |
| 65 | + tmpl, err := template.New(targetPath).Parse(string(templateContent)) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("parse template %s: %w", templatePath, err) |
| 68 | + } |
| 69 | + |
| 70 | + // Write to file |
| 71 | + outFile, err := os.Create(fullTargetPath) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("create file %s: %w", fullTargetPath, err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := tmpl.Execute(outFile, data); err != nil { |
| 77 | + outFile.Close() |
| 78 | + return fmt.Errorf("execute template %s: %w", templatePath, err) |
| 79 | + } |
| 80 | + outFile.Close() |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// readEmbeddedFile is a wrapper around utils.ReadEmbeddedFile for testing |
| 87 | +func readEmbeddedFile(path string) ([]byte, error) { |
| 88 | + return utils.ReadEmbeddedFile(path) |
| 89 | +} |
0 commit comments