This repository was archived by the owner on Apr 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
852 lines (742 loc) · 22.1 KB
/
main.go
File metadata and controls
852 lines (742 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
package main
import (
"archive/zip"
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/supermodeltools/arch-docs/internal/graph2md"
"github.com/supermodeltools/arch-docs/internal/pssg/build"
"github.com/supermodeltools/arch-docs/internal/pssg/config"
)
const apiBaseURL = "https://api.supermodeltools.com/v1/graphs/supermodel"
const pollTimeout = 45 * time.Minute
const defaultPollInterval = 10 * time.Second
const maxFileSize = 10 * 1024 * 1024 // 10MB
// APIResponse matches the Supermodel API response structure.
type APIResponse struct {
Status string `json:"status"`
JobID string `json:"jobId"`
Error json.RawMessage `json:"error"`
Result json.RawMessage `json:"result"`
}
// pssgConfigTemplate is the pssg.yaml configuration template.
const pssgConfigTemplate = `site:
name: "%s"
base_url: "%s"
repo_url: "%s"
description: "Architecture documentation for the %s codebase. Explore files, functions, classes, domains, and dependencies."
author: "Supermodel"
language: "en"
paths:
data: "%s"
templates: "%s"
output: "%s"
source_dir: "%s"
data:
format: "markdown"
entity_type: "entity"
entity_slug:
source: "filename"
body_sections:
- name: "Functions"
header: "Functions"
type: "unordered_list"
- name: "Classes"
header: "Classes"
type: "unordered_list"
- name: "Types"
header: "Types"
type: "unordered_list"
- name: "Dependencies"
header: "Dependencies"
type: "unordered_list"
- name: "Imported By"
header: "Imported By"
type: "unordered_list"
- name: "Calls"
header: "Calls"
type: "unordered_list"
- name: "Called By"
header: "Called By"
type: "unordered_list"
- name: "Source Files"
header: "Source Files"
type: "unordered_list"
- name: "Subdirectories"
header: "Subdirectories"
type: "unordered_list"
- name: "Files"
header: "Files"
type: "unordered_list"
- name: "Source"
header: "Source"
type: "unordered_list"
- name: "Extends"
header: "Extends"
type: "unordered_list"
- name: "Defined In"
header: "Defined In"
type: "unordered_list"
- name: "Subdomains"
header: "Subdomains"
type: "unordered_list"
- name: "Domain"
header: "Domain"
type: "unordered_list"
- name: "faqs"
header: "FAQs"
type: "faq"
taxonomies:
- name: "node_type"
label: "Node Types"
label_singular: "Node Type"
field: "node_type"
multi_value: false
min_entities: 1
index_description: "Browse by entity type"
- name: "language"
label: "Languages"
label_singular: "Language"
field: "language"
multi_value: false
min_entities: 1
index_description: "Browse by programming language"
- name: "domain"
label: "Domains"
label_singular: "Domain"
field: "domain"
multi_value: false
min_entities: 1
index_description: "Browse by architectural domain"
- name: "subdomain"
label: "Subdomains"
label_singular: "Subdomain"
field: "subdomain"
multi_value: false
min_entities: 1
index_description: "Browse by architectural subdomain"
- name: "top_directory"
label: "Top Directories"
label_singular: "Directory"
field: "top_directory"
multi_value: false
min_entities: 1
index_description: "Browse by top-level directory"
- name: "extension"
label: "File Extensions"
label_singular: "Extension"
field: "extension"
multi_value: false
min_entities: 1
index_description: "Browse by file extension"
- name: "tags"
label: "Tags"
label_singular: "Tag"
field: "tags"
multi_value: true
min_entities: 1
index_description: "Browse by tag"
pagination:
per_page: 48
url_pattern: "/{taxonomy}/{entry}/{page}"
structured_data:
entity_type: "SoftwareSourceCode"
field_mappings:
name: "title"
description: "description"
programmingLanguage: "language"
codeRepository: "repo_url"
sitemap:
enabled: true
max_urls_per_file: 50000
priorities:
homepage: 1.0
entity: 0.8
taxonomy_index: 0.7
hub_page_1: 0.6
hub_page_n: 0.4
rss:
enabled: true
title: "%s"
description: "Architecture documentation for the %s codebase"
robots:
enabled: true
llms_txt:
enabled: true
title: "%s"
description: "Architecture documentation for the %s codebase"
search:
enabled: true
templates:
entity: "entity.html"
homepage: "index.html"
hub: "hub.html"
taxonomy_index: "taxonomy_index.html"
all_entities: "all_entities.html"
output:
clean_before_build: true
extract_css: "styles.css"
extract_js: "main.js"
share_images: false
extra:
cta:
enabled: true
heading: "Analyze Your Own Codebase"
description: "Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes."
button_text: "Try Supermodel Free"
button_url: "https://dashboard.supermodeltools.com/billing/"
`
func main() {
// Step 1: Read inputs
apiKey := getInput("supermodel-api-key")
if apiKey == "" {
fatal("supermodel-api-key input is required")
}
siteName := getInput("site-name")
baseURL := getInput("base-url")
outputDir := getInput("output-dir")
templatesDir := getInput("templates-dir")
maxSourceFiles := 3000
if v := getInput("max-source-files"); v != "" {
if n, err := strconv.Atoi(v); err == nil {
maxSourceFiles = n
}
}
maxEntities := 12000
if v := getInput("max-entities"); v != "" {
if n, err := strconv.Atoi(v); err == nil {
maxEntities = n
}
}
if outputDir == "" {
outputDir = "./arch-docs-output"
}
// Step 2: Derive repo info
// Allow the workflow to pass the customer repo explicitly via the "repo" input,
// since GITHUB_REPOSITORY cannot be overridden in Docker container actions.
ghRepo := getInput("repo")
if ghRepo == "" {
ghRepo = os.Getenv("GITHUB_REPOSITORY") // e.g. "owner/repo"
}
repoName := ""
repoURL := ""
if ghRepo != "" {
parts := strings.SplitN(ghRepo, "/", 2)
if len(parts) == 2 {
repoName = parts[1]
} else {
repoName = ghRepo
}
repoURL = "https://github.com/" + ghRepo
}
if siteName == "" {
if repoName != "" {
siteName = repoName + " Architecture Docs"
} else {
siteName = "Architecture Docs"
}
}
if baseURL == "" {
// Default to GitHub Pages URL for the repo
if ghRepo != "" {
parts := strings.SplitN(ghRepo, "/", 2)
if len(parts) == 2 {
// Check for custom domain via raw CNAME file in the org's .github.io repo
orgPagesURL := "https://" + parts[0] + ".github.io"
customDomain := fetchOrgCNAME(parts[0])
if customDomain != "" {
orgPagesURL = "https://" + customDomain
}
baseURL = orgPagesURL + "/" + parts[1]
} else {
baseURL = repoURL
}
} else {
baseURL = "https://example.com"
}
}
workspaceDir := os.Getenv("GITHUB_WORKSPACE")
if workspaceDir == "" {
workspaceDir = "."
}
// Resolve output dir
if !filepath.IsAbs(outputDir) {
outputDir = filepath.Join(workspaceDir, outputDir)
}
fmt.Printf("::group::Configuration\n")
fmt.Printf("Site name: %s\n", siteName)
fmt.Printf("Base URL: %s\n", baseURL)
fmt.Printf("Output dir: %s\n", outputDir)
fmt.Printf("Repo: %s\n", ghRepo)
fmt.Printf("Workspace: %s\n", workspaceDir)
logGroupEnd()
// Step 3: Zip the repo
logGroup("Creating repository archive")
if maxSourceFiles > 0 {
fmt.Printf("Source file cap: %d\n", maxSourceFiles)
}
zipPath, err := createRepoZip(workspaceDir, maxSourceFiles)
if err != nil {
fatal("Failed to create repo zip: %v", err)
}
defer os.Remove(zipPath)
info, _ := os.Stat(zipPath)
fmt.Printf("Archive created: %s (%.2f MB)\n", zipPath, float64(info.Size())/(1024*1024))
logGroupEnd()
// Step 4 & 5: Call Supermodel API and poll
logGroup("Calling Supermodel API")
graphJSON, err := callSupermodelAPI(apiKey, zipPath)
if err != nil {
fatal("API call failed: %v", err)
}
fmt.Printf("Graph data received (%d bytes)\n", len(graphJSON))
logGroupEnd()
// Step 6: Save graph JSON
logGroup("Saving graph data")
tmpDir, err := os.MkdirTemp("", "arch-docs-*")
if err != nil {
fatal("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
graphPath := filepath.Join(tmpDir, "graph.json")
if err := os.WriteFile(graphPath, graphJSON, 0644); err != nil {
fatal("Failed to write graph JSON: %v", err)
}
fmt.Printf("Graph saved to %s\n", graphPath)
logGroupEnd()
// Step 7: Run graph2md
logGroup("Generating markdown from graph")
contentDir := filepath.Join(tmpDir, "content")
if err := os.MkdirAll(contentDir, 0755); err != nil {
fatal("Failed to create content dir: %v", err)
}
if err := graph2md.Run(graphPath, contentDir, repoName, repoURL, maxEntities); err != nil {
fatal("graph2md failed: %v", err)
}
entityCount := countFiles(contentDir, ".md")
fmt.Printf("Generated %d markdown files\n", entityCount)
logGroupEnd()
// Step 8: Generate pssg.yaml and run pssg build
logGroup("Building static site")
// Determine templates path
tplDir := templatesDir
if tplDir == "" {
// Use bundled templates
tplDir = "/app/templates"
// If running outside Docker (e.g., local testing), fall back
if _, err := os.Stat(tplDir); os.IsNotExist(err) {
// Try relative to binary
execPath, _ := os.Executable()
tplDir = filepath.Join(filepath.Dir(execPath), "templates")
if _, err := os.Stat(tplDir); os.IsNotExist(err) {
tplDir = "templates"
}
}
} else if !filepath.IsAbs(tplDir) {
tplDir = filepath.Join(workspaceDir, tplDir)
}
configPath := filepath.Join(tmpDir, "pssg.yaml")
if err := generateConfig(configPath, siteName, baseURL, repoURL, repoName, contentDir, tplDir, outputDir, workspaceDir); err != nil {
fatal("Failed to generate pssg config: %v", err)
}
cfg, err := config.Load(configPath)
if err != nil {
fatal("Failed to load pssg config: %v", err)
}
builder := build.NewBuilder(cfg, false)
if err := builder.Build(); err != nil {
fatal("pssg build failed: %v", err)
}
pageCount := countFiles(outputDir, ".html")
fmt.Printf("Built %d HTML pages\n", pageCount)
logGroupEnd()
// Step 8b: Rewrite paths if base URL has a path prefix (e.g. GitHub Pages subdirectory)
pathPrefix := extractPathPrefix(baseURL)
if pathPrefix != "" {
logGroup("Rewriting paths for subdirectory deployment")
fmt.Printf("Path prefix: %s\n", pathPrefix)
if err := rewritePathPrefix(outputDir, pathPrefix); err != nil {
fatal("Failed to rewrite paths: %v", err)
}
logGroupEnd()
}
// Step 9: Set outputs
logGroup("Setting outputs")
absOutput, _ := filepath.Abs(outputDir)
setOutput("site-path", absOutput)
setOutput("entity-count", strconv.Itoa(entityCount))
setOutput("page-count", strconv.Itoa(pageCount))
fmt.Printf("site-path=%s\n", absOutput)
fmt.Printf("entity-count=%d\n", entityCount)
fmt.Printf("page-count=%d\n", pageCount)
logGroupEnd()
fmt.Println("Architecture docs generated successfully!")
}
// getInput reads a GitHub Actions input from the environment.
func getInput(name string) string {
// Docker actions receive env vars with hyphens preserved: INPUT_SUPERMODEL-API-KEY
envKey := "INPUT_" + strings.ToUpper(name)
val := os.Getenv(envKey)
if val == "" {
// Also check with hyphens replaced by underscores (for local testing)
envKey = "INPUT_" + strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
val = os.Getenv(envKey)
}
return strings.TrimSpace(val)
}
// setOutput writes a GitHub Actions output value.
func setOutput(name, value string) {
outputFile := os.Getenv("GITHUB_OUTPUT")
if outputFile == "" {
fmt.Printf("::set-output name=%s::%s\n", name, value)
return
}
f, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("::warning::Failed to write output %s: %v\n", name, err)
return
}
defer f.Close()
fmt.Fprintf(f, "%s=%s\n", name, value)
}
// logGroup starts a GitHub Actions log group.
func logGroup(name string) {
fmt.Printf("::group::%s\n", name)
}
// logGroupEnd ends a GitHub Actions log group.
func logGroupEnd() {
fmt.Println("::endgroup::")
}
// fatal prints an error and exits.
func fatal(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
fmt.Printf("::error::%s\n", msg)
os.Exit(1)
}
// createRepoZip walks the workspace directory and creates a zip archive.
// It skips .git/, node_modules/, binary files, and files > 10MB.
// If maxFiles > 0, stops after that many files are archived.
func createRepoZip(workspaceDir string, maxFiles int) (string, error) {
tmpFile, err := os.CreateTemp("", "repo-*.zip")
if err != nil {
return "", fmt.Errorf("creating temp file: %w", err)
}
defer tmpFile.Close()
zw := zip.NewWriter(tmpFile)
defer zw.Close()
skipDirs := map[string]bool{
".git": true,
"node_modules": true,
".next": true,
"dist": true,
"build": true,
"vendor": true,
"__pycache__": true,
".venv": true,
}
binaryExts := map[string]bool{
".exe": true, ".dll": true, ".so": true, ".dylib": true,
".bin": true, ".obj": true, ".o": true, ".a": true,
".png": true, ".jpg": true, ".jpeg": true, ".gif": true,
".ico": true, ".svg": true, ".webp": true,
".mp3": true, ".mp4": true, ".avi": true, ".mov": true,
".zip": true, ".tar": true, ".gz": true, ".bz2": true,
".rar": true, ".7z": true,
".woff": true, ".woff2": true, ".ttf": true, ".eot": true,
".pdf": true, ".doc": true, ".docx": true,
}
fileCount := 0
err = filepath.Walk(workspaceDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // skip errors
}
relPath, err := filepath.Rel(workspaceDir, path)
if err != nil {
return nil
}
// Skip root
if relPath == "." {
return nil
}
// Skip hidden dirs and known large dirs
baseName := filepath.Base(path)
if info.IsDir() {
if skipDirs[baseName] || strings.HasPrefix(baseName, ".") {
return filepath.SkipDir
}
return nil
}
// Skip binary files
ext := strings.ToLower(filepath.Ext(path))
if binaryExts[ext] {
return nil
}
// Skip files > 10MB
if info.Size() > maxFileSize {
return nil
}
// Skip hidden files
if strings.HasPrefix(baseName, ".") {
return nil
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return nil
}
header.Name = relPath
header.Method = zip.Deflate
writer, err := zw.CreateHeader(header)
if err != nil {
return nil
}
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
return nil
}
fileCount++
if maxFiles > 0 && fileCount >= maxFiles {
fmt.Printf("Source file cap reached (%d), stopping archive\n", maxFiles)
return filepath.SkipAll
}
return nil
})
if err != nil {
return "", fmt.Errorf("walking workspace: %w", err)
}
fmt.Printf("Archived %d files\n", fileCount)
return tmpFile.Name(), nil
}
// callSupermodelAPI sends the zip to the Supermodel API and polls for completion.
func callSupermodelAPI(apiKey, zipPath string) ([]byte, error) {
idempotencyKey := generateUUID()
// Initial POST
respBody, resp, err := postWithZip(apiKey, zipPath, idempotencyKey)
if err != nil {
return nil, fmt.Errorf("initial request: %w", err)
}
var apiResp APIResponse
if err := json.Unmarshal(respBody, &apiResp); err != nil {
return nil, fmt.Errorf("parsing response: %w (body: %s)", err, string(respBody))
}
if apiResp.Status == "completed" {
return apiResp.Result, nil
}
if apiResp.Status == "failed" {
return nil, fmt.Errorf("API returned failure: %s", string(apiResp.Error))
}
// Poll loop
deadline := time.Now().Add(pollTimeout)
for time.Now().Before(deadline) {
interval := getPollInterval(resp, defaultPollInterval)
fmt.Printf("Status: %s (job: %s), polling in %s...\n", apiResp.Status, apiResp.JobID, interval)
time.Sleep(interval)
respBody, resp, err = postWithZip(apiKey, zipPath, idempotencyKey)
if err != nil {
fmt.Printf("::warning::Poll request failed: %v, retrying...\n", err)
continue
}
if err := json.Unmarshal(respBody, &apiResp); err != nil {
return nil, fmt.Errorf("parsing poll response: %w", err)
}
if apiResp.Status == "completed" {
return apiResp.Result, nil
}
if apiResp.Status == "failed" {
return nil, fmt.Errorf("API returned failure: %s", string(apiResp.Error))
}
}
return nil, fmt.Errorf("timeout waiting for API response after %s", pollTimeout)
}
// postWithZip sends a multipart POST request with the zip file.
func postWithZip(apiKey, zipPath, idempotencyKey string) ([]byte, *http.Response, error) {
body, contentType, err := createMultipartBody(zipPath)
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("POST", apiBaseURL, body)
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("X-Api-Key", apiKey)
req.Header.Set("Idempotency-Key", idempotencyKey)
client := &http.Client{Timeout: 5 * time.Minute}
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, fmt.Errorf("reading response: %w", err)
}
if resp.StatusCode >= 400 {
return nil, nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody))
}
return respBody, resp, nil
}
// createMultipartBody builds the multipart form body with the zip file.
func createMultipartBody(zipPath string) (*bytes.Buffer, string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
file, err := os.Open(zipPath)
if err != nil {
return nil, "", fmt.Errorf("opening zip: %w", err)
}
defer file.Close()
part, err := writer.CreateFormFile("file", filepath.Base(zipPath))
if err != nil {
return nil, "", fmt.Errorf("creating form file: %w", err)
}
if _, err := io.Copy(part, file); err != nil {
return nil, "", fmt.Errorf("copying zip data: %w", err)
}
if err := writer.Close(); err != nil {
return nil, "", fmt.Errorf("closing multipart writer: %w", err)
}
return body, writer.FormDataContentType(), nil
}
// generateUUID generates a UUID v4.
func generateUUID() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
b[6] = (b[6] & 0x0f) | 0x40 // version 4
b[8] = (b[8] & 0x3f) | 0x80 // variant 10
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
}
// getPollInterval reads the Retry-After header or returns the default.
func getPollInterval(resp *http.Response, defaultInterval time.Duration) time.Duration {
if resp == nil {
return defaultInterval
}
retryAfter := resp.Header.Get("Retry-After")
if retryAfter == "" {
return defaultInterval
}
seconds, err := strconv.Atoi(retryAfter)
if err != nil {
return defaultInterval
}
if seconds < 1 {
return defaultInterval
}
if seconds > 120 {
seconds = 120
}
return time.Duration(seconds) * time.Second
}
// generateConfig writes a pssg.yaml config file.
func generateConfig(configPath, siteName, baseURL, repoURL, repoName, contentDir, tplDir, outputDir, sourceDir string) error {
config := fmt.Sprintf(pssgConfigTemplate,
siteName, // site.name
baseURL, // site.base_url
repoURL, // site.repo_url
repoName, // site.description
contentDir, // paths.data
tplDir, // paths.templates
outputDir, // paths.output
sourceDir, // paths.source_dir
siteName, // rss.title
repoName, // rss.description
siteName, // llms_txt.title
repoName, // llms_txt.description
)
return os.WriteFile(configPath, []byte(config), 0644)
}
// extractPathPrefix returns the path component of a URL (e.g. "/graph2md" from
// "https://supermodeltools.github.io/graph2md"). Returns "" if no prefix.
func extractPathPrefix(baseURL string) string {
u, err := url.Parse(baseURL)
if err != nil {
return ""
}
p := strings.TrimRight(u.Path, "/")
if p == "" || p == "/" {
return ""
}
return p
}
// rewritePathPrefix rewrites all root-relative paths in HTML and JS files to
// include the given prefix. This is needed when deploying to a subdirectory
// (e.g. GitHub Pages project sites at username.github.io/repo/).
func rewritePathPrefix(dir, prefix string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext != ".html" && ext != ".js" {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
content := string(data)
original := content
// Rewrite href="/..." to href="/prefix/..."
content = strings.ReplaceAll(content, `href="/`, `href="`+prefix+`/`)
// Rewrite src="/..." to src="/prefix/..."
content = strings.ReplaceAll(content, `src="/`, `src="`+prefix+`/`)
// Rewrite fetch("/..." to fetch("/prefix/..."
content = strings.ReplaceAll(content, `fetch("/`, `fetch("`+prefix+`/`)
// Rewrite JS navigation: window.location.href = "/" + ...
content = strings.ReplaceAll(content, `window.location.href = "/"`, `window.location.href = "`+prefix+`/"`)
content = strings.ReplaceAll(content, `window.location.href = "/" + `, `window.location.href = "`+prefix+`/" + `)
if content != original {
if err := os.WriteFile(path, []byte(content), info.Mode()); err != nil {
return fmt.Errorf("writing %s: %w", path, err)
}
}
return nil
})
}
// fetchOrgCNAME fetches the raw CNAME file from the org's .github.io repo.
// Returns the custom domain string or "" if not found.
func fetchOrgCNAME(org string) string {
rawURL := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s.github.io/main/CNAME", org, org)
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(rawURL)
if err != nil || resp.StatusCode != 200 {
return ""
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
return strings.TrimSpace(string(body))
}
// countFiles counts files with the given extension in a directory tree.
func countFiles(dir, ext string) int {
count := 0
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() && strings.HasSuffix(path, ext) {
count++
}
return nil
})
return count
}