Possibility to add additional "conditions" that leave/remove code?

Sep 6, 2012 at 12:38 PM

The ///#DEBUG ///#ENDDEBUG is ok for debug information, but I'm thinking in something similar to the #if/#endif block in the c# compiler.

It doesn't has to be a full expression parser, not even have else blocks. Just like the #DEBUG above, but the name of the section (DEBUG in this case) should be specified via the command line for the section to appear on the code. If the name is not specified, that section should be removed from the code.

In that way, I can enable/disable parts of the code based on my needs for a specific project and reduce even more the output size.

So, for example, I can have a ///#TEST ///#ENDTEST block that will only be included in the compressed output if I, through the command line/task properties I enable that block by something like -define:TEST,DEBUG,....

 

Coordinator
Sep 6, 2012 at 3:18 PM

Feels good to give good news for a change. So you're in luck: that functionality is already in there! Now, the documentation isn't up to date, but it does describe one way of doing exactly what you ask: ///#ifdef NAME ... ///#ENDIF will switch on any arbitrary define name that you specify on your command line. For instance, if your command line was: "-define:test", you could do this:

///#IFDEF TEST
some code
///#ENDIF

What isn't documented [yet] is that there is also another variant: ///#IF, which can also do very crude expression comparisons. So if your command line was something like: "-define:test=10", you could have code that does this:

///#IF TEST < 42
do something
///#ENDIF

The syntax for the new ///#IF directive is:

///#IF NAME[ OP [VALUE]]
some code
///#ENDIF

If the NAME preprocessor name is not defined, the condition is always false.

The OP operator can be == (converting equal), != (converting not-equal), === (strict equal), !== (strict not-equal), <, >, <=, and >=. The operators <, >, <= and >= will always try converting the value of the preprocessor name and the VALUE string to type double before doing the comparison. The == and != operators will first do a string compare, and if false, will then try converting the value of the preprocessor name and the VALUE to double and doing a comparison. The === and !== operators will only do a string comparison.

If the VALUE portion is a string, don't quote it -- its value is simply everything after the operator to the end of the line, with whitespace trimmed from the ends. So if you set your command line to: "-define:arg=yo ho ho and a bottle of rum" and want to do a comparison, don't quote the string, just go:

///#if arg === yo ho ho and a bottle of rum
some code
///#endif

If there is no OP or VALUE portion, the ///#if simply tests for the definition of the NAME preprocessor name. Since the DEBUG preprocessor name is already set/reset with the -debug switch, ///#DEBUG is the same as ///#IFDEF DEBUG is the same as ///#IF DEBUG.

Sep 6, 2012 at 5:37 PM

Excellent news indeed!. I will try right away.

Do you know how can I get the defined defines in VS so I can assign them into the msbuild task?

Coordinator
Sep 6, 2012 at 5:40 PM

I assume you are using the AjaxMin build task in AjaxMinTask.dll, right? Use the Switches property for the build task:

<AjaxMin Switches="-define:test" .../>

Sep 6, 2012 at 5:52 PM
Edited Sep 6, 2012 at 6:07 PM

Yes, you are right. But what I want to do is to get the "defined" conditional compilation symbols from the build page in VS into the task. So, something like <AjaxMin Switches="-define:@(DEFINES)"... /> (obviously, that won't work as I just made up).

Edit: Just found the right syntax: Switches="-define:$(DefineConstants)"

I believe that should work, unless the way VS defines constants (as seen here and here) is not the same as the one AjaxMin needs...

Coordinator
Sep 6, 2012 at 7:07 PM

Oh, I get it. The $(DefineConstants) property should be a semicolon-delimited list of the your compiler defines. You can specify the Switches property on your AjaxMin build task as:

Switches="-define:$(DefineConstants.Replace(';',','))"

Coordinator
Sep 6, 2012 at 7:08 PM

Heh -- we hit the same solution at the same time. I think you need to change the semicolons to commas, though. Although I'll update AjaxMin to work with either.

Sep 6, 2012 at 9:35 PM

Thanks!