<commit_before>﻿/**
 * Copyright 2016 d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.ComponentModel.Composition;
using biz.dfch.CS.Appclusive.Public;
using biz.dfch.CS.Appclusive.Scheduler.Public;
using biz.dfch.CS.Appclusive.Public.Plugins;
using System.Diagnostics;
using System.Threading;

namespace biz.dfch.CS.Appclusive.Scheduler.Extensions
{
    [Export(typeof(IAppclusivePlugin))]
    [ExportMetadata("Type", "PowerShellScript")]
    [ExportMetadata("Priority", int.MinValue)]
    [ExportMetadata("Role", "default")]
    public class PowerShellScriptPlugin : SchedulerPluginBase
    {
        public const string SCRIPT_NAME_KEY = "ScriptName";

        private PowerShellScriptPluginConfiguration configuration;
        public override DictionaryParameters Configuration 
        { 
            get
            {
                return configuration.Convert(true);
            }
            set
            {
                var result = UpdateConfiguration(value);
                configuration = result;
            }
        }

        private PowerShellScriptPluginConfiguration UpdateConfiguration(DictionaryParameters parameters)
        {
            Contract.Requires(parameters.IsValid());

            // check for endpoint
            var configurationBase = SchedulerPluginConfigurationBase.Convert<SchedulerPluginConfigurationBase>(parameters);
            Contract.Assert(configurationBase.IsValid());
            
            var configurationManager = new PowerShellScriptPluginConfigurationManager(configurationBase.Endpoints);
            parameters.Add("ComputerName", configurationManager.GetComputerName());
            parameters.Add("ConfigurationName", configurationManager.GetConfigurationNameTemplate());
            parameters.Add("ScriptBase", configurationManager.GetScriptBase());
            parameters.Add("Credential", configurationManager.GetCredentials());

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration ...");
            message.AppendLine();

            foreach(KeyValuePair<string, object> item in parameters)
            {
                message.AppendFormat("{0}: '{1}'", item.Key, item.Value ?? item.Value.ToString());
                message.AppendLine();
            }
            message.AppendLine();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration COMPLETED.");
            
            Logger.WriteLine(message.ToString());

            configuration = PowerShellScriptPluginConfiguration.Convert<PowerShellScriptPluginConfiguration>(parameters, true);
            Contract.Assert(configuration.IsValid());

            return configuration;
        }

        public override bool Initialise(DictionaryParameters parameters, IAppclusivePluginLogger logger, bool activate)
        {
            var result = false;

            result = base.Initialise(parameters, logger, activate);
            if(!configuration.IsValid())
            {
                return result;
            }

            return result;
        }

        public override bool Invoke(DictionaryParameters parameters, IInvocationResult invocationResult)
        {
            Contract.Requires("2" == invocationResult.Version, "This plugin only supports non-serialisable invocation results.");

            var fReturn = false;

            var result = base.Invoke(parameters, invocationResult);
            if(!result)
            {
                return result;
            }

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.Invoke ...");
            message.AppendLine();

            Logger.WriteLine(message.ToString());
            message.Clear();

            var scriptPathAndName = parameters[SCRIPT_NAME_KEY] as string;
<commit_msg>remove script name from parameters that get passed to the script invocation<commit_after>﻿/**
 * Copyright 2016 d-fens GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.ComponentModel.Composition;
using biz.dfch.CS.Appclusive.Public;
using biz.dfch.CS.Appclusive.Scheduler.Public;
using biz.dfch.CS.Appclusive.Public.Plugins;
using System.Diagnostics;
using System.Threading;

namespace biz.dfch.CS.Appclusive.Scheduler.Extensions
{
    [Export(typeof(IAppclusivePlugin))]
    [ExportMetadata("Type", "PowerShellScript")]
    [ExportMetadata("Priority", int.MinValue)]
    [ExportMetadata("Role", "default")]
    public class PowerShellScriptPlugin : SchedulerPluginBase
    {
        public const string SCRIPT_NAME_KEY = "ScriptName";

        private PowerShellScriptPluginConfiguration configuration;
        public override DictionaryParameters Configuration 
        { 
            get
            {
                return configuration.Convert(true);
            }
            set
            {
                var result = UpdateConfiguration(value);
                configuration = result;
            }
        }

        private PowerShellScriptPluginConfiguration UpdateConfiguration(DictionaryParameters parameters)
        {
            Contract.Requires(parameters.IsValid());

            // check for endpoint
            var configurationBase = SchedulerPluginConfigurationBase.Convert<SchedulerPluginConfigurationBase>(parameters);
            Contract.Assert(configurationBase.IsValid());
            
            var configurationManager = new PowerShellScriptPluginConfigurationManager(configurationBase.Endpoints);
            parameters.Add("ComputerName", configurationManager.GetComputerName());
            parameters.Add("ConfigurationName", configurationManager.GetConfigurationNameTemplate());
            parameters.Add("ScriptBase", configurationManager.GetScriptBase());
            parameters.Add("Credential", configurationManager.GetCredentials());

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration ...");
            message.AppendLine();

            foreach(KeyValuePair<string, object> item in parameters)
            {
                message.AppendFormat("{0}: '{1}'", item.Key, item.Value ?? item.Value.ToString());
                message.AppendLine();
            }
            message.AppendLine();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration COMPLETED.");
            
            Logger.WriteLine(message.ToString());

            configuration = PowerShellScriptPluginConfiguration.Convert<PowerShellScriptPluginConfiguration>(parameters, true);
            Contract.Assert(configuration.IsValid());

            return configuration;
        }

        public override bool Initialise(DictionaryParameters parameters, IAppclusivePluginLogger logger, bool activate)
        {
            var result = false;

            result = base.Initialise(parameters, logger, activate);
            if(!configuration.IsValid())
            {
                return result;
            }

            return result;
        }

        public override bool Invoke(DictionaryParameters parameters, IInvocationResult invocationResult)
        {
            Contract.Requires("2" == invocationResult.Version, "This plugin only supports non-serialisable invocation results.");

            var fReturn = false;

            var result = base.Invoke(parameters, invocationResult);
            if(!result)
            {
                return result;
            }

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.Invoke ...");
            message.AppendLine();

            Logger.WriteLine(message.ToString());
            message.Clear();

            var scriptPathAndName = parameters[SCRIPT_NAME_KEY] as string;
            parameters.Remove(SCRIPT_NAME_KEY);

{'fraction_non_alphanumeric': 0.08645720476706392, 'fraction_numerical': 0.0023835319609967496, 'mean_word_length': 2.663095238095238, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3874589', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2030, 'n_words': 520}