如何创建动态的构建任务,在PowerShell中调用,生成 [英] How to create dynamic build tasks with Invoke-Build in PowerShell

查看:203
本文介绍了如何创建动态的构建任务,在PowerShell中调用,生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用调用 - 建造我想创建动态的构建任务,该方法的一个例子如下。在那里,以类似的方式,以经典的UNIX使设施的输出文件将用于当且仅当输入文件的每个输入文件生成比输出文件更新

Using Invoke-Build I am trying to create dynamic build tasks, an example of the approach is below. Where, in a similar fashion to the classic UNIX make facility an output file will be generated for each input file iff the input file is newer than the output file.

它通过动态生成一组增量的任务,然后调用任务,实现这一目的。

It accomplishes this by dynamically generating a set of incremental tasks and then calling the tasks.

的增量任务的生成概述如下:

The generation of the incremental tasks is outlined below:

    $Jobs = Foreach($_ in $Conf ) {
        $SourceFile = Get-Item $_
        $FileName = $SourceFile.Name
        $ObjectName = $FileName -replace '^Thing-(.*)\.conf\.ps1$','$1'
        $TaskName = "Task-$ObjectName"
        $OutFile = "Thing-$ObjectName.ps1"
        # Import the script, overwriting the "$MetaData" variable    
        $MetaData = . $SourceFile

    $TaskCode = @'
.\BuildThing.ps1
BuildThing -MetaData $MetaData
'@
        Write-Verbose "$SourceFile $OutFile"
        $SBInputs = [scriptblock]::Create(  { $SourceFile } ).GetNewClosure()
        $SBOutputs = [scriptblock]::Create( { $OutFile } ).GetNewClosure()
        $SBMain = [scriptblock]::Create($TaskCode).GetNewClosure()
        Task "$TaskName" -Inputs $SBInputs -Outputs $SBOutputs ($SBMain)

        Write-Output $TaskName
    }

这确实出现了从所要求的其他职能范围界定分开工作。无论在哪里我尝试源时任务任务的东西─执行调用编译成BuildThing.ps1脚本抱怨BuildThing不被识别为:

This does appear to work well apart from the scoping of required other functions. No matter where I try to source the BuildThing.ps1 script when the task Task-Thing- is executed Invoke-Build complains that BuildThing is not recognized as:

cmdlet,函数,脚本文件或可操作的程序的名称。

"the name of a cmdlet, function, script file or operable program."

如果我点运行Works根据需要调用 - 建造前的一切源相关的文件。

If I dot source the relevant files before running Invoke-Build everything works as desired.

这似乎是一个作用域的问题 - 但它并不清楚如何解决它在一个简单的方式

This seems like a scoping problem - but it's not clear how to resolve it in a straightforward manner.

如果我尝试包括在静态的方式循环之外BuildThing.ps1,它似乎被调用,通过调试为证实,但不会出现在范围由动态任务被执行。

If I try to include BuildThing.ps1 outside of the loop in a static way, it does seem to be called, as confirmed by debugging, but does not appear to be in scope to be executed by the dynamic task.

推荐答案

对于上述情况,适当的方法似乎是修改$任务code的定义点源文件。

With regard to the above, the appropriate approach appears to be to modify the definition of $TaskCode to dot source the file.

如果我们假设\\ BuildThing.ps1包含一个函数。 - 例如

If we assume .\BuildThing.ps1 contains a function - e.g.

function BuildThing {
param(...)
...
}

然后,动态任务code需要点源这个code。这似乎是低效的大套类似的建设任务,但它确实解决问题。

Then the dynamic task code needs to dot source this code. This seems inefficient for a large set of similar build tasks, however it does resolve the issue.

$TaskCode = @'
. .\BuildThing.ps1
BuildThing -MetaData $MetaData
'@

这篇关于如何创建动态的构建任务,在PowerShell中调用,生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆