Skip to content

Commit 121e608

Browse files
Afroz Mohammedafroz429
authored andcommitted
added generate report only option in the generator
1 parent f028859 commit 121e608

File tree

7 files changed

+143
-26
lines changed

7 files changed

+143
-26
lines changed

buildtools/Build.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,37 @@ try {
125125
else {
126126
throw "ERROR: Preview build is missing specific SDK artifacts."
127127
}
128+
129+
# during preview, if there is overrides.xml, use the overrides.xml and generate auto-configuration for any missing operations that don't have errors.
130+
# if buildconfig is reset, then overrides.xml will have empty config and a new report.xml will be generated.
131+
# the report.xml will have all the operations that were in overrides.xml including new generated operations.
132+
# if the report.xml is generated, rename it to overrides.xml so that auto-configured operations are included during cmdlet generation.
133+
try {
134+
Write-Host "Generating report.xml"
135+
# build-report-only generates report.xml only when there is a new operation and
136+
# there are no errors during auto-generation of the buildconfig
137+
dotnet msbuild ./buildtools/build.proj /t:build-report-only `
138+
/p:CleanSdkReferences=false `
139+
/p:BreakOnNewOperations=false `
140+
/p:Configuration=$Configuration
141+
}
142+
catch {
143+
# don't error out. The same failure would be caught in the later step and reported.
144+
Write-Host "failed generating report.xml"
145+
Write-Host $_.Exception.Message
146+
}
147+
148+
if (Test-Path 'report.xml') {
149+
Write-Host "report.xml generated. renaming report.xml to overrides.xml"
150+
if (Test-Path 'overrides.xml') {
151+
Remove-Item 'overrides.xml'
152+
}
153+
Rename-Item -Path 'report.xml' -NewName 'overrides.xml'
154+
}
155+
else {
156+
Write-Host "report.xml not generated"
157+
}
158+
128159

129160
dotnet msbuild ./buildtools/build.proj /t:preview-build `
130161
/p:RunTests=$RunTests `
@@ -159,6 +190,36 @@ try {
159190
throw "ERROR: Release build is missing specific SDK artifacts."
160191
}
161192

193+
# during release/dryrun, if there is overrides.xml, use the overrides.xml and generate auto-configuration for any missing operations that don't have errors.
194+
# the report.xml will have all the operations that were in overrides.xml including new generated operations.
195+
# if the report.xml is generated, rename it to overrides.xml so that auto-configured operations are included during cmdlet generation.
196+
try {
197+
Write-Host "Generating report.xml"
198+
# build-report-only generates report.xml only when there is a new operation and
199+
# there are no errors during auto-generation of the buildconfig
200+
dotnet msbuild ./buildtools/build.proj /t:build-report-only `
201+
/p:CleanSdkReferences=false `
202+
/p:BreakOnNewOperations=false `
203+
/p:Configuration=$Configuration
204+
}
205+
catch {
206+
# don't error out. The same failure would be caught in the later step and reported.
207+
Write-Host "failed generating report.xml"
208+
Write-Host $_.Exception.Message
209+
}
210+
211+
if (Test-Path 'report.xml') {
212+
Write-Host "report.xml generated. renaming report.xml to overrides.xml"
213+
if (Test-Path 'overrides.xml') {
214+
Remove-Item 'overrides.xml'
215+
}
216+
Rename-Item -Path 'report.xml' -NewName 'overrides.xml'
217+
}
218+
else {
219+
Write-Host "report.xml not generated"
220+
}
221+
222+
162223
if ($RunAsStagingBuild -eq 'true') {
163224
msbuild ./buildtools/build.proj `
164225
/t:staging-build `

buildtools/build.proj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
<Message Text="Generates all cmdlets and then builds AWSPowerShell.NetCore deployment artifacts and powershell help files."/>
7676
</Target>
7777

78+
<Target Name="build-report-only" DependsOnTargets="clean-deployment;build-generator">
79+
<Message Text="Generates report.xml only when there is a new operation and there are no errors during auto-generation of the buildconfig."/>
80+
<Exec Command="dotnet $(GeneratorPath)/$(Generator) -sdk $(SDKAssembliesFolder) -rp $(RootPath) -t cmdlets -cnc true -scg false -bno false -ro true -vn $(PatchNumber)" />
81+
</Target>
82+
7883
<Target Name="staging-build" DependsOnTargets="clean-bin;clean-deployment;clean-sdk-references;build-generator;create-cmdlets">
7984
<Message Text="Generates and then builds all cmdlets and PowerShell deployment artifacts."/>
8085
</Target>

generator/AWSPSGenerator/CommandLineParser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,16 @@ class ArgDeclaration
251251
HasValue = true,
252252
Parse = (arguments, argValue) => arguments.ParsedOptions.VersionNumber = argValue,
253253
HelpText = "The four-components version number e.g. 4.0.0.0."
254+
},
255+
new ArgDeclaration
256+
{
257+
OptionName = "reportonly",
258+
ShortName = "ro",
259+
HasValue = true,
260+
Parse = (arguments, argValue) => arguments.ParsedOptions.GenerateReportOnly = bool.Parse(argValue),
261+
HelpText = "Only generate report.xml if there are operations not in config and no errors."
254262
}
263+
255264
};
256265

257266
static readonly char[] ArgumentPrefixes = { '-', '/' };

generator/AWSPSGeneratorLib/ConfigModel/XmlReportWriter.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace AWSPowerShellGenerator.ServiceConfig
1414
{
1515
class XmlReportWriter
1616
{
17-
public static void SerializeReport(string folderPath, IEnumerable<ConfigModel> models)
17+
public static void SerializeReport(string folderPath, IEnumerable<ConfigModel> models, bool generateReportOnly)
1818
{
1919
if (!Directory.Exists(folderPath))
2020
{
@@ -39,6 +39,11 @@ public static void SerializeReport(string folderPath, IEnumerable<ConfigModel> m
3939
overrides.ContainsKey(configModel.C2jFilename) ||
4040
configModel.ServiceOperationsList.Where(op => op.IsAutoConfiguring || op.AnalysisErrors.Any()).Any())
4141
.ToArray();
42+
bool hasErrors = models.Any(configModel =>
43+
configModel.AnalysisErrors.Any() ||
44+
configModel.ServiceOperationsList.Any(op => op.AnalysisErrors.Any()));
45+
46+
bool hasNewOperations = models.Any(model => model.ServiceOperationsList.Any(op => op.IsAutoConfiguring));
4247

4348
var doc = new XDocument();
4449

@@ -179,7 +184,19 @@ public static void SerializeReport(string folderPath, IEnumerable<ConfigModel> m
179184
}
180185
}
181186

182-
doc.Save(filename);
187+
if (!generateReportOnly)
188+
{
189+
doc.Save(filename);
190+
}
191+
else if (hasNewOperations && !hasErrors)
192+
{
193+
Console.WriteLine("New operations were auto-configured without errors and saved in report.xml");
194+
doc.Save(filename);
195+
}
196+
else
197+
{
198+
Console.WriteLine($"Skipping saving report: hasNewOperations:{hasNewOperations}, hasErrors: {hasErrors} ");
199+
}
183200
}
184201
catch (Exception e)
185202
{

generator/AWSPSGeneratorLib/Generator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void Execute(GeneratorOptions options)
7676

7777
if (options.ShouldRunTask(GeneratorTasknames.GenerateCmdlets))
7878
{
79-
Console.WriteLine("Executing task 'GenerateCmdlets'");
79+
Console.WriteLine($"Executing task 'GenerateCmdlets' {(options.GenerateReportOnly ? "with reportonly option" : "")}");
8080

8181
var cmdletGenerator = new CmdletGenerator
8282
{

generator/AWSPSGeneratorLib/GeneratorOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public class GeneratorOptions
3737
/// </summary>
3838
public bool CreateNewCmdlets { get; set; } = true;
3939

40+
/// <summary>
41+
/// If true, report.xml is generated when there is a new operation and
42+
/// there are no errors during auto-generation of the buildconfig
43+
/// When this is set, the generator won't throw exception for any analysis errors.
44+
/// The cmdlets will not be generated when this is true.
45+
/// </summary>
46+
public bool GenerateReportOnly { get; set; } = false;
47+
4048
/// <summary>
4149
/// If true (the default is false), the build will fail if new operations are
4250
/// present in the SDK. Set to true for release builds when all configurations
@@ -196,6 +204,7 @@ public GeneratorOptions(GeneratorOptions rhs)
196204
CNNorth1RegionDocsDomain = rhs.CNNorth1RegionDocsDomain;
197205
Edition = rhs.Edition;
198206
SkipCmdletGeneration = rhs.SkipCmdletGeneration;
207+
GenerateReportOnly = rhs.GenerateReportOnly;
199208
}
200209
}
201210
}

generator/AWSPSGeneratorLib/Generators/CmdletGenerator.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected override void GenerateHelper()
207207

208208
CheckForServicePrefixDuplication();
209209

210-
if (!Options.SkipCmdletGeneration)
210+
if (!Options.SkipCmdletGeneration || Options.GenerateReportOnly)
211211
{
212212
//We clean and setup service folders, some services share a folder, project and module, so we use distinct
213213
foreach (var project in ModelCollection.ConfigModels.Values
@@ -238,7 +238,7 @@ protected override void GenerateHelper()
238238
LoadCurrentService(CurrentModel);
239239
DocumentationUtils.CacheMemberDocumentationSummary(CurrentServiceNDoc);
240240

241-
if (!Options.SkipCmdletGeneration)
241+
if (!Options.SkipCmdletGeneration || Options.GenerateReportOnly)
242242
{
243243
GenerateClientAndCmdlets();
244244
// if the service contains any hand-maintained cmdlets, scan them to update the
@@ -259,7 +259,11 @@ protected override void GenerateHelper()
259259
var allFoundSdkAssemblies = GenerationSources.SDKFindAssemblyFilenames(SdkAssembliesFolder, Directory.EnumerateFiles);
260260
VerifyAllAssembliesHaveConfiguration(SdkVersionsUtils.ReadSdkVersionFile(SdkAssembliesFolder), ModelCollection, allFoundSdkAssemblies);
261261

262-
if (!Options.SkipCmdletGeneration)
262+
if (Options.GenerateReportOnly)
263+
{
264+
WriteConfigurationChanges();
265+
}
266+
else if (!Options.SkipCmdletGeneration)
263267
{
264268
ScanAdvancedCmdletsForCommonModule();
265269

@@ -291,6 +295,15 @@ protected override void GenerateHelper()
291295

292296
foreach (var configModel in ModelCollection.ConfigModels.Values)
293297
{
298+
// if Options.GenerateReportOnly, clear AnalysisErrors at model level and ServiceOperations Level
299+
if (Options.GenerateReportOnly)
300+
{
301+
configModel.AnalysisErrors.Clear();
302+
foreach (var operation in configModel.ServiceOperationsList)
303+
{
304+
operation.AnalysisErrors.Clear();
305+
}
306+
}
294307
foreach (var error in configModel.AnalysisErrors.Concat(
295308
configModel.ServiceOperationsList
296309
.OrderBy(so => so.MethodName)
@@ -360,7 +373,7 @@ public static void VerifyAllAssembliesHaveConfiguration(JObject sdkVersionsJson,
360373

361374
private void WriteConfigurationChanges()
362375
{
363-
XmlReportWriter.SerializeReport(Options.RootPath, ModelCollection.ConfigModels.Values);
376+
XmlReportWriter.SerializeReport(Options.RootPath, ModelCollection.ConfigModels.Values, Options.GenerateReportOnly);
364377
}
365378

366379
private void CheckForServicePrefixDuplication()
@@ -445,29 +458,32 @@ private void GenerateClientAndCmdlets()
445458

446459
var outputRoot = Path.Combine(CmdletsOutputPath, CurrentModel.AssemblyName);
447460

448-
try
461+
if (!Options.GenerateReportOnly)
449462
{
450-
using (var sw = new StringWriter())
463+
try
451464
{
452-
// if the service has operations requiring anonymous access, we'll generate two clients
453-
// one for regular authenticated calls and one using anonymous credentials
454-
using (var writer = new IndentedTextWriter(sw))
465+
using (var sw = new StringWriter())
455466
{
456-
CmdletServiceClientWriter.Write(writer,
457-
CurrentModel,
458-
CurrentModel.ServiceName,
459-
GetServiceVersion(CurrentModel.ServiceNamespace, CurrentModel.ServiceClient),
460-
awsSignerAttributeTypeValue);
461-
}
467+
// if the service has operations requiring anonymous access, we'll generate two clients
468+
// one for regular authenticated calls and one using anonymous credentials
469+
using (var writer = new IndentedTextWriter(sw))
470+
{
471+
CmdletServiceClientWriter.Write(writer,
472+
CurrentModel,
473+
CurrentModel.ServiceName,
474+
GetServiceVersion(CurrentModel.ServiceNamespace, CurrentModel.ServiceClient),
475+
awsSignerAttributeTypeValue);
476+
}
462477

463-
var fileContents = sw.ToString();
464-
var fileName = CurrentModel.GetServiceCmdletClassName(false) + "Cmdlet.cs";
465-
File.WriteAllText(Path.Combine(outputRoot, fileName), fileContents);
478+
var fileContents = sw.ToString();
479+
var fileName = CurrentModel.GetServiceCmdletClassName(false) + "Cmdlet.cs";
480+
File.WriteAllText(Path.Combine(outputRoot, fileName), fileContents);
481+
}
482+
}
483+
catch (Exception e)
484+
{
485+
AnalysisError.ExceptionWhileWritingServiceClientCode(CurrentModel, e);
466486
}
467-
}
468-
catch (Exception e)
469-
{
470-
AnalysisError.ExceptionWhileWritingServiceClientCode(CurrentModel, e);
471487
}
472488

473489
// process the methods in order to make debugging more convenient
@@ -631,7 +647,7 @@ private void CreateCmdlet(MethodInfo method, ConfigModel configModel, string aws
631647
AnalysisError.ExceptionWhileAnalyzingSDKLibrary(CurrentModel, CurrentOperation, e);
632648
}
633649

634-
if (serviceOperation.AnalysisErrors.Count == 0)
650+
if (serviceOperation.AnalysisErrors.Count == 0 && !Options.GenerateReportOnly)
635651
{
636652
// set file name and location
637653
var filePath = Path.Combine(configModel.AssemblyName, GeneratedCmdletsFoldername, $"{serviceOperation.SelectedVerb}-{serviceOperation.SelectedNoun}-Cmdlet.cs");

0 commit comments

Comments
 (0)