|
| 1 | +using System.CommandLine.Invocation; |
| 2 | +using System.CommandLine.IO; |
| 3 | +using Dax.Metadata; |
| 4 | +using Dax.Model.Extractor; |
| 5 | +using Dax.Vpax.Tools; |
| 6 | +using static Dax.Vpax.CLI.Commands.ExportCommandOptions; |
| 7 | + |
| 8 | +namespace Dax.Vpax.CLI.Commands; |
| 9 | + |
| 10 | +internal sealed class ExportCommandHandler : ICommandHandler |
| 11 | +{ |
| 12 | + public int Invoke(InvocationContext context) => throw new NotSupportedException("Use InvokeAsync instead."); |
| 13 | + |
| 14 | + public async Task<int> InvokeAsync(InvocationContext context) |
| 15 | + { |
| 16 | + // TODO: forward cancellation token to vertipaq-analyzer extractor |
| 17 | + var cancellationToken = context.GetCancellationToken(); |
| 18 | + var extractorAppName = ThisAssembly.AssemblyName; |
| 19 | + var extractorAppVersion = ThisAssembly.AssemblyFileVersion; |
| 20 | + |
| 21 | + var path = context.ParseResult.GetValueForArgument(PathArgument); |
| 22 | + var connectionString = context.ParseResult.GetValueForArgument(ConnectionStringArgument); |
| 23 | + var overwrite = context.ParseResult.GetValueForOption(OverwriteOption); |
| 24 | + var excludeTom = context.ParseResult.GetValueForOption(ExcludeTomOption); |
| 25 | + var excludeVpa = context.ParseResult.GetValueForOption(ExcludeVpaOption); |
| 26 | + |
| 27 | + using var vpaxStream = new MemoryStream(); |
| 28 | + |
| 29 | + // TODO: improve logging and support platform-specific commands such as those used in Azure DevOps and GitHub |
| 30 | + context.Console.Out.Write("Extracting VPAX metadata..."); |
| 31 | + { |
| 32 | + var daxModel = TomExtractor.GetDaxModel( |
| 33 | + connectionString: connectionString, |
| 34 | + applicationName: extractorAppName, |
| 35 | + applicationVersion: extractorAppVersion, |
| 36 | + readStatisticsFromData: true, |
| 37 | + sampleRows: 0, // RI violation sampling is not applicable to VPAX files |
| 38 | + analyzeDirectQuery: true, |
| 39 | + analyzeDirectLake: DirectLakeExtractionMode.Full |
| 40 | + ); |
| 41 | + |
| 42 | + var vpaModel = excludeVpa ? null : new ViewVpaExport.Model(daxModel); |
| 43 | + var tomDatabase = excludeTom ? null : TomExtractor.GetDatabase(connectionString); |
| 44 | + |
| 45 | + VpaxTools.ExportVpax(vpaxStream, daxModel, vpaModel, tomDatabase); |
| 46 | + } |
| 47 | + context.Console.Out.WriteLine("done."); |
| 48 | + context.Console.Out.Write("Exporting VPAX file..."); |
| 49 | + { |
| 50 | + var mode = overwrite ? FileMode.Create : FileMode.CreateNew; |
| 51 | + using var fileStream = new FileStream(path, mode, FileAccess.Write, FileShare.Read); |
| 52 | + await vpaxStream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false); |
| 53 | + } |
| 54 | + context.Console.Out.WriteLine("done."); |
| 55 | + |
| 56 | + return context.ExitCode; |
| 57 | + } |
| 58 | +} |
0 commit comments