Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var host = HostUtilities.BuildAndRunHost(args, x => x.AddSingleton <IProcessUtility, ProcessUtility>().AddSingleton <IComparer <string>, WinStringComparer>());

            var logger     = host.Services.GetService <ILogger <Program> >();
            var sourcePath = args?.Length == 1 ? Path.GetFullPath(args[0]) : AppDomain.CurrentDomain.BaseDirectory;

            _ = sourcePath ?? throw new InvalidOperationException("sourcePath is null");
            if (!Directory.Exists(sourcePath))
            {
                throw new InvalidOperationException($"Directory does not exist: {sourcePath}");
            }

            var destinationPath = Path.Combine(sourcePath, ProjectBuiltinNugetDirectoryName);

            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }

            var csprojFiles        = Directory.EnumerateFiles(sourcePath, "*.csproj", SearchOption.AllDirectories);
            var packages           = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var nugetCacheRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages");
            var processUtility     = host.Services.GetService <IProcessUtility>();
            var tempPath           = PathExtensions.CreateTempDirectory();

            logger.LogTrace("Using temp path {TempPath}...", tempPath);

            async Task CopyAllPackageDependenciesAsync(string packageFilePath)
            {
                await NugetUtilities.ExtractPackageAndApplyActionAsync(
                    packageFilePath,
                    processUtility ?? throw new InvalidOperationException("processUtility is null"),
                    tempPath ?? throw new InvalidOperationException("tempPath is null"),
                    async (dllFilePath, nuspecFilePath) =>
                {
                    var dependencies = await GetScarPackageReferencesFromNuspecFileAsync(nuspecFilePath).ConfigureAwait(false);
                    foreach (var nugetPackageInfo in dependencies)
                    {
                        logger.LogTrace("Trying to clone {PackageName} which is a dependency of {DependentPackageName}...", nugetPackageInfo.ToString(), NugetUtilities.ParseNugetPackageInfoForPath(packageFilePath)?.ToString());
                        await ClonePackageIfExistsInCacheAsync(
                            logger ?? throw new InvalidOperationException("logger is null"),
                            packages ?? throw new InvalidOperationException("packages is null"),
                            nugetPackageInfo,
                            nugetCacheRootPath ?? throw new InvalidOperationException("nugetCacheRootPath is null"),
                            destinationPath ?? throw new InvalidOperationException("destinationPath is null"),
                            CopyAllPackageDependenciesAsync)
                        .ConfigureAwait(false);
                    }
                })
                .ConfigureAwait(false);
            }

            var tasks = csprojFiles.Select(
                async csprojFilePath =>
            {
                var scarPackageReferences = await GetScarPackageReferencesFromProjectFileAsync(csprojFilePath).ConfigureAwait(false);
                foreach (var nugetPackageInfo in scarPackageReferences)
                {
                    logger.LogTrace("Trying to clone {PackageName}...", nugetPackageInfo.ToString());
                    await ClonePackageIfExistsInCacheAsync(logger, packages, nugetPackageInfo, nugetCacheRootPath, destinationPath, CopyAllPackageDependenciesAsync).ConfigureAwait(false);
                }
            });

            await Task.WhenAll(tasks).ConfigureAwait(false);

            DeleteNonExistingPackages(logger, packages, destinationPath);

            var fileNameComparer = host.Services.GetService <IComparer <string> >();

            DeleteOutdatedPackagesFromCache(logger, nugetCacheRootPath, fileNameComparer);
            DeleteOutdatedPackagesFromCache(logger, ScarLocalNugetPath, fileNameComparer);
            Directory.Delete(tempPath, true);
            logger.LogInformation("Deleted {TempPath}", tempPath);
        }