Skip to content

Commit

Permalink
Deal with not empty folder; Fix console project suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
pergerch committed May 18, 2020
1 parent 9b4e732 commit d8eb972
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@

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

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:

- .gitignore and .editorconfig
- stylecop.json and ReSharper settings
- MSBuild properties
- Solution file including the default _Solution Items_ solution folder
- README.md

Placeholders in these files will be filled with solution name and author information.

Additionally, the wizard supports the creation of .NET Core projects in the solution. Currently, these project templates are available:

1) Console app
2) Empty web
3) Web API
4) Web MVC
5) Xamarin Forms Shell (using our [Xamarin Forms Shell template](https://github.com/SpatialFocus/DotNetNew.XamarinFormsShell))
6) Blazor Server
7) Blazor Wasm

After setting up one of these demo projects, typical generic projects can be added as well:

- Business (class library for business logic)
- Shared (class library for shared data)
- Test (nunit test project)

## Install the dotnet tool

```
dotnet tool install --global SpatialFocus.FocusInit --version 0.2.0
dotnet tool install --global SpatialFocus.FocusInit --version 0.3.0
```

Install the tool globally. You can invoke the tool using the following command: `focus-init`
Expand All @@ -32,12 +58,21 @@ d----- 13.05.2020 15:19 NewConsoleProject
PS C:\temp> cd .\NewConsoleProject\
PS C:\temp\NewConsoleProject> focus-init
__ _ _ _
/ _| ___ ___ _ _ ___ (_)_ __ (_) |_
| |_ / _ \ / __| | | / __|_____| | '_ \| | __|
| _| (_) | (__| |_| \__ \_____| | | | | | |_
|_| \___/ \___|\__,_|___/ |_|_| |_|_|\__|
Initialize this folder for a new project.
Enter solution name [NewConsoleProject]
Enter company name [Spatial Focus GmbH]
Template repository cloned successfully.
Solution files have been copied and modified.
Create additional projects? [Y/n] n
Finished.
Finished. Have fun!
PS C:\temp\NewConsoleProject> dir
Expand Down
7 changes: 4 additions & 3 deletions src/FocusInit/DotnetCliHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ public string CreateMultiProject(string type)
return $"{srcFolder}/{SolutionName}";
}

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

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

process.WaitForExit(3000);

return $"{srcFolder}/{SolutionName}.{projectSuffix}";
return $"{srcFolder}/{projectName}";
}

public void InstallCustomProjectTemplate(string type)
Expand Down
24 changes: 19 additions & 5 deletions src/FocusInit/FileSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@ public class FileSystemHelper
{
public void CleanupAndCreateWorkDir()
{
if (Directory.Exists(Settings.WorkingDir))
if (!string.IsNullOrEmpty(Settings.WorkingDir) && !Directory.Exists(Settings.WorkingDir))
{
// Deal with readonly files (typically in .git folder)
SetDirectoryNormal(Settings.WorkingDir);
Directory.Delete(Settings.WorkingDir, true);
Directory.CreateDirectory(Settings.WorkingDir);
return;
}

Directory.CreateDirectory(Settings.WorkingDir);
string targetDirectory = !string.IsNullOrEmpty(Settings.WorkingDir) ? Settings.WorkingDir : Directory.GetCurrentDirectory();

DirectoryInfo currentDirectory = new DirectoryInfo(targetDirectory);

// Deal with readonly files (typically in .git folder)
SetDirectoryNormal(currentDirectory.FullName);

foreach (FileInfo file in currentDirectory.GetFiles())
{
file.Delete();
}

foreach (DirectoryInfo dir in currentDirectory.GetDirectories())
{
dir.Delete(true);
}
}

public void DeleteRepository()
Expand Down
2 changes: 1 addition & 1 deletion src/FocusInit/FocusInit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ToolCommandName>focus-init</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>

<Version>0.2.0</Version>
<Version>0.3.0</Version>
<PackageId>SpatialFocus.FocusInit</PackageId>
<Title>Spatial Focus initialize project wizard</Title>
<Description>DotNet Tool for initializing an empty folder, create a solution with stylecop, ReSharper and license settings, and optionally add projects.</Description>
Expand Down
25 changes: 19 additions & 6 deletions src/FocusInit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ public static void Main()
{
FileSystemHelper fileSystemHelper = new FileSystemHelper();

if (!string.IsNullOrEmpty(Settings.WorkingDir))
Console.WriteLine(@"
__ _ _ _
/ _| ___ ___ _ _ ___ (_)_ __ (_) |_
| |_ / _ \ / __| | | / __|_____| | '_ \| | __|
| _| (_) | (__| |_| \__ \_____| | | | | | |_
|_| \___/ \___|\__,_|___/ |_|_| |_|_|\__|
");
Console.WriteLine("Initialize this folder for a new project.");

string targetDirectory = string.IsNullOrEmpty(Settings.WorkingDir) ? Directory.GetCurrentDirectory() : Settings.WorkingDir;

if (Directory.GetFiles(targetDirectory).Length > 0 || Directory.GetDirectories(targetDirectory).Length > 0)
{
fileSystemHelper.CleanupAndCreateWorkDir();
if (!Prompt.GetYesNo("Directory is not empty. Delete all files/folders and continue?", false, ConsoleColor.Red))
{
return;
}
}

Console.WriteLine("Welcome to focus-init");
Console.WriteLine();
Console.WriteLine("Initialize this folder for a new project.");
fileSystemHelper.CleanupAndCreateWorkDir();

string currentDirectoryName = new DirectoryInfo(Directory.GetCurrentDirectory()).Name;
string solutionName = Prompt.GetString("Enter solution name", currentDirectoryName, ConsoleColor.DarkCyan);
Expand Down Expand Up @@ -76,7 +89,7 @@ private static void CreateProjects(string solutionName)
switch (i)
{
case 1:
string consoleProject = cliHelper.CreateProject("console", "Console");
string consoleProject = cliHelper.CreateProject("console");
cliHelper.AddProjectToSolution(consoleProject);
projectsToReference.Add(consoleProject);
break;
Expand Down

0 comments on commit d8eb972

Please sign in to comment.