2017-01-23 116 views
1

我正在使用'SlowCheetah'VS擴展根據項目配置使用不同的值轉換我們的app.config。因此,'Debug'配置會生成一個具有適合Dev/Qa用戶的值的app.config,而'Release'構建會生成帶有生產值的app.config。MSBuild條件,以防止app.config XML轉換

的.csproj的包含這樣一段:

<ItemGroup> 
<None Include="App.config"> 
    <SubType>Designer</SubType> 
    <TransformOnBuild>true</TransformOnBuild> 
</None> 
<None Include="App.Debug.config"> 
    <DependentUpon>App.config</DependentUpon> 
    <IsTransformFile>True</IsTransformFile> 
    <SubType>Designer</SubType> 
</None>  
<None Include="App.Release.config"> 
    <DependentUpon>App.config</DependentUpon> 
    <IsTransformFile >True</IsTransformFile>  
</None> 
<None Include="packages.config" /> 
<None Include="Properties\SlowCheetah\SlowCheetah.Transforms.targets" /> 

而且MSBuild的邏輯主要包含在 'SlowCheetah.Transforms.targets' 文件。我的文件正在正確轉換。

我想要防止開發人員意外地在Visual Studio中運行「發佈」版本,並在無意中使用生產配置文件運行我的應用程序。我的想法是使用一個MSBuild情況,可能是這樣的:

Condition=" '$(BuildingInsideVisualStudio)'=='true' " 

我在沒有成功的.csproj文件的幾個地方使用這個條件嘗試。如果我修改'SlowCheetah.Transforms.targets'文件本身,我懷疑我可以讓它工作,但不應該根據頂部的註釋修改該文件。

理想情況下,我希望Visual Studio中的所有配置可以使用我的調試配置文件,而'發佈'構建Visual Studio外部(例如構建在持續集成服務器上)以使用Prod app.config,但是我對於能夠防止Visual Studio中'發佈'版本的意外運行,我感到很滿意。任何有關如何/如何實現這一目標的建議都會受到讚賞。

回答

1

只是</Project>前補充一點:

<Target Name="BeforeBuild"> 
    <Error Condition=" '$(BuildingInsideVisualStudio)'=='true' And '$(Configuration)'=='Release' " 
      Text="JMc is so mad at you you trying to build using the Release configuration from Visual Studio." /> 
    </Target> 
+0

很好地做這項工作。謝謝。 – JMc

+1

使用相同條件的一個好的替代方法是在app.config中指定DEV值併爲「發佈」配置指定一個單獨的變換。然後將「條件」放置在「SlowCheetah」屬性組中。結果是,轉換隻發生在VS外部時。當在VS內部構建時,您總是以DEV app.config文件結束。 – JMc

+1

$([System.IO.Path]: :GetFullPath($(MSBuildProjectDirectory)\ .. \ packages \ SlowCheetah.2.5.15 \ tools \)) .... – JMc