Skip to content

Commit

Permalink
完成根据模版创建项目
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Jan 3, 2025
1 parent 01f7d50 commit 1b88497
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
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; }
}
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>
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

0 comments on commit 1b88497

Please sign in to comment.