Skip to content

Commit

Permalink
PR Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zahalzel committed Dec 9, 2021
1 parent 8986f70 commit 14fdced
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task AddAuthCodeAsync()
}

CodeModifierConfig? codeModifierConfig = GetCodeModifierConfig();
if (codeModifierConfig == null || codeModifierConfig.Files.Any() is false)
if (codeModifierConfig is null || !codeModifierConfig.Files.Any())
{
return;
}
Expand Down Expand Up @@ -109,8 +109,23 @@ private async Task HandleCodeFileAsync(CodeFile file, CodeAnalysis.Project proje
}
}

/// <summary>
/// Determines if specified file exists, and if not then creates the
/// file based on template stored in AppProvisioningTool.Properties
/// and adds file to the project
/// </summary>
/// <param name="file"></param>
/// <exception cref="FormatException"></exception>
private void AddFile(CodeFile file)
{
var filePath = Path.Combine(_toolOptions.ProjectPath, file.AddFilePath);
if (File.Exists(filePath))
{
return; // File exists, don't need to create
}

// Resource names for addFiles prefixed with "add" and contain '_' in place of '.'
// fileName: "ShowProfile.razor" -> resourceName: "add_ShowProfile_razor"
var resourceName = file.FileName.Replace('.', '_');
var propertyInfo = AppProvisioningTool.Properties.Where(
p => p.Name.StartsWith("add") && p.Name.EndsWith(resourceName)).FirstOrDefault();
Expand All @@ -121,22 +136,13 @@ private void AddFile(CodeFile file)
}

byte[] content = (propertyInfo.GetValue(null) as byte[])!;

string codeFileString = Encoding.UTF8.GetString(content);

if (string.IsNullOrEmpty(codeFileString))
{
throw new FormatException($"Resource file { propertyInfo.Name } could not be parsed. ");
}

var filePath = Path.Combine(_toolOptions.ProjectPath, file.AddFilePath);
if (File.Exists(filePath))
{
return;
}

var fileDir = Path.GetDirectoryName(filePath);

if (!string.IsNullOrEmpty(fileDir))
{
Directory.CreateDirectory(fileDir);
Expand All @@ -146,21 +152,15 @@ private void AddFile(CodeFile file)

internal async Task ModifyRazorFile(string fileName, CodeFile file, CodeAnalysis.Project project, CodeChangeOptions toolOptions)
{
string? filePath = Directory.EnumerateFiles(_toolOptions.ProjectPath, fileName, SearchOption.AllDirectories).FirstOrDefault();
if (string.IsNullOrEmpty(filePath))
{
return;
}

var razorFile = project.Documents.Where(d => d.Name.Equals(filePath)).FirstOrDefault();
if (razorFile is null)
var document = project.Documents.Where(d => d.Name.EndsWith(fileName)).FirstOrDefault();
if (document is null)
{
return;
}

var razorChanges = file?.RazorChanges?.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, toolOptions));
var editedDocument = await ProjectModifierHelper.AddTextToDocument(razorFile, razorChanges);
await ProjectModifierHelper.UpdateDocument(razorFile, editedDocument, _consoleLogger);
var razorChanges = file?.RazorChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, toolOptions));
var editedDocument = await ProjectModifierHelper.ModifyDocumentText(document, razorChanges);
await ProjectModifierHelper.UpdateDocument(document, editedDocument, _consoleLogger);
}

internal async Task ModifyCshtmlFile(string fileName, CodeFile cshtmlFile, CodeAnalysis.Project project, CodeChangeOptions options)
Expand All @@ -178,8 +178,8 @@ internal async Task ModifyCshtmlFile(string fileName, CodeFile cshtmlFile, CodeA
var globalChanges = cshtmlFile.Methods.TryGetValue("Global", out var globalMethod);
if (globalMethod != null)
{
var filteredCodeChanges = globalMethod.CodeChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, options));
var editedDocument = await ProjectModifierHelper.AddTextToDocument(fileDoc, filteredCodeChanges);
var filteredCodeFiles = globalMethod.CodeChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, options));
var editedDocument = await ProjectModifierHelper.ModifyDocumentText(fileDoc, filteredCodeFiles);
//replace the document
await ProjectModifierHelper.UpdateDocument(fileDoc, editedDocument, _consoleLogger);
}
Expand Down Expand Up @@ -214,8 +214,8 @@ internal async Task ModifyProgramCs(CodeFile programCsFile, CodeAnalysis.Project
var globalChanges = programCsFile.Methods.TryGetValue("Global", out var globalMethod);
if (globalMethod != null)
{
var filteredCodeChanges = globalMethod.CodeChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, toolOptions));
foreach (var change in filteredCodeChanges)
var filteredCodeFiles = globalMethod.CodeChanges.Where(cc => ProjectModifierHelper.FilterOptions(cc.Options, toolOptions));
foreach (var change in filteredCodeFiles)
{
//Modify CodeSnippet to have correct variable identifiers present.
var formattedChange = ProjectModifierHelper.FormatCodeSnippet(change, variableDict);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="add_ShowProfile_razor" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\CodeReaderWriter\CodeFiles\ShowProfile.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>..\CodeReaderWriter\CodeFiles\Blazor\Server\ShowProfile.razor;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="AuthNotEnabled" xml:space="preserve">
<value>Authentication is not enabled yet in this project. An app registration will be created, but the tool does not add the code yet (work in progress).</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,16 @@ internal static bool FilterOptions(string[] options, CodeChangeOptions codeChang
return false;
}

internal static async Task<Document> AddTextToDocument(Document fileDoc, IEnumerable<CodeSnippet> codeChanges)
/// <summary>
/// Replaces text within document or appends text to the end of the document
/// depending on whether change.ReplaceSnippet is set
/// </summary>
/// <param name="fileDoc"></param>
/// <param name="codeChanges"></param>
/// <returns></returns>
internal static async Task<Document> ModifyDocumentText(Document fileDoc, IEnumerable<CodeSnippet> codeChanges)
{
if (fileDoc is null || codeChanges?.Any() != true)
if (fileDoc is null || codeChanges is null || !codeChanges.Any())
{
return null;
}
Expand All @@ -392,19 +399,19 @@ internal static async Task<Document> AddTextToDocument(Document fileDoc, IEnumer
return null;
}

var trimmedStatement = ProjectModifierHelper.TrimStatement(sourceFileString);
foreach (var change in codeChanges)
var trimmedSourceFile = ProjectModifierHelper.TrimStatement(sourceFileString);
var applicableCodeChanges = codeChanges.Where(
c => !string.IsNullOrEmpty(c.Block) && !trimmedSourceFile.Contains(ProjectModifierHelper.TrimStatement(c.Block)));
foreach (var change in applicableCodeChanges)
{
if (!string.IsNullOrEmpty(change.Block) && !trimmedStatement.Contains(ProjectModifierHelper.TrimStatement(change.Block)))
// If doing a code replacement, replace ReplaceSnippet in source with Block
if (!string.IsNullOrEmpty(change.ReplaceSnippet) && sourceFileString.Contains(change.ReplaceSnippet))
{
if (!string.IsNullOrEmpty(change.ReplaceSnippet) && sourceFileString.Contains(change.ReplaceSnippet))
{
sourceFileString = sourceFileString.Replace(change.ReplaceSnippet, change.Block);
}
else
{
sourceFileString += change.Block;
}
sourceFileString = sourceFileString.Replace(change.ReplaceSnippet, change.Block);
}
else
{
sourceFileString += change.Block; // Otherwise appending block to end of file
}
}

Expand Down

0 comments on commit 14fdced

Please sign in to comment.