コード例 #1
0
ファイル: Program.cs プロジェクト: peters/snapx
        static int MainImpl([NotNull] SnapInstallerEnvironment snapInstallerEnvironment, [NotNull] ILog snapInstallerLogger, bool headless, bool unitTest)
        {
            if (snapInstallerEnvironment == null)
            {
                throw new ArgumentNullException(nameof(snapInstallerEnvironment));
            }
            if (snapInstallerLogger == null)
            {
                throw new ArgumentNullException(nameof(snapInstallerLogger));
            }

            if (unitTest)
            {
                return(UnitTestExitCode);
            }

            var snapOs = snapInstallerEnvironment.Container.GetInstance <ISnapOs>();
            var snapEmbeddedResources = snapInstallerEnvironment.Container.GetInstance <ISnapEmbeddedResources>();
            var snapCryptoProvider    = snapInstallerEnvironment.Container.GetInstance <ISnapCryptoProvider>();

            var thisExeWorkingDirectory = snapInstallerEnvironment.Io.ThisExeWorkingDirectory;
            var workingDirectory        = snapInstallerEnvironment.Io.WorkingDirectory;

            snapEmbeddedResources.ExtractCoreRunLibAsync(snapOs.Filesystem, snapCryptoProvider, thisExeWorkingDirectory, snapOs.OsPlatform).GetAwaiter().GetResult();
            var coreRunLib    = new CoreRunLib(snapOs.Filesystem, snapOs.OsPlatform, thisExeWorkingDirectory);
            var snapInstaller = snapInstallerEnvironment.Container.GetInstance <ISnapInstaller>();
            var snapInstallerEmbeddedResources = snapInstallerEnvironment.Container.GetInstance <ISnapInstallerEmbeddedResources>();
            var snapPack       = snapInstallerEnvironment.Container.GetInstance <ISnapPack>();
            var snapAppReader  = snapInstallerEnvironment.Container.GetInstance <ISnapAppReader>();
            var snapAppWriter  = snapInstallerEnvironment.Container.GetInstance <ISnapAppWriter>();
            var snapFilesystem = snapInstallerEnvironment.Container.GetInstance <ISnapFilesystem>();

            snapFilesystem.DirectoryCreateIfNotExists(snapOs.SpecialFolders.InstallerCacheDirectory);
            var snapPackageManager         = snapInstallerEnvironment.Container.GetInstance <ISnapPackageManager>();
            var snapExtractor              = snapInstallerEnvironment.Container.GetInstance <ISnapExtractor>();
            var nugetServiceCommandInstall = new NugetService(snapOs.Filesystem, new NugetLogger(snapInstallerLogger));

            int RunInstaller()
            {
                return(Install(snapInstallerEnvironment, snapInstallerEmbeddedResources,
                               snapInstaller, snapFilesystem, snapPack, snapOs, coreRunLib, snapAppReader,
                               snapAppWriter, nugetServiceCommandInstall, snapPackageManager, snapExtractor, snapInstallerLogger,
                               headless));
            }

            try
            {
                var mutexName = snapCryptoProvider.Sha256(Encoding.UTF8.GetBytes(workingDirectory));
                _mutexSingleInstanceWorkingDirectory = new Mutex(true, $"Global\\{mutexName}", out var createdNew);
                if (!createdNew)
                {
                    snapInstallerLogger.Error("Setup is already running, exiting...");
                    return(-1);
                }

                _mutexIsTaken = true;
            }
            catch (Exception e)
            {
                snapInstallerLogger.ErrorException("Error creating installer mutex, exiting...", e);
                return(-1);
            }

            return(RunInstaller());
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: peters/snapx
        static SnapInstallerEnvironment BuildEnvironment(ISnapOs snapOs, CancellationTokenSource globalCts, LogLevel logLevel, ILog logger)
        {
            var container = new ServiceContainer();

            var thisExeWorkingDirectory = snapOs.Filesystem.PathGetDirectoryName(typeof(Program).Assembly.Location);
            var workingDirectory        = Environment.CurrentDirectory;

            container.Register(c => snapOs);
            container.Register(c => snapOs.SpecialFolders);

            container.Register(c => snapOs.Filesystem);
            container.Register <ISnapHttpClient>(c => new SnapHttpClient(new HttpClient()));
            container.Register <ISnapEmbeddedResources>(c => new SnapEmbeddedResources());
            container.Register <ISnapInstallerEmbeddedResources>(c => new SnapInstallerEmbeddedResources());
            container.Register <INuGetPackageSources>(c =>
                                                      new NuGetMachineWidePackageSources(
                                                          c.GetInstance <ISnapFilesystem>(),
                                                          workingDirectory
                                                          )
                                                      );
            container.Register <ISnapCryptoProvider>(c => new SnapCryptoProvider());
            container.Register <ISnapAppReader>(c => new SnapAppReader());
            container.Register <ISnapAppWriter>(c => new SnapAppWriter());
            container.Register <ISnapPack>(c => new SnapPack(
                                               c.GetInstance <ISnapFilesystem>(),
                                               c.GetInstance <ISnapAppReader>(),
                                               c.GetInstance <ISnapAppWriter>(),
                                               c.GetInstance <ISnapCryptoProvider>(),
                                               c.GetInstance <ISnapEmbeddedResources>()));
            container.Register <ISnapExtractor>(c => new SnapExtractor(
                                                    c.GetInstance <ISnapFilesystem>(),
                                                    c.GetInstance <ISnapPack>(),
                                                    c.GetInstance <ISnapEmbeddedResources>()));
            container.Register <ISnapInstaller>(c => new SnapInstaller(
                                                    c.GetInstance <ISnapExtractor>(),
                                                    c.GetInstance <ISnapPack>(),
                                                    c.GetInstance <ISnapOs>(),
                                                    c.GetInstance <ISnapEmbeddedResources>(),
                                                    c.GetInstance <ISnapAppWriter>()
                                                    ));
            container.Register <ISnapNugetLogger>(c => new NugetLogger(logger));
            container.Register <INugetService>(c => new NugetService(
                                                   c.GetInstance <ISnapFilesystem>(),
                                                   c.GetInstance <ISnapNugetLogger>())
                                               );
            container.Register <ISnapPackageManager>(c => new SnapPackageManager(
                                                         c.GetInstance <ISnapFilesystem>(),
                                                         c.GetInstance <ISnapOsSpecialFolders>(),
                                                         c.GetInstance <INugetService>(),
                                                         c.GetInstance <ISnapHttpClient>(),
                                                         c.GetInstance <ISnapCryptoProvider>(),
                                                         c.GetInstance <ISnapExtractor>(),
                                                         c.GetInstance <ISnapAppReader>(),
                                                         c.GetInstance <ISnapPack>()));

            var ioEnvironment = new SnapInstallerIoEnvironment
            {
                WorkingDirectory        = workingDirectory,
                ThisExeWorkingDirectory = thisExeWorkingDirectory,
                SpecialFolders          = container.GetInstance <ISnapOsSpecialFolders>()
            };

            var environment = new SnapInstallerEnvironment(logLevel, globalCts, ApplicationName)
            {
                Container = container,
                Io        = ioEnvironment
            };

            container.Register <ISnapInstallerEnvironment>(_ => environment);
            container.Register(_ => environment.Io);

            return(environment);
        }