Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the issue where the contents of the output file for SingleFileGeneration are appended to each time Autorest is run #968

Merged
merged 5 commits into from
Apr 22, 2016
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
3 changes: 3 additions & 0 deletions AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public void OutputToSingleFile()
};

string path = Path.Combine(settings.OutputDirectory, "test.file.cs");
string existingContents = "this is dummy";
_fileSystem.VirtualStore[path] = new StringBuilder(existingContents);
var codeGenerator = new SampleCodeGenerator(settings);
codeGenerator.Generate(new ServiceClient()).GetAwaiter().GetResult();
Assert.DoesNotContain(existingContents, _fileSystem.VirtualStore[path].ToString());
Assert.Equal(4, _fileSystem.VirtualStore.Count);
Assert.True(_fileSystem.VirtualStore.ContainsKey(path));
Assert.True(_fileSystem.VirtualStore.ContainsKey("AutoRest.json"));
Expand Down
21 changes: 12 additions & 9 deletions AutoRest/AutoRest.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Rest.Generator
public abstract class CodeGenerator
{
public const string EnumObject = "x-ms-enum";
private bool firstTimeWriteSingleFile = true;

protected CodeGenerator(Settings settings)
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public async Task Write(ITemplate template, string fileName)
/// <returns></returns>
public async Task Write(string template, string fileName)
{
string relativeFilePath = null;
string filePath = null;

if (Settings.OutputFileName != null)
{
Expand All @@ -110,17 +111,19 @@ public async Task Write(string template, string fileName)
ErrorManager.ThrowErrors();
}

relativeFilePath = Settings.OutputFileName;
filePath = Path.Combine(Settings.OutputDirectory, Settings.OutputFileName);

if (firstTimeWriteSingleFile)
{
// for SingleFileGeneration clean the file before writing only if its the first time
Settings.FileSystem.DeleteFile(filePath);
firstTimeWriteSingleFile = false;
}
}
else
{
relativeFilePath = fileName;
}
string filePath = Path.Combine(Settings.OutputDirectory, relativeFilePath);

// cleans file before writing unless single file
if (!(Settings.OutputFileName != null && IsSingleFileGenerationSupported))
{
filePath = Path.Combine(Settings.OutputDirectory, fileName);
// cleans file before writing
Settings.FileSystem.DeleteFile(filePath);
}
// Make sure the directory exist
Expand Down