|
1 | 1 | using System; |
2 | | -using System.Diagnostics; |
3 | | -using System.IO; |
4 | | -using System.Linq; |
5 | | -using System.Text.RegularExpressions; |
6 | 2 | using Nuke.Common; |
7 | | -using Nuke.Common.IO; |
8 | | -using static Nuke.Common.Tools.DotNet.DotNetTasks; |
9 | 3 |
|
10 | 4 | partial class BuildTask |
11 | 5 | { |
12 | | - const string OldSampleName = "Dotnet.CI.Template.Sample"; |
13 | | - const string OldSlnName = "Dotnet.CI.Template"; |
14 | | - |
15 | | - static readonly string[] ExcludedDirectories = |
16 | | - [".git", "node_modules", "artifacts", "dist", "bin", "obj", ".vitepress"]; |
17 | | - |
18 | 6 | Target Init => _ => _ |
19 | | - .Requires(() => !string.IsNullOrWhiteSpace(ProjectName)) |
20 | | - .Requires(() => Regex.IsMatch(ProjectName, @"^[A-Za-z][A-Za-z0-9.]*$")) |
21 | 7 | .Executes(() => |
22 | 8 | { |
23 | | - Console.WriteLine($"[1/7] Replacing file contents..."); |
24 | | - ReplaceFileContents(); |
25 | | - |
26 | | - Console.WriteLine($"[2/7] Updating author..."); |
27 | | - UpdateAuthor(); |
28 | | - |
29 | | - Console.WriteLine($"[3/7] Renaming directories..."); |
30 | | - RenameDirectories(); |
31 | | - |
32 | | - Console.WriteLine($"[4/7] Renaming files..."); |
33 | | - RenameFiles(); |
34 | | - |
35 | | - Console.WriteLine($"[5/7] Resetting version to 0.1.0..."); |
36 | | - ResetVersion(); |
37 | | - |
38 | | - Console.WriteLine($"[6/7] Refreshing lock files..."); |
39 | | - DotNet($"restore {ProjectName}.slnx --force-evaluate", workingDirectory: RootDirectory); |
40 | | - |
41 | | - if (ResetGit) |
42 | | - { |
43 | | - Console.WriteLine("[7/7] Resetting git history..."); |
44 | | - ResetGitHistory(); |
45 | | - } |
46 | | - else |
47 | | - { |
48 | | - Console.WriteLine("[7/7] Git history preserved."); |
49 | | - } |
50 | | - |
51 | | - DeleteInitScripts(); |
52 | | - |
53 | | - Console.WriteLine(); |
54 | | - Console.WriteLine($"Done! Your project '{ProjectName}' is ready."); |
55 | | - Console.WriteLine(); |
56 | | - Console.WriteLine("Next steps:"); |
57 | | - Console.WriteLine(" 1. Update GitHub URL in docs/.vitepress/config.ts"); |
58 | | - Console.WriteLine(" 2. Run: dotnet build"); |
| 9 | + throw new InvalidOperationException( |
| 10 | + "The legacy initialization flow is disabled while the ChengYuan template family implementation is in progress. " + |
| 11 | + "Rename the repository explicitly and extend the framework and application modules directly."); |
59 | 12 | }); |
60 | | - |
61 | | - void ReplaceFileContents() |
62 | | - { |
63 | | - var files = RootDirectory |
64 | | - .GlobFiles("**/*") |
65 | | - .Where(f => !IsExcludedPath(f) && !IsInitScript(f) && IsTextFile(f)); |
66 | | - |
67 | | - foreach (var file in files) |
68 | | - { |
69 | | - var content = File.ReadAllText(file); |
70 | | - if (!content.Contains(OldSampleName) && !content.Contains(OldSlnName)) |
71 | | - continue; |
72 | | - |
73 | | - content = content.Replace(OldSampleName, ProjectName); |
74 | | - content = content.Replace(OldSlnName, ProjectName); |
75 | | - File.WriteAllText(file, content); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - void UpdateAuthor() |
80 | | - { |
81 | | - if (string.IsNullOrWhiteSpace(Author)) |
82 | | - return; |
83 | | - |
84 | | - var csprojFiles = RootDirectory |
85 | | - .GlobFiles("src/**/*.csproj") |
86 | | - .Where(f => !IsExcludedPath(f)); |
87 | | - |
88 | | - foreach (var csproj in csprojFiles) |
89 | | - { |
90 | | - var content = File.ReadAllText(csproj); |
91 | | - if (!content.Contains("<Authors>")) |
92 | | - continue; |
93 | | - |
94 | | - content = Regex.Replace(content, @"<Authors>[^<]*</Authors>", $"<Authors>{Author}</Authors>"); |
95 | | - File.WriteAllText(csproj, content); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - void RenameDirectories() |
100 | | - { |
101 | | - var dirs = Directory.GetDirectories(RootDirectory, $"*{OldSampleName}*", SearchOption.AllDirectories) |
102 | | - .Where(d => !IsExcludedPath((AbsolutePath)d)) |
103 | | - .OrderByDescending(d => d.Length); |
104 | | - |
105 | | - foreach (var dir in dirs) |
106 | | - { |
107 | | - var newName = Path.GetFileName(dir).Replace(OldSampleName, ProjectName); |
108 | | - var newPath = Path.Combine(Path.GetDirectoryName(dir)!, newName); |
109 | | - Console.WriteLine($" {dir} -> {newPath}"); |
110 | | - Directory.Move(dir, newPath); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - void RenameFiles() |
115 | | - { |
116 | | - var files = Directory.GetFiles(RootDirectory, $"*{OldSampleName}*", SearchOption.AllDirectories) |
117 | | - .Where(f => !IsExcludedPath((AbsolutePath)f)); |
118 | | - |
119 | | - foreach (var file in files) |
120 | | - { |
121 | | - var newName = Path.GetFileName(file).Replace(OldSampleName, ProjectName); |
122 | | - var newPath = Path.Combine(Path.GetDirectoryName(file)!, newName); |
123 | | - Console.WriteLine($" {file} -> {newPath}"); |
124 | | - File.Move(file, newPath); |
125 | | - } |
126 | | - |
127 | | - var slnFile = RootDirectory / $"{OldSlnName}.slnx"; |
128 | | - if (File.Exists(slnFile)) |
129 | | - { |
130 | | - var newSlnFile = RootDirectory / $"{ProjectName}.slnx"; |
131 | | - File.Move(slnFile, newSlnFile); |
132 | | - Console.WriteLine($" {slnFile} -> {newSlnFile}"); |
133 | | - } |
134 | | - } |
135 | | - |
136 | | - void ResetVersion() |
137 | | - { |
138 | | - var propsFile = DirectoryBuildPropsFile; |
139 | | - if (!File.Exists(propsFile)) |
140 | | - return; |
141 | | - |
142 | | - var content = File.ReadAllText(propsFile); |
143 | | - content = Regex.Replace(content, @"<VersionPrefix>[^<]*</VersionPrefix>", "<VersionPrefix>0.1.0</VersionPrefix>"); |
144 | | - File.WriteAllText(propsFile, content); |
145 | | - } |
146 | | - |
147 | | - void ResetGitHistory() |
148 | | - { |
149 | | - var gitDir = RootDirectory / ".git"; |
150 | | - if (Directory.Exists(gitDir)) |
151 | | - Directory.Delete(gitDir, recursive: true); |
152 | | - |
153 | | - RunGit("init"); |
154 | | - RunGit("add ."); |
155 | | - RunGit("commit -m \"Initial commit from dotnet.CI.template\""); |
156 | | - } |
157 | | - |
158 | | - void DeleteInitScripts() |
159 | | - { |
160 | | - File.Delete(RootDirectory / "init.sh"); |
161 | | - File.Delete(RootDirectory / "init.ps1"); |
162 | | - } |
163 | | - |
164 | | - void RunGit(string arguments) |
165 | | - { |
166 | | - var psi = new ProcessStartInfo("git", arguments) |
167 | | - { |
168 | | - WorkingDirectory = RootDirectory, |
169 | | - UseShellExecute = false |
170 | | - }; |
171 | | - using var process = Process.Start(psi) |
172 | | - ?? throw new InvalidOperationException($"Failed to start: git {arguments}"); |
173 | | - process.WaitForExit(); |
174 | | - if (process.ExitCode != 0) |
175 | | - throw new InvalidOperationException($"git {arguments} exited with code {process.ExitCode}"); |
176 | | - } |
177 | | - |
178 | | - bool IsExcludedPath(AbsolutePath path) |
179 | | - { |
180 | | - var relative = RootDirectory.GetRelativePathTo(path).ToString(); |
181 | | - return ExcludedDirectories.Any(d => |
182 | | - relative.Contains($"{d}/", StringComparison.OrdinalIgnoreCase) || |
183 | | - relative.Contains($"{d}\\", StringComparison.OrdinalIgnoreCase) || |
184 | | - relative.StartsWith($"{d}/", StringComparison.OrdinalIgnoreCase) || |
185 | | - relative.StartsWith($"{d}\\", StringComparison.OrdinalIgnoreCase)); |
186 | | - } |
187 | | - |
188 | | - static bool IsInitScript(AbsolutePath path) |
189 | | - { |
190 | | - var name = Path.GetFileName(path); |
191 | | - return name is "init.sh" or "init.ps1"; |
192 | | - } |
193 | | - |
194 | | - static bool IsTextFile(AbsolutePath path) |
195 | | - { |
196 | | - var extension = Path.GetExtension(path).ToLowerInvariant(); |
197 | | - return extension is ".cs" or ".csproj" or ".props" or ".targets" or ".slnx" or ".sln" |
198 | | - or ".json" or ".yml" or ".yaml" or ".xml" or ".md" or ".txt" or ".sh" or ".ps1" |
199 | | - or ".ts" or ".js" or ".css" or ".html" or ".editorconfig" or ".gitignore" |
200 | | - or ".config" or ".mdc" or ".toml"; |
201 | | - } |
202 | 13 | } |
0 commit comments