-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
SoDBench.cs
754 lines (646 loc) · 28.6 KB
/
SoDBench.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
using CommandLine;
using CommandLine.Text;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace SoDBench
{
// A simple tree node for tracking file and directory names and sizes
// Does not have to accurately represent the true file system; only what we care about
class SizeReportingNode
{
public SizeReportingNode(string name, long? size=null, bool expand=true)
{
Name = name;
_size = size;
Expanded = expand;
}
public SizeReportingNode(FileInfo file, bool expand=true)
{
Name = file.Name;
_size = file.Length;
Expanded = expand;
}
// Builds out the tree starting from a directory
public SizeReportingNode(DirectoryInfo dir, int? reportingDepth=null)
{
Name = dir.Name;
foreach (var childDir in dir.EnumerateDirectories())
{
AddChild(new SizeReportingNode(childDir));
}
foreach (var childFile in dir.EnumerateFiles())
{
AddChild(new SizeReportingNode(childFile));
}
if (reportingDepth != null)
{
LimitReportingDepth(reportingDepth ?? 0);
}
}
// The directory containing this node
public SizeReportingNode Parent { get; set; }
// All the directories and files this node contains
public List<SizeReportingNode> Children {get; private set;} = new List<SizeReportingNode>();
// The file or directory name
public string Name { get; set; }
public bool Expanded { get; set; } = true;
// A list version of the path up to the root level we care about
public List<string> SegmentedPath {
get
{
if (Parent != null)
{
var path = Parent.SegmentedPath;
path.Add(Name);
return path;
}
return new List<string> { Name };
}
}
// The size of the file or directory
public long Size {
get
{
if (_size == null)
{
_size = 0;
foreach (var node in Children)
{
_size += node.Size;
}
}
return _size ?? 0;
}
private set
{
_size = value;
}
}
// Add the adoptee node as a child and set the adoptee's parent
public void AddChild(SizeReportingNode adoptee)
{
Children.Add(adoptee);
adoptee.Parent = this;
_size = null;
}
public void LimitReportingDepth(int depth)
{
if (depth <= 0)
{
Expanded = false;
}
foreach (var childNode in Children)
{
childNode.LimitReportingDepth(depth-1);
}
}
// Return a CSV formatted string representation of the tree
public string FormatAsCsv()
{
return FormatAsCsv(new StringBuilder()).ToString();
}
// Add to the string build a csv formatted representation of the tree
public StringBuilder FormatAsCsv(StringBuilder builder)
{
string path = String.Join(",", SegmentedPath.Select(s => Csv.Escape(s)));
builder.AppendLine($"{path},{Size}");
if (Expanded)
{
foreach (var childNode in Children)
{
childNode.FormatAsCsv(builder);
}
}
return builder;
}
private long? _size = null;
}
class Program
{
public static readonly string NugetConfig =
@"<?xml version='1.0' encoding='utf-8'?>
<configuration>
<packageSources>
<add key='dotnet-public' value='https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json' protocolVersion='3' />
<add key='myget-legacy' value='https://pkgs.dev.azure.com/dnceng/public/_packaging/myget-legacy/nuget/v3/index.json' protocolVersion='3' />
</packageSources>
</configuration>";
public static readonly string[] NewTemplates = new string[] {
"console",
"classlib",
"mstest",
"xunit",
"web",
"mvc",
"razor",
"webapi",
"nugetconfig",
"webconfig",
"sln",
"page",
"viewimports",
"viewstart"
};
public static readonly string[] OperatingSystems = new string[] {
"win10-x64",
"win10-x86",
"ubuntu.16.10-x64",
"rhel.7-x64"
};
static FileInfo s_dotnetExe;
static DirectoryInfo s_sandboxDir;
static DirectoryInfo s_fallbackDir;
static DirectoryInfo s_corelibsDir;
static bool s_keepArtifacts;
static string s_targetArchitecture;
static string s_dotnetChannel;
static void Main(string[] args)
{
try
{
var options = SoDBenchOptions.Parse(args);
s_targetArchitecture = options.TargetArchitecture;
s_dotnetChannel = options.DotnetChannel;
s_keepArtifacts = options.KeepArtifacts;
if (!String.IsNullOrWhiteSpace(options.DotnetExecutable))
{
s_dotnetExe = new FileInfo(options.DotnetExecutable);
}
if (s_sandboxDir == null)
{
// Truncate the Guid used for anti-collision because a full Guid results in expanded paths over 260 chars (the Windows max)
s_sandboxDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), $"sod{Guid.NewGuid().ToString().Substring(0,13)}"));
s_sandboxDir.Create();
Console.WriteLine($"** Running inside sandbox directory: {s_sandboxDir}");
}
if (s_dotnetExe == null)
{
if(!String.IsNullOrEmpty(options.CoreLibrariesDirectory))
{
Console.WriteLine($"** Using core libraries found at {options.CoreLibrariesDirectory}");
s_corelibsDir = new DirectoryInfo(options.CoreLibrariesDirectory);
}
else
{
var coreroot = Environment.GetEnvironmentVariable("CORE_ROOT");
if (!String.IsNullOrEmpty(coreroot) && Directory.Exists(coreroot))
{
Console.WriteLine($"** Using core libraries from CORE_ROOT at {coreroot}");
s_corelibsDir = new DirectoryInfo(coreroot);
}
else
{
Console.WriteLine("** Using default dotnet-cli core libraries");
}
}
PrintHeader("** Installing Dotnet CLI");
s_dotnetExe = SetupDotnet();
}
if (s_fallbackDir == null)
{
s_fallbackDir = new DirectoryInfo(Path.Combine(s_sandboxDir.FullName, "fallback"));
s_fallbackDir.Create();
}
Console.WriteLine($"** Path to dotnet executable: {s_dotnetExe.FullName}");
PrintHeader("** Starting acquisition size test");
var acquisition = GetAcquisitionSize();
PrintHeader("** Running deployment size test");
var deployment = GetDeploymentSize();
var root = new SizeReportingNode("Dotnet Total");
root.AddChild(acquisition);
root.AddChild(deployment);
var formattedStr = root.FormatAsCsv();
File.WriteAllText(options.OutputFilename, formattedStr);
if (options.Verbose)
Console.WriteLine($"** CSV Output:\n{formattedStr}");
}
finally
{
if (!s_keepArtifacts && s_sandboxDir != null)
{
PrintHeader("** Cleaning up sandbox directory");
DeleteDirectory(s_sandboxDir);
s_sandboxDir = null;
}
}
}
private static void PrintHeader(string message)
{
Console.WriteLine();
Console.WriteLine("**********************************************************************");
Console.WriteLine($"** {message}");
Console.WriteLine("**********************************************************************");
}
private static SizeReportingNode GetAcquisitionSize()
{
var result = new SizeReportingNode("Acquisition Size");
// Arbitrary command to trigger first time setup
ProcessStartInfo dotnet = new ProcessStartInfo()
{
WorkingDirectory = s_sandboxDir.FullName,
FileName = s_dotnetExe.FullName,
Arguments = "new"
};
// Used to set where the packages will be unpacked to.
// There is a no guarantee that this is a stable method, but is the only way currently to set the fallback folder location
dotnet.Environment["DOTNET_CLI_TEST_FALLBACKFOLDER"] = s_fallbackDir.FullName;
LaunchProcess(dotnet, 180000);
Console.WriteLine("\n** Measuring total size of acquired files");
result.AddChild(new SizeReportingNode(s_fallbackDir, 1));
var dotnetNode = new SizeReportingNode(s_dotnetExe.Directory);
var reportingDepths = new Dictionary<string, int>
{
{"additionalDeps", 1},
{"host", 0},
{"sdk", 2},
{"shared", 2},
{"store", 3}
};
foreach (var childNode in dotnetNode.Children)
{
int depth = 0;
if (reportingDepths.TryGetValue(childNode.Name, out depth))
{
childNode.LimitReportingDepth(depth);
}
}
result.AddChild(dotnetNode);
return result;
}
private static SizeReportingNode GetDeploymentSize()
{
// Write the NuGet.Config file
var nugetConfFile = new FileInfo(Path.Combine(s_sandboxDir.FullName, "NuGet.Config"));
File.WriteAllText(nugetConfFile.FullName, NugetConfig);
var result = new SizeReportingNode("Deployment Size");
foreach (string template in NewTemplates)
{
var templateNode = new SizeReportingNode(template);
result.AddChild(templateNode);
foreach (var os in OperatingSystems)
{
Console.WriteLine($"\n\n** Deploying {template}/{os}");
var deploymentSandbox = new DirectoryInfo(Path.Combine(s_sandboxDir.FullName, template, os));
var publishDir = new DirectoryInfo(Path.Combine(deploymentSandbox.FullName, "publish"));
deploymentSandbox.Create();
ProcessStartInfo dotnetNew = new ProcessStartInfo()
{
FileName = s_dotnetExe.FullName,
Arguments = $"new {template}",
UseShellExecute = false,
WorkingDirectory = deploymentSandbox.FullName
};
dotnetNew.Environment["DOTNET_CLI_TEST_FALLBACKFOLDER"] = s_fallbackDir.FullName;
ProcessStartInfo dotnetRestore = new ProcessStartInfo()
{
FileName = s_dotnetExe.FullName,
Arguments = $"restore --runtime {os}",
UseShellExecute = false,
WorkingDirectory = deploymentSandbox.FullName
};
dotnetRestore.Environment["DOTNET_CLI_TEST_FALLBACKFOLDER"] = s_fallbackDir.FullName;
ProcessStartInfo dotnetPublish = new ProcessStartInfo()
{
FileName = s_dotnetExe.FullName,
// The UserSharedCompiler flag is set to false to prevent handles from being held that will later cause deletion of the installed SDK to fail.
Arguments = $"publish -c Release --runtime {os} --output {publishDir.FullName} /p:UseSharedCompilation=false /p:UseRazorBuildServer=false",
UseShellExecute = false,
WorkingDirectory = deploymentSandbox.FullName
};
dotnetPublish.Environment["DOTNET_CLI_TEST_FALLBACKFOLDER"] = s_fallbackDir.FullName;
try
{
LaunchProcess(dotnetNew, 180000);
if (deploymentSandbox.EnumerateFiles().Any(f => f.Name.EndsWith("proj")))
{
LaunchProcess(dotnetRestore, 180000);
LaunchProcess(dotnetPublish, 180000);
}
else
{
Console.WriteLine($"** {template} does not have a project file to restore or publish");
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
continue;
}
// If we published this project, only report it's published size
if (publishDir.Exists)
{
var publishNode = new SizeReportingNode(publishDir, 0);
publishNode.Name = deploymentSandbox.Name;
templateNode.AddChild(publishNode);
if (publishNode.Size <= 0) {
throw new InvalidOperationException($"{publishNode.Name} reports as invalid size {publishNode.Size}");
}
}
else
{
templateNode.AddChild(new SizeReportingNode(deploymentSandbox, 0));
}
}
}
return result;
}
private static void DownloadDotnetInstaller()
{
var psi = new ProcessStartInfo() {
WorkingDirectory = s_sandboxDir.FullName,
FileName = @"powershell.exe",
Arguments = $"-NoProfile wget https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.ps1 -OutFile Dotnet-Install.ps1"
};
LaunchProcess(psi, 180000);
}
private static void InstallSharedRuntime()
{
var psi = new ProcessStartInfo() {
WorkingDirectory = s_sandboxDir.FullName,
FileName = @"powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\Dotnet-Install.ps1 -Runtime dotnet -InstallDir .dotnet -Channel {s_dotnetChannel} -Architecture {s_targetArchitecture}"
};
LaunchProcess(psi, 180000);
}
private static void InstallDotnet()
{
var psi = new ProcessStartInfo() {
WorkingDirectory = s_sandboxDir.FullName,
FileName = @"powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\Dotnet-Install.ps1 -InstallDir .dotnet -Channel {s_dotnetChannel} -Architecture {s_targetArchitecture}"
};
LaunchProcess(psi, 180000);
}
private static void ModifySharedFramework()
{
// Current working directory is the <coreclr repo root>/sandbox directory.
Console.WriteLine($"** Modifying the shared framework.");
var sourcedi = s_corelibsDir;
// Get the directory containing the newest version of Microsodt.NETCore.App libraries
var targetdi = new DirectoryInfo(
new DirectoryInfo(Path.Combine(s_sandboxDir.FullName, ".dotnet", "shared", "Microsoft.NETCore.App"))
.GetDirectories("*")
.OrderBy(s => s.Name)
.Last()
.FullName);
Console.WriteLine($"| Source : {sourcedi.FullName}");
Console.WriteLine($"| Target : {targetdi.FullName}");
var compiledBinariesOfInterest = new string[] {
"clretwrc.dll",
"clrjit.dll",
"coreclr.dll",
"mscordaccore.dll",
"mscordbi.dll",
"mscorrc.dll",
"sos.dll",
"SOS.NETCore.dll",
"System.Private.CoreLib.dll"
};
foreach (var compiledBinaryOfInterest in compiledBinariesOfInterest)
{
foreach (FileInfo fi in targetdi.GetFiles(compiledBinaryOfInterest))
{
var sourceFilePath = Path.Combine(sourcedi.FullName, fi.Name);
var targetFilePath = Path.Combine(targetdi.FullName, fi.Name);
if (File.Exists(sourceFilePath))
{
File.Copy(sourceFilePath, targetFilePath, true);
Console.WriteLine($"| Copied file - '{fi.Name}'");
}
}
}
}
private static FileInfo SetupDotnet()
{
DownloadDotnetInstaller();
InstallSharedRuntime();
InstallDotnet();
if (s_corelibsDir != null)
{
ModifySharedFramework();
}
var dotnetExe = new FileInfo(Path.Combine(s_sandboxDir.FullName, ".dotnet", "dotnet.exe"));
Debug.Assert(dotnetExe.Exists);
return dotnetExe;
}
private static void LaunchProcess(ProcessStartInfo processStartInfo, int timeoutMilliseconds, IDictionary<string, string> environment = null)
{
Console.WriteLine();
Console.WriteLine($"{System.Security.Principal.WindowsIdentity.GetCurrent().Name}@{Environment.MachineName} \"{processStartInfo.WorkingDirectory}\"");
Console.WriteLine($"[{DateTime.Now}] $ {processStartInfo.FileName} {processStartInfo.Arguments}");
if (environment != null)
{
foreach (KeyValuePair<string, string> pair in environment)
{
if (!processStartInfo.Environment.ContainsKey(pair.Key))
processStartInfo.Environment.Add(pair.Key, pair.Value);
else
processStartInfo.Environment[pair.Key] = pair.Value;
}
}
using (var p = new Process() { StartInfo = processStartInfo })
{
p.Start();
if (p.WaitForExit(timeoutMilliseconds) == false)
{
// FIXME: What about clean/kill child processes?
p.Kill();
throw new TimeoutException($"The process '{processStartInfo.FileName} {processStartInfo.Arguments}' timed out.");
}
if (p.ExitCode != 0)
throw new Exception($"{processStartInfo.FileName} exited with error code {p.ExitCode}");
}
}
/// <summary>
/// Provides an interface to parse the command line arguments passed to the SoDBench.
/// </summary>
private sealed class SoDBenchOptions
{
public SoDBenchOptions() { }
private static string NormalizePath(string path)
{
if (String.IsNullOrWhiteSpace(path))
throw new InvalidOperationException($"'{path}' is an invalid path: cannot be null or whitespace");
if (path.Any(c => Path.GetInvalidPathChars().Contains(c)))
throw new InvalidOperationException($"'{path}' is an invalid path: contains invalid characters");
return Path.IsPathRooted(path) ? path : Path.GetFullPath(path);
}
[Option('o', Required = false, HelpText = "Specifies the output file name for the csv document")]
public string OutputFilename
{
get { return _outputFilename; }
set
{
_outputFilename = NormalizePath(value);
}
}
[Option("dotnet", Required = false, HelpText = "Specifies the location of dotnet cli to use.")]
public string DotnetExecutable
{
get { return _dotnetExe; }
set
{
_dotnetExe = NormalizePath(value);
}
}
[Option("corelibs", Required = false, HelpText = "Specifies the location of .NET Core libraries to patch into dotnet. Cannot be used with --dotnet")]
public string CoreLibrariesDirectory
{
get { return _corelibsDir; }
set
{
_corelibsDir = NormalizePath(value);
}
}
[Option("architecture", Required = false, Default = "x64", HelpText = "JitBench target architecture (It must match the built product that was copied into sandbox).")]
public string TargetArchitecture { get; set; }
[Option("channel", Required = false, Default = "master", HelpText = "Specifies the channel to use when installing the dotnet-cli")]
public string DotnetChannel { get; set; }
[Option('v', Required = false, HelpText = "Sets output to verbose")]
public bool Verbose { get; set; }
[Option("keep-artifacts", Required = false, HelpText = "Specifies that artifacts of this run should be kept")]
public bool KeepArtifacts { get; set; }
public static SoDBenchOptions Parse(string[] args)
{
using (var parser = new Parser((settings) => {
settings.CaseInsensitiveEnumValues = true;
settings.CaseSensitive = false;
settings.HelpWriter = new StringWriter();
settings.IgnoreUnknownArguments = true;
}))
{
SoDBenchOptions options = null;
parser.ParseArguments<SoDBenchOptions>(args)
.WithParsed(parsed => options = parsed)
.WithNotParsed(errors => {
foreach (Error error in errors)
{
switch (error.Tag)
{
case ErrorType.MissingValueOptionError:
throw new ArgumentException(
$"Missing value option for command line argument '{(error as MissingValueOptionError).NameInfo.NameText}'");
case ErrorType.HelpRequestedError:
Console.WriteLine(Usage());
Environment.Exit(0);
break;
case ErrorType.VersionRequestedError:
Console.WriteLine(new AssemblyName(typeof(SoDBenchOptions).GetTypeInfo().Assembly.FullName).Version);
Environment.Exit(0);
break;
case ErrorType.BadFormatTokenError:
case ErrorType.UnknownOptionError:
case ErrorType.MissingRequiredOptionError:
case ErrorType.MutuallyExclusiveSetError:
case ErrorType.BadFormatConversionError:
case ErrorType.SequenceOutOfRangeError:
case ErrorType.RepeatedOptionError:
case ErrorType.NoVerbSelectedError:
case ErrorType.BadVerbSelectedError:
case ErrorType.HelpVerbRequestedError:
break;
}
}
});
if (options != null && !String.IsNullOrEmpty(options.DotnetExecutable) && !String.IsNullOrEmpty(options.CoreLibrariesDirectory))
{
throw new ArgumentException("--dotnet and --corlibs cannot be used together");
}
return options;
}
}
public static string Usage()
{
var parser = new Parser((parserSettings) =>
{
parserSettings.CaseInsensitiveEnumValues = true;
parserSettings.CaseSensitive = false;
parserSettings.EnableDashDash = true;
parserSettings.HelpWriter = new StringWriter();
parserSettings.IgnoreUnknownArguments = true;
});
var helpTextString = new HelpText
{
AddDashesToOption = true,
AddEnumValuesToHelpText = true,
AdditionalNewLineAfterOption = false,
Heading = "SoDBench",
MaximumDisplayWidth = 80,
}.AddOptions(parser.ParseArguments<SoDBenchOptions>(new string[] { "--help" })).ToString();
return helpTextString;
}
private string _dotnetExe;
private string _corelibsDir;
private string _outputFilename = "measurement.csv";
}
private static void DeleteDirectory(DirectoryInfo dir, uint maxWait=10000)
{
foreach (var subdir in dir.GetDirectories())
{
DeleteDirectory(subdir);
}
// Give it time to actually delete all the files
var files = dir.GetFiles();
bool wait = true;
uint waitTime = 0;
while (wait)
{
wait = false;
foreach (var f in files)
{
if (File.Exists(f.FullName))
{
try
{
File.Delete(f.FullName);
}
catch (IOException) { if (waitTime > maxWait) throw; }
catch (UnauthorizedAccessException) { if (waitTime > maxWait) throw; }
if (File.Exists(f.FullName))
{
wait = true;
// Print a message every 3 seconds if the thread is stuck
if (waitTime != 0 && waitTime % 3000 == 0)
{
Console.WriteLine($"Waiting to delete {f.FullName}");
}
}
}
}
// Try again in 100ms
if (wait)
{
Thread.Sleep(100);
waitTime += 100;
}
}
Directory.Delete(dir.FullName);
}
}
// A simple class for escaping strings for CSV writing
// https://stackoverflow.com/a/769713
// Used instead of a package because only these < 20 lines of code are needed
public static class Csv
{
public static string Escape( string s )
{
if ( s.Contains( QUOTE ) )
s = s.Replace( QUOTE, ESCAPED_QUOTE );
if ( s.IndexOfAny( CHARACTERS_THAT_MUST_BE_QUOTED ) > -1 )
s = QUOTE + s + QUOTE;
return s;
}
private const string QUOTE = "\"";
private const string ESCAPED_QUOTE = "\"\"";
private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
}
}