通过tf.exe确定TFS工作区的本地路径

5
我正在使用批处理脚本获取特定项目的最新版本。该脚本仅运行tf.exe并获取一些二进制文件的最新版本。一切正常,但我想更改下载文件的属性以使其可写(默认情况下这些文件是只读)。为此,我想确定文件的本地路径并使用批处理的attrib命令。
tf.exe workfold [Workspace]会显示一些列出的本地路径,但如果它只显示我想要的内容,那将更容易让我使用提示符。到目前为止,它看起来像这样:
tf.exe workfold [Workspace]

=======================================

Arbeitsbereich: XYZ-xxxxxx (Username)

Auflistung: TFS-URL

[Workspace]:  C:\xxx\TFS\xxx

是否可以确定TFS工作区的本地路径映射,以便我可以在不解析的情况下使用prompt进行attrib命令?

3个回答

1
以下(粗糙!)概念怎么样?
function Get-TfsWorkfold([string]$TfsCollection, [string]$TfsWorkspace)
{
    $TfExePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe"
    Write-Output "Getting workfold for '$TfsCollection'->'$TfsWorkspace'..."
    Push-Location $LocalPath
    & "$TfExePath" workfold /collection:$TfsCollection /workspace:$TfsWorkspace
}

function Handle-Path()
{
    param([Parameter(ValueFromPipeline=$true,Position=0)] [string] $line)
    $startIndex = $line.IndexOf(': ') + 2;
    $correctedLine = $line.subString($startIndex, $line.length - $startIndex - 1);
    Write-Output $correctedLine;
    Get-ChildItem $correctedLine
}

Get-TfsWorkfold "{serverAndcollection}"  "{workspace}" > c:\temp\test.txt
Select-String c:\temp\test.txt -pattern:': ' | Select-Object Line | Handle-Path

Handle-Path 中的最后一行是示例,您可以使用任何您想要的方法进行重写。它是 PowerShell 代码,但应按照您的需求工作。
请替换 {serverAndcollection} 和 {workspace}。

1

真正的男人可以用一行代码完成

powershell -command "& {tf workfold | Select-String -pattern:' $' -SimpleMatch | Select-Object Line | ForEach-Object {$startIndex = $_.Line.IndexOf(': ') + 2; $_.Line.subString($startIndex, $_.Line.length - $startIndex - 1)}}"

当前答案只会返回最后一个路径,如果有多个路径。

1
这很棒,但是 substring 中的 -1 会截断路径的最后一个字符 $_.Line.subString($startIndex, $_.Line.length - $startIndex) - SlaterCodes

1

您也可以通过调用TF.exe来完成,无需进行任何字符串操作。我已经将其包装在PowerShell脚本中,因此您可以得到以下内容:

function Add-TfsTypes
{
  # NOTE: Not all of the below are needed, but these are all the assemblies we load at the moment. Please note that especially NewtonSoft dll MUST be loaded first!
  $PathToAssemblies = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
  Add-Type -Path "$PathToAssemblies\NewtonSoft.Json.dll"
  Add-Type -Path "$PathToAssemblies\System.Net.http.formatting.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.Client.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.Common.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.VersionControl.Client.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
}

function Get-TfsServerPathFromLocalPath {
  param(
    [parameter(Mandatory=$true)][string]$LocalPath,
    [switch]$LoadTfsTypes
  )

  if ($LoadTfsTypes) {
    Add-TfsTypes # Loads dlls
  }

  $workspaceInfo = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfo($LocalPath)
  $server = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $workspaceInfo.ServerUri
  $workspace = $workspaceInfo.GetWorkspace($server)

  return $workspace.GetServerItemForLocalItem($LocalPath)
}

上述方法可以这样调用:
$serverFolderPath = Get-TfsServerPathFromLocalPath $folderPath -LoadTfsTypes
$anotherServerPath = Get-TfsServerPathFromLocalPath $anotherItemToTestPathOn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接