MvsSln 2.7.0

dotnet add package MvsSln --version 2.7.0
NuGet\Install-Package MvsSln -Version 2.7.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MvsSln" Version="2.7.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MvsSln --version 2.7.0
#r "nuget: MvsSln, 2.7.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MvsSln as a Cake Addin
#addin nuget:?package=MvsSln&version=2.7.0

// Install MvsSln as a Cake Tool
#tool nuget:?package=MvsSln&version=2.7.0

alternate text is missing from this package README image 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 contributors https://github.com/3F/MvsSln/graphs/contributors

MvsSln is waiting for your awesome contributions!

<table><tr><td>

Releases Windows Linux
📦 NuGet status status
release Tests Tests

</td><td>

Build history

</td></tr></table>

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,

https://github.com/3F/MvsSln/discussions/43#discussioncomment-371185

// 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:

alternate text is missing from this package README image

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:

alternate text is missing from this package README image

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

alternate text is missing from this package README image

vsSolutionBuildEvent

vsSolutionBuildEvent now is completely integrated with MvsSln [?]

Fully removed original parser and replaced related processing from Environment/IsolatedEnv/MSBuild/CIM. Now it just contains lightweight invoking of relevant methods.

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

alternate text is missing from this package README image

alternate text is missing from this package README image

Map of .sln & Writers

v2+ now also may provide map of analyzed data. To enable this, define a bit 0x0080 for type of operations to parser.

Parser will expose map through list of ISection for each line. For example:

alternate text is missing from this package README image

  • Each section contains handler which processes this line + simple access via RawText if not.
  • All this may be overloaded by any custom handlers (readers - ISlnHandler) if it's required by your environment.

This map may be used for modification / define new .sln data through writers (IObjHandler). For example:

var data = new List<IConfPlatform>() {
    new ConfigSln("Debug", "Any CPU"),
    new ConfigSln("Release_net45", "x64"),
    new ConfigSln("Release", "Any CPU"),
};

var whandlers = new Dictionary<Type, HandlerValue>() {
    [typeof(LSolutionConfigurationPlatforms)] = new HandlerValue(new WSolutionConfigurationPlatforms(data)),
};

using(var w = new SlnWriter("<path_to>.sln", whandlers)) {
    w.Write(map);
}

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();
    }
}

Download MvsSln

NuGet GetNuTool
package gnt MvsSln

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 20.04

dotnet build -c Release

run unit tests

dotnet test -c Release --no-build --no-restore
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.7.0 120 4/29/2024
2.6.2 12,908 7/12/2023
2.6.1 39,761 1/31/2022
2.6.0 13,453 8/23/2021
2.5.3 31,752 7/23/2020
2.5.2 6,804 5/5/2020
2.5.1 33,840 1/30/2020
2.5.0 1,532 10/17/2019
2.4.0 1,366 8/10/2019
2.3.0 1,536 6/17/2019