Beispiel #1
0
        static int MainImplAsync([NotNull] string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            using var cts = new CancellationTokenSource();

            ISnapOs snapOs;

            try
            {
                snapOs = SnapOs.AnyOs;
            }
            catch (PlatformNotSupportedException)
            {
                SnapLogger.Error($"Platform is not supported: {RuntimeInformation.OSDescription}");
                return(1);
            }
            catch (Exception e)
            {
                SnapLogger.ErrorException("Exception thrown while initializing snap os", e);
                return(1);
            }

            snapOs.InstallExitSignalHandler(async() =>
            {
                cts.Cancel();
                await OnExitAsync();
            });

            var workingDirectory = Environment.CurrentDirectory;

            if (!workingDirectory.EndsWith(snapOs.Filesystem.DirectorySeparator))
            {
                workingDirectory += snapOs.Filesystem.DirectorySeparator;
            }

            var toolWorkingDirectory = snapOs.Filesystem.PathGetDirectoryName(typeof(Program).Assembly.Location);

            var snapCryptoProvider    = new SnapCryptoProvider();
            var snapEmbeddedResources = new SnapEmbeddedResources();

            snapEmbeddedResources.ExtractCoreRunLibAsync(snapOs.Filesystem, snapCryptoProvider,
                                                         toolWorkingDirectory, snapOs.OsPlatform).GetAwaiter().GetResult();
            var snapXEmbeddedResources = new SnapxEmbeddedResources();

            var coreRunLib              = new CoreRunLib(snapOs.Filesystem, snapOs.OsPlatform, toolWorkingDirectory);
            var snapAppReader           = new SnapAppReader();
            var snapAppWriter           = new SnapAppWriter();
            var snapPack                = new SnapPack(snapOs.Filesystem, snapAppReader, snapAppWriter, snapCryptoProvider, snapEmbeddedResources);
            var snapExtractor           = new SnapExtractor(snapOs.Filesystem, snapPack, snapEmbeddedResources);
            var snapSpecsReader         = new SnapAppReader();
            var snapNetworkTimeProvider = new SnapNetworkTimeProvider("time.cloudflare.com", 123);
            var snapHttpClient          = new SnapHttpClient(new HttpClient());

            var nugetServiceCommandPack    = new NugetService(snapOs.Filesystem, new NugetLogger(SnapPackLogger));
            var nugetServiceCommandPromote = new NugetService(snapOs.Filesystem, new NugetLogger(SnapPromoteLogger));
            var nugetServiceCommandDemote  = new NugetService(snapOs.Filesystem, new NugetLogger(SnapDemoteLogger));
            var nugetServiceCommandRestore = new NugetService(snapOs.Filesystem, new NugetLogger(SnapRestoreLogger));
            var nugetServiceNoopLogger     = new NugetService(snapOs.Filesystem, new NugetLogger(new LogProvider.NoOpLogger()));

            var snapPackageRestorer = new SnapPackageManager(snapOs.Filesystem, snapOs.SpecialFolders,
                                                             nugetServiceCommandPack, snapHttpClient,
                                                             snapCryptoProvider, snapExtractor, snapAppReader, snapPack);

            var distributedMutexClient = new DistributedMutexClient(new JsonServiceClient("https://snapx.dev"));

            Console.CancelKeyPress += async(sender, eventArgs) =>
            {
                eventArgs.Cancel = !cts.IsCancellationRequested;
                cts.Cancel();

                await OnExitAsync();
            };

            return(MainAsync(args, coreRunLib, snapOs, snapExtractor, snapOs.Filesystem,
                             snapSpecsReader, snapCryptoProvider,
                             snapPack, snapAppWriter, snapXEmbeddedResources, snapPackageRestorer, snapNetworkTimeProvider,
                             nugetServiceCommandPack, nugetServiceCommandPromote, nugetServiceCommandDemote,
                             nugetServiceCommandRestore, nugetServiceNoopLogger, distributedMutexClient,
                             toolWorkingDirectory, workingDirectory, cts.Token));
        }
Beispiel #2
0
        static int MainAsync([NotNull] string[] args,
                             [NotNull] CoreRunLib coreRunLib,
                             [NotNull] ISnapOs snapOs, [NotNull] ISnapExtractor snapExtractor,
                             [NotNull] ISnapFilesystem snapFilesystem,
                             [NotNull] ISnapAppReader snapAppReader,
                             [NotNull] ISnapCryptoProvider snapCryptoProvider,
                             [NotNull] ISnapPack snapPack,
                             [NotNull] ISnapAppWriter snapAppWriter,
                             [NotNull] SnapxEmbeddedResources snapXEmbeddedResources,
                             [NotNull] SnapPackageManager snapPackageManager,
                             [NotNull] ISnapNetworkTimeProvider snapNetworkTimeProvider,
                             [NotNull] INugetService nugetServiceCommandPack,
                             [NotNull] INugetService nugetServiceCommandPromote,
                             [NotNull] INugetService nugetServiceCommandDemote,
                             INugetService nugetServiceCommandRestore,
                             [NotNull] INugetService nugetServiceNoopLogger,
                             [NotNull] IDistributedMutexClient distributedMutexClient,
                             [NotNull] string toolWorkingDirectory,
                             [NotNull] string workingDirectory,
                             CancellationToken cancellationToken)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (coreRunLib == null)
            {
                throw new ArgumentNullException(nameof(coreRunLib));
            }
            if (snapOs == null)
            {
                throw new ArgumentNullException(nameof(snapOs));
            }
            if (snapExtractor == null)
            {
                throw new ArgumentNullException(nameof(snapExtractor));
            }
            if (snapFilesystem == null)
            {
                throw new ArgumentNullException(nameof(snapFilesystem));
            }
            if (snapAppReader == null)
            {
                throw new ArgumentNullException(nameof(snapAppReader));
            }
            if (snapCryptoProvider == null)
            {
                throw new ArgumentNullException(nameof(snapCryptoProvider));
            }
            if (snapPack == null)
            {
                throw new ArgumentNullException(nameof(snapPack));
            }
            if (snapAppWriter == null)
            {
                throw new ArgumentNullException(nameof(snapAppWriter));
            }
            if (snapXEmbeddedResources == null)
            {
                throw new ArgumentNullException(nameof(snapXEmbeddedResources));
            }
            if (snapPackageManager == null)
            {
                throw new ArgumentNullException(nameof(snapPackageManager));
            }
            if (snapNetworkTimeProvider == null)
            {
                throw new ArgumentNullException(nameof(snapNetworkTimeProvider));
            }
            if (nugetServiceCommandPromote == null)
            {
                throw new ArgumentNullException(nameof(nugetServiceCommandPromote));
            }
            if (nugetServiceNoopLogger == null)
            {
                throw new ArgumentNullException(nameof(nugetServiceNoopLogger));
            }
            if (distributedMutexClient == null)
            {
                throw new ArgumentNullException(nameof(distributedMutexClient));
            }
            if (toolWorkingDirectory == null)
            {
                throw new ArgumentNullException(nameof(toolWorkingDirectory));
            }
            if (workingDirectory == null)
            {
                throw new ArgumentNullException(nameof(workingDirectory));
            }

            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            return(Parser
                   .Default
                   .ParseArguments <DemoteOptions, PromoteOptions, PackOptions, Sha256Options, RcEditOptions, ListOptions, RestoreOptions, LockOptions>(args)
                   .MapResult(
                       (DemoteOptions opts) =>
            {
                var nuGetPackageSources = BuildNuGetPackageSources(snapFilesystem, SnapDemoteLogger);
                if (nuGetPackageSources == null)
                {
                    return 1;
                }
                return CommandDemoteAsync(opts, snapFilesystem, snapAppReader, snapAppWriter,
                                          nuGetPackageSources, nugetServiceCommandDemote, distributedMutexClient, snapPackageManager, snapPack,
                                          snapNetworkTimeProvider, snapExtractor, snapOs, snapXEmbeddedResources, coreRunLib,
                                          SnapDemoteLogger, workingDirectory, cancellationToken).GetAwaiter().GetResult();
            },
                       (PromoteOptions opts) =>
            {
                var nuGetPackageSources = BuildNuGetPackageSources(snapFilesystem, SnapPromoteLogger);
                if (nuGetPackageSources == null)
                {
                    return 1;
                }
                return CommandPromoteAsync(opts, snapFilesystem, snapAppReader, snapAppWriter,
                                           nuGetPackageSources, nugetServiceCommandPromote, distributedMutexClient, snapPackageManager, snapPack,
                                           snapOs.SpecialFolders,
                                           snapNetworkTimeProvider, snapExtractor, snapOs, snapXEmbeddedResources, coreRunLib,
                                           SnapPromoteLogger, workingDirectory, cancellationToken).GetAwaiter().GetResult();
            },
                       (PackOptions opts) =>
            {
                var nuGetPackageSources = BuildNuGetPackageSources(snapFilesystem, SnapPackLogger);
                if (nuGetPackageSources == null)
                {
                    return 1;
                }
                return CommandPackAsync(opts, snapFilesystem, snapAppReader, snapAppWriter,
                                        nuGetPackageSources, snapPack, nugetServiceCommandPack, snapOs, snapXEmbeddedResources,
                                        snapExtractor, snapPackageManager, coreRunLib,
                                        snapNetworkTimeProvider, SnapPackLogger, distributedMutexClient,
                                        workingDirectory, cancellationToken).GetAwaiter().GetResult();
            },
                       (Sha256Options opts) => CommandSha256(opts, snapFilesystem, snapCryptoProvider, SnapLogger),
                       (RcEditOptions opts) => CommandRcEdit(opts, coreRunLib, snapFilesystem, SnapLogger),
                       (ListOptions opts) =>
            {
                var nuGetPackageSources = BuildNuGetPackageSources(snapFilesystem, SnapListLogger);
                if (nuGetPackageSources == null)
                {
                    return 1;
                }
                return CommandListAsync(opts, snapFilesystem, snapAppReader,
                                        nuGetPackageSources, nugetServiceNoopLogger, snapExtractor, SnapListLogger,
                                        workingDirectory, cancellationToken).GetAwaiter().GetResult();
            },
                       (RestoreOptions opts) =>
            {
                var nuGetPackageSources = BuildNuGetPackageSources(snapFilesystem, SnapRestoreLogger);
                if (nuGetPackageSources == null)
                {
                    return 1;
                }
                return CommandRestoreAsync(opts, snapFilesystem, snapAppReader, snapAppWriter,
                                           nuGetPackageSources, snapPackageManager, snapOs,
                                           snapXEmbeddedResources, coreRunLib, snapPack,
                                           SnapRestoreLogger, workingDirectory, cancellationToken).GetAwaiter().GetResult();
            },
                       (LockOptions opts) => CommandLock(opts, distributedMutexClient, snapFilesystem, snapAppReader, SnapLockLogger, workingDirectory, cancellationToken).GetAwaiter().GetResult(),
                       errs =>
            {
                snapOs.EnsureConsole();
                return 0;
            }));
        }