Output content file to specific folder while building project - visual-studio

I have VS 2012 project with structure like this:
Project
Folder1
file.xml
schema.xsd
code.cs
Folder2
code1.cs
code2.cs
I set Copy to output directory property of file.xml and schema.xsd to Copy always and want to output them to the same folder where assemblies outputed (bin\Debug) but they always copied to folder bin\Debug\Folder1. Is there a way to achieve my goal without moving files to the root of the project?

I recommend you do this with a Post-built event script. It's not as hectic as it sounds, and gives you loads of flexibility.
Right-click on your project and select Properties.
Go to the Build Events tab and click Edit Post-build...
(See screenshots below)
You can now specify shell commands to be executed after each successful build of your project. The following achieves the example in your question:
copy "$(ProjectDir)Folder1\file.xml" "$(TargetDir)"
copy "$(ProjectDir)Folder1\schema.xsd" "$(TargetDir)"
$(ProjectDir) and $(TargetDir) are macros which insert the respective values relevant to your project. You can select and insert macros from the macro menu in the edit window.
The quotes are included above, because your ProjectDir and TargetDir might resolve to full paths that include spaces, and that will break the copy command if the spaces aren't there.

Related

Sublime Text 2 project default file(s) for building

I am a beginner user of Sublime Text 2, but I was trying to set up project specific default file(s) to build from. I'll explain myself:
When writing in LaTeX, I tend to separate the sections or chapters in different .tex files. When I build, I would like the main file to be the one passed to the compiler no matter what file has focus (a section/chapter .tex or the .log file or any other file for that matter).
Similarly, when writing C++ code, I would like to be able to specify a list of files to pass to the compiler.
Right now I am using a copy of the C++ build-system with *.cpp instead of the current file in the "cmd" line, but not always I want to build every cpp file in the folder. A similar modification on the LaTeX builder would be possible changing the file to build to something like main.tex and using main.tex for the main file every time.
Even though these are workarounds, I was wondering if there was a way of defining the default file(s) to the be passed to the compiler (or to a batch file if necessary) from the project specific files.

How do I make sure every file in an included directory is copied to the destination in Visual Studio?

I'm building an application that has quite a few files that need to be included in the destination directory. So far, there haven't been very many files so I would simply change the settings of each file individually to Copy to Output Directory :: Copy if newer. The problem now is that I'm adding files at an exponential rate.
Do any of you know how to make all of the files that are sub of the "Some_Directory" have the Copy to Output Directory :: Copy if newer set how I want it?
You cam use a post-build event.
Using following posts, I made a post event that does exactly what you want:
Copying files into the application folder at compile time
Copy to Output Directory copies folder structure but only want to copy files
Visual Studio adds .dll and .pdb to project after compiling
Now, you can easily edit Post-build events, go to your project settings and go to the Compile tab, now click the Build Events and put following line in the *Post-build event command line" text box:
xcopy "$(ProjectDir)Test\*.*" "$(TargetDir)Test\\" /E /I /F /Y
See this image:
In your case you will have to change "Test" by "some_directory" and that's all.
There is one caveat, however. This copies every file every time my project builds. The folder that is being copied is over 3MB (I'm developing quite a large project). This makes debugging take quite a long time because every time I do a build it has to move all of the content over. Is there a way to make it only copy over files that have been updated since the last build? This is why I was using the Copy if newer option.
You can add the /D parameter (you can look that up with xcopy /? in your command prompt).
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
You can write your own custom Post-Build event (Select project -> Properties -> Build Events) with xcopy command. You can use $(solution) macro for your solution directory and specify relative path to your files.
I had the same issue, but the accepted answer didn't solve it. What worked for me was to enable deployment in Local.testsettings (under Solution Items).

Finding all the visual studio projects a file belongs to

I'm working on a visual studio solution with over 30 projects and multiple filters.
What is the easiest way to determine all the projects a file belongs too?
First, open a command shell window and create a list of all project files in a text file. For example, for C# projects (having the ending .csproj), run this command in the root folder of your solution:
dir /s /b *.csproj >projectlist.txt
Then, you can easily determine all projects containing a specific file by the command
findstr /f:projectlist.txt /m Name_Of_Your_File
Just a suggestion: you can avoid much trouble for the future if you make sure each project has it's own folder, and all files belonging to that project are in or below that folder.
Use AgentRansack or similar tool that allows searching text contained in a file.
Use the following settings:
File Name: *.csproj
Containing Text: YourCodeFile.cs
Look in: YourSolutionFolder
Run the search and you will get a list of all project files that are holders of the CS file.

How to force the build to be out of date, when a text file is modified?

The Scenario
My project has a post-build phase set up to run a batch file, which reads a text file "version.txt". The batch file uses the information in version.txt to inject the DLL with a version block using this tool.
The version.txt is included in my project to make it easy to modify. It looks a bit like this:
#set #Description="TankFace Utility Library"
#set #FileVersion="0.1.2.0"
#set #Comments=""
Basically the batch file renames this file to version.bat, calls it, then renames it back to version.txt afterwards.
The Problem
When I modify version.txt (e.g. to increment the file version), and then press F7, the build is not seen as out-of-date, so the post-build step is not executed, so the DLL's version doesn't get updated.
I really want to include the .txt file as an input to the build, but without anything actually trying to use it.
If I #include the .txt file from a CPP file in the project, the compiler fails because it obviously doesn't understand what "#set" means.
If I add /* ... */ comments around the #set commands, then the batch file has some syntax errors but eventually succeeds. But this is a poor solution I think.
So... how would you do it?
This works in VS2005. If you're not using that, some of the settings may be in different places or with different names.
Add the text file to your project, right click on it in the Solution Explorer and select 'Properties'. Under Configuration Properties > General make sure that the file is not excluded from the build. Under Custom Build Step > General, put your existing post-build command as the Command Line setting. Make sure you specify your .txt file as the output file. Now F7 should spot changes to the text file and run your batch file.
This may be too "hacky" but this might work:
Write a quick Perl (etc.) script to check version.txt has been updated.
If the file has been modified, have this script update a dummy source file that is compiled with your project.
Run the script as a pre-build event.
This way if the script sees that the file has changed, it will change the other one, which will force a re-build.
Hacky, but try it if you're scraping the bottom of the barrel.

Add files to an Xcode project from a script?

Right now I'm using a few scripts to generate files that I'm including as resources in Xcode. The thing is I'm running the script, then deleting from the project, then adding back into the project. There must be a way to automate this last step, so that the script can generate the files and automatically add them into the xcode project for me.
I'm using bash but any language examples would help.
Thanks,
Andrew
I had a similar need as Andrew. I needed to be able to include resources from a script without knowing those resources ahead of time. Here's the solutions I came up with:
Add a new Run Script build phase after “Copy Bundle Resource” that contains the following command:
find -L ${SRCROOT}/SomeDerivedResources \
-type f -not -name ".*" \
-not -name "`basename ${INFOPLIST_FILE}`" \
| xargs -t -I {} \
cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
Looks scary, but let’s break it down:
find -L ${SRCROOT}/SomeDerivedResources
This crawls the directory SomeDerivedResources in our source root (-L tells it to follow symbolic links)
-type f
Only include regular files
-not -name ".*"
Ignore files starting with a dot
-not -name "`basename ${INFOPLIST_FILE}`"
In my case, my Info plists live in my SomeDerivedResources directory so we need to exclude that file from being copied to our product
| xargs -t -I {}
Pipe the results of find into xargs with -t (echo resulting commands to stderr so they show up in our build log), -I (run the command once for each input file) and use {} as our argument placeholder
cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
Lastly, copy each found file (denoted by {}) to our product’s resource directory.
I realized when typing this that using an rsync setup instead of cp could prevent us from copying resources each time you build. If your resources are very large it might be worth looking in to.
(Also, a folder reference wouldn’t work for my need for a few reasons. One, my icons are in my DerivedResources directory and having them in a subdirectory in the bundle seems not to work. Also, I ideally wanted to be able to use [UIImage imageNamed:#"MyAwesomeHappyImage.png"] and -pathForResource:ofType: (and some of my files are nested further inside my DerivedResources directory). If your needs don’t contain those restraints, I highly suggest you go the folder reference route.)
This can be done by adding a new build phase to your application.
In your Xcode project browser, find the target for your application, and expand it to show all of the build phases.
Add a new "run script" build phase to your target. The easiest way is to right-click on the target and choose "Add/New Build Phase/New Run Script Build Phase"
Adding the new build phase should bring up an inspector window. In this window, you can enter the entire shell script, or simply a command line to run the script.
Here's the gold: At the bottom of the inspector window you can specify input files and output files. Specifying input files sets up dependencies automatically (the shell script will only be executed if some of the input files have been modified). Specifying output files automatically propagates the dependencies to those files. When your shell script is run, Xcode knows that it needs to deal with those files that the shell script has modified.
Be sure to drag your new build phase up to the top of the list of phases as shown in the screenshot below. The order will be important if you need those resource files to be included in the bundle.
Save, build, commit to the repository, ask for a raise, get some fresh air and have a nice day! :)
For those with large number of files, to avoid having to recopy (or recheck) each file, as suggested by #Ben Cochran (thanks a lot for the great script), this is how to do it with rsync:
Basically, the files just need to be copied into the main bundle
In that case just add a folder reference to the project (Create a folder in your project folder and then drag it into your projects "Resources" group (in the "Files & Groups" list; then in the sheet that appears select the "Create Folder References for any added Folder" radio button) and Xcode will copy the folder and all of its contents into the target bundle at build time.
Just an additional note: If you use this method to add image subfolders you'll have to prefix the image name with the subfolder name to use '[UIImage imageNamed:]'. For example if you have an image named "Rendezvous.png" in a subfolder named "MyImages":
`
// this won't work
UIImage * image = [UIImage imageNamed:#"Rendezvous"];
if (image) {
NSLog(#"Found Rendezvous!");
} else {
NSLog(#"Didn't find Rendezvous.");
}
// but this will!
image = [UIImage imageNamed:#"MyImages/Rendezvous"];
if (image) {
NSLog(#"Found MyImages/Rendezvous!");
} else {
NSLog(#"Didn't find MyImages/Rendezvous.");
}
`
If you already have the files somewhere on your system, relative to your source root, you can always add a "Copy Files" phase. This will allow you to specify a directory where your resources should be copied from.
You can combine this with the Build Script phase answer provided to you already. For instance, run a script to check out your assets from Subversion into a subdirectory of your project, and then follow that up with a Copy Files phase that copies from "$(SRCROOT)/Assets".
I know it's a bit late, but I just came across this article explaining how to do something that sounds like what you're looking for.
I found myself with a similar situation using Ionic Capacitor. What I was expecting was to include files on the "Copy Bundle Resources" bundle phase. What I found is that Ionic already packs you some inclusions and if you slip your files along this folders you get it included as well.
Do you see the App folder inclusion? It our entry point.
To include on it I add a script that do something like this:
cp -Rf ./includes/yourfolder/ ./ios/App/App/
I managed to solve the issue
"Code object is not signed at all"
that can be encountered during build upload to iTunes Connect in this way:
I didnot include the script to Bundle resources.
So the script (in this case Python file) is executed during build, (it does what it has to do) but it is not included in the bundle of the app.
How to do?
Open Build Phases, go to Copy Bundle Resources section, select the file and remove it with (-).

Resources