在删除未使用的解决方案CS-文件 [英] Remove unused cs-files in solution

查看:113
本文介绍了在删除未使用的解决方案CS-文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的解决方案,并有实际不属于我的解决方案很多*的.cs-文件(不包括到的csproj文件)了。有没有办法找到所有这些并删除?

I have a big solution and there are many *.cs-files that actually don't belong to my solution (not included to csproj-files) any more. Is there any way to find all of them and remove?

推荐答案

这PowerShell脚本应该做你在找什么。它分析项目文件,以获得包括code文件。然后,它比较了单磁盘上的实际文件。剩下的文件是未使用的/过时的文件。

This PowerShell script should do what you are looking for. It parses the project file to get the included code files. Then it compares that list to the actual files on disk. The remaining files are your unused/obsolete files.

该脚本可以从磁盘或挂起它们删除不用的文件,在TFS删除。

The script can either delete the unused files from disk or pend them as deletes in TFS.

<#
.SYNOPSIS
Find and process files in a project folder that are not included in the project. 


.DESCRIPTION
Find and process files in a project folder that are not included in the project. 
Options to delete the files or to add them as pending deletes for TFS. Use TF.exe to pend the deletes and start the check-in process for the files.
This is necessary when trying to delete files that are not currently included in a Visual Studio project.

.PARAMETER Project
The path/name for the project file. 

.PARAMETER VsVersion
The Visual Studio version (10, 11, 12). Used to locate the tf.exe file.  

.PARAMETER DeleteFromDisk
Just delete the files from disk. No interaction with any source control.

.PARAMETER TfsCheckin
After pending the deletes, open the check-in dialog.

#>

[CmdletBinding()]
param(
    [Parameter(Position=0, Mandatory=$true)]
    [string]$Project,  
    [Parameter(Mandatory=$false)]
    [ValidateRange(10,12)] 
    [int] $VsVersion = 12,
    [switch]$DeleteFromDisk,
    [switch]$TfsCheckin
)

$ErrorActionPreference = "Stop"
$tfPath = "${env:ProgramFiles(X86)}\Microsoft Visual Studio $VsVersion.0\Common7\IDE\TF.exe"

$projectPath = Split-Path $project


if($Project.EndsWith("csproj"))
{
    $fileType = "*.cs"
}
else
{
    $fileType = "*.vb"
}
$fileType


$projectFiles = Select-String -Path $project -Pattern '<compile'  | % { $_.Line -split '\t' } | `
     % {$_ -replace "(<Compile Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ }
Write-Host "Project files:" $projectFiles.Count


$diskFiles = gci -Path $path -Recurse -Filter $fileType | % { $_.FullName}
Write-Host "Disk files:" $diskFiles.Count


$diff = (compare-object $diskFiles $projectFiles  -PassThru) 
Write-Host "Excluded Files:" $diff.Count

#create a text file for log purposes
$diffFilePath = Join-Path $projectPath "DiffFileList.txt" 
$diff | Out-File $diffFilePath  -Encoding UTF8
notepad $diffFilePath


#just remove the files from disk
if($DeleteFileOnly)
{
    $diff | % { Remove-Item -Path $_ -Force -Verbose}
}
else #TFS options
{
    #this will add the files as pending deletes in TFS (awaiting check-in)
    $diff | % {
        [Array]$arguments = @("delete", "`"$_`"")
        & "$tfPath" $arguments
    }

    if($Checkin)
    {
        #start the check-in process for the pending deletes
        [Array]$arguments = "checkin", "/recursive", "$projectPath"
        & $tfPath $arguments
    }
}

这篇关于在删除未使用的解决方案CS-文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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