public Dictionary <string, Entity> AnalyzeAPIs(Dictionary <string, string> uriStartingPoints, string baseUri) { Dictionary <string, Entity> result = new Dictionary <string, Entity>(StringComparer.OrdinalIgnoreCase); // Create the initial stack of the uris to analyze Stack <KeyValuePair <string, string> > stack = new Stack <KeyValuePair <string, string> >(); stack.PushRange(uriStartingPoints); HashSet <string> alreadyVisited = new HashSet <string>(StringComparer.OrdinalIgnoreCase); while (stack.Count > 0) { var uri = stack.Pop(); ConsolePrinter.Write(ConsoleColor.DarkGreen, "Processing {0} ...", uri.Value); string normalized = RegExCreator.FromUri(baseUri, uri.Value); ConsolePrinter.Write(ConsoleColor.Gray, "Normalized {0}", normalized); alreadyVisited.Add(normalized); var obj = ProcessUriLocation(uri.Value); if (obj == null) { continue; } // If the entity does not have a name (or one could not be retrieved), use the one // created when the key was inserted. obj.Name = obj.Name ?? uri.Key; Entity current; if (result.TryGetValue(obj.Name ?? uri.Key, out current)) { // we need to merge the current object with a new one current.MergeWith(obj); } else { if (!string.IsNullOrEmpty(obj.Name)) { result[obj.Name] = obj; } } ConsolePrinter.Write(ConsoleColor.Cyan, "Found {0} types", result.Count); foreach (var item in obj.Methods) { string uriAddr = Constants.Addresses.SmugMug + item.Uri + Constants.RequestModifiers; if (!alreadyVisited.Contains(RegExCreator.FromUri(baseUri, uriAddr))) { stack.Push(new KeyValuePair <string, string>(item.ReturnType, uriAddr)); ConsolePrinter.Write(ConsoleColor.DarkYellow, "Queued {0}", uriAddr); } } } return(result); }
public static StringBuilder BuildMethods(List <Method> unsortedList) { StringBuilder sb = new StringBuilder(); var list = unsortedList.OrderBy(m => m.Uri); HashSet <string> methodMap = new HashSet <string>(); foreach (var item in list) { string returnType, methodName, uri, returnCode, parameters; returnType = Constants.VoidMethodReturnType; // this is the default if (!string.IsNullOrEmpty(item.ReturnType)) { returnType = item.ReturnType; if (returnType.EndsWith("[]")) { returnType = returnType.Replace("[]", ""); } returnType = Helpers.NormalizeString(returnType) + "Entity"; if (item.ReturnType.EndsWith("[]")) { returnType += "[]"; } } int paramCount; uri = RegExCreator.FromUri(SmugMug.v2.Constants.Addresses.SmugMugApi, SmugMug.v2.Constants.Addresses.SmugMug + item.Uri, out paramCount); methodName = Helpers.NormalizeString(uri, '!', '(', ')', '*'); parameters = GenerateParams(paramCount); returnCode = GenerateMethodCall(uri, returnType, paramCount); string key = returnType + " " + methodName; if (methodMap.Contains(key)) { continue; } methodMap.Add(key); string methodDefinitionFormat = returnType == Constants.VoidMethodReturnType ? Constants.VoidMethodDefinition : Constants.MethodDefinition; /*public async Task<{returnType}> {methodName} ({parameters}) * { * // {comment} * * {returnCode} * }*/ sb.AppendLine(string.Format(methodDefinitionFormat, /*return type*/ returnType, /*method name*/ methodName, /*parameters*/ parameters, /*comment*/ uri, /*return statement*/ returnCode)); } ConsolePrinter.Write(ConsoleColor.Cyan, "Generated {0} methods", methodMap.Count); return(sb); }
public static StringBuilder BuildManualMethods(List <Method> unsortedList) { StringBuilder sb = new StringBuilder(); var list = unsortedList.OrderBy(m => m.Uri); HashSet <string> methodMap = new HashSet <string>(); foreach (var item in list) { string returnType, methodName, returnCode = string.Empty, uri; returnType = Constants.VoidMethodReturnType; // this is the default if (!string.IsNullOrEmpty(item.ReturnType)) { returnType = item.ReturnType; if (returnType.EndsWith("[]")) { returnType = returnType.Replace("[]", ""); } returnType = Helpers.NormalizeString(returnType) + "Entity"; if (item.ReturnType.EndsWith("[]")) { returnType += "[]"; } } int paramCount; uri = RegExCreator.FromUri(SmugMug.v2.Constants.Addresses.SmugMugApi, SmugMug.v2.Constants.Addresses.SmugMug + item.Uri, out paramCount); methodName = Helpers.NormalizeString(uri, '!', '(', ')', '*'); // parameters = GenerateParams(paramCount); StringBuilder parameterBuilder = new StringBuilder(); for (int i = 0; i < paramCount; i++) { parameterBuilder.Append("string.Empty"); if (i != paramCount - 1) { parameterBuilder.Append(", "); } } if (returnType != Constants.VoidMethodReturnType) { returnCode = "return "; } returnCode += string.Format("await {0}({1});", methodName, parameterBuilder.ToString()); methodName = "Fixup_" + methodName; string key = returnType + " " + methodName; if (methodMap.Contains(key)) { continue; } methodMap.Add(key); string methodDefinitionFormat = returnType == Constants.VoidMethodReturnType ? Constants.VoidMethodDefinition : Constants.MethodDefinition; sb.AppendLine(string.Format(methodDefinitionFormat, /*return type*/ returnType, /*method name*/ methodName, /*parameters*/ string.Empty, /*comment*/ uri, /*return statement*/ returnCode)); } return(sb); }