<commit_before>//-----------------------------------------------------------------------
// <copyright file="EnvironmentVariable.cs">(c) http://www.codeplex.com/MSBuildExtensionPack. This source is subject to the Microsoft Permissive License. See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx. All other rights reserved.</copyright>
//-----------------------------------------------------------------------
namespace MSBuild.ExtensionPack.Computer
{
    using System;
    using System.Globalization;
    using System.Management;
    using Microsoft.Build.Framework;

    /// <summary>
    /// <b>Valid TaskActions are:</b>
    /// <para><i>Get</i> (<b>Required: </b> Variable  <b>Output: </b> Value)</para>
    /// <para><i>Set</i> (<b>Required: </b> Variable, Value)</para>
    /// <para><b>Remote Execution Support:</b> For Get TaskAction only</para>
    /// </summary>
    /// <example>
    /// <code lang="xml"><![CDATA[
    /// <Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    ///     <PropertyGroup>
    ///         <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
    ///         <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
    ///     </PropertyGroup>
    ///     <Import Project="$(TPath)"/>
    ///     <Target Name="Default">
    ///         <!-- Set an environment variable. Note how special characters need to be escaped (http://msdn.microsoft.com/en-us/library/ms228186(VS.80).aspx) -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="PATH" Value="$(VCInstallDir)Common7\IDE%3B$(VCInstallDir)VC\BIN%3B$(VCInstallDir)Common7\Tools%3B$(VCInstallDir)Common7\Tools\bin%3B$(VCInstallDir)VC\PlatformSDK\bin%3B$(SDKInstallDir)bin%3B$(PATH)"/>
    ///         <!-- Set a new Environment Variable. The default target is Process -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="bddd"/>
    ///         <!-- Get the Environment Variable -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="ANewEnvSample">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="Get: $(EnvValue)"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="newddd"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="ANewEnvSample">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="Get: $(EnvValue)"/>
    ///         <!-- Set a new Environment Variable on a remote machine -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="bddd" MachineName="MediaHub"/>
    ///         <!-- Get an Environment Variable from a remote machine -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="INOCULAN" Target="Machine" MachineName="machinename" UserName="Administrator" UserPassword="<PASSWORD>">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="INOCULAN Get: $(EnvValue)"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="FT" Target="User" MachineName="machinename" UserName="Administrator" UserPassword="<PASSWORD>">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="FT Get: $(EnvValue)"/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    [HelpUrl("http://www.msbuildextensionpack.com/help/3.5.2.0/html/17390384-36ed-d9a9-b208-7e5207c778af.htm")]
    public class EnvironmentVariable : BaseTask
    {
        private const string GetTaskAction = "Get";
        private const string SetTaskAction = "Set";
        
        private EnvironmentVariableTarget target = EnvironmentVariableTarget.Process;

        [DropdownValue(GetTaskAction)]
        [DropdownValue(SetTaskAction)]
        public override string TaskAction
        {
            get { return base.TaskAction; }
            set { base.TaskAction = value; }
        }

        /// <summary>
        /// Gets or sets the value. May be a string array. If Value is not passed or empty for Set, the environment variable is deleted.
        /// </summary>
        [Output]
        [TaskAction(GetTaskAction, false)]
        [TaskAction(SetTaskAction, true)]
        public string[] Value { get; set; }

        /// <summary>
        /// The name of the Environment Variable to get or set.
        /// </summary>
        [Required]
        [TaskAction(GetTaskAction, true)]
        [TaskAction(SetTaskAction, true)]
        public string Variable { get; set; }

        /// <summary>
        /// Machine, Process or User. Defaults to Process
        /// </summary>
        public string Target
        {
            get
            {
                return this.target.ToString();
            }

            set
            {
                if (Enum.IsDefined(typeof(EnvironmentVariableTarget), value))
                {
                    this.target = (EnvironmentVariableTarget) Enum.Parse(typeof(EnvironmentVariableTarget), value);
                }
                else
                {
                    this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "The value '{0}' is not in the EnvironmentVariableTarget Enum. Use Process, User or Machine.", value));
                    return;
                }
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            switch (this.TaskAction)
            {
                case GetTaskAction:
                    this.Get();
                    break;
                case SetTaskAction:
                    this.Set();
                    break;
                default:
                    this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Invalid TaskAction passed: {0}", this.TaskAction));
                    return;
            }
        }

        /// <summary>
        /// Sets this instance.
        /// </summary>
        private void Set()
        {
            if (this.Value == null)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Removing Environment Variable: \"{0}\" for target \"{1}\" to \"{2}\".", this.Variable, this.target, string.Empty));
                Environment.SetEnvironmentVariable(this.Variable, string.Empty, this.target);
            }
            else
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Environment Variable: \"{0}\" for target \"{1}\" to \"{2}\".", this.Variable, this.target, this.Value[0]));
                Environment.SetEnvironmentVariable(this.Variable, this.Value[0], this.target);
            }
        }

        /// <summary>
        /// Gets this instance.
        /// </summary><commit_msg>Computer\EnvironmentVariable.cs: Fix to support setting environment variables with ; separators. Resolves #2872 (The environment variable becomes one.) reported by teta 28th April 2009 #2872      The environment variable becomes one.<commit_after>//-----------------------------------------------------------------------
// <copyright file="EnvironmentVariable.cs">(c) http://www.codeplex.com/MSBuildExtensionPack. This source is subject to the Microsoft Permissive License. See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx. All other rights reserved.</copyright>
//-----------------------------------------------------------------------
namespace MSBuild.ExtensionPack.Computer
{
    using System;
    using System.Globalization;
    using System.Management;
    using System.Text;
    using Microsoft.Build.Framework;

    /// <summary>
    /// <b>Valid TaskActions are:</b>
    /// <para><i>Get</i> (<b>Required: </b> Variable  <b>Output: </b> Value)</para>
    /// <para><i>Set</i> (<b>Required: </b> Variable, Value)</para>
    /// <para><b>Remote Execution Support:</b> For Get TaskAction only</para>
    /// </summary>
    /// <example>
    /// <code lang="xml"><![CDATA[
    /// <Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    ///     <PropertyGroup>
    ///         <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
    ///         <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
    ///     </PropertyGroup>
    ///     <Import Project="$(TPath)"/>
    ///     <Target Name="Default">
    ///         <!-- Set an environment variable. Note how special characters need to be escaped (http://msdn.microsoft.com/en-us/library/ms228186(VS.80).aspx) -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="PATH" Value="$(VCInstallDir)Common7\IDE%3B$(VCInstallDir)VC\BIN%3B$(VCInstallDir)Common7\Tools%3B$(VCInstallDir)Common7\Tools\bin%3B$(VCInstallDir)VC\PlatformSDK\bin%3B$(SDKInstallDir)bin%3B$(PATH)"/>
    ///         <!-- Set a new Environment Variable. The default target is Process -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="bddd"/>
    ///         <!-- Get the Environment Variable -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="ANewEnvSample">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="Get: $(EnvValue)"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="newddd"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="ANewEnvSample">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="Get: $(EnvValue)"/>
    ///         <!-- Set a new Environment Variable on a remote machine -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Set" Variable="ANewEnvSample" Value="bddd" MachineName="MediaHub"/>
    ///         <!-- Get an Environment Variable from a remote machine -->
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="INOCULAN" Target="Machine" MachineName="machinename" UserName="Administrator" UserPassword="<PASSWORD>">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="INOCULAN Get: $(EnvValue)"/>
    ///         <MSBuild.ExtensionPack.Computer.EnvironmentVariable TaskAction="Get" Variable="FT" Target="User" MachineName="machinename" UserName="Administrator" UserPassword="<PASSWORD>">
    ///             <Output PropertyName="EnvValue" TaskParameter="Value"/>
    ///         </MSBuild.ExtensionPack.Computer.EnvironmentVariable>
    ///         <Message Text="FT Get: $(EnvValue)"/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    [HelpUrl("http://www.msbuildextensionpack.com/help/3.5.2.0/html/17390384-36ed-d9a9-b208-7e5207c778af.htm")]
    public class EnvironmentVariable : BaseTask
    {
        private const string GetTaskAction = "Get";
        private const string SetTaskAction = "Set";
        
        private EnvironmentVariableTarget target = EnvironmentVariableTarget.Process;

        [DropdownValue(GetTaskAction)]
        [DropdownValue(SetTaskAction)]
        public override string TaskAction
        {
            get { return base.TaskAction; }
            set { base.TaskAction = value; }
        }

        /// <summary>
        /// Gets or sets the value. May be a string array for Get. If Value is not passed or empty for Set, the environment variable is deleted.
        /// </summary>
        [Output]
        [TaskAction(GetTaskAction, false)]
        [TaskAction(SetTaskAction, true)]
        public string[] Value { get; set; }

        /// <summary>
        /// The name of the Environment Variable to get or set.
        /// </summary>
        [Required]
        [TaskAction(GetTaskAction, true)]
        [TaskAction(SetTaskAction, true)]
        public string Variable { get; set; }

        /// <summary>
        /// Machine, Process or User. Defaults to Process
        /// </summary>
        public string Target
        {
            get
            {
                return this.target.ToString();
            }

            set
            {
                if (Enum.IsDefined(typeof(EnvironmentVariableTarget), value))
                {
                    this.target = (EnvironmentVariableTarget) Enum.Parse(typeof(EnvironmentVariableTarget), value);
                }
                else
                {
                    this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "The value '{0}' is not in the EnvironmentVariableTarget Enum. Use Process, User or Machine.", value));
                    return;
                }
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            switch (this.TaskAction)
            {
                case GetTaskAction:
                    this.Get();
                    break;
                case SetTaskAction:
                    this.Set();
                    break;
                default:
                    this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Invalid TaskAction passed: {0}", this.TaskAction));
                    return;
            }
        }

        /// <summary>
        /// Sets this instance.
        /// </summary>
        private void Set()
        {
            if (this.Value == null)
            {
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Removing Environment Variable: \"{0}\" for target \"{1}\" to \"{2}\".", this.Variable, this.target, string.Empty));
                Environment.SetEnvironmentVariable(this.Variable, string.Empty, this.target);
            }
            else
            {
                StringBuilder s = new StringBuilder(this.Value.Length);
                foreach (string val in this.Value)
                {
                    s.Append(val + ";");
                }

                string newValue = s.ToString();
                newValue = newValue.Remove(newValue.Length - 1, 1);
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Setting Environment Variable: \"{0}\" for target \"{1}\" to \"{2}\".", this.Variable, this.target, newValue));
                Environment.SetEnvironmentVariable(this.Variable, newValue, this.target);
            }
        }

        /// <summary>
        /// Gets this instance.
        /// </summary>
{'fraction_non_alphanumeric': 0.15616698292220113, 'fraction_numerical': 0.008475648323845668, 'mean_word_length': 2.0116190476190474, 'pattern_counts': {'":': 0, '<': 161, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 4}, 'pii_count': 2, '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': '1218585', 'n_tokens_mistral': 4214, 'n_tokens_neox': 3975, 'n_words': 833}