Пример #1
0
        internal static int Compare(FoundationDiskFileDescriptor x, FoundationDiskFileDescriptor y)
        {
            var result = 0;

            if (x == null)
            {
                if (y != null)
                {
                    result = -1;
                }
            }
            else
            {
                if (y == null)
                {
                    result = 1;
                }
                else
                {
                    result = x.ModuleOrder.CompareTo(y.ModuleOrder);
                    if (result == 0)
                    {
                        result = string.Compare(x.FileNameWithoutExtension, y.FileNameWithoutExtension, StringComparison.OrdinalIgnoreCase);
                        if (result == 0)
                        {
                            result = x.FileCreationTimeTicks.CompareTo(y.FileCreationTimeTicks);
                        }
                    }
                }
            }
            return(result);
        }
        public override FileDescriptor[] GetFiles(string structureName, string searchPattern, Selector[] selectors)
        {
            // Collect all files in given directories conforming to search pattern and selectors.
            List <FoundationDiskFileDescriptor> list;

            var cacheKey = BuildCacheKey(selectors, ModulesDirectoryPath, structureName, searchPattern);

            lock (_cache)
            {
                if (_cache.ContainsKey(cacheKey))
                {
                    list = _cache[cacheKey];
                }
                else
                {
                    list = new List <FoundationDiskFileDescriptor>();

                    var moduleOrderByName = new Dictionary <string, int>();
                    var index             = 0;
                    foreach (var name in FileResolver.GetOrderedModules())
                    {
                        moduleOrderByName.Add(name.ToUpperInvariant(), index++);
                    }

                    if (Directory.Exists(ModulesDirectoryPath))
                    {
                        foreach (var modulePath in Directory.GetDirectories(ModulesDirectoryPath))
                        {
                            var searchPath = string.Concat(modulePath, Path.DirectorySeparatorChar, SearchDirectoryName, Path.DirectorySeparatorChar, structureName);
                            if (Directory.Exists(searchPath))
                            {
                                foreach (var filePath in Directory.GetFiles(searchPath, searchPattern, _searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                                {
                                    var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

                                    if (selectors == null || ValidateFileName(fileNameWithoutExtension, selectors))
                                    {
                                        var moduleName = Path.GetFileName(modulePath);

                                        var descriptor = new FoundationDiskFileDescriptor(this)
                                        {
                                            ModuleName = moduleName,
                                            FileNameWithoutExtension = fileNameWithoutExtension,
                                            FilePath = filePath
                                        };

                                        var moduleOrder = int.MaxValue;

                                        var moduleKey = moduleName.ToUpperInvariant();
                                        if (moduleOrderByName.ContainsKey(moduleKey))
                                        {
                                            moduleOrder = moduleOrderByName[moduleKey];
                                        }

                                        descriptor.ModuleOrder           = moduleOrder;
                                        descriptor.FileCreationTimeTicks = File.GetCreationTime(filePath).Ticks;

                                        list.Add(descriptor);
                                    }
                                }
                            }
                        }
                    }

                    list.Sort(FoundationDiskFileDescriptor.Compare);
                    _cache.Add(cacheKey, list);
                }
            }

            return(list.ToArray());
        }