Files
PowerShell/src/System.Management.Automation/namespaces/EnvironmentProvider.cs
T
Dongbo Wang 7a55bf98b2 Move powershell to .NET Core 2.0 (#3556)
This change moves powershell to .NET Core 2.0. Major changes are:
1. PowerShell assemblies are now targeting `netcoreapp2.0`. We are using `microsoft.netcore.app-2.0.0-preview1-001913-00`, which is from dotnet-core build 4/4/17. We cannot target `netstandard2.0` because the packages `System.Reflection.Emit` and `System.Reflection.Emit.Lightweight`, which are needed for powershell class, cannot be referenced when targeting `netstandard2.0`.
2. Refactor code to remove most CLR stub types and extension types.
3. Update build scripts to enable CI builds. The `-cache` section is specified to depend on `appveyor.yml`, so the cache will be invalidated if `appveyor.yml` is changed.
4. Ship `netcoreapp` reference assemblies with powershell to fix the issues in `Add-Type` (#2764). By default `Add-Type` will reference all those reference assemblies when compiling C# code. If `-ReferenceAssembly` is specified, then we search reference assemblies first, then the framework runtime assemblies, and lastly the loaded assemblies (possibly a third-party one that was already loaded).
5. `dotnet publish` generates executable on Unix platforms, but doesn't set "x" permission and thus it cannot execute. Currently, the "x" permission is set in the build script, `dotnet/cli` issue [#6286](https://github.com/dotnet/cli/issues/6286) is tracking this.
6. Replace the use of some APIs with the ones that take `SecureString`.
7. osx.10.12 is required to update to `netcoreapp2.0` because `dotnet-cli` 2.0.0-preview only works on osx.10.12.
8. Add dependency to `System.ValueTuple` to work around a ambiguous type identity issue in coreclr. The issue is tracked by `dotnet/corefx` [#17797](https://github.com/dotnet/corefx/issues/17797). When moving to newer version of `netcoreapp2.0`, we need to verify if this dependency is still needed.
2017-04-17 11:52:38 -07:00

241 lines
7.7 KiB
C#

/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using Dbg = System.Management.Automation;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This provider is the data accessor for environment variables. It uses
/// the SessionStateProviderBase as the base class to produce a view on
/// session state data.
/// </summary>
[CmdletProvider(EnvironmentProvider.ProviderName, ProviderCapabilities.ShouldProcess)]
public sealed class EnvironmentProvider : SessionStateProviderBase
{
/// <summary>
/// Gets the name of the provider
/// </summary>
public const string ProviderName = "Environment";
#region Constructor
/// <summary>
/// The constructor for the provider that exposes environment variables to the user
/// as drives.
/// </summary>
public EnvironmentProvider()
{
} // constructor
#endregion Constructor
#region DriveCmdletProvider overrides
/// <summary>
/// Initializes the alias drive
/// </summary>
///
/// <returns>
/// An array of a single PSDriveInfo object representing the alias drive.
/// </returns>
///
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
{
string description = SessionStateStrings.EnvironmentDriveDescription;
PSDriveInfo envDrive =
new PSDriveInfo(
DriveNames.EnvironmentDrive,
ProviderInfo,
String.Empty,
description,
null);
Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
drives.Add(envDrive);
return drives;
} // InitializeDefaultDrives
#endregion DriveCmdletProvider overrides
#region protected members
/// <summary>
/// Gets a environment variable from session state
/// </summary>
///
/// <param name="name">
/// The name of the environment variable to retrieve.
/// </param>
///
/// <returns>
/// A DictionaryEntry that represents the value of the environment variable.
/// </returns>
///
internal override object GetSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
object result = null;
string value = Environment.GetEnvironmentVariable(name);
if (value != null)
{
result = new DictionaryEntry(name, value);
}
return result;
} // GetSessionStateItem
/// <summary>
/// Sets the environment variable of the specified name to the specified value
/// </summary>
///
/// <param name="name">
/// The name of the environment variable to set.
/// </param>
///
/// <param name="value">
/// The new value for the environment variable.
/// </param>
///
/// <param name="writeItem">
/// If true, the item that was set should be written to WriteItemObject.
/// </param>
///
internal override void SetSessionStateItem(string name, object value, bool writeItem)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
if (value == null)
{
Environment.SetEnvironmentVariable(name, null);
}
else
{
// First see if we got a DictionaryEntry which represents
// an item for this provider. If so, use the value from
// the dictionary entry.
if (value is DictionaryEntry)
{
value = ((DictionaryEntry)value).Value;
}
string stringValue = value as string;
if (stringValue == null)
{
// try using ETS to convert to a string.
PSObject wrappedObject = PSObject.AsPSObject(value);
stringValue = wrappedObject.ToString();
}
Environment.SetEnvironmentVariable(name, stringValue);
DictionaryEntry item = new DictionaryEntry(name, stringValue);
if (writeItem)
{
WriteItemObject(item, name, false);
}
}
} // SetSessionStateItem
/// <summary>
/// Removes the specified environment variable from session state.
/// </summary>
///
/// <param name="name">
/// The name of the environment variable to remove from session state.
/// </param>
///
internal override void RemoveSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!String.IsNullOrEmpty(name),
"The caller should verify this parameter");
Environment.SetEnvironmentVariable(name, null);
} // RemoveSessionStateItem
/// <summary>
/// Gets a flattened view of the environment variables in session state
/// </summary>
///
/// <returns>
/// An IDictionary representing the flattened view of the environment variables in
/// session state.
/// </returns>
///
internal override IDictionary GetSessionStateTable()
{
// Environment variables are case-sensitive on Unix and
// case-insensitive on Windows
#if UNIX
Dictionary<string, DictionaryEntry> providerTable =
new Dictionary<string, DictionaryEntry>(StringComparer.Ordinal);
#else
Dictionary<string, DictionaryEntry> providerTable =
new Dictionary<string, DictionaryEntry>(StringComparer.OrdinalIgnoreCase);
#endif
// The environment variables returns a dictionary of keys and values that are
// both strings. We want to return a dictionary with the key as a string and
// the value as the DictionaryEntry containing both the name and env variable
// value.
IDictionary environmentTable = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry entry in environmentTable)
{
providerTable.Add((string)entry.Key, entry);
}
return providerTable;
} // GetSessionStateTable
/// <summary>
/// Gets the Value property of the DictionaryEntry item
/// </summary>
///
/// <param name="item">
/// The item to get the value from.
/// </param>
///
/// <returns>
/// The value of the item.
/// </returns>
///
internal override object GetValueOfItem(object item)
{
Dbg.Diagnostics.Assert(
item != null,
"Caller should verify the item parameter");
object value = item;
if (item is DictionaryEntry)
{
value = ((DictionaryEntry)item).Value;
}
return value;
} // GetValueOfItem
#endregion protected members
} // EnvironmentProvider
}