|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "text/template" |
| 7 | +) |
| 8 | + |
| 9 | +type TemplateData struct { |
| 10 | + SampleName string |
| 11 | + Workflows []string |
| 12 | + Activities []string |
| 13 | +} |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Define the data for HelloWorld |
| 17 | + data := TemplateData{ |
| 18 | + SampleName: "Hello World", |
| 19 | + Workflows: []string{"HelloWorldWorkflow"}, |
| 20 | + Activities: []string{"HelloWorldActivity"}, |
| 21 | + } |
| 22 | + |
| 23 | + // Generate worker.go |
| 24 | + generateFile("../../template/worker.tmpl", "../worker.go", data) |
| 25 | + println("Generated worker.go") |
| 26 | + |
| 27 | + // Generate main.go |
| 28 | + generateFile("../../template/main.tmpl", "../main.go", data) |
| 29 | + println("Generated main.go") |
| 30 | + |
| 31 | + // Generate README.md (combine template + specific + references) |
| 32 | + generateREADME("../../template/README.tmpl", "README_specific.md", "../../template/README_references.md", "../README.md", data) |
| 33 | + println("Generated README.md") |
| 34 | +} |
| 35 | + |
| 36 | +func generateFile(templatePath, outputPath string, data TemplateData) { |
| 37 | + tmpl, err := template.ParseFiles(templatePath) |
| 38 | + if err != nil { |
| 39 | + panic("Failed to parse template " + templatePath + ": " + err.Error()) |
| 40 | + } |
| 41 | + |
| 42 | + f, err := os.Create(outputPath) |
| 43 | + if err != nil { |
| 44 | + panic("Failed to create output file " + outputPath + ": " + err.Error()) |
| 45 | + } |
| 46 | + defer f.Close() |
| 47 | + |
| 48 | + err = tmpl.Execute(f, data) |
| 49 | + if err != nil { |
| 50 | + panic("Failed to execute template: " + err.Error()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func generateREADME(templatePath, specificPath, referencesPath, outputPath string, data TemplateData) { |
| 55 | + // Create output file |
| 56 | + f, err := os.Create(outputPath) |
| 57 | + if err != nil { |
| 58 | + panic("Failed to create README file: " + err.Error()) |
| 59 | + } |
| 60 | + defer f.Close() |
| 61 | + |
| 62 | + // First, write the generic template part |
| 63 | + tmpl, err := template.ParseFiles(templatePath) |
| 64 | + if err != nil { |
| 65 | + panic("Failed to parse README template: " + err.Error()) |
| 66 | + } |
| 67 | + |
| 68 | + err = tmpl.Execute(f, data) |
| 69 | + if err != nil { |
| 70 | + panic("Failed to execute README template: " + err.Error()) |
| 71 | + } |
| 72 | + |
| 73 | + // Then, append the specific content |
| 74 | + specific, err := os.Open(specificPath) |
| 75 | + if err != nil { |
| 76 | + panic("Failed to open specific README content: " + err.Error()) |
| 77 | + } |
| 78 | + defer specific.Close() |
| 79 | + |
| 80 | + _, err = io.Copy(f, specific) |
| 81 | + if err != nil { |
| 82 | + panic("Failed to append specific README content: " + err.Error()) |
| 83 | + } |
| 84 | + |
| 85 | + // Finally, append the references |
| 86 | + references, err := os.Open(referencesPath) |
| 87 | + if err != nil { |
| 88 | + panic("Failed to open references content: " + err.Error()) |
| 89 | + } |
| 90 | + defer references.Close() |
| 91 | + |
| 92 | + _, err = io.Copy(f, references) |
| 93 | + if err != nil { |
| 94 | + panic("Failed to append references content: " + err.Error()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
0 commit comments