Exemple #1
0
        public async Task <IReadOnlyList <PackageCarrier> > Load(PackagesLock packagesLock)
        {
            var mapper       = new ModuleMapper(_framework, _modulesDirectory, Runtime.Windows, Environment.Is64BitProcess ? Architecture.x64 : Architecture.x86);
            var packageStack = mapper.BuildMap(packagesLock);

            var loadedPackages = new ConcurrentBag <PackageCarrier>();

            while (packageStack.Any())
            {
                var dependencyLayer = packageStack.Pop();

                await TaskCombinators.ThrottledAsync(dependencyLayer, (context, token) =>
                {
                    return(Task.Run(() =>
                    {
                        foreach (var module in LoadModule(context))
                        {
                            loadedPackages.Add(module);
                        }
                    }));
                }, CancellationToken.None);
            }

            Packages = Packages.AddRange(loadedPackages);

            return(loadedPackages.ToList());
        }
Exemple #2
0
        public async Task <FileCheckReport> Execute(ImportFilesRequest request, CancellationToken cancellationToken = default)
        {
            State.Status = ImportFilesStatus.Scanning;

            var fileInfos = new FileInformation[request.Files.Count];
            var counter   = -1; // as we always increment and the first index is zero

            await TaskCombinators.ThrottledAsync(request.Files, async (filename, _) =>
            {
                var index = Interlocked.Increment(ref counter);
                try
                {
                    var file = request.Directory.GetFile(filename);
                    if (file == null)
                    {
                        return;               // not found
                    }
                    fileInfos[index] = await _fileInformationLoader.Load(file);
                }
                finally
                {
                    State.Progress = (double)counter / request.Files.Count;
                }
            }, cancellationToken, 4);

            State.Status = ImportFilesStatus.Querying;
            _checkFilesWorker.State.PropertyChanged += (_, __) => State.Progress = _checkFilesWorker.State.Progress;

            return(await _checkFilesWorker.Execute(new CheckFilesRequest(fileInfos.Where(x => x != null).ToList(), request.Directory), cancellationToken));
        }
Exemple #3
0
        public async Task Load(IEnumerable <PackageIdentity> primaryPackages, PackagesLock packagesLock)
        {
            var mapper = new ModuleMapper(_project.Framework, _project.ModulesDirectory, _project.Runtime, _project.Architecture);
            var map    = mapper.BuildMap(packagesLock);

            var dependencyPaths = new Dictionary <AssemblyName, AssemblyInfo>(new AssemblyNameComparer());

            _dependencyAssemblies = dependencyPaths;

            while (map.TryPop(out var dependencyLayer))
            {
                foreach (var context in dependencyLayer.Where(x => !x.IsMazeModule))
                {
                    foreach (var file in Directory.GetFiles(context.LibraryDirectory, "*.dll"))
                    {
                        dependencyPaths.Add(AssemblyName.GetAssemblyName(file), new AssemblyInfo(file));
                    }
                }

                await TaskCombinators.ThrottledAsync(dependencyLayer.Where(x => x.IsMazeModule), (context, token) => Task.Run(() =>
                {
                    foreach (var file in Directory.GetFiles(context.LibraryDirectory, "*.dll"))
                    {
                        var assembly = _assemblyLoadContext.LoadFromAssemblyPath(file);

                        var typeDiscoveryService = new TypeDiscoveryService(assembly, context.Package);
                        typeDiscoveryService.DiscoverTypes(ModuleTypeMap);
                    }
                }), CancellationToken.None);
            }
        }
        public async Task EnsurePackagesInstalled(PackagesLock packagesLock)
        {
            var resources = _project.PrimarySources.Concat(_project.DependencySources)
                            .ToImmutableDictionary(x => x.GetResource <FindPackageByIdResource>(), x => x);
            var cache = GetDefaultResolutionContext().SourceCacheContext;

            var actions = (await TaskCombinators.ThrottledAsync(packagesLock, async(package, token) =>
            {
                if (!_project.ModulesDirectory.ModuleExists(package.Key))
                {
                    foreach (var(resource, repo) in resources)
                    {
                        var versions =
                            await resource.GetAllVersionsAsync(package.Key.Id, cache, _logger, CancellationToken.None);
                        if (versions.Any(x => x == package.Key.Version))
                        {
                            return(ResolvedAction.CreateInstall(package.Key, repo));
                        }
                    }
                }
                return(null);
            }, CancellationToken.None)).Where(x => x != null).ToList();

            if (actions.Any())
            {
                await ExecuteActionsAsync(actions, GetDefaultDownloadContext(), CancellationToken.None);
            }
        }
Exemple #5
0
        public async Task <IActionResult> GetPackageInfo([FromBody] List <PackageIdentity> packages, [FromServices] IModuleProject moduleProject)
        {
            var resourceAsync = await moduleProject.LocalSourceRepository.GetResourceAsync <PackageMetadataResource>();

            var context = new SourceCacheContext();

            var metadata = await TaskCombinators.ThrottledAsync(packages,
                                                                (identity, token) => resourceAsync.GetMetadataAsync(identity, context, NullLogger.Instance, token), HttpContext.RequestAborted);

            return(Ok(metadata.Select(Mapper.Map <PackageSearchMetadata>).ToList()));
        }
Exemple #6
0
        public Task <IEnumerable <DirectoryEntry> > GetDirectoryEntries(DirectoryInfoEx directory, CancellationToken token)
        {
            var entries = directory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly,
                                                         () => token.IsCancellationRequested);

            return(TaskCombinators.ThrottledAsync(entries, (entry, _) => Task.Run(() =>
            {
                using (entry)
                    return GetDirectoryEntry(entry, directory);
            }), token));
        }
Exemple #7
0
        public Task Execute(IEnumerable <TargetId> attenders, ConcurrentDictionary <TargetId, IServiceScope> attenderScopes, CancellationToken cancellationToken)
        {
            return(TaskCombinators.ThrottledAsync(attenders, async(id, token) =>
            {
                if (!attenderScopes.TryGetValue(id, out var scope))
                {
                    scope = _services.CreateScope();
                }

                using (scope)
                {
                    await ExecuteAttender(id, _services, token);
                }
            }, cancellationToken));
        }
Exemple #8
0
        public Task <IEnumerable <FileExplorerEntry> > GetEntries(DirectoryInfoEx directory, CancellationToken token)
        {
            var entries = directory.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly,
                                                             () => token.IsCancellationRequested);

            return(TaskCombinators.ThrottledAsync(entries, (entry, _) => Task.Run(() =>
            {
                using (entry)
                {
                    if (entry.IsFolder)
                    {
                        return GetDirectoryEntry((DirectoryInfoEx)entry, directory);
                    }

                    return GetFileEntry((FileInfoEx)entry);
                }
            }, token), CancellationToken.None));
        }
Exemple #9
0
        public async Task <IReadOnlyDictionary <PackageIdentity, List <Type> > > GetControllers()
        {
            var result             = new ConcurrentDictionary <PackageIdentity, List <Type> >();
            var mazeControllerType = typeof(MazeController);

            await TaskCombinators.ThrottledAsync(_catalog.Packages, (carrier, token) => Task.Run(() =>
            {
                var types = carrier.Assembly.GetExportedTypes();

                var controllers = types.Where(x => mazeControllerType.IsAssignableFrom(x)).ToList();
                result.AddOrUpdate(carrier.Context.Package, controllers, (identity, list) =>
                {
                    list.AddRange(controllers);
                    return(list);
                });
            }), CancellationToken.None);

            return(result);
        }
Exemple #10
0
        public async Task <IEnumerable <FileExplorerEntry> > GetEntriesKeepOrder(DirectoryInfoEx directory, CancellationToken token)
        {
            var entries = directory.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToList();
            var result  = new FileExplorerEntry[entries.Count];

            await TaskCombinators.ThrottledAsync(entries, (entry, _) => Task.Run(() =>
            {
                var index = entries.IndexOf(entry);
                using (entry)
                {
                    if (entry.IsFolder)
                    {
                        result[index] = GetDirectoryEntry((DirectoryInfoEx)entry, directory);
                    }
                    else
                    {
                        result[index] = GetFileEntry((FileInfoEx)entry);
                    }
                }
            }), token);

            return(result);
        }
        public async Task <IActionResult> GetPathTree([FromBody] PathTreeRequestDto request, [FromQuery] bool keepOrder,
                                                      CancellationToken cancellationToken)
        {
            var response        = new PathTreeResponseDto();
            var directoryHelper = new DirectoryHelper();

            Task <IEnumerable <FileExplorerEntry> > entriesTask = null;

            if (request.RequestEntries)
            {
                entriesTask = keepOrder
                    ? directoryHelper.GetEntriesKeepOrder(request.Path, cancellationToken)
                    : directoryHelper.GetEntries(request.Path, cancellationToken);
            }

            if (request.RequestedDirectories?.Count > 0)
            {
                var pathDirectories = PathHelper.GetPathDirectories(request.Path).ToList();
                var directories     = new ConcurrentDictionary <int, List <DirectoryEntry> >();

                await TaskCombinators.ThrottledAsync(request.RequestedDirectories, async (i, token) =>
                {
                    var directoryPath = pathDirectories[i];
                    directories.TryAdd(i,
                                       (await directoryHelper.GetDirectoryEntries(directoryPath, cancellationToken)).ToList());
                }, CancellationToken.None);

                response.Directories = directories;
            }

            if (entriesTask != null)
            {
                response.Entries = (await entriesTask).ToList();
            }

            return(Ok(response));
        }
        public async Task <FileCheckReport> Execute(CheckFilesRequest request, CancellationToken cancellationToken = default)
        {
            var directory = request.Directory;
            var fileInfos = request.Files;

            // get all files from the repository
            var context = await _fileBaseContextFactory.BuildContext(directory);

            State.Status = CheckFilesStatus.Querying;

            var result  = new ConcurrentBag <IFileIssue>();
            var counter = -1;

            State.TotalFiles = context.IndexedFiles.Count;

            await TaskCombinators.ThrottledAsync(fileInfos, async (fileInformation, token) =>
            {
                var useCase  = _serviceProvider.GetRequiredService <ICheckFileIntegrityUseCase>();
                var response = await useCase.Handle(new CheckFileIntegrityRequest(fileInformation, context));
                if (useCase.HasError)
                {
                    State.Errors.Add(fileInformation, useCase.Error !);
                }
                else
                {
                    foreach (var fileIssue in response !.Issues)
                    {
                        result.Add(fileIssue);
                    }
                }

                State.FilesProcessed = Interlocked.Increment(ref counter);
            }, cancellationToken);

            return(new FileCheckReport(result.Distinct(new EqualityComparerByValue <IFileIssue, string>(x => x.Identity)).ToList()));
        }
Exemple #13
0
        public async Task <List <ProcessDto> > GetProcesses()
        {
            await _getProcessesLock.WaitAsync();

            try
            {
                using (var processCollection = _searcher.Get())
                {
                    var dtos = (await TaskCombinators.ThrottledAsync(processCollection.Cast <ManagementObject>(), CreateProcessDto, CancellationToken))
                               .Where(x => x != null).ToList();

                    _latestProcessIds = new HashSet <int>(dtos.Select(x => x.ProcessId));
                    return(dtos);
                }
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _getProcessesLock.Release();
            }
        }