Skip to content

Commit d8eb972

Browse files
committed
Deal with not empty folder; Fix console project suffix
1 parent 9b4e732 commit d8eb972

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@
22

33
[![Nuget](https://img.shields.io/nuget/v/SpatialFocus.FocusInit)](https://www.nuget.org/packages/SpatialFocus.FocusInit/)
44

5+
Initialize an empty folder for a new project by copying and modifying files from our [repository-template](https://github.com/SpatialFocus/repository-template). This includes:
6+
7+
- .gitignore and .editorconfig
8+
- stylecop.json and ReSharper settings
9+
- MSBuild properties
10+
- Solution file including the default _Solution Items_ solution folder
11+
- README.md
12+
13+
Placeholders in these files will be filled with solution name and author information.
14+
15+
Additionally, the wizard supports the creation of .NET Core projects in the solution. Currently, these project templates are available:
16+
17+
1) Console app
18+
2) Empty web
19+
3) Web API
20+
4) Web MVC
21+
5) Xamarin Forms Shell (using our [Xamarin Forms Shell template](https://github.com/SpatialFocus/DotNetNew.XamarinFormsShell))
22+
6) Blazor Server
23+
7) Blazor Wasm
24+
25+
After setting up one of these demo projects, typical generic projects can be added as well:
26+
27+
- Business (class library for business logic)
28+
- Shared (class library for shared data)
29+
- Test (nunit test project)
30+
531
## Install the dotnet tool
632

733
```
8-
dotnet tool install --global SpatialFocus.FocusInit --version 0.2.0
34+
dotnet tool install --global SpatialFocus.FocusInit --version 0.3.0
935
```
1036

1137
Install the tool globally. You can invoke the tool using the following command: `focus-init`
@@ -32,12 +58,21 @@ d----- 13.05.2020 15:19 NewConsoleProject
3258
PS C:\temp> cd .\NewConsoleProject\
3359
3460
PS C:\temp\NewConsoleProject> focus-init
61+
62+
__ _ _ _
63+
/ _| ___ ___ _ _ ___ (_)_ __ (_) |_
64+
| |_ / _ \ / __| | | / __|_____| | '_ \| | __|
65+
| _| (_) | (__| |_| \__ \_____| | | | | | |_
66+
|_| \___/ \___|\__,_|___/ |_|_| |_|_|\__|
67+
68+
69+
Initialize this folder for a new project.
3570
Enter solution name [NewConsoleProject]
3671
Enter company name [Spatial Focus GmbH]
3772
Template repository cloned successfully.
3873
Solution files have been copied and modified.
3974
Create additional projects? [Y/n] n
40-
Finished.
75+
Finished. Have fun!
4176
4277
PS C:\temp\NewConsoleProject> dir
4378

src/FocusInit/DotnetCliHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ public string CreateMultiProject(string type)
4848
return $"{srcFolder}/{SolutionName}";
4949
}
5050

51-
public string CreateProject(string type, string projectSuffix)
51+
public string CreateProject(string type, string projectSuffix = null)
5252
{
5353
string srcFolder = !string.IsNullOrEmpty(WorkDir) ? WorkDir + "/src" : "src";
54+
string projectName = !string.IsNullOrEmpty(projectSuffix) ? $"{SolutionName}.{projectSuffix}" : SolutionName;
5455

55-
string createProjectCmd = $"new {type} -n {SolutionName}.{projectSuffix} -o {srcFolder}/{SolutionName}.{projectSuffix}";
56+
string createProjectCmd = $"new {type} -n {projectName} -o {srcFolder}/{projectName}";
5657
Process process = Process.Start("dotnet", createProjectCmd);
5758

5859
process.WaitForExit(3000);
5960

60-
return $"{srcFolder}/{SolutionName}.{projectSuffix}";
61+
return $"{srcFolder}/{projectName}";
6162
}
6263

6364
public void InstallCustomProjectTemplate(string type)

src/FocusInit/FileSystemHelper.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@ public class FileSystemHelper
1010
{
1111
public void CleanupAndCreateWorkDir()
1212
{
13-
if (Directory.Exists(Settings.WorkingDir))
13+
if (!string.IsNullOrEmpty(Settings.WorkingDir) && !Directory.Exists(Settings.WorkingDir))
1414
{
15-
// Deal with readonly files (typically in .git folder)
16-
SetDirectoryNormal(Settings.WorkingDir);
17-
Directory.Delete(Settings.WorkingDir, true);
15+
Directory.CreateDirectory(Settings.WorkingDir);
16+
return;
1817
}
1918

20-
Directory.CreateDirectory(Settings.WorkingDir);
19+
string targetDirectory = !string.IsNullOrEmpty(Settings.WorkingDir) ? Settings.WorkingDir : Directory.GetCurrentDirectory();
20+
21+
DirectoryInfo currentDirectory = new DirectoryInfo(targetDirectory);
22+
23+
// Deal with readonly files (typically in .git folder)
24+
SetDirectoryNormal(currentDirectory.FullName);
25+
26+
foreach (FileInfo file in currentDirectory.GetFiles())
27+
{
28+
file.Delete();
29+
}
30+
31+
foreach (DirectoryInfo dir in currentDirectory.GetDirectories())
32+
{
33+
dir.Delete(true);
34+
}
2135
}
2236

2337
public void DeleteRepository()

src/FocusInit/FocusInit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<ToolCommandName>focus-init</ToolCommandName>
99
<PackageOutputPath>./nupkg</PackageOutputPath>
1010

11-
<Version>0.2.0</Version>
11+
<Version>0.3.0</Version>
1212
<PackageId>SpatialFocus.FocusInit</PackageId>
1313
<Title>Spatial Focus initialize project wizard</Title>
1414
<Description>DotNet Tool for initializing an empty folder, create a solution with stylecop, ReSharper and license settings, and optionally add projects.</Description>

src/FocusInit/Program.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@ public static void Main()
1616
{
1717
FileSystemHelper fileSystemHelper = new FileSystemHelper();
1818

19-
if (!string.IsNullOrEmpty(Settings.WorkingDir))
19+
Console.WriteLine(@"
20+
__ _ _ _
21+
/ _| ___ ___ _ _ ___ (_)_ __ (_) |_
22+
| |_ / _ \ / __| | | / __|_____| | '_ \| | __|
23+
| _| (_) | (__| |_| \__ \_____| | | | | | |_
24+
|_| \___/ \___|\__,_|___/ |_|_| |_|_|\__|
25+
26+
");
27+
Console.WriteLine("Initialize this folder for a new project.");
28+
29+
string targetDirectory = string.IsNullOrEmpty(Settings.WorkingDir) ? Directory.GetCurrentDirectory() : Settings.WorkingDir;
30+
31+
if (Directory.GetFiles(targetDirectory).Length > 0 || Directory.GetDirectories(targetDirectory).Length > 0)
2032
{
21-
fileSystemHelper.CleanupAndCreateWorkDir();
33+
if (!Prompt.GetYesNo("Directory is not empty. Delete all files/folders and continue?", false, ConsoleColor.Red))
34+
{
35+
return;
36+
}
2237
}
2338

24-
Console.WriteLine("Welcome to focus-init");
25-
Console.WriteLine();
26-
Console.WriteLine("Initialize this folder for a new project.");
39+
fileSystemHelper.CleanupAndCreateWorkDir();
2740

2841
string currentDirectoryName = new DirectoryInfo(Directory.GetCurrentDirectory()).Name;
2942
string solutionName = Prompt.GetString("Enter solution name", currentDirectoryName, ConsoleColor.DarkCyan);
@@ -76,7 +89,7 @@ private static void CreateProjects(string solutionName)
7689
switch (i)
7790
{
7891
case 1:
79-
string consoleProject = cliHelper.CreateProject("console", "Console");
92+
string consoleProject = cliHelper.CreateProject("console");
8093
cliHelper.AddProjectToSolution(consoleProject);
8194
projectsToReference.Add(consoleProject);
8295
break;

0 commit comments

Comments
 (0)