Merge lp:~mandel/wadlsharp/refactor_code_serialization into lp:wadlsharp/trunk
- refactor_code_serialization
- Merge into trunk
Proposed by
Manuel de la Peña
Status: | Merged |
---|---|
Merged at revision: | 32 |
Proposed branch: | lp:~mandel/wadlsharp/refactor_code_serialization |
Merge into: | lp:wadlsharp/trunk |
Prerequisite: | lp:~mandel/wadlsharp/increase_lpnamespace_coverage |
Diff against target: |
458 lines (+315/-39) 8 files modified
LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs (+60/-0) LpNet.WadlSharp.Common/Interface/IConverter.cs (+11/-1) LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj (+2/-0) LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs (+113/-0) LpNet.WadlSharp.Common/WadlConverter.cs (+14/-37) LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj (+1/-0) LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs (+111/-0) WadlConsole/Program.cs (+3/-1) |
To merge this branch: | bzr merge lp:~mandel/wadlsharp/refactor_code_serialization |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Manish Sinha (मनीष सिन्हा) | Approve | ||
Review via email: mp+36118@code.launchpad.net |
Commit message
Description of the change
Refactored the serialization code to allow more options. Increased the code coverage.
To post a comment you must log in.
Revision history for this message
Manish Sinha (मनीष सिन्हा) (manishsinha) : | # |
review:
Approve
Revision history for this message
Manish Sinha (मनीष सिन्हा) (manishsinha) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs' |
2 | --- LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 1970-01-01 00:00:00 +0000 |
3 | +++ LpNet.WadlSharp.Common/Interface/ICodeSerializer.cs 2010-09-21 10:04:56 +0000 |
4 | @@ -0,0 +1,60 @@ |
5 | +/* |
6 | + * The MIT License (MIT) |
7 | + * |
8 | + * Copyright 2010 Canonical Ltd. |
9 | + * |
10 | + * Permission is hereby granted, free of charge, to any person |
11 | + * obtaining a copy of this software and associated documentation |
12 | + * files (the "Software"), to deal in the Software without |
13 | + * restriction, including without limitation the rights to use, |
14 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
15 | + * copies of the Software, and to permit persons to whom the |
16 | + * Software is furnished to do so, subject to the following |
17 | + * conditions: |
18 | + |
19 | + * The above copyright notice and this permission notice shall be |
20 | + * included in all copies or substantial portions of the Software. |
21 | + * |
22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
23 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
24 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
25 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
26 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
27 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
28 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
29 | + * OTHER DEALINGS IN THE SOFTWARE. |
30 | + * |
31 | + * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
32 | + */ |
33 | +using System.CodeDom; |
34 | +using System.CodeDom.Compiler; |
35 | +using System.Collections.Generic; |
36 | + |
37 | +namespace LpNet.WadlSharp.Common.Interface |
38 | +{ |
39 | + /// <summary> |
40 | + /// Interface to be implemented by those objects that know how to serialize |
41 | + /// the generated code |
42 | + /// </summary> |
43 | + public interface ICodeSerializer |
44 | + { |
45 | + /// <summary> |
46 | + /// Gets the options to be used in the code generation. |
47 | + /// </summary> |
48 | + CodeGeneratorOptions Options { get; } |
49 | + |
50 | + /// <summary> |
51 | + /// Gets and sets the string that will use to state that the code was |
52 | + /// generated by a tool. |
53 | + /// </summary> |
54 | + string AutoGeneratedMessage { get; set; } |
55 | + |
56 | + /// <summary> |
57 | + /// Serializes the given code and returns an enumerable with the names of the files |
58 | + /// in which the code was serialized. |
59 | + /// </summary> |
60 | + /// <returns>The enumerable with the files in which the code was serialized.</returns> |
61 | + IEnumerable<string> Serialize(CodeNamespace codeNamespace); |
62 | + } |
63 | + |
64 | +} |
65 | |
66 | === modified file 'LpNet.WadlSharp.Common/Interface/IConverter.cs' |
67 | --- LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-05-02 07:49:15 +0000 |
68 | +++ LpNet.WadlSharp.Common/Interface/IConverter.cs 2010-09-21 10:04:56 +0000 |
69 | @@ -27,8 +27,18 @@ |
70 | |
71 | namespace LpNet.WadlSharp.Common.Interface |
72 | { |
73 | + /// <summary> |
74 | + /// Interface to be implemented by an object that is able to generate the C# code required to use a |
75 | + /// wadl service. |
76 | + /// </summary> |
77 | public interface IConverter |
78 | { |
79 | - void Convert(string inputFileName, string outputFileName, string rootNamespace); |
80 | + /// <summary> |
81 | + /// Gets and sets the code serializer that will be used to serialize the code to a collection |
82 | + /// of files. |
83 | + /// </summary> |
84 | + ICodeSerializer CodeSerializer { get; set; } |
85 | + |
86 | + void Convert(string inputFileName, string rootNamespace); |
87 | } |
88 | } |
89 | |
90 | === modified file 'LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj' |
91 | --- LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000 |
92 | +++ LpNet.WadlSharp.Common/LpNet.WadlSharp.Common.csproj 2010-09-21 10:04:56 +0000 |
93 | @@ -76,6 +76,7 @@ |
94 | <Compile Include="Interface\Generator\IParamater.cs" /> |
95 | <Compile Include="Interface\Generator\IProperty.cs" /> |
96 | <Compile Include="Interface\Generator\IReturn.cs" /> |
97 | + <Compile Include="Interface\ICodeSerializer.cs" /> |
98 | <Compile Include="Interface\IConverter.cs" /> |
99 | <Compile Include="Interface\ILoader.cs" /> |
100 | <Compile Include="Properties\AssemblyInfo.cs" /> |
101 | @@ -83,6 +84,7 @@ |
102 | <Compile Include="Resources\wadl.cs"> |
103 | <DependentUpon>wadl.xsd</DependentUpon> |
104 | </Compile> |
105 | + <Compile Include="Serialize\SingleFileCodeSerializer.cs" /> |
106 | <Compile Include="Utils.cs" /> |
107 | <Compile Include="WadlConverter.cs" /> |
108 | <Compile Include="WadlLoader.cs" /> |
109 | |
110 | === added directory 'LpNet.WadlSharp.Common/Serialize' |
111 | === added file 'LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs' |
112 | --- LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 1970-01-01 00:00:00 +0000 |
113 | +++ LpNet.WadlSharp.Common/Serialize/SingleFileCodeSerializer.cs 2010-09-21 10:04:56 +0000 |
114 | @@ -0,0 +1,113 @@ |
115 | +/* |
116 | + * The MIT License (MIT) |
117 | + * |
118 | + * Copyright 2010 Canonical Ltd. |
119 | + * |
120 | + * Permission is hereby granted, free of charge, to any person |
121 | + * obtaining a copy of this software and associated documentation |
122 | + * files (the "Software"), to deal in the Software without |
123 | + * restriction, including without limitation the rights to use, |
124 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
125 | + * copies of the Software, and to permit persons to whom the |
126 | + * Software is furnished to do so, subject to the following |
127 | + * conditions: |
128 | + |
129 | + * The above copyright notice and this permission notice shall be |
130 | + * included in all copies or substantial portions of the Software. |
131 | + * |
132 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
133 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
134 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
135 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
136 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
137 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
138 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
139 | + * OTHER DEALINGS IN THE SOFTWARE. |
140 | + * |
141 | + * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
142 | + */ |
143 | +using System.CodeDom; |
144 | +using System.CodeDom.Compiler; |
145 | +using System.Collections.Generic; |
146 | +using System.IO; |
147 | +using System.Text; |
148 | +using LpNet.WadlSharp.Common.Interface; |
149 | +using Microsoft.CSharp; |
150 | + |
151 | +namespace LpNet.WadlSharp.Common.Serialize |
152 | +{ |
153 | + /// <summary> |
154 | + /// Serializer that writes all the code that has been generated in a single file. |
155 | + /// </summary> |
156 | + public class SingleFileCodeSerializer : ICodeSerializer |
157 | + { |
158 | + #region Variables |
159 | + |
160 | + private readonly string _outputFile; |
161 | + |
162 | + #endregion |
163 | + |
164 | + #region Construtors |
165 | + |
166 | + /// <summary> |
167 | + /// Creates a new serializer that will serialize the code in a single file. |
168 | + /// </summary> |
169 | + /// <param name="outputFile">The path in which the code will be serialized.</param> |
170 | + public SingleFileCodeSerializer(string outputFile) |
171 | + { |
172 | + _outputFile = outputFile; |
173 | + Options = new CodeGeneratorOptions(); |
174 | + } |
175 | + |
176 | + #endregion |
177 | + |
178 | + #region Implementation of ICodeSerializer |
179 | + |
180 | + /// <summary> |
181 | + /// Gets the options to be used in the code generation. |
182 | + /// </summary> |
183 | + public CodeGeneratorOptions Options { get; private set; } |
184 | + |
185 | + /// <summary> |
186 | + /// Gets and sets the string that will use to state that the code was |
187 | + /// generated by a tool. |
188 | + /// </summary> |
189 | + public string AutoGeneratedMessage { get;set; } |
190 | + |
191 | + /// <summary> |
192 | + /// Serializes the given code and returns an enumerable with the names of the files |
193 | + /// in which the code was serialized. |
194 | + /// </summary> |
195 | + /// <returns>The enumerable with the files in which the code was serialized.</returns> |
196 | + public IEnumerable<string> Serialize(CodeNamespace codeNamespace) |
197 | + { |
198 | + var sb = new StringBuilder(); |
199 | + |
200 | + var sw = new StringWriter(sb); |
201 | + |
202 | + var provider = new CSharpCodeProvider(); |
203 | + provider.GenerateCodeFromNamespace(codeNamespace, |
204 | + sw, Options); |
205 | + var codeString = sb.ToString(); |
206 | + if (!string.IsNullOrEmpty(AutoGeneratedMessage)) |
207 | + { |
208 | + codeString = string.Format("{0}\n{1}", AutoGeneratedMessage, codeString); |
209 | + sb.Append(AutoGeneratedMessage); |
210 | + } |
211 | + var code = Encoding.ASCII.GetBytes(codeString); |
212 | + var outputFileInfo = new FileInfo(_outputFile); |
213 | + // if the files exists, delete it |
214 | + if (outputFileInfo.Exists) |
215 | + { |
216 | + outputFileInfo.Delete(); |
217 | + } |
218 | + |
219 | + using(var fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write)) |
220 | + fileStream.Write(code, 0, code.Length); |
221 | + |
222 | + return new[] {_outputFile}; |
223 | + } |
224 | + |
225 | + #endregion |
226 | + } |
227 | +} |
228 | |
229 | === modified file 'LpNet.WadlSharp.Common/WadlConverter.cs' |
230 | --- LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-17 16:07:11 +0000 |
231 | +++ LpNet.WadlSharp.Common/WadlConverter.cs 2010-09-21 10:04:56 +0000 |
232 | @@ -36,11 +36,8 @@ |
233 | using System.CodeDom; |
234 | using Generator; |
235 | using Interface.Generator; |
236 | - using System.CodeDom.Compiler; |
237 | - using System.Text; |
238 | - using Microsoft.CSharp; |
239 | |
240 | - public class WadlConverter :IConverter |
241 | + public class WadlConverter : IConverter |
242 | { |
243 | #region Vars |
244 | |
245 | @@ -73,12 +70,17 @@ |
246 | |
247 | #region IConverter Members |
248 | |
249 | - public void Convert(string inputFileName, string outputFileName, string rootNamespace) |
250 | + /// <summary> |
251 | + /// Gets and sets the code serializer that will be used to serialize the code to a collection |
252 | + /// of files. |
253 | + /// </summary> |
254 | + public ICodeSerializer CodeSerializer { get; set; } |
255 | + |
256 | + public void Convert(string inputFileName, string rootNamespace) |
257 | { |
258 | #region Check if the input file is correct or not |
259 | |
260 | - FileInfo inputFileInfo = new FileInfo(inputFileName); |
261 | - FileInfo outputFileInfo = new FileInfo(outputFileName); |
262 | + var inputFileInfo = new FileInfo(inputFileName); |
263 | |
264 | #endregion |
265 | |
266 | @@ -96,36 +98,11 @@ |
267 | |
268 | #endregion |
269 | |
270 | - #region Get the CSharpCodeProvider |
271 | - |
272 | - CodeGeneratorOptions options = new CodeGeneratorOptions(); |
273 | - options.BracingStyle = "C"; |
274 | - options.IndentString = " "; |
275 | - |
276 | - StringBuilder sbCode = new StringBuilder(); |
277 | - StringWriter sw = new StringWriter(sbCode); |
278 | - |
279 | - CSharpCodeProvider provider = new CSharpCodeProvider(); |
280 | - provider.GenerateCodeFromNamespace(nameSpace, |
281 | - sw, options); |
282 | - |
283 | - #endregion |
284 | - |
285 | - #region Serialize the DOM |
286 | - |
287 | - string finalCode = sbCode.ToString(); |
288 | - |
289 | - byte[] code = Encoding.ASCII.GetBytes(finalCode); |
290 | - |
291 | - if(outputFileInfo.Exists) |
292 | - { |
293 | - outputFileInfo.Delete(); |
294 | - } |
295 | - |
296 | - FileStream fileStream = new FileStream(outputFileInfo.FullName, FileMode.OpenOrCreate, FileAccess.Write); |
297 | - fileStream.Write(code, 0, code.Length); |
298 | - |
299 | - #endregion |
300 | + // serialize the code using the serializer |
301 | + CodeSerializer.Options.BracingStyle = "C"; |
302 | + CodeSerializer.Options.IndentString = " "; |
303 | + CodeSerializer.Serialize(nameSpace); |
304 | + |
305 | } |
306 | |
307 | #endregion |
308 | |
309 | === modified file 'LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj' |
310 | --- LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000 |
311 | +++ LpNet.WadlSharp.UnitTest/LpNet.WadlSharp.UnitTest.csproj 2010-09-21 10:04:56 +0000 |
312 | @@ -55,6 +55,7 @@ |
313 | <ItemGroup> |
314 | <Compile Include="Generator\LpClassFactoryFixture.cs" /> |
315 | <Compile Include="Generator\LpNamespaceFixture.cs" /> |
316 | + <Compile Include="Serialize\SingleFileCodeSerializerFixture.cs" /> |
317 | <Compile Include="WadlLoaderTest.cs" /> |
318 | <Compile Include="Properties\AssemblyInfo.cs" /> |
319 | </ItemGroup> |
320 | |
321 | === added directory 'LpNet.WadlSharp.UnitTest/Serialize' |
322 | === added file 'LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs' |
323 | --- LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 1970-01-01 00:00:00 +0000 |
324 | +++ LpNet.WadlSharp.UnitTest/Serialize/SingleFileCodeSerializerFixture.cs 2010-09-21 10:04:56 +0000 |
325 | @@ -0,0 +1,111 @@ |
326 | +/* |
327 | + * The MIT License (MIT) |
328 | + * |
329 | + * Copyright 2010 Canonical Ltd. |
330 | + * |
331 | + * Permission is hereby granted, free of charge, to any person |
332 | + * obtaining a copy of this software and associated documentation |
333 | + * files (the "Software"), to deal in the Software without |
334 | + * restriction, including without limitation the rights to use, |
335 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
336 | + * copies of the Software, and to permit persons to whom the |
337 | + * Software is furnished to do so, subject to the following |
338 | + * conditions: |
339 | + |
340 | + * The above copyright notice and this permission notice shall be |
341 | + * included in all copies or substantial portions of the Software. |
342 | + * |
343 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
344 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
345 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
346 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
347 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
348 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
349 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
350 | + * OTHER DEALINGS IN THE SOFTWARE. |
351 | + * |
352 | + * Authors: Manuel de la Peña <manuel.delapena@canonical.com> |
353 | + */ |
354 | +using System.CodeDom; |
355 | +using System.IO; |
356 | +using LpNet.WadlSharp.Common.Serialize; |
357 | +using NUnit.Framework; |
358 | + |
359 | +namespace LpNet.WadlSharp.UnitTest.Serialize |
360 | +{ |
361 | + [TestFixture] |
362 | + public class SingleFileCodeSerializerFixture |
363 | + { |
364 | + #region Variables |
365 | + |
366 | + private CodeNamespace _nameSpace; |
367 | + private string _expectedOutput; |
368 | + private string _outputFilePath; |
369 | + private SingleFileCodeSerializer _serializer; |
370 | + |
371 | + #endregion |
372 | + |
373 | + #region Setup |
374 | + |
375 | + [SetUp] |
376 | + public void Setup() |
377 | + { |
378 | + _nameSpace = new CodeNamespace("TestNamespace"); |
379 | + _expectedOutput = "namespace TestNamespace\r\n{\r\n \r\n}\r\n"; |
380 | + _outputFilePath = Path.GetTempFileName(); |
381 | + _serializer = new SingleFileCodeSerializer(_outputFilePath); |
382 | + } |
383 | + |
384 | + #endregion |
385 | + |
386 | + #region Tests |
387 | + |
388 | + [Test] |
389 | + public void SerializeAutoGeneratedCommentTest() |
390 | + { |
391 | + // set the auto gen comment to ensure that it is present |
392 | + var autogenComment = "/// autogenerated by a tool\r\n"; |
393 | + |
394 | + _serializer.AutoGeneratedMessage = autogenComment; |
395 | + _serializer.Options.BracingStyle = "C"; |
396 | + _serializer.Options.IndentString = " "; |
397 | + _serializer.Serialize(_nameSpace); |
398 | + |
399 | + Assert.IsTrue(File.Exists(_outputFilePath)); |
400 | + // read the contents of the file an assert they are the expected |
401 | + var contents = File.ReadAllText(_outputFilePath); |
402 | + var expected = string.Format("{0}\n{1}", autogenComment, _expectedOutput); |
403 | + Assert.AreEqual(expected, contents); |
404 | + } |
405 | + |
406 | + [Test] |
407 | + public void SerializeFileExistsTest() |
408 | + { |
409 | + // create the file to see if we do not have any issues |
410 | + File.WriteAllText(_outputFilePath, "Test file!"); |
411 | + // serialize the code, make sure the file is created with the correct contents |
412 | + _serializer.Options.BracingStyle = "C"; |
413 | + _serializer.Options.IndentString = " "; |
414 | + _serializer.Serialize(_nameSpace); |
415 | + Assert.IsTrue(File.Exists(_outputFilePath)); |
416 | + // read the contents of the file an assert they are the expected |
417 | + var contents = File.ReadAllText(_outputFilePath); |
418 | + Assert.AreEqual(_expectedOutput, contents); |
419 | + } |
420 | + |
421 | + [Test] |
422 | + public void SerializeTest() |
423 | + { |
424 | + // serialize the code, make sure the file is created with the correct contents |
425 | + _serializer.Options.BracingStyle = "C"; |
426 | + _serializer.Options.IndentString = " "; |
427 | + _serializer.Serialize(_nameSpace); |
428 | + Assert.IsTrue(File.Exists(_outputFilePath)); |
429 | + // read the contents of the file an assert they are the expected |
430 | + var contents = File.ReadAllText(_outputFilePath); |
431 | + Assert.AreEqual(_expectedOutput, contents); |
432 | + } |
433 | + |
434 | + #endregion |
435 | + } |
436 | +} |
437 | |
438 | === modified file 'WadlConsole/Program.cs' |
439 | --- WadlConsole/Program.cs 2010-09-20 13:59:06 +0000 |
440 | +++ WadlConsole/Program.cs 2010-09-21 10:04:56 +0000 |
441 | @@ -30,6 +30,7 @@ |
442 | using System.IO; |
443 | using System.Net; |
444 | using LpNet.WadlSharp.Common; |
445 | +using LpNet.WadlSharp.Common.Serialize; |
446 | using NDesk.Options; |
447 | |
448 | namespace Wadl |
449 | @@ -89,7 +90,8 @@ |
450 | { |
451 | converter = new WadlConverter(); |
452 | } |
453 | - converter.Convert(inputFileName, outputFileName, rootNamespace); |
454 | + converter.CodeSerializer = new SingleFileCodeSerializer(outputFileName); |
455 | + converter.Convert(inputFileName, rootNamespace); |
456 | } |
457 | |
458 | #endregion |