Search code examples
code-duplicationchocolatey

Avoid code duplication Chocolatey Packages


Introduction

Multiple functions have been added to a Chocolatey package.

These functions would like to be applied to other Chocolatey Packages as well.

This could be done by copying the functions to each of them. However, if a certain function will be optimized then these changes needs to be copied to all the packages.

Question

How to avoid code duplication in Chocolatey Packages?


Solution

  • I have a few suggestions as to how you could proceed with this...

    1. (Probably the one that would take the longest due to dependency on other people) If these functions are fully re-usable, by yourself, and other people, then I would suggest submitting a Pull Request into Chocolatey Core, to add these functions into Chocolatey. That way, everyone gets the benefit.
    2. Split the functions into a separate file, and then dot source them into the Chocolatey Packages you are creating. Within your source control repository, store the file in a common location, and then add it into each Chocolatey Package that it is required.
    3. Further refactor the extracted .ps1 file, so that it runs as a module that you create. Create a Chocolatey Package where all it does it deploys the PowerShell Module (there are some examples of doing this on Chocolatey.org - shout if you don't find one). Then, in your Chocolatey Package that needs the functions exported by this module, take a depenedency on the new Chocolatey Package, and then simply import-module in the consuming Chocolatey Package.
    4. The little known extensions. Look at this package: http://chocolatey.org/packages/Devbox-Common.extension

    Hope that makes sense, and provides help!

    Gary