| 1 | namespace AgentForge.Plugin.Ci.Ir; |
| 2 | |
| 3 | public static class ForgeCiTemplates |
| 4 | { |
| 5 | public static ForgeCiDocument CreateDefault(string name) |
| 6 | { |
| 7 | var normalized = NormalizeDefinitionName(name); |
| 8 | return new ForgeCiDocument |
| 9 | { |
| 10 | Schema = ForgeCiDocument.SchemaVersion, |
| 11 | Name = normalized, |
| 12 | Triggers = |
| 13 | [ |
| 14 | new ForgeCiTrigger { Event = "git.push", Branches = ["main", "master", "feat/*"] }, |
| 15 | new ForgeCiTrigger { Event = "mr.opened" }, |
| 16 | ], |
| 17 | Variables = new Dictionary<string, string>(StringComparer.Ordinal) |
| 18 | { |
| 19 | ["Configuration"] = "Release", |
| 20 | }, |
| 21 | Nodes = |
| 22 | [ |
| 23 | new ForgeCiNode { Id = "phase-build", Kind = "phase", Name = "Build" }, |
| 24 | new ForgeCiNode { Id = "restore", Kind = "task", Task = "dotnet.restore", Inputs = new() }, |
| 25 | new ForgeCiNode |
| 26 | { |
| 27 | Id = "build", |
| 28 | Kind = "task", |
| 29 | Task = "dotnet.build", |
| 30 | Inputs = new Dictionary<string, string>(StringComparer.Ordinal) |
| 31 | { |
| 32 | ["configuration"] = "$(Configuration)", |
| 33 | }, |
| 34 | }, |
| 35 | new ForgeCiNode { Id = "test", Kind = "task", Task = "dotnet.test", Inputs = new() }, |
| 36 | ], |
| 37 | Edges = |
| 38 | [ |
| 39 | new ForgeCiEdge { Kind = "contains", From = "phase-build", To = "restore" }, |
| 40 | new ForgeCiEdge { Kind = "contains", From = "phase-build", To = "build" }, |
| 41 | new ForgeCiEdge { Kind = "contains", From = "phase-build", To = "test" }, |
| 42 | new ForgeCiEdge { Kind = "dependsOn", From = "restore", To = "build" }, |
| 43 | new ForgeCiEdge { Kind = "dependsOn", From = "build", To = "test" }, |
| 44 | ], |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | public static string NormalizeDefinitionName(string name) |
| 49 | { |
| 50 | var trimmed = name.Trim(); |
| 51 | if (trimmed.Length == 0) |
| 52 | throw new ForgeCiValidationException("definition name is required."); |
| 53 | |
| 54 | foreach (var ch in trimmed) |
| 55 | { |
| 56 | if (char.IsLetterOrDigit(ch) || ch is '-' or '_') |
| 57 | continue; |
| 58 | throw new ForgeCiValidationException( |
| 59 | "definition name may contain letters, digits, hyphen, underscore only."); |
| 60 | } |
| 61 | |
| 62 | return trimmed; |
| 63 | } |
| 64 | } |
| 65 | |