Awesome.

I'll see ya at DDD.

From: Greg Keogh <gfke...@gmail.com>
Sent: Tuesday, December 5, 2023 2:30 PM
To: David Kean <david.k...@microsoft.com>
Cc: ozDotNet <ozdotnet@ozdotnet.com>
Subject: Re: Conditional builds of arbitrary files

Look at VS Text Templates, or we sometimes make use the C-preprocessor.

Bingo! I'm a big fan of T4 templates and have used them in anger in many 
projects ... but I didn't think of using them in this case. I just did a sanity 
check and it works. Thankfully someone published an example of how to get the 
active configuration value inside the tt file (in brief).

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System" #>
<#@ import namespace="EnvDTE" #>
<#@ output extension=".html" #>
<#
    var serviceProvider = Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
    var configName = 
dte.Solution.SolutionBuild.ActiveConfiguration.Name<http://dte.solution.solutionbuild.activeconfiguration.name/>;
#>
<html> ... (etc)

With the config value I can now <# if #> control the contents of the html file. 
It looks a bit strange in the VS solution tree, but so what, it works.

There is one quirk ... the T4 generator does not run automatically when the 
configuration changes or when you build or publish, so I must remember to 
Alt+B+4 as needed.

Thanks,
Greg

P.S. If I see you at DDD I'll shout you a drink as thanks.


From: Greg Keogh via ozdotnet 
<ozdotnet@ozdotnet.com<mailto:ozdotnet@ozdotnet.com>>
Sent: Tuesday, December 5, 2023 11:59 AM
To: ozDotNet <ozdotnet@ozdotnet.com<mailto:ozdotnet@ozdotnet.com>>
Cc: Greg Keogh <gfke...@gmail.com<mailto:gfke...@gmail.com>>
Subject: Conditional builds of arbitrary files

Folks, the MSBuild process provides Condition= and #if to provide lots of 
control over which files and code fragments are compiled and published for a 
given Configuration.

My projects contain increasing numbers of non-cs files like html, css, js, txt, 
etc which often need conditional processing, especially the equivalent of a #if 
to select fragments of those sorts of files for different configurations. 
There's no way to select fragments of arbitrary files with a #if, so I've been 
wondering for years about the best way to simulate the feature.

I can't find any tools to do this, so I think that a custom MSBuild Task might 
be the way to go. The task could be given some parameters and it would edit the 
files and add and remove sections based upon the configuration before the build 
starts.

Does anyone think that's a good idea? Or maybe there's a better way.

Cheers,
Greg Keogh
-- 
ozdotnet mailing list 
To manage your subscription, access archives: https://codify.mailman3.com/ 

Reply via email to