Skip to content

Commit

Permalink
Create parent directory before saving solution file (#45)
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
jeffkl authored Oct 3, 2018
1 parent cefb686 commit 681d6d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/SlnGen.Build.Tasks.UnitTests/SlnFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SlnGen.Build.Tasks.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using MSBuildSolutionFile = Microsoft.Build.Construction.SolutionFile;
Expand Down Expand Up @@ -69,6 +70,22 @@ public void NoFolders()
ValidateProjectInSolution((s, p) => p.ParentProjectGuid.ShouldBe(null), projects, false);
}

[Fact]
public void SaveToCustomLocationCreatesDirectory()
{
DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(TestRootPath, "1", "2", "3"));

directoryInfo.Exists.ShouldBeFalse();

string fullPath = Path.Combine(directoryInfo.FullName, Path.GetRandomFileName());

SlnFile slnFile = new SlnFile();

slnFile.Save(fullPath, folders: false);

File.Exists(fullPath).ShouldBeTrue();
}

private void ValidateProjectInSolution(Action<SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool folders)
{
string solutionFilePath = GetTempFileName();
Expand Down
7 changes: 7 additions & 0 deletions src/SlnGen.Build.Tasks/Internal/SlnFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public void AddSolutionItems(IEnumerable<string> items)
/// <param name="folders">Specifies if folders should be created.</param>
public void Save(string path, bool folders)
{
string directoryName = Path.GetDirectoryName(path);

if (!String.IsNullOrWhiteSpace(directoryName))
{
Directory.CreateDirectory(directoryName);
}

using (StreamWriter writer = File.CreateText(path))
{
Save(writer, folders);
Expand Down

0 comments on commit 681d6d2

Please sign in to comment.