Skip to content

Commit

Permalink
--update-project feature work (#1593)
Browse files Browse the repository at this point in the history
* Added update-project command feature (#1592)

* update-project with code changes

* minor fix

* more updates

* minor fixes

* minor fixes

* fixed Resources and other minor fixes

* fixes and updates

* added DocumentBuilder, and unit tests.

* switched to using Resources. Important for localization later.

* PR comment fixes.

* version.props fixes.

* pr comment fixes 2

* final fixes.

* minor fix and preview version update.

* skip stuck tests for now

Co-authored-by: Deep Choudhery <[email protected]>
  • Loading branch information
deepchoudhery and Deep Choudhery authored Jul 12, 2021
1 parent 2d6dbca commit c49241c
Show file tree
Hide file tree
Showing 41 changed files with 2,546 additions and 87 deletions.
2 changes: 1 addition & 1 deletion eng/Versions.MSIdentity.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<PreReleaseVersionLabel>preview</PreReleaseVersionLabel>
<PreReleaseVersionIteration>2</PreReleaseVersionIteration>
<PreReleaseVersionIteration>3</PreReleaseVersionIteration>
<IsServicingBuild Condition="'$(PreReleaseVersionLabel)' == 'servicing'">false</IsServicingBuild>
<!--
When StabilizePackageVersion is set to 'true', this branch will produce stable outputs for 'Shipping' packages
Expand Down
6 changes: 4 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
<!-- Microsoft.Build.Utilities-->
<MicrosoftBuildUtilitiesCorePackageVersion>16.8.0 </MicrosoftBuildUtilitiesCorePackageVersion>
<!-- Microsoft.CodeAnalysis.CSharp -->
<MicrosoftCodeAnalysisCSharpPackageVersion>3.8.0</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>3.9.0</MicrosoftCodeAnalysisCSharpPackageVersion>
<!-- Microsoft.CodeAnalysis.Razor -->
<MicrosoftCodeAnalysisRazorPackageVersion>6.0.0-preview.5.21301.17</MicrosoftCodeAnalysisRazorPackageVersion>
<!-- Microsoft.CodeAnalysis.CSharp.Workspaces -->
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>3.8.0</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>3.9.0</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<!-- Microsoft.Extensions.CommandLineUtils.Sources -->
<MicrosoftExtensionsCommandLineUtilsSourcesPackageVersion>6.0.0-preview.1.21076.2</MicrosoftExtensionsCommandLineUtilsSourcesPackageVersion>
<!-- Microsoft.EntityFrameworkCore.Design -->
Expand Down Expand Up @@ -125,11 +125,13 @@
<!-- Package versions for MSIdentity projects-->
<PropertyGroup>
<AzureIdentityVersion>1.3.0</AzureIdentityVersion>
<CodeAnalysisVersion>3.9.0</CodeAnalysisVersion>
<MicrosoftExtensionsConfigurationVersion>3.1.9</MicrosoftExtensionsConfigurationVersion>
<MicrosoftExtensionsConfigurationBinderVersion>3.1.9</MicrosoftExtensionsConfigurationBinderVersion>
<MicrosoftExtensionsConfigurationCommandLineVersion>3.1.9</MicrosoftExtensionsConfigurationCommandLineVersion>
<MicrosoftGraphVersion>3.25.0</MicrosoftGraphVersion>
<MicrosoftIdentityClientExtensionsMsalVersion>2.18.0</MicrosoftIdentityClientExtensionsMsalVersion>
<NuGetProjectModelVersion>5.9.1</NuGetProjectModelVersion>
<SystemTextJsonVersion>4.7.2</SystemTextJsonVersion>
<SystemCommandLineVersion>2.0.0-beta1.20574.7</SystemCommandLineVersion>
<NewtonsoftJsonMsIdentityPackageVersion>13.0.1</NewtonsoftJsonMsIdentityPackageVersion>
Expand Down
5 changes: 4 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"tools": {
"dotnet": "5.0.102"
"dotnet": "5.0.301"
},
"sdk" : {
"version": "5.0.301"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "6.0.0-beta.21222.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;

namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
{
//static class helper for CodeAnalysis
public static class CodeAnalysisHelper
{
//helps create a CodeAnalysis.Project with project files given a project path.
public static async Task<CodeAnalysis.Project> LoadCodeAnalysisProjectAsync(string projectFilePath)
{
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(projectFilePath);
var projectWithFiles = project.WithAllSourceFiles();
project = projectWithFiles ?? project;
return project;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter.CodeChange
{
public class CodeChangeType
{
public const string MemberAccess = nameof(MemberAccess);
public const string InLambdaBlock = nameof(InLambdaBlock);
public const string LambdaExpression = nameof(LambdaExpression);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter.CodeChange
{
public class CodeFile
{
public Dictionary<string, Method>? Methods { get; set; }
public string[]? Usings { get; set; }
public string? FileName { get; set; }
public string[]? ClassProperties { get; set; }
public string[]? ClassAttributes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter.CodeChange
{
public class CodeSnippet
{
public string? InsertAfter { get; set; }
public string? Block { get; set; }
public string? Parent { get; set; }
public string? Type { get; set; }
public bool? Append { get; set; } = false;
public string? Parameter { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter.CodeChange
{
public class Method
{
public string[]? Parameters { get; set; }
public CodeSnippet[]? CodeChanges { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.DotNet.MSIdentity.CodeReaderWriter.CodeChange;

namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
{
public class CodeModifierConfig
{
public string? Identifier { get; set; }
public CodeFile[]? Files { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ach project type/scenario should have a json config file for itself.

class CodeModifierConfig
{
public string? Identifier { get; set; } --> identifier for project type/scenario.
public File[]? Files { get; set; } --> All the files that need editing in the project.
}

class File
{
public Dictionary<string, Method>? Methods { get; set; } --> all the methods in the file that need editing
public string[]? Usings { get; set; } --> any `usings` that need to be added to a C# file.
public string? FileName { get; set; } --> .cs file to edit
public string[]? ClassProperties { get; set; } --> any properties that need to be added to the class' members.
public string[]? ClassAttributes { get; set; } --> [Attribute] that need to added to the class.
}

class Method
{
public string[]? Parameters { get; set; } --> parameter types for the method. Used to get the correct ParameterSyntax.
public CodeChange[]? CodeChanges { get; set; } --> All the changes within a particular method.
}
class CodeChange
{
public string? InsertAfter { get; set; } --> Insert new statement block after this statement syntax node.
public string? Block { get; set; } --> C# statement that is parsed using SyntaxFactory.ParseStatement
public string? Parent { get; set; } --> Add C# statement syntax node upon this parent statement syntax node based on Type
public string? Type { get; set; } --> CodeChangeType (below) string.
public bool? Append { get; set; } = false; --> Insert Block at the top of the method.
}

class CodeChangeType
{
public const string MemberAccess = nameof(MemberAccess); --> Add a SimpleMemberAccess expression to the parent statement syntax.
public const string InLambdaBlock = nameof(InBlock); --> Add in lambda block to the parent statement syntax.
}
This info is also available in CodeModifierConfig folder as well.

The scenarios below need to be supported for all project types :
| Scenario | Status|
| --- | --- |
| ASP .NET Core Web App | Config w/out layout(.cshtml) files |
| ASP .NET Core Web App (w/ B2C tenant) | Config w/out layout(.cshtml) files |
| ASP .NET Core Web Api | Config w/out layout(.cshtml) files |
| ASP .NET Core Web Api (w/ B2C tenant) | Config w/out layout(.cshtml) files |
| Blazor Server App | Need config |
| Blazor Server App (w/ B2C tenant) | Need config |
| Blazor WebAssembly App | Need config |
| Blazor WebAssembly App (w/ B2C tenant) | Need config |
| Blazor Hosted WebAssembly App | Need config |
| Blazor Hosted WebAssembly App (w/ B2C tenant) | Need config |

Blazor server updates-
- App.razor changes
+ Shared/LoginDisplay.razor
- Shared/MainLayout.razor changes
- Startup.cs changes
- appsettings.json changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"Identifier": "dotnet-blazorserver",
"Files": [
{
"FileName": "Startup.cs",
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
"CodeChanges": [
{
"InsertAfter": "IApplicationBuilder.UseRouting()",
"Block": "IApplicationBuilder.UseAuthentication()"
},
{
"InsertAfter": "IApplicationBuilder.UseAuthentication()",
"Block": "IApplicationBuilder.UseAuthorization()"
},
{
"Parent": "IApplicationBuilder.UseEndpoints",
"Type": "InLambdaBlock",
"Block": "endpoints.MapControllers()"
}
]
},
"ConfigureServices": {
"Parameters": [ "IServiceCollection" ],
"CodeChanges": [
{
"Block": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)"
},
{
"Block": "IServiceCollection.AddControllersWithViews().AddMicrosoftIdentityUI()",
"InsertAfter":"IApplicationBuilder.AddAuthentication()"
},
{
"Parent": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityWebApp(Configuration.GetSection(\"AzureAd\"))"
},
{
"Block": "IServiceCollection.AddAuthorization(options => { options.FallbackPolicy = options.DefaultPolicy; })"
},
{
"Block": "IServiceCollection.AddServerSideBlazor()",
"InsertAfter":"IServiceCollection.AddRazorPages()"
},
{
"Parent": "IServiceCollection.AddServerSideBlazor()",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityConsentHandler()"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authentication",
"Microsoft.Identity.Web",
"Microsoft.Identity.Web.UI",
"Microsoft.AspNetCore.Authentication.OpenIdConnect",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc.Authorization"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"Identifier": "dotnet-webapi",
"Files": [
{
"FileName": "Startup.cs",
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
"CodeChanges": [
{
"InsertAfter": "IApplicationBuilder.UseRouting()",
"Block": "IApplicationBuilder.UseAuthentication()"
}
]
},
"ConfigureServices": {
"Parameters": [ "IServiceCollection" ],
"CodeChanges": [
{
"Block": "IServiceCollection.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)"
},
{
"Parent": "IServiceCollection.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityWebApi(Configuration.GetSection(\"AzureAd\"));",
"BlockB2C": "AddMicrosoftIdentityWebApi(Configuration.GetSection(\"AzureAdB2C\"))"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authentication",
"Microsoft.Identity.Web",
"Microsoft.AspNetCore.Authentication.JwtBearer"
]
},
{
"FileName": "WeatherForecastController.cs",
"ClassAttributes": [ "Authorize" ],
"ClassProperties": [
"static readonly string[] scopeRequiredByApi = new string[] { \"access_as_user\" }"
],
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
"CodeChanges": [
{
"InsertAfter": "IApplicationBuilder.UseRouting()",
"Block": "IApplicationBuilder.UseAuthentication()"
}
]
},
"Get": {
"Parameters": [ "IEnumerable<WeatherForecast>" ],
"CodeChanges": [
{
"Append": true,
"Block": "HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authorization",
"Microsoft.Identity.Web.Resource"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"Identifier": "dotnet-webapp",
"Files": [
{
"FileName": "Startup.cs",
"Methods": {
"Configure": {
"Parameters": [ "IApplicationBuilder", "IWebHostEnvironment" ],
"CodeChanges": [
{
"InsertAfter": "IApplicationBuilder.UseRouting()",
"Block": "IApplicationBuilder.UseAuthentication()"
},
{
"Parent": "IApplicationBuilder.UseEndpoints",
"Type": "InLambdaBlock",
"InsertAfter": "endpoints.MapRazorPages()",
"Block": "endpoints.MapControllers()"
}
]
},
"ConfigureServices": {
"Parameters": [ "IServiceCollection" ],
"CodeChanges": [
{
"Block": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)"
},
{
"Parent": "IServiceCollection.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityWebApp(Configuration.GetSection(\"AzureAd\"));"
},
{
"Parent": "IServiceCollection.AddRazorPages()",
"Type": "MemberAccess",
"Block": "AddMvcOptions(options => {})"
},
{
"Block": "IServiceCollection.AddAuthorization",
"Type": "LambdaExpression",
"Parameter": "options"
},
{
"Parent": "services.AddAuthorization",
"Type": "InLambdaBlock",
"Block": "options.FallbackPolicy = options.DefaultPolicy"
},
{
"Parent": "IServiceCollection.AddRazorPages()",
"Type": "MemberAccess",
"Block": "AddMicrosoftIdentityUI()"
}
]
}
},
"Usings": [
"Microsoft.AspNetCore.Authentication",
"Microsoft.Identity.Web",
"Microsoft.Identity.Web.UI",
"Microsoft.AspNetCore.Authentication.OpenIdConnect",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc.Authorization"
]
}
]
}
Loading

0 comments on commit c49241c

Please sign in to comment.