Airistotal
Airistotal

Reputation: 105

Visual Studio: stop from modifying CSPROJ and custom includes

I have successfully included all my project files by putting these lines in my .csproj file:

<ItemGroup>
  <Content Include="**\*.*" Exclude="**\*.cs"></Content>
</ItemGroup>

<ItemGroup>
  <Compile Include="**\*.cs" Exclude="**\*.as?x.*"></Compile>

  <Compile Include="**\*.aspx.cs">
    <SubType>ASPXCodeBehind</SubType>
  </Compile>
  <Compile Include="**\*.ascx.cs">
    <SubType>ASPXCodeBehind</SubType>
  </Compile>
  <Compile Include="**\*.aspx.designer.cs"></Compile>
  <Compile Include="**\*.ascx.designer.cs"></Compile>
</ItemGroup>

and then I successfully copy all my files into another directory using these lines:

<Target Name="AfterRebuild">
    <Copy SourceFiles="@(Content)" DestinationFolder="\wwwroot\%(Content.RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
<Target Name="AfterRebuild">
  <Copy SourceFiles="@(Compile)" DestinationFolder="\wwwroot\%(Compile.RecursiveDir)" SkipUnchangedFiles="true" />
</Target>

The first problem I'm having is that every time I add another file to the project Visual Studio decides to regenerate every .designer.cs file include as well as add the new file include. It looks like this after adding the file:

<ItemGroup>
  <Compile Include="a_bunch_of_these.designer.cs" />
  <Compile Include="a_bunch_of_these.designer.cs" />
  <Compile Include="a_bunch_of_these.designer.cs" />
  ...

  <Compile Include="Business\WebForm1.aspx.designer.cs">
    <DependentUpon>WebForm1.aspx</DependentUpon>
  </Compile>

  <Compile Include="**\*.cs" Exclude="**\*.as?x.*"></Compile>

  <Compile Include="**\*.aspx.cs">
    <SubType>ASPXCodeBehind</SubType>
  </Compile>
  <Compile Include="**\*.ascx.cs">
    <SubType>ASPXCodeBehind</SubType>
  </Compile>
  <Compile Include="**\*.aspx.designer.cs"></Compile>
  <Compile Include="**\*.ascx.designer.cs"></Compile>
</ItemGroup>

to the project. The second problem I'm having is less important, but still slightly annoying. I don't have a dependantupon attribute in the Item elements under the ItemGroup elements, so in the solution explorer the files are not coupled together: See WebForm1 vs WebForm2

I have two questions:

  1. How do I stop Visual Studio from adding those new lines? Since it is being included twice in the project it shows up twice in the solution explorer which is very annoying.

  2. How do I associate a DependentUpon element with each file when using a wildcard to choose the files? Is there a way to mine the name out of the chosen file to relatively associate an aspx file automatically? Something like:


<ItemGroup>
<Compile Include="**\*.aspx.cs">
  <SubType>ASPXCodeBehind</SubType>
  <DependentUpon>%(Filename)%(Extension)</DependentUpon>
</Compile>
</ItemGroup>

EDIT: Just so you guys can have a big picture of what I'm trying to do: My project is to automate the deployment of a website onto a server. I have decided to use Team City to do the CI. It clones the repo it's connected to, then builds the project. This code makes it so after the build is completed, the project will automatically be copied to the correct folder with the correct structure.

I had also tried ftpUpload from msbuild community tasks, but the project is large and ftp takes a prohibitive amount of time.

The other thing I had looked at was WebDeploy, but seeing as I could find no documentation for Visual Studio 2015 projects with Web Deploy I just decided to try something else.

Upvotes: 0

Views: 2103

Answers (2)

martinrhan
martinrhan

Reputation: 493

I have answer for you second question. This question, which was asked and then answer by my self, is pretty similar to what you need. You may try this:

<ItemGroup>
  <AnyNameYouLike Include="**\*.aspx.cs"/>
  <Content Include="@(AnyNameYouLike)">
      <DependentUpon>$([System.String]::Join([System.String]::Copy('%(FileName)'),'.aspx')</DependentUpon>
  </Content>
</ItemGroup>

Edit

the keyword "content" actually mark this files type as content which is usually not the correct one. Instead, it should be replaced by "compile" if its a c# code file. Further more, as by default all .cs files not declared in .csproj file are marked as compile, using include will cause double include error, so it should be update instead.

My much better version is like this:

<ItemGroup>
  <Compile Update="**\*.aspx.cs"
      <DependentUpon>$([System.String]::Join([System.String]::Copy('%(FileName)'),'.aspx')</DependentUpon>
  </Compile>
</ItemGroup>

Upvotes: 1

Airistotal
Airistotal

Reputation: 105

So here's the solution to my problems that I've found:

<Target Name="AfterRebuild">
  <ItemGroup>
    <cpyContent Include="**\*.*"></cpyContent>
  </ItemGroup>
  <Copy SourceFiles="@(cpyContent)" DestinationFolder="\wwwroot\%(cpyContent.RecursiveDir)" Retries="2" SkipUnchangedFiles="true" />
</Target>

Since the ItemGroup is in the Target element, it looks like VS doesn't include it in the GUI. This lets Visual Studio fool around with including the files in the GUI, but still let's me have the proper metadata for copying.

Upvotes: 1

Related Questions