Skip to content

Commit 71baca5

Browse files
author
Yura Variat
committed
Transformer for ini files was added
1 parent 86bdac1 commit 71baca5

2 files changed

Lines changed: 206 additions & 0 deletions

File tree

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.SlowCheetah
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using Microsoft.VisualStudio.Jdt;
10+
11+
/// <summary>
12+
/// Transforms Ini files
13+
/// </summary>
14+
public class IniTransformer : ITransformer
15+
{
16+
private IJsonTransformationLogger logger;
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="IniTransformer"/> class.
20+
/// </summary>
21+
public IniTransformer()
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="IniTransformer"/> class with an external logger
27+
/// </summary>
28+
/// <param name="logger">The external logger</param>
29+
public IniTransformer(ITransformationLogger logger)
30+
{
31+
if (logger == null)
32+
{
33+
throw new ArgumentNullException(nameof(logger));
34+
}
35+
36+
this.logger = new JsonShimLogger(logger);
37+
}
38+
39+
/// <inheritdoc/>
40+
public void CreateTransformFile(string sourcePath, string transformPath, bool overwrite)
41+
{
42+
if (string.IsNullOrWhiteSpace(sourcePath))
43+
{
44+
throw new ArgumentNullException(nameof(sourcePath));
45+
}
46+
47+
if (string.IsNullOrWhiteSpace(transformPath))
48+
{
49+
throw new ArgumentNullException(nameof(transformPath));
50+
}
51+
52+
if (!File.Exists(sourcePath))
53+
{
54+
throw new FileNotFoundException(Resources.Resources.ErrorMessage_SourceFileNotFound, sourcePath);
55+
}
56+
57+
// If the file should be overwritten or if it doesn't exist, we create it
58+
if (overwrite || !File.Exists(transformPath))
59+
{
60+
var encoding = TransformUtilities.GetEncoding(sourcePath);
61+
File.WriteAllText(transformPath, string.Empty, encoding);
62+
}
63+
}
64+
65+
/// <inheritdoc/>
66+
public bool IsFileSupported(string filePath)
67+
{
68+
if (string.IsNullOrWhiteSpace(filePath))
69+
{
70+
throw new ArgumentNullException(nameof(filePath));
71+
}
72+
73+
if (!File.Exists(filePath))
74+
{
75+
throw new FileNotFoundException(Resources.Resources.ErrorMessage_FileNotFound, filePath);
76+
}
77+
78+
return Path.GetExtension(filePath).Equals(".ini", StringComparison.OrdinalIgnoreCase);
79+
}
80+
81+
/// <inheritdoc/>
82+
public bool Transform(string sourcePath, string transformPath, string destinationPath)
83+
{
84+
if (string.IsNullOrWhiteSpace(sourcePath))
85+
{
86+
throw new ArgumentException($"{nameof(sourcePath)} cannot be null or whitespace");
87+
}
88+
89+
if (string.IsNullOrWhiteSpace(transformPath))
90+
{
91+
throw new ArgumentException($"{nameof(transformPath)} cannot be null or whitespace");
92+
}
93+
94+
if (string.IsNullOrWhiteSpace(destinationPath))
95+
{
96+
throw new ArgumentException($"{nameof(destinationPath)} cannot be null or whitespace");
97+
}
98+
99+
if (!File.Exists(sourcePath))
100+
{
101+
throw new FileNotFoundException(Resources.Resources.ErrorMessage_SourceFileNotFound, sourcePath);
102+
}
103+
104+
if (!File.Exists(transformPath))
105+
{
106+
throw new FileNotFoundException(Resources.Resources.ErrorMessage_TransformFileNotFound, transformPath);
107+
}
108+
109+
try
110+
{
111+
Dictionary<string, string> transformDictionary = new Dictionary<string, string>();
112+
string currentRoot = null;
113+
string[] keyPair = null;
114+
115+
if (!System.IO.File.Exists(sourcePath) || !System.IO.File.Exists(transformPath))
116+
{
117+
return false;
118+
}
119+
120+
// Collect what we need to replace
121+
using (StreamReader iniFile = new StreamReader(transformPath))
122+
{
123+
string strLine = iniFile.ReadLine();
124+
while (strLine != null)
125+
{
126+
if (!string.IsNullOrEmpty(strLine))
127+
{
128+
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
129+
{
130+
currentRoot = strLine.Substring(1, strLine.Length - 2);
131+
}
132+
else if (!strLine.StartsWith(";") && !strLine.StartsWith("#"))
133+
{
134+
keyPair = strLine.Split(new char[] { '=' }, 2);
135+
if (keyPair.Length > 1 && !transformDictionary.ContainsKey(currentRoot + "-" + keyPair[0]))
136+
{
137+
transformDictionary.Add(currentRoot + "-" + keyPair[0], keyPair[1]);
138+
}
139+
}
140+
}
141+
strLine = iniFile.ReadLine();
142+
}
143+
}
144+
145+
// read the source file and replace lines where is necessary
146+
using (StreamReader iniFile = new StreamReader(sourcePath))
147+
{
148+
using (StreamWriter destFile = new StreamWriter(destinationPath))
149+
{
150+
string strLine = iniFile.ReadLine();
151+
while (strLine != null)
152+
{
153+
if (!string.IsNullOrEmpty(strLine))
154+
{
155+
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
156+
{
157+
currentRoot = strLine.Substring(1, strLine.Length - 2);
158+
}
159+
else if (!strLine.StartsWith(";") && !strLine.StartsWith("#"))
160+
{
161+
keyPair = strLine.Split(new char[] { '=' }, 2);
162+
if (keyPair.Length > 1 && transformDictionary.ContainsKey(currentRoot + "-" + keyPair[0]))
163+
{
164+
strLine = keyPair[0] + "=" + transformDictionary[currentRoot + "-" + keyPair[0]];
165+
}
166+
}
167+
destFile.WriteLine(strLine);
168+
}
169+
else
170+
{
171+
destFile.WriteLine(strLine);
172+
}
173+
174+
strLine = iniFile.ReadLine();
175+
}
176+
}
177+
}
178+
179+
return true;
180+
}
181+
catch
182+
{
183+
// JDT exceptions are handled by it's own logger
184+
return false;
185+
}
186+
}
187+
188+
/// <inheritdoc/>
189+
public ITransformer WithLogger(ITransformationLogger logger)
190+
{
191+
if (logger == this.logger)
192+
{
193+
return this;
194+
}
195+
else if (logger == null)
196+
{
197+
return new IniTransformer();
198+
}
199+
else
200+
{
201+
return new IniTransformer(logger);
202+
}
203+
}
204+
}
205+
}

src/Microsoft.VisualStudio.SlowCheetah/Transformer/TransformerFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class TransformerFactory
1818
{
1919
new JsonTransformer(),
2020
new XmlTransformer(),
21+
new IniTransformer()
2122
};
2223

2324
/// <summary>

0 commit comments

Comments
 (0)