Shared AssemblyInfo for uniform versioning across the solution

55,099

Solution 1

First point could be solved with simple text editor that could handle several files at once and find/replace. Just open all of your csproj in it and replace string <Compile Include="Properties\AssemblyInfo.cs" /> with

<Compile Include="..\SharedAssemblyInfo.cs">
  <Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>

Alternatively you could write a utility like that:

var files = Directory.GetFiles(yourSolutionDir, "*.csproj", SearchOption.AllDirectories);
foreach (var f in files) {
  string contents = File.ReadAllText(f);
  string result = contents.Replace("<Compile Include=\"Properties\\AssemblyInfo.cs\" />", putSecondStringHere_ItIsJustTooLong); // :)
  File.WriteAllText(f, contents);
}

As for the second question... You could take a look at Visual Studio custom project templates , but I'm not sure it worth the efforts. You should IMO write test that will check this instead. It will be much simpler and outcome is actually almost the same.

UPD: About writing tests for checking solution/project files against some custom rules. Basically, sln/csproj format is simple enough to be parseable without much efforts. So if you want to have SharedAssemblyInfo.cs linked into every project - just parse csproj's and check that. Then put that checker in your build server and run it on each build. We have such system working currently and it costs something about two days to write but saved us many more (we have there more sophisticated rules and multi-solution project, so it was worth the efforts).

I won't write about this checking in detail here right now (it is not that short), but I'm going to write blog post about it soon - most probably till the end of this week. So, if you're interested - just check my blog soon :)

UPD: Here it is.

Solution 2

It is possible to link to a shared assembly info file in VS 2010. Ashish Jain has a good blog post about it: Sharing assembly version across projects in a solution.

After creating the shared assembly info file at the solution level, his instructions for linking to it from a project are:

  1. Right click on the project, in which you wish to add the Shared assembly file, and select Add -> Existing Item...

  2. Select the file “SharedAssemblyInfo.cs” from the solution folder.

  3. Instead of Add, click on the the arrow next to Add and click “Add as Link”

  4. Drag down the added linked file alongside AssemblyInfo.cs in the same folder.

  5. Repeat steps 1 – 4 for all projects for which you wish to add shared assembly file.

I've tried this and it works.

Share:
55,099
lysergic-acid
Author by

lysergic-acid

Software Engineer, Game Developer, Cat Owner Blogging @ http://www.tallior.com Check out my gigs @ http://www.fiverr.com/lysergide

Updated on July 05, 2022

Comments

  • lysergic-acid
    lysergic-acid almost 2 years

    I've read about this technique: Shared assembly info in VS projects - JJameson's blog

    Basically it means to create a SharedAssemblyInfo.cs with versioning information about the assembly, and adding this file as Link to all projects of the solution, so the actual file resides only in 1 location on disk.

    My question deals with 2 scenarios:

    1. Existing solution that doesn't use this mechanism: Is there a way to easily add the ShareAssemblyInfo to all projects? (lets say i have a solution with 50 projects).

    2. When creating a new project, by default a new AssemblyInfo.cs is created. However i'd like to link automatically to the SharedAssemblyInfo as well.

    Is there any solution for this? what is the common practice?

  • lysergic-acid
    lysergic-acid almost 13 years
    #1 - i agree this can be done with a text editor. I was wondering is VS itself supports this somehow. (btw - is EditPad Pro better than Notepad++ in your opinion?) #2 - what do u mean by write a test that will check this instead?
  • Ivan Danilov
    Ivan Danilov almost 13 years
    Yes, EditPad Pro is better IMO. At least it certainly was better when I checked N++ last time. It is also not freeware, so you should think yourself do you need it.
  • Ivan Danilov
    Ivan Danilov almost 13 years
    I've updated the answer with some details about your second point.
  • Brock Hensley
    Brock Hensley over 10 years
    Thank you for explaining about the little arrow I had overlooked!
  • ristaloff
    ristaloff over 8 years
    There is no need to repeat step 1-4. You could just drag the linked file from your first project to the next. This will copy the link file to the next project.
  • Ludwo
    Ludwo over 8 years
    You can do it also by replacing text in all *.csproj files. Update one project and compare result in new and old csproj file and appy on all other projects in your solution ;)
  • rollsch
    rollsch over 7 years
    This is a much better answer than the accepted answer. Something like this should be build into visual studio due to how simple it would be to implement.