diff --git a/ci/main.go b/ci/main.go index 4c4b66544..c964521d5 100644 --- a/ci/main.go +++ b/ci/main.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "fmt" "log" "os" @@ -9,182 +8,148 @@ import ( "regexp" "strconv" "strings" + "sync" "time" -) - -func main() { - log.Output(1, "## Generating PUtils docs") - - goDocOutputBytes, err := exec.Command("go", "doc", "-all", "./putils").Output() - if err != nil { - log.Panic(err) - } - goDocOutput := string(goDocOutputBytes) - goDocOutput = strings.Join(strings.Split(goDocOutput, "FUNCTIONS")[1:], "") - goDocOutputLines := strings.Split(goDocOutput, "\n") - var goDocOutputFiltered []string - for _, line := range goDocOutputLines { - if strings.HasPrefix(line, "func") { - goDocOutputFiltered = append(goDocOutputFiltered, line) - } - } - putilsTemplateBytes, err := os.ReadFile("./putils/README.template.md") - if err != nil { - log.Panic(err) - } + "github.com/pterm/pterm" +) - putilsReadme := string(putilsTemplateBytes) - putilsReadme += "\n## Util Functions\n\n" +type Examples struct { + sync.Mutex + m map[string]string +} - putilsReadme += fmt.Sprintf("```go\n%s\n```\n", strings.Join(goDocOutputFiltered, "\n")) +func (e *Examples) Add(name, content string) { + e.Lock() + defer e.Unlock() + e.m[name] = content +} - os.WriteFile("./putils/README.md", []byte(putilsReadme), 0600) - os.WriteFile("./docs/docs/putils.md", []byte(putilsReadme), 0600) +func main() { + var wg sync.WaitGroup - log.Output(1, "## Generating Examples") - files, _ := os.ReadDir("./_examples/") - var readmeExamples string - for _, section := range files { - var sectionExamples string - log.Output(2, "Section: "+section.Name()) - examples, _ := os.ReadDir("./_examples/" + section.Name()) + pterm.Info.Prefix = pterm.Prefix{ + Text: "LOG", + Style: pterm.NewStyle(pterm.FgGray), + } + pterm.Info.MessageStyle = pterm.NewStyle(pterm.FgDefault) - for _, example := range examples { - if example.Name() == "README.md" { - continue - } - processFile(section.Name() + "/" + example.Name()) - log.Output(2, "## Generating readme for example: "+example.Name()) - exampleCode, err := os.ReadFile("./_examples/" + section.Name() + "/" + example.Name() + "/main.go") + do("Running PTerm CI System", 1, func(currentLevel int) { + do("Generating PUtils Docs", currentLevel, func(currentLevel int) { + pterm.Info.Println("Getting docs from 'go doc'...") + goDocOutputBytes, err := exec.Command("go", "doc", "-all", "./putils").Output() if err != nil { log.Panic(err) } + goDocOutput := string(goDocOutputBytes) + goDocOutput = strings.Join(strings.Split(goDocOutput, "FUNCTIONS")[1:], "") + goDocOutputLines := strings.Split(goDocOutput, "\n") + + pterm.Info.Println("Parsing docs...") + var goDocOutputFiltered []string + for _, line := range goDocOutputLines { + if strings.HasPrefix(line, "func") { + goDocOutputFiltered = append(goDocOutputFiltered, line) + } + } - sectionExamples += "### " + section.Name() + "/" + example.Name() + "\n\n" - sectionExamples += "![Animation](https://raw.githubusercontent.com/pterm/pterm/master/_examples/" + section.Name() + "/" + example.Name() + "/animation.svg)\n\n" - sectionExamples += "
\n\nSHOW SOURCE\n\n" - sectionExamples += "```go\n" - sectionExamples += string(exampleCode) + "\n" - sectionExamples += "```\n\n" - sectionExamples += "
\n\n" - - readmeExamples += sectionExamples - } - os.WriteFile("./_examples/"+section.Name()+"/README.md", []byte(sectionExamples), 0600) - } - - log.Output(3, "### Generating examples README") - examplesReadme, _ := os.ReadFile("./_examples/README.md") - examplesReadmeContent := string(examplesReadme) - examplesReadmeContent = writeBetween("examples", examplesReadmeContent, "\n"+readmeExamples+"\n") - os.WriteFile("./_examples/README.md", []byte(examplesReadmeContent), 0600) - - log.Output(3, "### Appending examples to root README.md") - - readmeContent, err := os.ReadFile("./README.md") - if err != nil { - log.Panic(err) - } - - var newReadmeContent string - - log.Output(3, "### Counting unit tests...") - - unittestTimeout := make(chan string, 1) - - go func() { - cmd := exec.Command("bash", "-c", "go test -v -p 1 ./...") - json, _ := cmd.CombinedOutput() - unitTestCount := fmt.Sprint(strings.Count(string(json), "RUN")) - log.Output(4, "### Unit test count: "+unitTestCount) - unittestTimeout <- unitTestCount - }() - - log.Output(4, "#### Replacing strings in readme") - - newReadmeContent = string(readmeContent) - - select { - case res := <-unittestTimeout: - newReadmeContent = writeBetween("unittestcount", newReadmeContent, `Forks`) - newReadmeContent = writeBetween("unittestcount2", newReadmeContent, "**`"+res+"`**") - case <-time.After(time.Second * 10): - log.Output(4, "Timeout in counting unit tests!") - } - - newReadmeContent = writeBetween("examples", newReadmeContent, "\n"+readmeExamples+"\n") - - log.Output(4, "### Writing readme") - err = os.WriteFile("./README.md", []byte(newReadmeContent), 0600) - if err != nil { - log.Panic(err) - } - - log.Output(4, "### Writing readme to pterm.sh") - err = os.WriteFile("./docs/README.md", []byte(newReadmeContent), 0600) - if err != nil { - log.Panic(err) - } -} - -func processFile(dir string) { - log.Output(3, "### ['"+dir+"'] Generating animations for example") - animationDataPath := "./_examples/" + dir + "/animation_data.json" - animationSvgPath := "./_examples/" + dir + "/animation.svg" - exampleCode, err := os.ReadFile("./_examples/" + dir + "/main.go") - if err != nil { - log.Panic(err) - } - - if fileExists(animationDataPath) { - log.Output(4, "#### ['"+dir+"'] animation_data.json already exists. Removing it.") - err = os.Remove(animationDataPath) - if err != nil { - log.Panic(err) - } - } - if fileExists(animationSvgPath) { - log.Output(4, "#### ['"+dir+"'] animation.svg already exists. Removing it.") - err := os.Remove(animationSvgPath) - if err != nil { - log.Panic(err) - } - } - - log.Output(4, "#### ['"+dir+"'] Running asciinema") - execute(`asciinema rec ` + animationDataPath + ` -c "go run ./_examples/` + dir + `"`) - - log.Output(4, "#### ['"+dir+"'] Adding sleep to end of animation_data.json") - animationDataLines := getLinesFromFile(animationDataPath) - animationDataLastLine := animationDataLines[len(animationDataLines)-1] - re := regexp.MustCompile(`\[\d[^,]*`).FindAllString(animationDataLastLine, 1)[0] - lastTime, _ := strconv.ParseFloat(strings.ReplaceAll(re, "[", ""), 64) - sleepString := `[` + strconv.FormatFloat(lastTime+5, 'f', 6, 64) + `, "o", "\nRestarting animation...\n"]` - animationDataFile, err := os.OpenFile(animationDataPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - log.Panicf("[%s] %s", dir, err.Error()) - } - defer animationDataFile.Close() - _, err = animationDataFile.WriteString(sleepString) - if err != nil { - log.Panicf("[%s] %s", dir, err.Error()) - } - - log.Output(4, "#### ['"+dir+"'] Generating SVG") - execute(`svg-term --in ` + animationDataPath + ` --out ` + animationSvgPath + ` --no-cursor --window true --no-optimize --profile "./ci/terminal-theme.txt" --term "iterm2"`) - - log.Output(4, "#### ['"+dir+"'] Overwriting SVG font") - - svgContent, err := os.ReadFile(animationSvgPath) - if err != nil { - log.Panicf("[%s] %s", dir, err.Error()) - } - - svgContent = []byte(strings.ReplaceAll(string(svgContent), `font-family:`, `font-family:'Courier New',`)) - svgContent = []byte(strings.ReplaceAll(string(svgContent), `font-family="`, `font-family="'Courier New',`)) + pterm.Info.Println("Reading README Template") + putilsTemplateBytes, err := os.ReadFile("./putils/README.template.md") + if err != nil { + log.Panic(err) + } - svgContent = []byte(strings.Replace(string(svgContent), "