-
Notifications
You must be signed in to change notification settings - Fork 27
/
ConvertTranscriptHandler.cs
78 lines (66 loc) · 2.88 KB
/
ConvertTranscriptHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
namespace Microsoft.Bot.Builder.Testing.TranscriptConverter
{
public class ConvertTranscriptHandler
{
/// <summary>
/// Creates the convert command.
/// </summary>
/// <returns>The created command.</returns>
public Command Create()
{
var cmd = new Command("convert", "Converts transcript files into test script to be executed by the TranscriptTestRunner");
cmd.AddArgument(new Argument<string>("source"));
cmd.AddOption(new Option<string>("--target", "Path to the target test script file."));
cmd.Handler = CommandHandler.Create<string, string>((source, target) =>
{
try
{
var rootStopwatch = new Stopwatch();
var stopwatch = new Stopwatch();
rootStopwatch.Start();
Console.WriteLine("Reading TranScript file\n - Path: {0}", source);
var testScript = Converter.ConvertTranscript(source);
stopwatch.Start();
var targetPath = string.IsNullOrEmpty(target) ? source.Replace(".transcript", ".json", StringComparison.InvariantCulture) : target;
WriteTestScript(testScript, targetPath);
stopwatch.Stop();
Console.WriteLine("Saving TestScript file ({0}ms)\n - Path: {1}", stopwatch.ElapsedMilliseconds, targetPath);
rootStopwatch.Stop();
Console.WriteLine("\nFinished processing ({0}ms)", rootStopwatch.ElapsedMilliseconds);
}
catch (FileNotFoundException e)
{
Console.WriteLine("{0}: {1}", "Error", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("{0}: {1}", "Error", e.Message);
}
});
return cmd;
}
/// <summary>
/// Writes the test script content to the path set in the target argument.
/// </summary>
private static void WriteTestScript(TestScript testScript, string targetScript)
{
var json = JsonConvert.SerializeObject(
testScript,
Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
using var streamWriter = new StreamWriter(Path.GetFullPath(targetScript));
streamWriter.Write(json);
}
}
}