Esempio n. 1
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var script = await Script.Run(stateMonad, cancellationToken).Map(x => x.GetStringAsync());

            if (script.IsFailure)
            {
                return(script.ConvertFailure <Unit>());
            }

            Entity?vars = null;

            if (Variables != null)
            {
                var variables = await Variables.Run(stateMonad, cancellationToken);

                if (variables.IsFailure)
                {
                    return(variables.ConvertFailure <Unit>());
                }

                vars = variables.Value;
            }

            await PwshRunner.RunScript(script.Value, stateMonad.Logger, vars);

            return(Unit.Default);
        }
Esempio n. 2
0
        /// <inheritdoc />
        protected override async Task <Result <Array <Entity>, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var script = await Script.Run(stateMonad, cancellationToken).Map(x => x.GetStringAsync());

            if (script.IsFailure)
            {
                return(script.ConvertFailure <Array <Entity> >());
            }

            Entity?vars = null;

            if (Variables != null)
            {
                var variables = await Variables.Run(stateMonad, cancellationToken);

                if (variables.IsFailure)
                {
                    return(variables.ConvertFailure <Array <Entity> >());
                }

                vars = variables.Value;
            }

            PSDataCollection <PSObject>?input = null;

        #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
            async ValueTask <Result <Unit, IError> > AddObject(Entity x, CancellationToken ct)
            {
                input.Add(PwshRunner.PSObjectFromEntity(x));
                return(Unit.Default);
            }

        #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

            if (Input != null)
            {
                var inputStream = await Input.Run(stateMonad, cancellationToken);

                if (inputStream.IsFailure)
                {
                    return(inputStream.ConvertFailure <Array <Entity> >());
                }

                input = new PSDataCollection <PSObject>();

                _ = inputStream.Value.ForEach(AddObject, cancellationToken)
                    .ContinueWith(_ => input.Complete(), cancellationToken);
            }

            var stream = PwshRunner.GetEntityEnumerable(script.Value, stateMonad.Logger, vars, input)
                         .ToSCLArray();

            return(stream);
        }