public override bool Execute() { Log.LogMessage("GenerateParameterNames Task"); Log.LogMessage(" ApiOutputFile: {0}", ApiOutputFile); Log.LogMessage(" GeneratedFile: {0}", GeneratedFile); Log.LogMessage(" SourceJars: {0}", string.Join(";", SourceJars.Select(x => x.ItemSpec))); Log.LogMessage(" TransformFiles: {0}", string.Join(";", TransformFiles.Select(x => x.ItemSpec))); var generatorParameters = new GeneratorParameters { ReservedPrefix = ReservedPrefix ?? string.Empty, ReservedSuffix = ReservedSuffix ?? string.Empty, ParameterCasing = (TextCasing)Enum.Parse(typeof(TextCasing), ParameterCasing, true), ForceMeaningfulParameterNames = ForceMeaningfulParameterNames }; // create the folder var dir = Path.GetDirectoryName(GeneratedFile.ItemSpec); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // read the jar files var classPath = new ClassPath() { ApiSource = "class-parse" }; foreach (var jarFile in SourceJars) { if (ClassPath.IsJarFile(jarFile.ItemSpec)) { classPath.Load(jarFile.ItemSpec); } } // build up the object tree var metadataElement = classPath.ToXElement(); // remove any nodes that the user wants metadataElement = TransformXml(metadataElement); var packages = JavaPackage.Parse(metadataElement); var xParameters = packages.SelectMany(p => p.ToXElement(generatorParameters)); // create the new xml document var xDoc = new XDocument( new XElement("metadata", xParameters.ToArray())); // make sure we don't have anything not in the api.xml RemoveIgnoredApiXmlMembers(xDoc); // save xDoc.Save(GeneratedFile.ItemSpec); return(true); }
public IEnumerable <XNode> ToXElement(GeneratorParameters generatorParameters) { const string packagePathTemplate = "/api/package[@name='{0}']"; const string typePathTemplate = "/{0}[@name='{1}']"; const string memberPathTemplate = "/{0}[@name='{1}' and count(parameter)={2} and {3}]"; const string paramSeparator = " and "; const string paramPath = "parameter[{0}][@type='{1}']"; const string parameterPathTemplate = "/parameter[{0}]"; var package = this; if (package.HasTypes) { var packagePath = string.Format(packagePathTemplate, package.Name); foreach (var type in package.Types.Where(t => t.IsVisible && t.HasMembers)) { // add some comments yield return(new XComment(string.Format("{0} {1}.{2}", type.Kind, package.Name, type.Name))); var typePath = packagePath + string.Format(typePathTemplate, type.Kind, type.Name); foreach (var member in type.Members.Where(m => m.IsVisible && m.HasParameters)) { // make sure the parameter names are valid and meaningful member.EnsueValidAndUnique(generatorParameters); // build the member selection path bit of the parameter var paramArray = new string[member.ParameterCount]; for (int idx = 0; idx < member.ParameterCount; idx++) { var parameter = member.Parameters[idx]; paramArray[idx] = string.Format(paramPath, idx + 1, parameter.Type); } var parameterString = string.Join(paramSeparator, paramArray); var memberPath = typePath + string.Format(memberPathTemplate, member.Kind, member.Name, member.ParameterCount, parameterString); // build the actual parameter path for (int idx = 0; idx < member.ParameterCount; idx++) { var eachPath = memberPath + string.Format(parameterPathTemplate, idx + 1); var parameter = member.Parameters[idx]; // return the actual metadata entry yield return(new XElement("attr", new[] { new XAttribute("path", eachPath), new XAttribute("name", "managedName") }) { Value = parameter.ManagedName }); } } } } }
public void EnsueValidAndUnique(GeneratorParameters generatorParameters) { var addedParamNames = new List <string>(); for (int idx = 0; idx < ParameterCount; idx++) { var parameter = Parameters[idx]; var managedName = parameter.Name; if (generatorParameters.ForceMeaningfulParameterNames) { // if the parameter name is generated, try harder var isGenerated = managedName.StartsWith("p") && managedName.Length > 1 && char.IsDigit(managedName[1]); if (isGenerated) { // remove generics part (eg: SomeType<T>) var type = genericTemplate.Replace(parameter.Type, string.Empty); // get the type as the parameter name type = type.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault() ?? "param"; // change arrays if (type.EndsWith("[]")) { type = type.Replace("[]", "Array"); } // remove invalid characters type = alphanumericTemplate.Replace(type, string.Empty); // make sure it is camel case managedName = type[0].ToString().ToLower() + type.Substring(1); } } // fix any bad C# parameter names if (generatorParameters.ParameterCasing != TextCasing.Original) { if (generatorParameters.ParameterCasing == TextCasing.Pascal) { managedName = char.ToUpper(managedName[0]) + string.Concat(managedName.Skip(1)); } else if (generatorParameters.ParameterCasing == TextCasing.Camel) { managedName = char.ToLower(managedName[0]) + string.Concat(managedName.Skip(1)); } } if (ReservedWords.Contains(managedName)) { managedName = generatorParameters.ReservedPrefix + managedName + generatorParameters.ReservedSuffix; } //if (!managedName.StartsWith("@")) //{ // managedName = "@" + managedName; //} // make sure the name is unique for this method var tmpName = managedName; int pi = 2; while (addedParamNames.Contains(tmpName)) { tmpName = managedName + pi++; } addedParamNames.Add(tmpName); parameter.ManagedName = tmpName; } }