4

背景:ReSharper的 - 在代碼檢查包括外部源文件

我們正在利用我們的.NET C#解決方案第三方工具。該工具具有自己的語法並與Visual Studio集成。當我們使用這個工具時,我們在Visual Studio中編寫它的標記,然後當我們構建解決方案時,定製工具運行並根據我們寫的標記生成一個.cs文件。

這個生成的源文件包含一個版本號,當我們將這些版本控制到版本控制(無盡衝突)時,會導致問題。我們的理解是,不檢查生成的源文件是最佳實踐。

所以我們排除了SVN生成的.cs文件,然後我們遇到的下一個問題是Visual Studio解決方案引用了這些文件,所以當TeamCity(我們的持續構建/集成軟件)去構建解決方案時會立即失敗,因爲它無法找到這些文件。

然後,我們將這些從解決方案中刪除,並將它們從SVN中排除,這固定了原始問題,我們不再檢查生成的代碼,並且它在TeamCity中正常生成(因爲每次都會重新生成文件建立)。

我們現在有一個新問題 - 由於生成的文件不再包含在解決方案中,因此無法找到生成的類,因此智能感知和代碼檢查失敗。該解決方案構建得很好(因爲代碼在構建期間被重新生成)。

問題

有沒有辦法告訴ReSharper的,包括在其代碼檢查產生的.cs文件?這些文件在解決方案的外部,但它們位於obj目錄中。

乾杯,

泰勒

+0

難道你不能保留解決方案中的文件,並添加一個預生成步驟來生成一個空的.cs文件,如果它不存在?這將保持構建的快樂。我們在我們的項目中使用了一個類似的技術,用於包含開發人員可覆蓋的數據庫連接字符串如果有幫助,我有一些示例MSBuild目標。 –

+0

嗨RichTea,聽起來不錯,我很樂意看到你的樣品,如果可能的話? – Tyler

回答

2

正如我在評論中提到,一個解決辦法是保持生成文件的解決方案(而不是在源代碼控制),同時加入了預生成步驟創建空.cs文件(如果真實生成的文件不存在),以便該文件在構建期間始終可用。

在我的項目中,我使用以下MSBuild目標通過使用Touch任務生成空文件。您可能需要進行一些修改 - 就我而言,目標文件實際上是在項目中定義的,而不是在解決方案級別;並且將這些文件的構建操作設置爲「無」,這對了解這些目標的工作方式非常重要。

<?xml version="1.0" encoding="utf-8" ?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> 

<!-- 
Creates empty 'dummy' files for any files that are specified but do not exist. 
To be processed, the following must be true: 

1. The file is included in an ItemGroup called CanCreateDummy, e.g. 
     <ItemGroup> 
     <CanCreateDummy Include="SomeFile.cs" /> 
     </ItemGroup> 
    If you want to specify a CanCreateDummy file in the .csproj file, you would 
    modify the above slightly as follows to prevent it appearing twice: 
     <ItemGroup> 
     <CanCreateDummy Include="SomeFile.cs"> 
      <Visible>false</Visible> 
     </CanCreateDummy> 
     </ItemGroup> 

2. The file is included in the ItemGroup called None. This is normally performed 
    by adding the file to the project in the usual way through Visual Studio, and 
    then setting the file's Build Action property to None. 
--> 
<Target 
    Name="CreateDummyFiles" 
    AfterTargets="BeforeBuild" 
    > 
<!-- 
This voodoo creates the intersection of 2 lists - @(CanCreateDummy) and @(None) 
(this latter item is defined in the project file). We want to create a filtered 
list of all items that are in both these lists, which is called _ProjectDummyFiles. 
See http://blogs.msdn.com/b/msbuild/archive/2006/05/30/610494.aspx for how the 
Condition voodoo works. 
--> 
<CreateItem Include="@(CanCreateDummy)" Condition="'%(Identity)' != '' and '@(None)' != ''" > 
    <Output TaskParameter="Include" ItemName="_ProjectDummyFiles"/> 
</CreateItem> 

<Message 
    Text="Creating dummy settings file @(_ProjectDummyFiles)" 
    Condition=" !Exists('%(_ProjectDummyFiles.FullPath)')" 
     /> 

<Touch 
    AlwaysCreate="true" 
    Files="@(_ProjectDummyFiles)" 
    Condition=" !Exists('%(_ProjectDummyFiles.FullPath)')" 
     /> 

</Target> 
</Project> 

希望這有助於

豐富

+0

感謝隊友工作完美非常狡猾的解決方案!起初,我認爲它看起來很詭異,但實施後感覺非常乾淨(考慮到情況:p)。 – Tyler

+0

非常感謝 - 很高興它的工作。 '這有點哈克,但更重要的是它是可靠的:) –

4

我們也有類似的問題,並不能拿出一個很好的解決方案,所以我寫了一個ReSharper的擴展,包括外部代碼:

https://resharper-plugins.jetbrains.com/packages/ReSharper.ExternalCode

+0

這應該得到更多upvotes。安裝後,您必須將路徑添加到外部代碼(相對於項目文件夾)並重新加載項目。 – Vincent

+3

ReSharper 9.1是否有版本或解決方法? – Markus