Exemple #1
0
        public Task <int> Main(string[] args)
        {
            RuntimeOptions options;

            string[] programArgs;
            int      exitCode;

            bool shouldExit = ParseArgs(args, out options, out programArgs, out exitCode);

            if (shouldExit)
            {
                return(Task.FromResult(exitCode));
            }

            IFileWatcher watcher;

            if (options.WatchFiles)
            {
                watcher = new FileWatcher(ProjectResolver.ResolveRootDirectory(Path.GetFullPath(options.ApplicationBaseDirectory)));
            }
            else
            {
                watcher = NoopWatcher.Instance;
            }

            var host = new DefaultHost(options, _serviceProvider, _loadContextAccessor, watcher);

            if (host.Project == null)
            {
                return(Task.FromResult(-1));
            }

            var    lookupCommand = string.IsNullOrEmpty(options.ApplicationName) ? "run" : options.ApplicationName;
            string replacementCommand;

            if (host.Project.Commands.TryGetValue(lookupCommand, out replacementCommand))
            {
                // preserveSurroundingQuotes: false to imitate a shell. Shells remove quotation marks before calling
                // Main methods. Here however we are invoking Main() without involving a shell.
                var replacementArgs = CommandGrammar
                                      .Process(replacementCommand, GetVariable, preserveSurroundingQuotes: false)
                                      .ToArray();
                options.ApplicationName = replacementArgs.First();
                programArgs             = replacementArgs.Skip(1).Concat(programArgs).ToArray();
            }

            if (string.IsNullOrEmpty(options.ApplicationName) ||
                string.Equals(options.ApplicationName, "run", StringComparison.Ordinal))
            {
                options.ApplicationName = host.Project.EntryPoint ?? host.Project.Name;
            }

            IDisposable disposable = null;

            try
            {
                disposable = host.AddLoaders(_container);

                return(ExecuteMain(host, options.ApplicationName, programArgs)
                       .ContinueWith(async(t, state) =>
                {
                    ((IDisposable)state).Dispose();
                    return await t;
                },
                                     disposable).Unwrap());
            }
            catch
            {
                // If there's an error, dispose the host and throw
                if (disposable != null)
                {
                    disposable.Dispose();
                }

                throw;
            }
        }
Exemple #2
0
        public static int Main(string[] args)
        {
            DefaultHostOptions options;

            string[]    programArgs;
            int         exitCode;
            DefaultHost host;

            try
            {
                bool shouldExit = ParseArgs(args, out options, out programArgs, out exitCode);
                if (shouldExit)
                {
                    return(exitCode);
                }

                host = new DefaultHost(options, PlatformServices.Default.AssemblyLoadContextAccessor);

                if (host.Project == null)
                {
                    return(1);
                }

                var    lookupCommand = string.IsNullOrEmpty(options.ApplicationName) ? "run" : options.ApplicationName;
                string replacementCommand;
                if (host.Project.Commands.TryGetValue(lookupCommand, out replacementCommand))
                {
                    // preserveSurroundingQuotes: false to imitate a shell. Shells remove quotation marks before calling
                    // Main methods. Here however we are invoking Main() without involving a shell.
                    var replacementArgs = CommandGrammar
                                          .Process(replacementCommand, GetVariable, preserveSurroundingQuotes: false)
                                          .ToArray();
                    options.ApplicationName = replacementArgs.First();
                    programArgs             = replacementArgs.Skip(1).Concat(programArgs).ToArray();
                }

                if (string.IsNullOrEmpty(options.ApplicationName) ||
                    string.Equals(options.ApplicationName, "run", StringComparison.Ordinal))
                {
                    options.ApplicationName = host.Project.EntryPoint ?? host.Project.Name;
                }
            }
            catch (Exception ex)
            {
                throw SuppressStackTrace(ex);
            }

            IDisposable disposable = null;

            try
            {
                disposable = host.AddLoaders(PlatformServices.Default.AssemblyLoaderContainer);

                return(ExecuteMain(host, options.ApplicationName, programArgs)
                       .ContinueWith((t, state) =>
                {
                    ((IDisposable)state).Dispose();
                    return t.GetAwaiter().GetResult();
                },
                                     disposable).GetAwaiter().GetResult());
            }
            catch
            {
                // If there's an error, dispose the host and throw
                if (disposable != null)
                {
                    disposable.Dispose();
                }

                throw;
            }
        }