-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulkImportInputGenerator.cs
82 lines (73 loc) · 2.79 KB
/
BulkImportInputGenerator.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
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ADTBulkImportConverter
{
public class BulkImportInputGenerator
{
public void GenerateBulkModelImportInputFile(string inputPath, string outputFileName)
{
if (string.IsNullOrWhiteSpace(inputPath))
{
throw new ArgumentException("Input path is not valid");
}
if (string.IsNullOrWhiteSpace(outputFileName))
{
outputFileName = "output.ndjson";
}
if (!Directory.Exists(inputPath))
{
throw new ArgumentException("Input path must be a valid directory");
}
using (var inputStream = new MemoryStream())
{
using (var inputStreamWriter = new StreamWriter(inputStream))
{
ProcessDirectory(inputPath, inputStreamWriter);
inputStream.Position = 0;
using (var outputStreamWriter = new StreamWriter(outputFileName))
{
outputStreamWriter.WriteLine(@"{""Section"": ""Header""}");
outputStreamWriter.WriteLine(@"{""fileVersion"": ""1.0.0"", ""author"": ""foobar"", ""organization"": ""contoso""}");
outputStreamWriter.WriteLine(@"{""Section"": ""Models""}");
outputStreamWriter.Flush();
inputStream.CopyTo(outputStreamWriter.BaseStream);
}
}
}
}
private void ProcessDirectory(string inputPath, StreamWriter streamWriter)
{
var files = Directory.GetFiles(inputPath);
foreach (var file in files)
{
var fileContent = GetValidJsonFileContent(file);
if (fileContent.Length > 0)
{
streamWriter.WriteLine(fileContent);
streamWriter.Flush();
}
}
var subDirectories = Directory.GetDirectories(inputPath);
foreach (var subDirectory in subDirectories)
{
ProcessDirectory(subDirectory, streamWriter);
}
}
private string GetValidJsonFileContent(string filePath)
{
var jsonString = File.ReadAllText(filePath);
try
{
var json = JToken.Parse(jsonString);
return json.ToString(Formatting.None);
}
catch (Exception)
{
Console.WriteLine("Invalid JSON at path: " + filePath);
}
return string.Empty;
}
}
}