Пример #1
0
        public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception = null, params object[] formatParameters)
        {
            if (logLevel < _logLevel)
            {
                return(_logger.Log(logLevel, messageFunc, exception, formatParameters));
            }

            var message = messageFunc?.Invoke();

            if (message != null)
            {
                _logDelegate.Invoke(logLevel, () => message, exception, formatParameters);
            }

            return(_logger.Log(logLevel, () => message, exception, formatParameters));
        }
Пример #2
0
        public static int MainImpl([NotNull] string[] args, LogLevel logLevel)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var environmentCts = new CancellationTokenSource();

            int exitCode;

            try
            {
                var headless = args.Any(x => string.Equals("--headless", x, StringComparison.Ordinal));
                var unitTest = args.Any(x => string.Equals("--unit-test", x, StringComparison.Ordinal));

                var snapOs = SnapOs.AnyOs;

                ConfigureNlog(snapOs);
                LogProvider.SetCurrentLogProvider(new NLogLogProvider());

                var snapInstallerLogger = LogProvider.GetLogger(ApplicationName);

                var environment = BuildEnvironment(snapOs, environmentCts, logLevel, snapInstallerLogger);
                exitCode = MainImpl(environment, snapInstallerLogger, headless, unitTest);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Exception thrown during installation: {e.Message}");
                exitCode = 1;
            }

            try
            {
                if (_mutexIsTaken)
                {
                    _mutexSingleInstanceWorkingDirectory.Dispose();
                }
            }
            catch (Exception)
            {
                // ignore
            }

            return(exitCode);
        }
Пример #3
0
        public static async Task <(int finalExitCode, SnapInstallerType finalInstallerType)> MainImplAsync([NotNull] string[] args, LogLevel logLevel,
                                                                                                           [NotNull] CancellationTokenSource environmentCts, [NotNull] ISnapOs snapOs,
                                                                                                           Func <IServiceContainer, ISnapInstallerEnvironment> containerBuilder = null)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (environmentCts == null)
            {
                throw new ArgumentNullException(nameof(environmentCts));
            }
            if (snapOs == null)
            {
                throw new ArgumentNullException(nameof(snapOs));
            }

            if (Environment.GetEnvironmentVariable("SNAPX_WAIT_DEBUGGER") == "1")
            {
                var process = Process.GetCurrentProcess();

                while (!Debugger.IsAttached)
                {
                    Console.WriteLine($"Waiting for debugger to attach... Process id: {process.Id}");
                    Thread.Sleep(1000);
                }

                Console.WriteLine("Debugger attached.");
            }

            int finalExitCode;
            var finalInstallerType = SnapInstallerType.None;

            try
            {
                var headless = args.Any(x => string.Equals("--headless", x, StringComparison.Ordinal));

                ConfigureNlog(snapOs);
                LogProvider.SetCurrentLogProvider(new NLogLogProvider());

                var snapInstallerLogger = LogProvider.GetLogger(ApplicationName);

                var container = BuildEnvironment(snapOs, environmentCts, logLevel, snapInstallerLogger);

                var environment = containerBuilder != null?
                                  containerBuilder.Invoke(container) :
                                      container.GetInstance <ISnapInstallerEnvironment>();

                var(installerExitCode, installerType) = await MainImplAsync(environment, snapInstallerLogger, headless);

                finalExitCode      = installerExitCode;
                finalInstallerType = installerType;
            }
            catch (Exception e)
            {
                await Console.Error.WriteLineAsync($"Exception thrown during installation: {e.Message}");

                finalExitCode = 1;
            }

            try
            {
                if (_mutexIsTaken)
                {
                    _mutexSingleInstanceWorkingDirectory.Dispose();
                }
            }
            catch (Exception)
            {
                // ignore
            }

            return(finalExitCode, finalInstallerType);
        }
Пример #4
0
        static IServiceContainer 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 <ISnapBinaryPatcher>(c => new SnapBinaryPatcher());
            container.Register <ISnapPack>(c => new SnapPack(
                                               c.GetInstance <ISnapFilesystem>(),
                                               c.GetInstance <ISnapAppReader>(),
                                               c.GetInstance <ISnapAppWriter>(),
                                               c.GetInstance <ISnapCryptoProvider>(),
                                               c.GetInstance <ISnapEmbeddedResources>(),
                                               c.GetInstance <ISnapBinaryPatcher>()));
            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>()));

            container.Register <ISnapInstallerEnvironment>(c => new SnapInstallerEnvironment(logLevel, globalCts, ApplicationName)
            {
                Container = container,
                Io        = c.GetInstance <ISnapInstallerIoEnvironment>()
            });

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

            return(container);
        }
Пример #5
0
 public LogForwarder(LogLevel logLevel, [NotNull] ILog logger, [NotNull] LogDelegate logDelegate)
 {
     _logLevel    = logLevel;
     _logger      = logger ?? throw new ArgumentNullException(nameof(logger));
     _logDelegate = logDelegate ?? throw new ArgumentNullException(nameof(logDelegate));
 }
Пример #6
0
 public SnapInstallerEnvironment(LogLevel logLevel, [NotNull] CancellationTokenSource cancellationTokenSource, [NotNull] string loggerName)
 {
     LogLevel = logLevel;
     _cancellationTokenSource = cancellationTokenSource ?? throw new ArgumentNullException(nameof(cancellationTokenSource));
     _loggerName = loggerName ?? throw new ArgumentNullException(nameof(loggerName));
 }