Skip to content

Commit 6cd252d

Browse files
committed
Clean up
1 parent d73e0a2 commit 6cd252d

File tree

3 files changed

+24
-131
lines changed

3 files changed

+24
-131
lines changed
Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,14 @@
1-
using System.Linq;
2-
3-
namespace Typewriter
1+
namespace Typewriter
42
{
5-
using ApiDoctor.Validation;
6-
using ApiDoctor.Validation.Error;
73
using ApiDoctor.Validation.OData;
84
using ApiDoctor.Validation.Utility;
9-
using System;
10-
using System.Collections.Generic;
115
using System.Linq;
12-
using System.Text;
136

7+
/// <summary>
8+
/// From https://github.com/OneDrive/apidoctor/blob/master/ApiDoctor.Publishing/CSDL/csdlextensionmethods.cs
9+
/// </summary>
1410
internal static class CsdlExtensionMethods
1511
{
16-
17-
public static string RequestUriPathOnly(this MethodDefinition method, string[] baseUrlsToRemove, IssueLogger issues)
18-
{
19-
var path = method.Request.FirstLineOnly().TextBetweenCharacters(' ', '?').TrimEnd('/');
20-
21-
if (baseUrlsToRemove != null)
22-
{
23-
foreach (var baseUrl in baseUrlsToRemove)
24-
{
25-
if (!string.IsNullOrEmpty(baseUrl) && path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase))
26-
{
27-
path = path.Substring(baseUrl.Length);
28-
}
29-
}
30-
}
31-
32-
// just in case there's a stray example that doesn't match, at least chop the domain part off.
33-
foreach (var scheme in new[] { "https://", "http://" })
34-
{
35-
if (path.StartsWith(scheme, StringComparison.OrdinalIgnoreCase))
36-
{
37-
int pathStartIndex = path.IndexOf('/', scheme.Length);
38-
if (pathStartIndex != -1)
39-
{
40-
path = path.Substring(pathStartIndex);
41-
break;
42-
}
43-
}
44-
}
45-
46-
// Normalize variables in the request path
47-
path = path.ReplaceTextBetweenCharacters('{', '}', "var");
48-
49-
if (method.RequestMetadata.SampleKeys != null)
50-
{
51-
foreach (var key in method.RequestMetadata.SampleKeys)
52-
{
53-
path = path.Replace("/" + key, "/{var}");
54-
}
55-
}
56-
57-
// Normalize function params
58-
var substitutions = new Dictionary<string, string>();
59-
var parenIndex = path.IndexOf('(');
60-
for (int i = 0; i < path.Length; i++)
61-
{
62-
if (path[i] == '(')
63-
{
64-
// this is the start of a function. let's find the closing paren.
65-
var close = path.IndexOf(')', i);
66-
if (close > -1)
67-
{
68-
var inner = path.Substring(i + 1, close - i - 1);
69-
substitutions[inner] = NormalizeFunctionParameters(inner, issues);
70-
i = close;
71-
}
72-
}
73-
}
74-
75-
foreach (var sub in substitutions)
76-
{
77-
path = path.Replace(sub.Key, sub.Value);
78-
}
79-
80-
// Rewrite path syntax into what it logically means
81-
path = path.ReplaceTextBetweenCharacters(':', ':', "/children/{var}", requireSecondChar: false, removeTargetChars: true);
82-
83-
return path;
84-
}
85-
86-
private static string NormalizeFunctionParameters(string funcParams, IssueLogger issues)
87-
{
88-
// foo=bar, baz ='qux',x= 9
89-
var normalized = new StringBuilder();
90-
var allParams = funcParams.Split(',');
91-
for (int i = 0; i < allParams.Length; i++)
92-
{
93-
var param = allParams[i].Trim();
94-
var kvp = param.Split('=');
95-
if (kvp.Length != 2)
96-
{
97-
issues.Error(ValidationErrorCode.ParameterParserError, $"Malformed function params {funcParams}");
98-
return funcParams;
99-
}
100-
101-
allParams[i] = kvp[0].Trim() + "={var}";
102-
}
103-
104-
return string.Join(",", allParams.OrderBy(p => p));
105-
}
106-
107-
//public static string HttpMethodVerb(this MethodDefinition method)
108-
//{
109-
// HttpParser parser = new HttpParser();
110-
// var request = parser.ParseHttpRequest(method.Request);
111-
// return request.Method;
112-
113-
//}
114-
115-
internal static void AppendWithCondition(this System.Text.StringBuilder sb, bool condition, string text, string prefixIfExistingContent = null)
116-
{
117-
if (condition)
118-
{
119-
if (sb.Length > 0 && prefixIfExistingContent != null)
120-
sb.Append(prefixIfExistingContent);
121-
sb.Append(text);
122-
}
123-
}
124-
12512
/// <summary>
12613
/// Merge two EntityFramework instances together into the first framework
12714
/// </summary>
@@ -154,7 +41,6 @@ internal static EntityFramework MergeWith(this EntityFramework framework1, Entit
15441
}
15542

15643
return edmx;
157-
15844
}
15945
}
16046
}

src/Typewriter/DocAnnotationWriter.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,26 @@
1313

1414
namespace Typewriter
1515
{
16-
internal class DocAnnotationWriter : ApiDoctor.Publishing.CSDL.CsdlWriter// or this a helper, will name this later
16+
/// <summary>
17+
/// Creates a CSDL file with documentation annotations sourced from documentation.
18+
/// </summary>
19+
internal class DocAnnotationWriter : ApiDoctor.Publishing.CSDL.CsdlWriter
1720
{
1821
private static Logger Logger => LogManager.GetLogger("DocAnnotationWriter");
19-
//private string csdl;
20-
21-
internal IssueLogger IssueLogger { get; set; }
22-
internal DocSet DocSet { get; set; }
23-
internal string Csdl { get; set; }
24-
22+
2523
private readonly CsdlWriterOptions options;
2624

2725
internal DocAnnotationWriter(DocSet docSet, CsdlWriterOptions options, string csdl) : base(docSet, options)
2826
{
29-
this.Csdl = csdl;
30-
this.DocSet = docSet;
3127
this.options = options; // Can change the base access modifier so we could use it.
3228
}
3329

3430
public async Task<string> PublishToStringAsync(IssueLogger issues)
3531
{
3632
string outputFilenameSuffix = "";
3733

34+
Logger.Info("Begin creating metadata file with documentation annotations.");
35+
3836
// Step 1: Generate an EntityFramework OM from the documentation and/or template file
3937
EntityFramework framework = CreateEntityFrameworkFromDocs(issues);
4038
if (null == framework)
@@ -78,6 +76,8 @@ public async Task<string> PublishToStringAsync(IssueLogger issues)
7876
// Step 2: Generate XML representation of EDMX
7977
string xmlData = ODataParser.Serialize<EntityFramework>(framework, options.AttributesOnNewLines);
8078

79+
Logger.Info("Finish creating metadata file with documentation annotations.");
80+
8181
return xmlData;
8282
}
8383
}
@@ -90,7 +90,7 @@ internal async static Task<string> ApplyAnnotationsToCsdl(string csdl, Options o
9090
{
9191
// Get DocSet
9292
DocSet docs = GetDocSet(options, new IssueLogger());
93-
// Create CsdlWriterOptions
93+
9494
var csdlWriterOptions = new CsdlWriterOptions()
9595
{
9696
DocumentationSetPath = options.DocsRoot + "\\api-reference\\v1.0\\",
@@ -100,12 +100,12 @@ internal async static Task<string> ApplyAnnotationsToCsdl(string csdl, Options o
100100
Formats = MetadataFormat.EdmxInput
101101
};
102102

103-
// Create DocAnnotationWriter
104103
DocAnnotationWriter docWriter = new DocAnnotationWriter(docs, csdlWriterOptions, csdl);
105104

106105
return await docWriter.PublishToStringAsync(new IssueLogger());
107106
}
108107

108+
109109
private static DocSet GetDocSet(Options options, IssueLogger issues)
110110
{
111111
Logger.Info("Opening documentation from {0}", options.DocsRoot);
@@ -118,10 +118,10 @@ private static DocSet GetDocSet(Options options, IssueLogger issues)
118118
catch (System.IO.FileNotFoundException ex)
119119
{
120120
Logger.Error(ex.Message);
121-
return null; // Hmmmmm. TODO
121+
return null;
122122
}
123123

124-
Logger.Info("Scanning documentation files");
124+
Logger.Info("Parsing documentation files");
125125
var stopwatch = new Stopwatch();
126126
stopwatch.Start();
127127
docSet.ScanDocumentation(string.Empty, issues);

src/Typewriter/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private static void GenerateSDK(Options options)
4141
// Inject documentation annotations into the CSDL using ApiDoctor.
4242
string csdlWithDocAnnotations = AnnotationHelper.ApplyAnnotationsToCsdl(processedCsdlContents, options).Result;
4343

44+
// Create code files from the CSDL with annotations for the target platform and write those files to disk.
4445
var files = MetadataToClientSource(csdlWithDocAnnotations, options.Language);
4546
FileWriter.WriteAsync(files, options.Output);
4647

@@ -88,6 +89,12 @@ private static void HandleError(IEnumerable<Error> errors)
8889
}
8990
}
9091

92+
/// <summary>
93+
/// Generates code files from an edmx file.
94+
/// </summary>
95+
/// <param name="edmxString">The EDMX file as a string.</param>
96+
/// <param name="targetLanguage">Specifies the target language. Possible values are csharp, php, etc.</param>
97+
/// <returns></returns>
9198
static private IEnumerable<TextFile> MetadataToClientSource(string edmxString, string targetLanguage)
9299
{
93100
if (String.IsNullOrEmpty(edmxString))

0 commit comments

Comments
 (0)