Beispiel #1
0
        /// <summary>
        /// Handle cases of specs in subdirectories having external references to specs also in subdirectories
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="jsonPath"></param>
        /// <returns></returns>
        public static string HandleSubdirectoryRelativeReferences(string fullPath, string jsonPath)
        {
            try
            {
                if (!DynamicApis.DirectoryExists(DynamicApis.PathGetDirectoryName(fullPath)))
                {
                    string fileName      = DynamicApis.PathGetFileName(fullPath);
                    string directoryName = DynamicApis.PathGetDirectoryName(fullPath);
                    string folderName    = directoryName.Replace("\\", "/").Split('/').Last();
                    if (!string.IsNullOrWhiteSpace(DynamicApis.DirectoryGetParent(directoryName)))
                    {
                        foreach (string subDir in DynamicApis.DirectoryGetDirectories(DynamicApis.DirectoryGetParent(directoryName)))
                        {
                            string expectedDir  = DynamicApis.PathCombine(subDir, folderName);
                            string expectedFile = DynamicApis.PathCombine(expectedDir, fileName);
                            if (DynamicApis.DirectoryExists(expectedDir))
                            {
                                fullPath = DynamicApis.PathCombine(expectedDir, fileName);
                                break;
                            }
                        }
                    }
                }

                if (!DynamicApis.FileExists(fullPath))
                {
                    string fileDir = DynamicApis.PathGetDirectoryName(fullPath);
                    if (DynamicApis.DirectoryExists(fileDir))
                    {
                        string   fileName    = DynamicApis.PathGetFileName(fullPath);
                        string[] pathPieces  = fullPath.Replace("\\", "/").Split('/');
                        string   subDirPiece = pathPieces[pathPieces.Length - 2];
                        foreach (string subDir in DynamicApis.DirectoryGetDirectories(fileDir))
                        {
                            string expectedFile = DynamicApis.PathCombine(subDir, fileName);
                            if (DynamicApis.FileExists(expectedFile) && DynamicApis.FileReadAllText(expectedFile).Contains(jsonPath.Split('/').Last()))
                            {
                                fullPath = DynamicApis.PathCombine(subDir, fileName);
                                break;
                            }
                        }
                    }
                }

                return(fullPath);
            }
            catch
            {
                return(fullPath);
            }
        }
        private static string GetXmlDocumentationPath(dynamic assembly)
        {
            try
            {
                if (assembly == null)
                {
                    return(null);
                }

                if (string.IsNullOrEmpty(assembly.Location))
                {
                    return(null);
                }

                var assemblyName = assembly.GetName();
                if (string.IsNullOrEmpty(assemblyName.Name))
                {
                    return(null);
                }

                var path = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(assembly.Location), assemblyName.Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                if (((object)assembly).GetType().GetRuntimeProperty("CodeBase") != null)
                {
                    path = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(assembly.CodeBase.Replace("file:///", string.Empty)), assemblyName.Name + ".xml")
                           .Replace("file:\\", string.Empty);
                    if (DynamicApis.FileExists(path))
                    {
                        return(path);
                    }
                }

                dynamic currentDomain = Type.GetType("System.AppDomain").GetRuntimeProperty("CurrentDomain").GetValue(null);
                path = DynamicApis.PathCombine(currentDomain.BaseDirectory, assemblyName.Name + ".xml");
                if (DynamicApis.FileExists(path))
                {
                    return(path);
                }

                return(DynamicApis.PathCombine(currentDomain.BaseDirectory, "bin\\" + assemblyName.Name + ".xml"));
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>Returns the contents of the "returns" or "param" XML documentation tag for the specified parameter.</summary>
        /// <param name="parameter">The reflected parameter or return info.</param>
        /// <param name="pathToXmlFile">The path to the XML documentation file.</param>
        /// <returns>The contents of the "returns" or "param" tag.</returns>
        public static string GetXmlDocumentation(this ParameterInfo parameter, string pathToXmlFile)
        {
            try
            {
                if (pathToXmlFile == null || DynamicApis.SupportsXPathApis == false || DynamicApis.SupportsFileApis == false)
                {
                    return(string.Empty);
                }

                lock (Lock)
                {
                    var assemblyName = parameter.Member.Module.Assembly.GetName();
                    if (Cache.ContainsKey(assemblyName.FullName) && Cache[assemblyName.FullName] == null)
                    {
                        return(string.Empty);
                    }

                    if (!DynamicApis.FileExists(pathToXmlFile))
                    {
                        Cache[assemblyName.FullName] = null;
                        return(string.Empty);
                    }

                    if (!Cache.ContainsKey(assemblyName.FullName))
                    {
                        Cache[assemblyName.FullName] = XDocument.Load(pathToXmlFile);
                    }

                    return(GetXmlDocumentation(parameter, Cache[assemblyName.FullName]));
                }
            }
            catch
            {
                return(string.Empty);
            }
        }