4

NuGet パッケージ マネージャーコンソールがGitHub から Gistsを挿入するためのコマンドをいくつか書きたいと思っています。私は4つの基本的なコマンドを持っています

  • List-Gists 'ユーザー'
  • Gist-Info 'gistId'
  • Gist-Contents 'gistId' 'fileName'
  • Gist-Insert 'gistId' 'fileName'

私のコマンドはすべていくつかのユーティリティ関数に依存しており、グローバルにする必要があるかどうかで苦労しています。

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

これらのユーティリティ関数をグローバル関数の外に置くことはできますか?それとも、グローバル関数定義のスコープのそれぞれにそれらを含める必要がありますか?

4

1 に答える 1

7

いいえ、そうではありません。init.ps1 から、作成した (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 を参照してください。

于 2011-04-15T09:26:43.270 に答える