Skip to content
/ MvsSln Public

🧩 Customizable VisualStudio .sln parser with project support (.vcxproj, .csproj., …). Pluggable lightweight r/w handlers at runtime, and more …

License

Notifications You must be signed in to change notification settings

3F/MvsSln

Repository files navigation

MvsSln

Customizable VisualStudio .sln parser with project support (.vcxproj, .csproj., …). Pluggable lightweight r/w handlers at runtime, and more …

Copyright (c) 2013-2024  Denis Kuzmin <x-3F@outlook.com> github/3F

「 ❤ 」 License

MvsSln is waiting for your awesome contributions! https://github.com/3F/MvsSln/graphs/contributors

Download Windows Linux
NuGet
gnt MvsSln
status status
release Tests Tests

Why MvsSln

MvsSln provides the easiest way to complex work with Visual Studio .sln files and referenced projects (.vcxproj, .csproj., ...). Merge, Manage, Attach custom handlers and more. Because it's free, because it's open.

🌌 The most convenient work with projects, dependencies, their lazy loading, any folders, any items, references and much more in these different worlds;

💡 We are customizable and extensible library at runtime! Make your custom .sln and its parsing for everything you like at the moment you need just in a few steps;

🚀 We were born from other popular project to be more loyal for your preferences on the fly. Hello from 2013;

Even if you just need the basic access to information or more complex work through our readers and writers.

Create/modify/or just use parsed folders, projects, and other.

Safely compare anything,

if(new ProjectItem(...) == new ProjectItem(...)) { ... }
if(new SolutionFolder(...) == new SolutionFolder(...)) { ... }
if(new ConfigItem(...) == new ConfigItem(...)) { ... }
if(new PackageInfo(...) == new PackageInfo(...)) { ... }
...

Use 📂 Subdirectories,

new SolutionFolder("dir1", 
    new SolutionFolder("dir2", 
        new SolutionFolder("dir3", "hMSBuild.bat", "DllExport.bat")
    )
);
...
new SolutionFolder("{EE7DD6B7-56F4-478D-8745-3D204D915473}", "MyFolder2", dir1, ".gnt\\gnt.core");
...

Projects and Folders,

new ProjectItem("Project1", ProjectType.Cs);
new ProjectItem("Project1", ProjectType.Cs, new SolutionFolder("dir1"));
new ProjectItem("Project2", ProjectType.Vc, "path 1");
new ProjectItem("{EE7DD6B7-56F4-478D-8745-3D204D915473}", "Project1", ProjectType.Cs, dir2);
...

Detect the real* project types,

* IsCs() - Checking both legacy `ProjectType.Cs` and modern `ProjectType.CsSdk` types.
. . .
* IsSdk() - While ProjectType cannot inform the actual use of the modern Sdk style in projects,
            current method will try to detect this by using the extended logic:
            https://github.com/dotnet/project-system/blob/master/docs/opening-with-new-project-system.md

Load only what is needed at the moment,

// https://github.com/3F/MvsSln/discussions/49

using var sln = new Sln("Input.sln", SlnItems.Env);

sln.Result.Env
    .LoadProjects(sln.Result.ProjectItemsConfigs.Where(p => p.project.IsCs()))
    .ForEach(xp =>
    {
        xp.AddItem("Compile", @"financial\Invoice.cs");
    });

Modify .sln at runtime,

#43 (comment)

// new collection from available projects but without project 'UnLib'
var projects = sln.Result.ProjectItems.Where(p => p.name != "UnLib");

// prepare write-handlers
var whandlers = new Dictionary<Type, HandlerValue>() {
    [typeof(LProject)] = new HandlerValue(new WProject(projects, sln.Result.ProjectDependencies)),
};

// save result
using(var w = new SlnWriter(@"modified.sln", whandlers)) {
    w.Write(sln.Result.Map);
}

Manage packages.config,

// 2.6+
using Sln l = new("Input.sln", SlnItems.AllNoLoad | SlnItems.PackagesConfig);

IPackageInfo found = l.Result.PackagesConfigs
                                .SelectMany(s => s.Packages)
                                .FirstOrDefault(p => p.Id.StartsWith("Microsoft."));
// found.MetaTFM ...

Version v = l.Result.PackagesConfigs.First().GetPackage("LX4Cnh")?.VersionParsed;

Easily create files from scratch,

// 2.7+
LhDataHelper hdata = new();
hdata.SetHeader(SlnHeader.MakeDefault())
        .SetProjects(projects)
        .SetProjectConfigs(prjConfs)
        .SetSolutionConfigs(slnConf);

using(SlnWriter w = new(solutionFile, hdata))
{
    w.Options = SlnWriterOptions.CreateProjectsIfNotExist;
    w.Write();
}

using Sln sln = new(solutionFile, SlnItems.EnvWithMinimalProjects);
IXProject xp = sln.Result.Env.Projects.First();

xp.SetProperties(new Dictionary<string, string>()
{
    { "OutputType", "EXE" },
    { "TargetFramework", "net8.0" },
    { "Platforms", "x64" }
});
xp.Save();

Everything at hand,

using(var sln = new Sln(@"D:\projects\Conari\Conari.sln", SlnItems.All & ~SlnItems.ProjectDependencies))
{
    //sln.Result.Env.XProjectByGuid(
    //    sln.Result.ProjectDependencies.FirstBy(BuildType.Rebuild).pGuid,
    //    new ConfigItem("Debug", "Any CPU")
    //);

    var p = sln.Result.Env.GetOrLoadProject(
        sln.ProjectItems.FirstOrDefault(p => p.name == name)
    );

    var paths = sln.Result.ProjectItems
                            .Select(p => new { p.pGuid, p.fullPath })
                            .ToDictionary(p => p.pGuid, p => p.fullPath);

    // {[{27152FD4-7B94-4AF0-A7ED-BE7E7A196D57}, D:\projects\Conari\Conari\Conari.csproj]}
    // {[{0AEEC49E-07A5-4A55-9673-9346C3A7BC03}, D:\projects\Conari\ConariTest\ConariTest.csproj]}

    foreach(IXProject xp in sln.Result.Env.Projects)
    {
        xp.AddReference(typeof(JsonConverter).Assembly, true);
        xp.AddReference("System.Core");

        ProjectItem prj = ...
        xp.AddProjectReference(prj);

        xp.AddImport("../packages/DllExport.1.5.1/tools/net.r_eg.DllExport.targets", true);

        xp.SetProperty("JsonConverter", "30ad4fe6b2a6aeed", "'$(Configuration)' == 'Debug'");
        xp.SetProperties
        (
            new[]
            {
                new PropertyItem("IsCrossTargetingBuild", "true"),
                new PropertyItem("CSharpTargetsPath", "$(MSBToolsLocal)\\CrossTargeting.targets")
            },
            "!Exists('$(MSBuildToolsPath)\\Microsoft.CSharp.targets')"
        );
        // ...
    }

    sln.Result.ProjectConfigs.Where(c => c.Sln.Configuration == "Debug"); // project-cfgs by solution-cfgs
    // ...

} // release all loaded projects

And something more,

// https://github.com/3F/MvsSln/discussions/42

using var sln = new Sln("TestStruct.sln", SlnItems.Env);
//...
sln.Result.Env.Projects.ForEach(xp => 
    xp.Project.Xml.PropertyGroups.Where(p => p.Condition.Contains("'$(Configuration)|$(Platform)'"))
    .Where(p =>
        sln.Result.ProjectItemsConfigs.All(s => 
            !p.Condition.Contains($"'{s.projectConfig.ConfigurationByRule}|{s.projectConfig.PlatformByRule}'")
        )
    ).ForEach(p => p.Parent?.RemoveChild(p))
);

The any new solution handler (reader or writer) can be easily added because of our flexible architecture.

Control anything and have fun !

MvsSln was specially extracted and re-licensed from vsSolutionBuildEvent projects (GPL -> MIT) for https://github.com/3F/DllExport and others! Join us 🎈

High quality Project Icons. Visual Studio

Since Microsoft officially distributes 5,000 high quality free icons and bitmaps from products like Visual Studio,

You can also use related project icons together with MvsSln like it was already for .NET DllExport project:

Follow License Terms for icons and Find implementation in original repo: https://github.com/3F/DllExport

How or Where is used

Let's consider examples of use in real projects below.

.NET DllExport

DllExport project finally changed distribution of the packages starting with v1.6 release. The final manager now fully works via MvsSln:

MvsSln also is a core logic in Post-Processing feature [?]

vsSolutionBuildEvent

vsSolutionBuildEvent now is completely integrated with MvsSln [?]

Fully removed original parser and replaced related processing from Environment/IsolatedEnv/MSBuild/CIM. Now it just calls the corresponding modern methods from MvsSln.

https://github.com/3F/vsSolutionBuildEvent

Map & handlers

2.0+ can optionally provide a map of the analyzed data. To enable this, define a 0x0080 bit.

MvsSln's parser will fill the map using handlers that processed line and/or raw access through wrapper, like:

The map may be used for any modifications or creating a new solution or project files through other handlers etc.

Read more about map, skeleton, and SlnWriter here

Projects. Adding References

XProject.AddPackageReference("Conari", "1.5.0");
xp.AddProjectReference(projects.First());
xp.AddProjectReference(new ProjectItem(ProjectType.Cs, @$"{projName}\src.csproj"));
xp.AddReference(Assembly.GetExecutingAssembly());
XProject.AddReference("DllExport", lib, AddReferenceOptions.MakeRelativePath);
xp.AddReference(
    pathToDll,
    AddReferenceOptions.DefaultResolve | AddReferenceOptions.OmitVersion | AddReferenceOptions.HidePrivate
);
<Reference Include="MvsSln, PublicKeyToken=4bbd2ef743db151e" />
<Reference Include="DllExport, Version=1.6.4.15293, Culture=neutral, PublicKeyToken=8337224c9ad9e356">
  <HintPath>..\packages\DllExport.1.6.4\gcache\metalib\DllExport.dll</HintPath>
  <Private>False</Private>
</Reference>

Example of extending (your custom handlers)

Example of LProject handler (reader):

using static net.r_eg.MvsSln.Core.Keywords;

public class LProject: LAbstract, ISlnHandler
{
    public override ICollection<Type> CoHandlers { get; protected set; }
        = [typeof(LProjectDependencies)];

    public override bool IsActivated(ISvc svc)
        => (svc.Sln.ResultType & SlnItems.Projects) == SlnItems.Projects;

    public override bool Condition(RawText line)
        => line.trimmed.StartsWith(Project_, StringComparison.Ordinal);

    public override bool Positioned(ISvc svc, RawText line)
    {
        ProjectItem pItem = GetProjectItem(line.trimmed, svc.Sln.SolutionDir);
        if(pItem.pGuid == null) return false;
        if(svc.Sln.ProjectItemList == null) svc.Sln.ProjectItemList = [];

        svc.Sln.ProjectItemList.Add(pItem);
        return true;
    }
}

Example of WSolutionConfigurationPlatforms handler (writer):

using static net.r_eg.MvsSln.Core.Keywords;

public class WSolutionConfigurationPlatforms(IEnumerable<IConfPlatform> configs)
    : WAbstract, IObjHandler
{
    protected IEnumerable<IConfPlatform> configs = configs;

    public override string Extract(object data)
    {
        if(configs == null) return null;

        lbuilder.Clear();
        lbuilder.AppendLv1Line(SolutionConfigurationPlatformsPreSolution);

        configs.ForEach(cfg => lbuilder.AppendLv2Line($"{cfg} = {cfg}"));

        return lbuilder.AppendLv1(EndGlobalSection).ToString();
    }
}

Build MvsSln from source

git clone https://github.com/3F/MvsSln.git MvsSln
cd MvsSln

Windows. Visual Studio / MSBuild

.\build Release

or together with configured netfx4sdk

.\build-CI Release

Ubuntu. vscode / dotnet

dotnet build -c Release

run unit tests

dotnet test -c Release --no-build --no-restore