Do helper functions in NuGet init.ps1 packages be global?

I want to write a couple of commands for the NuGet Package Manager console to insert the β€œGists” from GitHub . I have 4 main teams

  • List-Gists 'user'
  • Gist-info 'gistId'
  • Gist-Contents 'gistId' 'fileName'
  • Gist-Insert 'gistId' 'fileName'

All my teams depend on a couple of utility functions, and I struggle with whether they need to be global or not.

# Json Parser function parseJson([string]$json, [bool]$throwError = $true) { try { $result = $serializer.DeserializeObject( $json ); return $result; } catch { if($throwError) { throw "ERROR: Parsing Error"} else { return $null } } } function downloadString([string]$stringUrl) { try { return $webClient.DownloadString($stringUrl) } catch { throw "ERROR: Problem downloading from $stringUrl" } } function parseUrl([string]$url) { return parseJson(downloadString($url)); } 

Can I just use these utility functions outside my global functions, or do I need to somehow include them in each area of ​​the definition of global functions?

+4
source share
1 answer

No, they do not. From your init.ps1, you can import the powershell file that you wrote (psm1) and move on, that's how we recommend adding methods to the console environment.

Your init.ps1 will look something like this:

 param($installPath, $toolsPath) Import-Module (Join-Path $toolsPath MyModule.psm1) 

In MyModule.psm1:

 function MyPrivateFunction { "Hello World" } function Get-Value { MyPrivateFunction } # Export only the Get-Value method from this module so that what gets added to the nuget console environment Export-ModuleMember Get-Value 

You can get more information about the modules here http://msdn.microsoft.com/en-us/library/dd878340(v=VS.85).aspx

+7
source

Source: https://habr.com/ru/post/1344930/


All Articles