Write a custom plugin/provider for OneGet

Post Tags: oneget, powershell, windows management framework

Two days ago I saw a really interesting tweet from Scott Hanselman:

I know chocolatey quite good and have used it several times to automate machine installs, but this "OneGet" was new to me. So I opened the link and went through this blog post from Microsoft. I almost freaked out when I realized this was the announcment of the preview of PowerShell 5, which includes chocolatey in its core. That's so cool, since it fills a big gap between Linux and Windows. But OneGet is not only Chocolatey it's a set set of command to talk with a package manager. Chocolatey is simply the first package provider which can be used with OneGet. In this blog post I won't explain a lot about OneGet, since others have done this before. If you never heard about OneGet, read first the blog post withinwindows.com before you continue.

Well, what about other package manager, how can they benifit from OneGet. In this blog post I you will explain how to write your custom provider from OneGet. Since there is no documentation, this post is based on reflection. So the official way will probably be a bit different than this, but I am sure the core concept will keep the same. According to the wiki on github (yes github, it will be open-source!!) it will be possible to write plugins in .NET or PowerShell. I will only cover the .NET-Part in this post.

Write own package provider

So let's start. Basicaly a plugin is a DLL which must contain two classes: A plugin and a provider. A plugin could provide different providers and is responsible to create this providers. In this example we only have one provider for the plugin. The DLL has then to be placed in the OneGet-Module folder under C:\Windows\System32\WindowsPowerShell\v1.0\Modules\OneGet and registered in the OneGet-Module Metadata file (OneGet.psd1).

The provider and the plugin don't need to implement any interface or to inherit from any class, but they have to implement a specific set of methods and properties.

The plugin needs this members:

And that provider needs to implement the following memebers:

Note: There could be more members and not all required, but this are the memebers, the chocolatey provider implements, so that is probably a good base.

Every method in the provider class has a parameter "c" of type Func<string, IEnumerable<object>, object>. This function is like a factory to create functions to interact with powershell. E.g. if you call c("YieldPackage", null) you get a function to give a package back to PowerShell in the GetInstalledPackages or FindPackages method.

I will not explain every method here, but you can download my example package provider to explor the details how to implement your own package provider.

Test the package provider

To test your provider, compile your package provider and put the DLL and the dependencies into C:\Windows\System32\WindowsPowerShell\v1.0\Modules\OneGet
Note: Do not forget to copy the Semver.dll library when you test my example.

Then you have to register the plugin. Edit the file C:\Windows\System32\WindowsPowerShell\v1.0\Modules\OneGet\OneGet.psd1 file as administrator. Then edit the line 'Microsoft.OneGet.Plugin.Chocolatey' to 'Microsoft.OneGet.Plugin.Chocolatey', 'NameOfMyDll'
Note: On Windows 8 you first have to be owner of the C:\Windows\System32\WindowsPowerShell\v1.0\Modules\OneGet folder, to edit the file.

Then test your package by running Get-Package in a new PowerShell console.

comments powered by Disqus