@@ -4,7 +4,10 @@ package main
44
55import (
66 "fmt"
7+ "io/fs"
78 "os"
9+ "path/filepath"
10+ "strings"
811)
912
1013// runCompile implements `apm compile [OPTIONS]`.
@@ -102,11 +105,17 @@ func runCompile(args []string) int {
102105 }
103106 switch t {
104107 case "copilot" :
105- fmt .Println (" [+] .github/copilot-instructions.md" )
108+ if code := compileCopilot (cwd , flagVerbose ); code != 0 {
109+ return code
110+ }
106111 case "claude" :
107- fmt .Println (" [+] CLAUDE.md" )
112+ if code := compileClaude (cwd , flagVerbose ); code != 0 {
113+ return code
114+ }
108115 case "cursor" :
109- fmt .Println (" [+] .cursor/rules/AGENTS.md" )
116+ if code := compileCursor (cwd , flagVerbose ); code != 0 {
117+ return code
118+ }
110119 default :
111120 fmt .Printf (" [+] AGENTS.md (target: %s)\n " , t )
112121 }
@@ -119,3 +128,82 @@ func runCompile(args []string) int {
119128 fmt .Println ("[+] Compilation complete." )
120129 return 0
121130}
131+
132+ // compileCopilot writes .github/copilot-instructions.md from .apm/prompts/*.md.
133+ func compileCopilot (cwd string , verbose bool ) int {
134+ promptsDir := filepath .Join (cwd , ".apm" , "prompts" )
135+ var content strings.Builder
136+ _ = filepath .WalkDir (promptsDir , func (path string , d fs.DirEntry , err error ) error {
137+ if err != nil || d .IsDir () || ! strings .HasSuffix (d .Name (), ".md" ) {
138+ return nil
139+ }
140+ data , readErr := os .ReadFile (path )
141+ if readErr != nil {
142+ return nil
143+ }
144+ content .Write (data )
145+ if ! strings .HasSuffix (string (data ), "\n " ) {
146+ content .WriteString ("\n " )
147+ }
148+ return nil
149+ })
150+ out := filepath .Join (cwd , ".github" , "copilot-instructions.md" )
151+ if err := os .MkdirAll (filepath .Dir (out ), 0o755 ); err != nil {
152+ fmt .Fprintf (os .Stderr , "[x] Failed to create .github/: %v\n " , err )
153+ return 1
154+ }
155+ if err := os .WriteFile (out , []byte (content .String ()), 0o644 ); err != nil {
156+ fmt .Fprintf (os .Stderr , "[x] Failed to write %s: %v\n " , out , err )
157+ return 1
158+ }
159+ if verbose {
160+ fmt .Printf (" [+] .github/copilot-instructions.md (%d bytes)\n " , content .Len ())
161+ } else {
162+ fmt .Println (" [+] .github/copilot-instructions.md" )
163+ }
164+ return 0
165+ }
166+
167+ // compileClaude writes CLAUDE.md from .apm/prompts/*.md.
168+ func compileClaude (cwd string , verbose bool ) int {
169+ return compileTarget (cwd , filepath .Join (cwd , "CLAUDE.md" ), verbose )
170+ }
171+
172+ // compileCursor writes .cursor/rules/AGENTS.md from .apm/prompts/*.md.
173+ func compileCursor (cwd string , verbose bool ) int {
174+ return compileTarget (cwd , filepath .Join (cwd , ".cursor" , "rules" , "AGENTS.md" ), verbose )
175+ }
176+
177+ func compileTarget (cwd , out string , verbose bool ) int {
178+ promptsDir := filepath .Join (cwd , ".apm" , "prompts" )
179+ var content strings.Builder
180+ _ = filepath .WalkDir (promptsDir , func (path string , d fs.DirEntry , err error ) error {
181+ if err != nil || d .IsDir () || ! strings .HasSuffix (d .Name (), ".md" ) {
182+ return nil
183+ }
184+ data , readErr := os .ReadFile (path )
185+ if readErr != nil {
186+ return nil
187+ }
188+ content .Write (data )
189+ if ! strings .HasSuffix (string (data ), "\n " ) {
190+ content .WriteString ("\n " )
191+ }
192+ return nil
193+ })
194+ if err := os .MkdirAll (filepath .Dir (out ), 0o755 ); err != nil {
195+ fmt .Fprintf (os .Stderr , "[x] Failed to create output dir: %v\n " , err )
196+ return 1
197+ }
198+ if err := os .WriteFile (out , []byte (content .String ()), 0o644 ); err != nil {
199+ fmt .Fprintf (os .Stderr , "[x] Failed to write %s: %v\n " , out , err )
200+ return 1
201+ }
202+ rel , _ := filepath .Rel (cwd , out )
203+ if verbose {
204+ fmt .Printf (" [+] %s (%d bytes)\n " , rel , content .Len ())
205+ } else {
206+ fmt .Printf (" [+] %s\n " , rel )
207+ }
208+ return 0
209+ }
0 commit comments