Skip to content

Commit

Permalink
Merge pull request #25 from rjrudman/master
Browse files Browse the repository at this point in the history
Add flag to convert line endings to unix
  • Loading branch information
lukemurray authored Nov 28, 2023
2 parents 51eeed0 + 2ed50a3 commit 9497644
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/dotnet-gqlgen-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class Program

[Option(LongName = "no_generated_timestamp", ShortName = "nt", Description = "Don't add 'Generated on abc from xyz' in generated files")]
public bool NoGeneratedTimestamp { get; }

[Option(LongName = "unix", ShortName = "un", Description = "Convert windows endings to unix")]
public bool ConvertToUnixLineEnding { get; }

public static Task<int> Main(string[] args) => CommandLineApplication.ExecuteAsync<Program>(args);

Expand All @@ -47,7 +50,8 @@ await Generator.Generate(new()
ScalarMapping = ScalarMapping,
OutputDir = OutputDir,
Usings = Usings,
NoGeneratedTimestamp = NoGeneratedTimestamp
NoGeneratedTimestamp = NoGeneratedTimestamp,
ConvertToUnixLineEnding = ConvertToUnixLineEnding
});
}
catch (Exception e)
Expand Down
27 changes: 19 additions & 8 deletions src/dotnet-gqlgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class GeneratorOptions
public string OutputDir { get; set; } = "output";
public string Usings { get; set; } = "";
public bool NoGeneratedTimestamp { get; set; }
public bool ConvertToUnixLineEnding { get; set; } = true;
}

public static class Generator
Expand Down Expand Up @@ -112,7 +113,7 @@ public static async Task Generate(GeneratorOptions options)
options.NoGeneratedTimestamp
});
Directory.CreateDirectory(options.OutputDir);
await File.WriteAllTextAsync($"{options.OutputDir}/GeneratedResultTypes.cs", resultTypes);
await NormalizeAndWriteIfChanged($"{options.OutputDir}/GeneratedResultTypes.cs", resultTypes, options.ConvertToUnixLineEnding);

string queryTypes = await engine.CompileRenderAsync("queryTypes.cshtml", new
{
Expand All @@ -125,7 +126,7 @@ public static async Task Generate(GeneratorOptions options)
options.NoGeneratedTimestamp
});
Directory.CreateDirectory(options.OutputDir);
await File.WriteAllTextAsync($"{options.OutputDir}/GeneratedQueryTypes.cs", queryTypes);
await NormalizeAndWriteIfChanged($"{options.OutputDir}/GeneratedQueryTypes.cs", queryTypes, options.ConvertToUnixLineEnding);

resultTypes = await engine.CompileRenderAsync("client.cshtml", new
{
Expand All @@ -138,20 +139,30 @@ public static async Task Generate(GeneratorOptions options)
CmdArgs = $"-n {options.Namespace} -c {options.ClientClassName} -m {options.ScalarMapping}",
options.NoGeneratedTimestamp
});
await File.WriteAllTextAsync($"{options.OutputDir}/{options.ClientClassName}.cs", resultTypes);
await NormalizeAndWriteIfChanged($"{options.OutputDir}/{options.ClientClassName}.cs", resultTypes, options.ConvertToUnixLineEnding);

await WriteResourceToFile(rootType, "BaseGraphQLClient.cs", $"{options.OutputDir}/BaseGraphQLClient.cs");
await WriteResourceToFile(rootType, "GqlFieldNameAttribute.cs", $"{options.OutputDir}/GqlFieldNameAttribute.cs");
await WriteResourceToFile(rootType, "BaseGraphQLClient.cs", $"{options.OutputDir}/BaseGraphQLClient.cs", options.ConvertToUnixLineEnding);
await WriteResourceToFile(rootType, "GqlFieldNameAttribute.cs", $"{options.OutputDir}/GqlFieldNameAttribute.cs", options.ConvertToUnixLineEnding);

Console.WriteLine($"Done.");
}

private static async Task NormalizeAndWriteIfChanged(string file, string text, bool convertToUnixLineEnding)
{
var normalizedText = convertToUnixLineEnding ? text.Replace("\r\n", "\n") : text;
if (File.Exists(file) && string.Equals(await File.ReadAllTextAsync(file), normalizedText)) return;

await File.WriteAllTextAsync(file, normalizedText);
}

private static async Task WriteResourceToFile(Type rootType, string resourceName, string outputLocation)
private static async Task WriteResourceToFile(Type rootType, string resourceName, string outputLocation, bool convertToUnixLineEnding)
{
var assembly = rootType.GetTypeInfo().Assembly;
await using var fileStream = File.Open(outputLocation, FileMode.Create);
await using var resourceStream = assembly.GetManifestResourceStream($"{rootType.Namespace}.{resourceName}")!;
await resourceStream.CopyToAsync(fileStream);
using var streamReader = new StreamReader(resourceStream);

var text = await streamReader.ReadToEndAsync();
await NormalizeAndWriteIfChanged(outputLocation, text, convertToUnixLineEnding);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-gqlgen/dotnet-gqlgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>dotnet_gqlgen</RootNamespace>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PackageVersion>0.6.2</PackageVersion>
<PackageVersion>0.6.4</PackageVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 9497644

Please sign in to comment.