DotnetProjectSystem is a nuget package for working with .sln, .csproj and .props files.
- Creating solution and project files structure from code, fluent API
- Parsing solution and project files
- Typed wrappers for reading and modifying solution and project files
- API for modifying solution and project files. Implementation of strategy for migration solution to Central Package Management
- Support of TestableIO.System.IO.Abstractions for testing
- Task: create solution file, project files and Directory.Build.props file.
- Use case: need to create sample solution for test proposes (on real file system or MockFileSystem).
Code sample for creating solution and project files structure:
varfactory=newSolutionFileStructureBuilderFactory(fileSystem,syntaxFormatter);varproject=newProjectFileStructureBuilder("FirstProject").SetContent(""" <Project> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> </PropertyGroup> </Project> """).AddFile(["Models","MyModel.cs"],"using System;");vardirectoryBuildPropsFile=DirectoryBuildPropsFile.CreateEmpty();directoryBuildPropsFile.File.Properties.SetProperty("Company","Kysect");factory.Create("NewSolution").AddProject(project).AddDirectoryBuildProps(directoryBuildPropsFile).Save(@"D:\Repositories\NewSolution");will create this file structure:
D:\Repositories\NewSolution\
|- FirstProject/
|- FirstProject.csproj
|- Models
|- MyModel.cs
|- Directory.Build.props
|- NewSolution.sln
Nuget provide API for parsing .sln and .csproj files:
stringsolutionFilePath= ...;varparser=newDotnetSolutionParser(_fileSystem,logger);varresult=parser.Parse(solutionFilePath);Parse method return model DotnetSolutionDescriptor that contains information about projects. Project represented by DotnetProjectFile model that provide access to project properties and items.
Some samples for working with DotnetProjectFile:
DotnetProjectFileproject= ...;varpackageReferences=project.PackageReferences.GetPackageReferences();// packageReferences = { { "System.Text.Json", "8.0.0" }, { "Microsoft.Extensions.Logging", "8.0.0" } }project.PackageReferences.SetPackageReference("System.Text.Json","9.0.0");// packageReferences = { { "System.Text.Json", "9.0.0" }, { "Microsoft.Extensions.Logging", "8.0.0" }project.PackageReferences.AddPackageReference("Kysect.Editorconfig","1.0.0");varimplicitUsings=project.Properties.GetProperty("ImplicitUsings");// implicitUsings = trueproject.Properties.SetProperty("ImplicitUsings","false");// implicitUsings = falseNuget provide API for modification of parsed solution. NuGet package shipped with implementation of strategy for migration solution to Central Package Management as sample.
stringsolutionFilePath= ...;varfactory=newDotnetSolutionModifierFactory(_fileSystem,_solutionFileContentParser,_formatter);varsolutionModifier=_solutionModifierFactory.Create(solutionFilePath);varmigrator=newCentralPackageManagementMigrator(logger);migrator.Migrate(solutionModifier);TestableIO.System.IO.Abstractions is NuGet that provide abstraction for file system. This abstraction can be used for testing. Nuget provide implementation of IFileSystem for TestableIO.System.IO.Abstractions. Kysect.DotnetProjectSystem fully support this abstraction.
Currently main use case for this library is https://github.com/kysect/Zeya - tool for managing .NET projects and modification them. You can check it for more examples of using this library.