MSBuild: How to exclude zero length files? - msbuild

I use some task that works with files (Copy for example), but I want to exclude all files that are empty (0 bytes length). Is there a syntax to do this?

You will have yo write a custom task for this. If you are using MSBuild 4, then you can also create an Inline Task (http://msdn.microsoft.com/en-us/library/dd722601.aspx).

Related

How to copy and rename multiple files in CMake with wildcards?

I have a CMake project and I need to copy files from one folder to another and do some additional renaming with the following pattern:
"xy-1" will be "abcd1",
"xy-2" will be "abcd2",
...,
"xy-123" will be "abcd123"
Currently I´m trying this with add_custom_command() and in general it works for a simpler pattern like
"xy-1" will be "abc1",
"xy-2" will be "abc2",
...,
"xy-123" will be "abc123",
I´m doing it with
COMMAND copy "${path_to_source_folder}xy-*.txt" "${path_to_destination_folder}abc*.txt"
In the working pattern the number of characters before the asterisk are the same for the source- and the destination-name.
But in the not working pattern, there are 3 characters in the source-name and four characters in the destination-name before the asterisk:
COMMAND copy "${path_to_source_folder}xy-*.txt" "${path_to_destination_folder}abcd*.txt"
This not working. There is no error coming from cmake, but it will not copy all files (only the first 99) and the names are not created properly.
A quick fix would be to list all (400+) files explicitly, but I´d like to avoid this ;-)
How to do this with add_custom_command() or if this is not possible, how to do it in another way?
Many thanks for your help!
Cheers Daniel
Upate:
Thanks for the hints in the comments! I´m running on Windows 10 and using powershell for running cmake.

msbuild - how to pre-pend string to ItemGroup without using intermediary (temp) value

I have an item group and I need to pre-pend the same string to all of them (to get the correct paths).
My reason for needing this: I'm translating from GNU make to MSBuild and I'm trying to maintain a "1-to-1" mapping between the two 'makefiles'.
In the GNU makefile, there's use of addprefix text macro - which doesn't need a temp variable - so I want to replicate that "step" in the MSBuild "makefile".
How do I do that without using a temporary ItemGroup?

Can I create Variable Names from Constants in Objective-C/Swift?

This question is related to Swift and Objective-C.
I want to create variables from Constant Strings. So, in future, when I change name of a variable though out app, I just need to change it at one place, it must be changed, wherever it is used.
Example:
I have user_id in 14 files, if I want to change user_id into userID I have to change in all 14 files, but I want to change at once place only.
One way to do this would be to use the Xcode build process and add a script (language can be of your choice, but the default is a BASH script)
Create string constant text file where you define all your variables you want to change in some format that expresses the change you want to make, for example:
"variable_one_name" = "new_variable_one_name"
Depending on how 'smart' you wanted your script to be you could also list all your variables and include some way of indicating when a variable is not to be replaced.
"variable_one_name" = "new_variable_one_name"
"variable_two_name" = "DO_NOT_CHANGE"
Run a pre build script on you project that reads in the string constant text file and then iterates through your source files and executes the desired replacement. Be careful to limit the directories you search to you OWN source files!
build project...
This would allow you to manage your constants from one place. However it clearly is only going to help you after you have created a project and written some code :)
BASH string replacement
Adding a run script to the Xcode build process

Why won't MSBuild build a project with a dot in the name?

The Story So Far
I've got a nice solution with a desktop application project, a few library projects, and a couple of development tools projects (also desktop applications). At the moment, my build server outputs all of the code into one OutputPath. So we end up with
drop-x.y.z\
Company.MainApplication.exe <-- main application
Company.MainApplicationCore.dll <-- libraries
Helper.exe <-- developer tools
Grapher.exe
Parser.exe
... <-- the rest of the output
But, we're growing up and people outside of our team want access to our tools. So I want to organize the output. I decided that what we would want is a different OutputPath per executable project
drop-x.y.z\
Company.MainApplication\
Company.MainApplication.exe <-- main application
Company.MainApplicationCore.dll <-- libraries
... <-- application specific output
Helper\
Helper.exe <-- developer tools
... <-- tool specific output
Grapher\
Grapher.exe
...
Parser\
Parser.exe
...
What I Did
I found this simple command. I like it because it retains all the Solution working-dir context that makes msbuild a pain.
msbuild /target:<ProjectName>
For example, from my solution root as a working directory, I would call
PS> msbuild /target:Helper /property:OutputPath="$pwd\out\Helper"
I'm testing this from PowerShell, so that $pwd resolves to the full path to my working directory, or the Solution root in this case. I get the output I desire.
However, when I run this command
PS> msbuild /target:Company.MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"
I get the following error output (there's no more information, I ran with /verbosity:diagnostic)
The target "Company.MainApplication" does not exist in the project.
What I Need
The command fails on any project with a dot or dots in the name. I tried with many combinations of working directories and properties. I tried several ways of escaping the property values. I also tried running the command from a <Task> in a targets file.
I need to know either
A) How to fix this command to work property
B) How to achieve the same output with minimal friction
Try using an underscore as an escape character for the dot in the target parameter, e.g.
msbuild /target:Company_MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"
Specify the target after the -target: switch in the format :. If the project name contains any of the characters %, $, #, ;, ., (, ), or ', replace them with an _ in the specified target name.
https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-build-specific-targets-in-solutions-by-using-msbuild-exe?view=vs-2019
Dan Nolan's answer and comments are correct. Just want to supplement the Microsoft documentation.
The /targets: switch is to identify a <Target to run in the project file. You need to supply your .csproj file as a an argument that is not prefixed by a /xx option marker.
You might also want to work based on the .sln file. In that case, you still dont specify the project in the .sln to build in this manner. I'll leave you to search up the correct syntax in case that's what you end up doing.

MSBuild: ReadLinesFromFile query

I am using ReadLinesFromFile for reading multiple lines from a txt file and appending it to the path in sql.execute. The problem is its reading all the lines and appending them at once:
Text File Contents:
a.sql
b.sql
Sql.Execute ServerName="$(ServerName)" DatabaseName="CDRCntroller" path="..\DB\CDRController\BROKER\#(Prop1).
MSBuild is taking the path as : .....\BROKER\a.sql;b.sql
Any way I can use Prop1 as an array and refer the contents in the file one by one?
I tried using StringToItemList also but no luck :(
Thanks.
You should try this
Sql.Execute ServerName="$(ServerName)" DatabaseName="CDRCntroller" path="..\DB\CDRController\BROKER\%(Prop1.Identity)
The % tells Msbuild to repeat the task for each item. Identity is a Metadata containing the item itself.
Hope it helps !
Use metadata notation instead. Like this :
Sql.Execute ServerName="$(ServerName)" DatabaseName="CDRCntroller" path="..\DB\CDRController\BROKER\%(Prop1.Identity)