3

I want to have a "manifest.json" file in my project, that contains a list of .cs and .dll files the project depends on, but are not part of the project. To compile those files also on build, I need to somehow tell Visual Studio to include those source files and assemblies into the build process.

Is there a way I could do this in the pre-build event?

Van Coding
  • 24,244
  • 24
  • 88
  • 132
  • why don't you put those file in another project. – giammin Feb 06 '13 at 14:08
  • @giammin Because depending on another project means depending on their resulting assembly. I want to directly depend on source files, that get compiled into my executable. – Van Coding Feb 06 '13 at 14:11
  • 1
    why don't you include those files in project? – giammin Feb 06 '13 at 14:22
  • @giammin that's not dynamic. To explain it better: a manifest file can also reference another manifest file that references additional source files and assemblies. The manifest file of my final project would reference all source files of the current project, all needed assemblies and all manifest files the project depends on (which do the same again). I hope you get the idea of what I want to do... – Van Coding Feb 06 '13 at 14:34

2 Answers2

2

I've made a custom ITaskItem now that adds the files before the build process.

Here's how I've done it:

1) Create custom ITaskItem

public class AddSourceFiles : Task
{
    private ITaskItem[] output = null;

    [Output]
    public ITaskItem[] Output
    {
        get
        {
            return output;
        }
    }

    public override bool Execute()
    {
        //gather a list of files to add:
        List<string> filepaths = new List<string>() { "a.cs", "b.cs", "d.cs" };

        //convert the list to a itaskitem array and set it as output
        output = new ITaskItem[filepaths.Count];
        int pos = 0;
        foreach (string filepath in filepaths)
        {
            output[pos++] = new TaskItem(filepath);
        }
    }
}

2) Create a *.targets file, for example "AddSourceFiles.targets":

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask AssemblyFile="AddSourceFiles.dll" TaskName="AddSourceFiles" />
    <PropertyGroup>
        <BuildDependsOn>
            AddSourceFiles;
            $(BuildDependsOn);
        </BuildDependsOn>
    </PropertyGroup>
    <Target Name="AddSourceFiles">
        <AddSourceFiles>
            <Output TaskParameter="Output" ItemName="Compile" />
        </AddSourceFiles>
    </Target>   
</Project>

As you can see, the resulting DLL of the class "AddSourceFiles" is referenced in the task file.

3) The last step is to import this .targets file into every .csproj file that you want to include files using your AddSourceFiles class.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  .
  .
  <Import Project="c:\path\to\AddSourceFiles.targets" />
  .
  .
</Project>

I'm also very new to this, so feel free to improve this one ;)

Marc
  • 3,905
  • 4
  • 21
  • 37
Van Coding
  • 24,244
  • 24
  • 88
  • 132
1

you should use VisualStudio Macros:

http://msdn.microsoft.com/en-us/library/b4c73967(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/8h31zbch.aspx

The Macros IDE contains an example that is similar to what you are trying to achieve:

AddDirAsSlnFolder — Imports a folder on disk into a solution folder structure.

------update-----

i've just find out that Vs2012 has Macro functionality removed....

giammin
  • 18,620
  • 8
  • 71
  • 89
  • where can I donwload the Macros IDE? The Microsoft site does not want to let me know :/ ..is there no possibility to do it using C#? – Van Coding Feb 06 '13 at 15:00
  • I also hate vb. when I have to create a macros I create a dll in c# and then call it with vb. To open Macros IDE Alt+F11 from visualstudio. If you want to avoid macros maybe visual studio automation model can fits your needs. – giammin Feb 06 '13 at 15:21
  • I've found a way to do it with a custom ITaskItem, but I'm still interested in a solution using macros or visual studio automation. Could you maybe provide a very basic example of how to do it? – Van Coding Feb 07 '13 at 17:49
  • @VanCoding the solution of this question has a Macro that you can easly adapt to your needs: http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-in-the-project – giammin Feb 08 '13 at 08:56