private static async Task <string> GetXmlDocumentationPathAsync(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 assemblyDirectory = DynamicApis.PathGetDirectoryName((string)assembly.Location);
                var path = DynamicApis.PathCombine(assemblyDirectory, (string)assemblyName.Name + ".xml");
                if (await DynamicApis.FileExistsAsync(path).ConfigureAwait(false))
                {
                    return(path);
                }

                if (ReflectionExtensions.HasProperty(assembly, "CodeBase"))
                {
                    path = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(assembly.CodeBase
                                                                                    .Replace("file:///", string.Empty)), assemblyName.Name + ".xml")
                           .Replace("file:\\", string.Empty);

                    if (await DynamicApis.FileExistsAsync(path).ConfigureAwait(false))
                    {
                        return(path);
                    }
                }

                var currentDomain = Type.GetType("System.AppDomain").GetRuntimeProperty("CurrentDomain").GetValue(null);
                if (currentDomain.HasProperty("BaseDirectory"))
                {
                    var baseDirectory = currentDomain.TryGetPropertyValue("BaseDirectory", "");
                    path = DynamicApis.PathCombine(baseDirectory, assemblyName.Name + ".xml");
                    if (await DynamicApis.FileExistsAsync(path).ConfigureAwait(false))
                    {
                        return(path);
                    }

                    return(DynamicApis.PathCombine(baseDirectory, "bin\\" + assemblyName.Name + ".xml"));
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 2
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);
            }
        }