Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;NU1608;NU1109;RS2008;RS1041</NoWarn>
<Version>0.4.0</Version>
<Version>0.5.0</Version>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<ResolveAssemblyReferencesSilent>true</ResolveAssemblyReferencesSilent>
Expand All @@ -11,4 +11,4 @@
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
<Description>A C# source generator that provides strongly-typed, compile-time access to project files marked with CopyToOutputDirectory in the .csproj file.</Description>
</PropertyGroup>
</Project>
</Project>
7 changes: 7 additions & 0 deletions src/ProjectFiles/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ static string ToFilePropertyName(string filePath)
var nameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
var extension = Path.GetExtension(filePath);

// Handle files that start with a dot (like ".txt")
if (string.IsNullOrEmpty(nameWithoutExtension) && !string.IsNullOrEmpty(extension))
{
var fileName = Path.GetFileName(filePath);
return Identifier.Build(fileName);
}

var propertyName = Identifier.Build(nameWithoutExtension);

if (!string.IsNullOrEmpty(extension))
Expand Down
5 changes: 5 additions & 0 deletions src/ProjectFiles/Identifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ public static class Identifier
{
public static string Build(string name)
{
if (name.Length == 0)
{
return "_";
}

var builder = new StringBuilder(name.Length + 1);
var first = name[0];
if (char.IsLetter(first) || first == '_')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//HintName: ProjectFiles.ProjectDirectory.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Collections.Generic;

partial class ProjectDirectory(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectDirectory temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectDirectory temp) =>
new(temp.Path);

public IEnumerable<string> EnumerateDirectories() =>
Directory.EnumerateDirectories(Path);

public IEnumerable<string> EnumerateFiles() =>
Directory.EnumerateFiles(Path);

public IEnumerable<string> GetFiles() =>
Directory.GetFiles(Path);

public IEnumerable<string> GetDirectories() =>
Directory.GetDirectories(Path);

public DirectoryInfo Info => new(Path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//HintName: ProjectFiles.ProjectFile.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

partial class ProjectFile(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectFile temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectFile temp) =>
new(temp.Path);

public FileStream OpenRead() =>
File.OpenRead(Path);

public StreamReader OpenText() =>
File.OpenText(Path);

public string ReadAllText() =>
File.ReadAllText(Path);

public string ReadAllText(Encoding encoding) =>
File.ReadAllText(Path, encoding);

public FileInfo Info => new(Path);

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, cancel);

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, encoding,cancel);
#else
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path));

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path, encoding));
#endif
}
18 changes: 18 additions & 0 deletions src/Tests/GeneratorTest.DotFileAtRoot#ProjectFiles.g.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//HintName: ProjectFiles.g.cs
// <auto-generated/>
#nullable enable

namespace ProjectFilesGenerator
{
using ProjectFilesGenerator.Types;

/// <summary>Provides strongly-typed access to project files marked with CopyToOutputDirectory.</summary>
static partial class ProjectFiles
{
public static ProjectFile _txt { get; } = new(".txt");
}
}

namespace ProjectFilesGenerator.Types
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//HintName: ProjectFiles.ProjectDirectory.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Collections.Generic;

partial class ProjectDirectory(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectDirectory temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectDirectory temp) =>
new(temp.Path);

public IEnumerable<string> EnumerateDirectories() =>
Directory.EnumerateDirectories(Path);

public IEnumerable<string> EnumerateFiles() =>
Directory.EnumerateFiles(Path);

public IEnumerable<string> GetFiles() =>
Directory.GetFiles(Path);

public IEnumerable<string> GetDirectories() =>
Directory.GetDirectories(Path);

public DirectoryInfo Info => new(Path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//HintName: ProjectFiles.ProjectFile.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

partial class ProjectFile(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectFile temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectFile temp) =>
new(temp.Path);

public FileStream OpenRead() =>
File.OpenRead(Path);

public StreamReader OpenText() =>
File.OpenText(Path);

public string ReadAllText() =>
File.ReadAllText(Path);

public string ReadAllText(Encoding encoding) =>
File.ReadAllText(Path, encoding);

public FileInfo Info => new(Path);

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, cancel);

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, encoding,cancel);
#else
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path));

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path, encoding));
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//HintName: ProjectFiles.g.cs
// <auto-generated/>
#nullable enable

namespace ProjectFilesGenerator
{
using ProjectFilesGenerator.Types;

/// <summary>Provides strongly-typed access to project files marked with CopyToOutputDirectory.</summary>
static partial class ProjectFiles
{
public static ConfigType Config { get; } = new();
}
}

namespace ProjectFilesGenerator.Types
{
partial class ConfigType() : ProjectDirectory("Config")
{
public ProjectFile _env { get; } = new("Config/.env");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//HintName: ProjectFiles.ProjectDirectory.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Collections.Generic;

partial class ProjectDirectory(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectDirectory temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectDirectory temp) =>
new(temp.Path);

public IEnumerable<string> EnumerateDirectories() =>
Directory.EnumerateDirectories(Path);

public IEnumerable<string> EnumerateFiles() =>
Directory.EnumerateFiles(Path);

public IEnumerable<string> GetFiles() =>
Directory.GetFiles(Path);

public IEnumerable<string> GetDirectories() =>
Directory.GetDirectories(Path);

public DirectoryInfo Info => new(Path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//HintName: ProjectFiles.ProjectFile.g.cs
namespace ProjectFilesGenerator;

using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

partial class ProjectFile(string path)
{
public string Path { get; } = path;

public string FullPath => System.IO.Path.GetFullPath(Path);

public override string ToString() => Path;

public static implicit operator string(ProjectFile temp) =>
temp.Path;

public static implicit operator FileInfo(ProjectFile temp) =>
new(temp.Path);

public FileStream OpenRead() =>
File.OpenRead(Path);

public StreamReader OpenText() =>
File.OpenText(Path);

public string ReadAllText() =>
File.ReadAllText(Path);

public string ReadAllText(Encoding encoding) =>
File.ReadAllText(Path, encoding);

public FileInfo Info => new(Path);

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, cancel);

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
File.ReadAllTextAsync(Path, encoding,cancel);
#else
public Task<string> ReadAllTextAsync(CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path));

public Task<string> ReadAllTextAsync(Encoding encoding, CancellationToken cancel = default) =>
Task.FromResult(File.ReadAllText(Path, encoding));
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//HintName: ProjectFiles.g.cs
// <auto-generated/>
#nullable enable

namespace ProjectFilesGenerator
{
using ProjectFilesGenerator.Types;

/// <summary>Provides strongly-typed access to project files marked with CopyToOutputDirectory.</summary>
static partial class ProjectFiles
{
public static ProjectFile _editorconfig { get; } = new(".editorconfig");
public static ProjectFile _env { get; } = new(".env");
public static ProjectFile _gitignore { get; } = new(".gitignore");
}
}

namespace ProjectFilesGenerator.Types
{
}
Loading