Skip to content

Commit

Permalink
fix for non normalized paths
Browse files Browse the repository at this point in the history
  • Loading branch information
AdaskoTheBeAsT committed Nov 27, 2023
1 parent 25703ea commit c581f70
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/Tests/LongPaths/FileWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Typewriter.Tests.LongPaths
public class FileWriterTest
{
[Fact]
public void ShouldCreateFileInDirectory()
public void ShouldCreateFileInDirectoryForLongPath()
{
var path = @"C:\Dev\SomeInnerPath\Github\Project\src\innerpath\another-subpath\A.Very.Long.Project.Name.Like.Dotnet.People.Like\packages\some-random-scope\some-package-name\src\lib\events\a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-file-name.ts";
var modifiedPath = path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)
Expand All @@ -27,5 +27,27 @@ public void ShouldCreateFileInDirectory()

action.Should().NotThrow();
}

[Fact]
public void ShouldCreateFileInDirectoryForNonNormalizedLongPath()
{
var path = @"C:\Dev\..\Dev\SomeInnerPath\Github\Project\src\innerpath\another-subpath\A.Very.Long.Project.Name.Like.Dotnet.People.Like\packages\some-random-scope\some-package-name\src\lib\events\a-very-very-very-very-very-very-very-very-very-very-very-very-very-very-long-file-name.ts";
var normalizedPath = Path.GetFullPath(path);
var longPath = normalizedPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)
? $@"\\?\UNC\{normalizedPath.Substring(2, normalizedPath.Length - 2)}"
: $@"\\?\{normalizedPath}";
Action action = () =>
{
var dir = Path.GetDirectoryName(longPath);
if (!Directory.Exists(dir) && dir != null)
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(longPath, "ok");
};

action.Should().NotThrow();
}
}
}
16 changes: 9 additions & 7 deletions src/Typewriter/Generation/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Typewriter.Generation.Controllers;
using Typewriter.VisualStudio;
using File = Typewriter.CodeModel.File;
using Path = System.IO.Path;

namespace Typewriter.Generation
{
Expand Down Expand Up @@ -165,18 +166,19 @@ public bool RenderFile(File[] files)

protected virtual void WriteFile(string outputPath, string outputContent)
{
var modifiedPath = outputPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)
? $@"\\?\UNC\{outputPath.Substring(2, outputPath.Length - 2)}"
: $@"\\?\{outputPath}";
var normalizedPath = Path.GetFullPath(outputPath);
var longPath = normalizedPath.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)
? $@"\\?\UNC\{normalizedPath.Substring(2, normalizedPath.Length - 2)}"
: $@"\\?\{normalizedPath}";

var dir = System.IO.Path.GetDirectoryName(modifiedPath);
if (!System.IO.Directory.Exists(dir) && dir != null)
var dir = Path.GetDirectoryName(longPath);
if (!Directory.Exists(dir) && dir != null)
{
System.IO.Directory.CreateDirectory(dir);
Directory.CreateDirectory(dir);
}

System.IO.File.WriteAllText(
modifiedPath,
longPath,
outputContent,
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Typewriter/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="68E244F1-14E1-4681-A51C-5B902B9EE96C" Version="2.21.0" Language="en-US" Publisher="frhagn" />
<Identity Id="68E244F1-14E1-4681-A51C-5B902B9EE96C" Version="2.22.0" Language="en-US" Publisher="frhagn" />
<DisplayName>Typewriter64</DisplayName>
<Description xml:space="preserve">Typewriter generates TypeScript files from c# code files using TypeScript Templates. This allows you to create fully typed TypeScript representations of server side API that automatically updates when you make changes to your c# code.</Description>
<MoreInfo>http://frhagn.github.io/Typewriter/</MoreInfo>
Expand Down

0 comments on commit c581f70

Please sign in to comment.