Skip to content
Jianyun edited this page Feb 25, 2016 · 8 revisions

PackageSource class

Supported PowerShell Streams :

# Supported streams:  
Write-Output  
Write-Warning
Write-Error
Write-Verbose
...

See [PackageProviderFunctions.psm1] (https://github.com/OneGet/oneget/blob/master/Providers/Inbox/PowerShell.MetaProvider/) for more streams.

Streams are captured and sent thru to send supported content back to the host. Unsupported content is dropped.

The Request class represents the current request context and provides access to OneGet functionality.

In PowerShell, the Request is exposed to the PowerShell session as $request and the Provider implementer does not need to specify a parameter for the value.

Property Type Description
IsCancelled bool indicates that the host has cancelled the operation, function should exit asap.
IsElevated bool indicates that the current process is elevated
PackageSources IEnumerable<string> a collection of strings representing the specified sources -- may be names or locations. Empty or null collection indicates "all"
Credential PSCredential any credential passed in from the user
Options Hashtable a set of key-value pairs of the dynamic options specified by the user
PackageManagementService PackageManagementService returns a reference to the [[PackageManagementService

For more other details, see the PSRequest class defined in the [PSRequest class] (https://github.com/OneGet/oneget/blob/master/Providers/Inbox/PowerShell.MetaProvider/Request.cs).

###Remarks $request.Options return a hash table, where key is a dynamic parameter name while value contains its corresponding data value. Please note there is no data type provided from $request.Options hash table. In fact, all data type is string, hence provider developer needs to manually cast the data returned from $request.Options to a proper type.

The following example defines the SkipValidate dynamic option in the provider. Its type is Switch.

 public void GetDynamicOptions(string category, Request request)
 {

    case "source":
        request.YieldDynamicOption("SkipValidate", Constants.OptionType.Switch, false);
        break;
     ...
 }

When you want to check whether a user specifies -SkipValidate in the cmdlet or not, you need to convert the string type to boolean.

      SkipValidate = new Lazy<bool>(() => GetOptionValue("SkipValidate").IsTrue());

      internal static bool IsTrue(this string text)
      {
            return !String.IsNullOrWhiteSpace(text) && text.Equals("true", StringComparison.CurrentCultureIgnoreCase);
      }

In PowerShell, you may do something like below:

    # get the cmdlet properties
    $Options = $request.Options
    if($Options.ContainsKey('SkipValidate'))
    {
        $SkipValidate= $Options['SkipValidate']

        if($SkipValidate -eq 'true')
        {
            $SkipValidate = $true
        }
        $SkipValidate= $false        
     }