2011-03-23 39 views
4

我想要爲NuGet package manager控制檯編寫幾條命令來插入Gists from GitHub。我有4個基本的命令在NuGet中做幫助函數包init.ps1必須是全局的嗎?

  • 列表的要旨 '用戶'
  • 吉斯特 - 信息 'gistId'
  • 吉斯特 - 內容'gistId '文件名'
  • Gist-插入'gistId''文件名'

我所有的命令d依賴於一對夫婦的功能,我正在努力,他們是否需要全球化。

# 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)); 
} 

可我只是有這些效用函數的我的全功能外,還是我需要將它們包括在每一個全局函數定義範圍莫名其妙的?

回答

6

不,他們沒有。從你的init.sp1中,你可以導入一個你寫的(psm1)文件並向前移動的powershell模塊,這將是我們建議的將方法添加到控制檯環境的方式。

你init.ps1會是這個樣子:

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

在MyModule.psm1:

function MyPrivateFunction { 
    "Hello World" 
} 

function Get-Value { 
    MyPrivateFunction 
} 

# Export only the Get-Value method from this module so that's what gets added to the nuget console environment 
Export-ModuleMember Get-Value 

你可以在這裏模塊http://msdn.microsoft.com/en-us/library/dd878340(v=VS.85).aspx

+0

良好習慣的詳細信息,但有一種方法可以從模塊中定義的函數訪問$ toolsPath? – gerichhome 2016-07-22 17:41:18