-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
app/Tool/创建随机项目/按照模版创建随机项目/WhitmanProjectWithTemplateCreator/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using System.Diagnostics; | ||
using dotnetCampus.Cli; | ||
using Lindexi.Src.WhitmanRandomIdentifier; | ||
|
||
var option = CommandLine.Parse(args).As<Option>(); | ||
|
||
if (string.IsNullOrEmpty(option.TemplateFolder) || string.IsNullOrEmpty(option.OutputFolder)) | ||
{ | ||
Console.WriteLine($"必须传入模版文件夹和输出文件夹"); | ||
return -1; | ||
} | ||
|
||
if (!Directory.Exists(option.TemplateFolder)) | ||
{ | ||
Console.WriteLine($"传入的模版文件夹不存在 {option.TemplateFolder}"); | ||
} | ||
|
||
var randomIdentifier = new RandomIdentifier(); | ||
var randomName = randomIdentifier.Generate(true); | ||
|
||
var templateFolder = new DirectoryInfo(option.TemplateFolder); | ||
var outputFolder = new DirectoryInfo(option.OutputFolder); | ||
|
||
var desFolder = outputFolder.CreateSubdirectory(randomName); | ||
|
||
CopyFolder(templateFolder, desFolder, name => name.Replace(templateFolder.Name, randomName)); | ||
|
||
return 0; | ||
|
||
static void CopyFolder(DirectoryInfo originFolder, DirectoryInfo desFolder, Rename rename) | ||
{ | ||
foreach (var directory in originFolder.EnumerateDirectories()) | ||
{ | ||
var originName = directory.Name; | ||
var newName = rename(originName); | ||
|
||
var newDirectory = desFolder.CreateSubdirectory(newName); | ||
CopyFolder(directory, newDirectory, rename); | ||
} | ||
|
||
foreach (var file in originFolder.EnumerateFiles()) | ||
{ | ||
var originName = file.Name; | ||
var newName = rename(originName); | ||
|
||
var destFileName = Path.Join(desFolder.FullName, newName); | ||
|
||
try | ||
{ | ||
// 尝试读取文件重写一下 | ||
var content = File.ReadAllText(file.FullName); | ||
var output = rename(content); | ||
File.WriteAllText(destFileName, output); | ||
} | ||
catch | ||
{ | ||
file.CopyTo(destFileName, overwrite: true); | ||
} | ||
} | ||
} | ||
|
||
delegate string Rename(string name); | ||
|
||
class Option | ||
{ | ||
[Option("TemplateFolder")] | ||
public string? TemplateFolder { get; set; } | ||
|
||
[Option("OutputFolder")] | ||
public string? OutputFolder { get; set; } | ||
} |
18 changes: 18 additions & 0 deletions
18
...机项目/按照模版创建随机项目/WhitmanProjectWithTemplateCreator/WhitmanProjectWithTemplateCreator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="dotnetCampus.CommandLine" Version="3.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\..\package\WhitmanRandomIdentifier\WhitmanRandomIdentifier.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
...创建随机项目/按照模版创建随机项目/WhitmanProjectWithTemplateCreator/WhitmanProjectWithTemplateCreator.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhitmanProjectWithTemplateCreator", "WhitmanProjectWithTemplateCreator.csproj", "{F8B4A852-D46E-499E-8634-94ED2D8041E8}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhitmanRandomIdentifier", "..\..\..\..\..\package\WhitmanRandomIdentifier\WhitmanRandomIdentifier.csproj", "{8AC729B9-BA64-4915-8A11-6A357E767E7D}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{F8B4A852-D46E-499E-8634-94ED2D8041E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{F8B4A852-D46E-499E-8634-94ED2D8041E8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{F8B4A852-D46E-499E-8634-94ED2D8041E8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{F8B4A852-D46E-499E-8634-94ED2D8041E8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{8AC729B9-BA64-4915-8A11-6A357E767E7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8AC729B9-BA64-4915-8A11-6A357E767E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8AC729B9-BA64-4915-8A11-6A357E767E7D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8AC729B9-BA64-4915-8A11-6A357E767E7D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |