2012-01-10 2 views
12

큰 해결책이 있으며 실제로 더 이상 내 솔루션에 속하지 않는 많은 * .cs 파일이 있습니다 (csproj 파일에 포함되지 않음). 그들 모두를 찾아서 제거 할 방법이 있습니까?솔루션에서 사용되지 않는 cs 파일을 제거하십시오.

+1

파일이 디스크에 있지만 프로젝트에 포함되어 있지 않다는 것을 의미하고 디스크에서 파일을 삭제 하시겠습니까? 아니면 솔루션에 포함되었지만 클래스가 사용되지 않았 음을 의미합니까? – Ray

+1

사용하지 않는 클래스는 Resharper를 사용하여 찾을 수 있습니다. http://stackoverflow.com/questions/4646174/resharper-find-all-unused-classes하지만 내가 묻는 것은 확실하지 않습니다. – Ray

+2

[Visual Studio 매크로 : 프로젝트에 포함되지 않은 파일 찾기?] (http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included- in-the-project) – Ray

답변

5

이 PowerShell 스크립트는 찾고있는 것을 수행해야합니다. 포함 된 코드 파일을 얻기 위해 프로젝트 파일을 구문 분석합니다. 그런 다음이 목록을 디스크의 실제 파일과 비교합니다. 나머지 파일은 사용되지 않거나 쓸모없는 파일입니다.

스크립트는 디스크에서 사용되지 않는 파일을 삭제하거나 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 
    } 
} 
+1

고마워!이 파일을 사용하여 다른 유형의 파일을 포함하고 TFS를 사용하지 않는 더 자세한 스크립트를 만들었다. https://gist.github.com/mcliment/d9008a9288cea9d088af –

+3

또한이 파일과 @ MarcCliment를 사용하여 하나의 proj 파일 대신 .sln 파일을 가져 오는 또 다른 PowerShell 스크립트를 만들었습니다. 모두에서 제외 된 모든 파일을 삭제합니다 프로젝트에서 제공되는 솔루션을 참조하십시오. http : // g oo.gl/PdR9Fg – mikesigs

+0

@mikesigs의 수정 된 스크립트를 사용합니다. 일부 파일을 WPF 응용 프로그램 (XAML 파일)에서 비활성으로 잘못 표시한다는 점을 제외하면 매력처럼 작동합니다. – Roemer

6

솔루션 탐색기에서 프로젝트를 선택할 때 솔루션 탐색기의 도구 모음에서 "모든 파일 표시"단추를 클릭하십시오. 그러면 프로젝트 디렉토리에 파일 및 폴더가 표시되지만 프로젝트에는 포함되지 않습니다. 이렇게하면 프로젝트를 삭제하거나 프로젝트에 미리 읽을 수 있습니다.

자동화 된 솔루션을 모르므로 각 프로젝트마다 수동으로 수행해야합니다.

+1

예, 나는 그 해결책을 안다. 그러나 나의 프로젝트는 아주 거대하다. 그래서 나는 이것을 자동화 할 무언가를 찾고있다 :-( – Nagg

2

소스 제어에 모든 파일을 추가하려면 Visual Studio를 사용하십시오. 프로젝트의 일부인 파일 만 추가하므로 프로젝트 파일이 아닌 파일은 추가되지 않습니다. 그런 다음 모든 파일을 커밋하고 다른 곳에서 프로젝트를 확인할 수 있습니다. 관련 파일 만 대상 위치에서 체크 아웃됩니다.

큰 프로젝트를 가지고 있다면 이미 소스 제어가 없으므로 기존 연결을 끊고 새 소스로의 체크 아웃 후 원래 소스 위치를 지워야 할 수 있습니다. 대상을 원본으로 복사하고 원본 scm이 원본에서 파일 제거를 감지하고 제거를 제출하게하십시오.

관련 문제