├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.md │ └── feature_request.md └── workflows │ └── dotnet.yml ├── .gitignore ├── LICENSE ├── README.md ├── Slnx.sln ├── Slnx.xml ├── benchmark ├── Slnx.ReadBenchmark │ ├── Program.cs │ └── Slnx.ReadBenchmark.csproj └── Slnx.WriteBenchmark │ ├── Program.cs │ └── Slnx.WriteBenchmark.csproj └── src └── Slnx ├── DescendantConfiguration.cs ├── Folder.cs ├── Project.cs ├── ProjectType.cs ├── ProjectTypeExtensions.cs ├── Property.cs ├── PropertyCollection.cs ├── Slnx.csproj ├── SlnxFactory.cs ├── SlnxModel.StoreAsync.cs ├── SlnxModel.cs ├── SlnxParser.cs └── SolutionException.cs /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.cs] 2 | 3 | # IDE0250: Make struct 'readonly' 4 | csharp_style_prefer_readonly_struct = true 5 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: Bug report 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | Run this code: 16 | ```cs 17 | Console.WriteLine("Replace this with the code that causes the issue"); 18 | ``` 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Environment (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - SLNX version: [e.g. 2.0] 29 | - Runtime version: [e.g. .NET 9.0 Preview] 30 | - Browser [e.g. chrome, safari] 31 | - Version [e.g. 22] 32 | 33 | **Additional context** 34 | Add any other context about the problem here. 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: Feature suggestion 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/dotnet.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a .NET project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net 3 | 4 | name: .NET 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Setup .NET 20 | uses: actions/setup-dotnet@v4 21 | with: 22 | dotnet-version: 8.0.x 23 | - name: Restore dependencies 24 | run: dotnet restore 25 | - name: Build 26 | run: dotnet build --no-restore 27 | - name: Test 28 | run: dotnet test --no-build --verbosity normal 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 winscripter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > [!IMPORTANT] 2 | > It is recommended to use an official library to work with SLNX files. See Microsoft.VisualStudio.SolutionPersistence 3 | > on [NuGet](https://nuget.org/packages/Microsoft.VisualStudio.SolutionPersistence) or [GitHub](https://github.com/microsoft/vs-solutionpersistence). 4 | > 5 | 6 | # Slnx 7 | SLNX is a fast parser and writer for the (currently) new in-preview Visual Studio XML Solution format with a .slnx extension, introduced in Visual Studio 2022 17.10 Preview 3. 8 | 9 | Slnx has been around since April 23, 2024 - days after SLNX file format was introduced. 10 | 11 | Available on NuGet: https://nuget.org/packages/Slnx 12 | 13 | ### Reading 14 | 15 | ```cs 16 | using Slnx; 17 | 18 | var model = SlnxModel.Load(File.ReadAllText("TestSlnx.txt")); 19 | 20 | foreach (Folder folder in model.TopLevelFolders!) 21 | { 22 | foreach (string file in folder.DescendantFiles!) 23 | { 24 | Console.WriteLine(file); 25 | } 26 | } 27 | ``` 28 | 29 | TestSlnx.txt content: 30 | ``` 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ``` 43 | Output: 44 | ``` 45 | File1.cs 46 | .editorconfig 47 | ``` 48 | 49 | ### Writing (applies to Slnx 3.0) 50 | ```cs 51 | using Slnx; 52 | using System.Text.Json; 53 | 54 | // Note: This is a .NET 8 project and uses Collection Expressions. 55 | 56 | var factory = new SlnxFactory(); 57 | 58 | var folder = new Folder("Solution Items"); 59 | folder.AddProjectWithPathOnly("./CSharp/CSharp.csproj"); 60 | folder.AddProjectWithPathOnly("./VB.NET/VB.NET.vbproj"); 61 | folder.AddProject(new Project("./DockerCompose/DockerCompose.dcproj", type: Guid.NewGuid().ToString(), config: new(solution: "*|*", project: "*|*|Deploy"))); 62 | var moreFolders = new Folder("C++"); 63 | moreFolders.AddFiles(["util.cpp", "util.h", "data.cc", "data.h"]); 64 | folder.AddFiles(["File1.cs", "File2.cs"]); 65 | folder.AddFolder(moreFolders); 66 | 67 | factory.AddFolder(folder); 68 | factory.AddProjectWithPathOnly("Slnx/Slnx.csproj"); 69 | factory.AddProjectWithPathOnly("App/App.shproj"); 70 | 71 | string content = factory.AsModel().Store(); 72 | File.AppendAllText("OutputSlnx.txt", content); 73 | 74 | // To provide detailed information, I'll just JSONify it with System.Text.Json. 75 | var model = SlnxModel.Load(File.ReadAllText("OutputSlnx.txt")); 76 | Console.WriteLine(JsonSerializer.Serialize(model)); 77 | ``` 78 | The program will generate a file named `OutputSlnx.txt` with these contents: 79 | 80 | ``` 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | ``` 101 | 102 | # Contributing 103 | Bug reports, feature suggestions, questions, and other feedback are welcome. 104 | 105 | # Compatibility 106 | Slnx uses the .NET 6.0 Runtime, but it works fine for preceding versions, including .NET 7.0, 8.0, and future releases. 107 | 108 | # Benchmarking 109 | 110 | ## Read Benchmark 111 | Input: 112 | ```xml 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | ``` 125 | 126 | Benchmark result: 127 | ``` 128 | 129 | BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.2861/23H2/2023Update/SunValley3) 130 | AMD Ryzen 7 4700U with Radeon Graphics, 1 CPU, 8 logical and 8 physical cores 131 | .NET SDK 8.0.303 132 | [Host] : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2 133 | DefaultJob : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2 134 | 135 | 136 | ``` 137 | | Method | Mean | Error | StdDev | Gen0 | Allocated | 138 | |------------ |---------:|----------:|----------:|-------:|----------:| 139 | | ReadBenchie | 4.486 μs | 0.0301 μs | 0.0267 μs | 7.1106 | 14.55 KB | 140 | 141 | ## Write Benchmark 142 | Code: 143 | ```cs 144 | var factory = new SlnxFactory(); 145 | 146 | var folder = new Folder("Solution Items"); 147 | folder.AddProjectWithPathOnly("./CSharp/CSharp.csproj"); 148 | folder.AddProjectWithPathOnly("./VB.NET/VB.NET.vbproj"); 149 | folder.AddProject(new Project("./DockerCompose/DockerCompose.dcproj", type: null, config: new(solution: "*|*", project: "*|*|Deploy"))); 150 | var moreFolders = new Folder("C++"); 151 | moreFolders.AddFiles(["util.cpp", "util.h", "data.cc", "data.h"]); 152 | folder.AddFiles(["File1.cs", "File2.cs"]); 153 | folder.AddFolder(moreFolders); 154 | 155 | factory.AddFolder(folder); 156 | factory.AddProjectWithPathOnly("Slnx/Slnx.csproj"); 157 | factory.AddProjectWithPathOnly("App/App.shproj"); 158 | 159 | _ = factory.AsModel().Store(); 160 | ``` 161 | To provide the accurate benchmark, we're not saving or logging the result anywhere like console or file - we're 162 | just focusing on how fast Slnx can export the SlnxFactory instance as a string. 163 | 164 | Benchmark result: 165 | ``` 166 | 167 | BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.2861/23H2/2023Update/SunValley3) 168 | AMD Ryzen 7 4700U with Radeon Graphics, 1 CPU, 8 logical and 8 physical cores 169 | .NET SDK 8.0.303 170 | [Host] : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2 171 | DefaultJob : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2 172 | 173 | 174 | ``` 175 | | Method | Mean | Error | StdDev | Gen0 | Allocated | 176 | |------------- |---------:|----------:|----------:|-------:|----------:| 177 | | WriteBenchie | 4.841 μs | 0.0371 μs | 0.0310 μs | 9.4299 | 19.33 KB | 178 | -------------------------------------------------------------------------------- /Slnx.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 17 3 | VisualStudioVersion = 17.10.35122.118 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4ED70E92-F679-4B86-A5B3-EC1234EA24D2}" 6 | EndProject 7 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Slnx", "src\Slnx\Slnx.csproj", "{D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}" 8 | EndProject 9 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{D32C2907-F140-4C3B-98A4-F5511A2C8D43}" 10 | EndProject 11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slnx.ReadBenchmark", "benchmark\Slnx.ReadBenchmark\Slnx.ReadBenchmark.csproj", "{BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}" 12 | EndProject 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slnx.WriteBenchmark", "benchmark\Slnx.WriteBenchmark\Slnx.WriteBenchmark.csproj", "{420A3B54-9B79-41A3-82D0-7C275212B44D}" 14 | EndProject 15 | Global 16 | GlobalSection(ExtensibilityGlobals) = postSolution 17 | SolutionGuid = {C1D04ABB-EF13-4243-A653-B1BC6CB05EB8} 18 | EndGlobalSection 19 | GlobalSection(NestedProjects) = preSolution 20 | {420A3B54-9B79-41A3-82D0-7C275212B44D} = {D32C2907-F140-4C3B-98A4-F5511A2C8D43} 21 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301} = {D32C2907-F140-4C3B-98A4-F5511A2C8D43} 22 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0} = {4ED70E92-F679-4B86-A5B3-EC1234EA24D2} 23 | EndGlobalSection 24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 25 | {420A3B54-9B79-41A3-82D0-7C275212B44D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {420A3B54-9B79-41A3-82D0-7C275212B44D}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {420A3B54-9B79-41A3-82D0-7C275212B44D}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {420A3B54-9B79-41A3-82D0-7C275212B44D}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Release|Any CPU.Build.0 = Release|Any CPU 33 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 34 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Debug|Any CPU.Build.0 = Debug|Any CPU 35 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Release|Any CPU.ActiveCfg = Release|Any CPU 36 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Release|Any CPU.Build.0 = Release|Any CPU 37 | EndGlobalSection 38 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 39 | Debug|Any CPU = Debug|Any CPU 40 | Release|Any CPU = Release|Any CPU 41 | EndGlobalSection 42 | GlobalSection(SolutionProperties) = preSolution 43 | HideSolutionNode = FALSE 44 | EndGlobalSection 45 | EndGlobal 46 | ny CPU.Build.0 = Release|Any CPU 47 | {BF6A75A8-2A70-42AC-A84A-547CED1EF608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {BF6A75A8-2A70-42AC-A84A-547CED1EF608}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {BF6A75A8-2A70-42AC-A84A-547CED1EF608}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {BF6A75A8-2A70-42AC-A84A-547CED1EF608}.Release|Any CPU.Build.0 = Release|Any CPU 51 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Release|Any CPU.ActiveCfg = Release|Any CPU 54 | {BFB1A338-7FE1-46A1-9F41-2BEF3ABBF301}.Release|Any CPU.Build.0 = Release|Any CPU 55 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Release|Any CPU.ActiveCfg = Release|Any CPU 58 | {D23FC02A-231A-48F1-86E3-1C6DD9D0D8C0}.Release|Any CPU.Build.0 = Release|Any CPU 59 | EndGlobalSection 60 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 61 | Debug|Any CPU = Debug|Any CPU 62 | Release|Any CPU = Release|Any CPU 63 | EndGlobalSection 64 | GlobalSection(SolutionProperties) = preSolution 65 | HideSolutionNode = FALSE 66 | EndGlobalSection 67 | EndGlobal 68 | -------------------------------------------------------------------------------- /Slnx.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Slnx 5 | 6 | 7 | 8 | 9 | Represents a "<Configuration>" like tag. 10 | 11 | 12 | 13 | 14 | Solution where this configuration applies to. 15 | 16 | 17 | 18 | 19 | Project where this configuration applies to. 20 | 21 | 22 | 23 | 24 | Initializes a new instance of the class. 25 | 26 | Solution where this configuration applies to. 27 | Project where this configuration applies to. 28 | 29 | 30 | 31 | Represents a folder in the same level as the solution. 32 | 33 | 34 | 35 | 36 | Gets an instance of with no content and name "(Folder Name)". 37 | 38 | 39 | 40 | 41 | Name of the folder. 42 | 43 | 44 | 45 | 46 | Nested projects in the folder. If this is , then there 47 | are no descendant projects. 48 | 49 | 50 | 51 | 52 | Nested folders in the folder. If this is , then there 53 | are no descendant folders. 54 | 55 | 56 | 57 | 58 | Files in the folder. If this is , then there 59 | are no descendant files. 60 | 61 | 62 | 63 | 64 | Initializes a new instance of the class. 65 | 66 | Folder name. 67 | All projects. 68 | All folders. 69 | All files. 70 | 71 | 72 | 73 | Initializes a new instance of the class. 74 | 75 | The name of the folder. 76 | 77 | Properties , , 78 | and will be . 79 | 80 | 81 | 82 | 83 | Initializes a new instance of the class. 84 | 85 | The name of the folder. 86 | All projects. 87 | 88 | Properties and 89 | will be . 90 | 91 | 92 | 93 | 94 | Initializes a new instance of the class. 95 | 96 | The name of the folder. 97 | All folders. 98 | 99 | Properties and 100 | will be . 101 | 102 | 103 | 104 | 105 | Initializes a new instance of the class. 106 | 107 | The name of the folder. 108 | All files. 109 | 110 | Properties and 111 | will be . 112 | 113 | 114 | 115 | 116 | Initializes a new instance of the class. 117 | 118 | The name of the folder. 119 | All folders. 120 | All files. 121 | 122 | Property will be . 123 | 124 | 125 | 126 | 127 | Initializes a new instance of the class. 128 | 129 | The name of the folder. 130 | All folders. 131 | All projects. 132 | 133 | Property will be . 134 | 135 | 136 | 137 | 138 | Initializes a new instance of the class. 139 | 140 | The name of the folder. 141 | All projects. 142 | All files. 143 | 144 | Property will be . 145 | 146 | 147 | 148 | 149 | Initializes a new instance of the class. 150 | 151 | The name of the folder. 152 | All projects. 153 | All folders. 154 | 155 | Property will be . 156 | 157 | 158 | 159 | 160 | Adds a given array of projects to this instance of . 161 | 162 | An array of projects to add. 163 | Current instance of after adding the given projects. 164 | 165 | 166 | 167 | Adds a single project to this instance of . 168 | 169 | A project that will be added. 170 | Current instance of after adding the given project. 171 | 172 | 173 | 174 | Adds a given array of files to this instance of . 175 | 176 | An array of files to add. 177 | Current instance of after adding the given files. 178 | 179 | 180 | 181 | Adds a single file to this instance of . 182 | 183 | A file that will be added. 184 | Current instance of after adding the given file. 185 | 186 | 187 | 188 | Adds a given array of folders to this instance of . 189 | 190 | An array of folders to add. 191 | Current instance of after adding the given folder. 192 | 193 | 194 | 195 | Adds a single folder to this instance of . 196 | 197 | A folder that will be added. 198 | Current instance of after adding the given folder. 199 | 200 | 201 | 202 | Adds a new project without any configuration to this instance of 203 | 204 | The path to the project. 205 | 206 | A tuple that contains two elements: 207 | 208 | 209 | Item 1 (): Current instance of 210 | after adding the project. 211 | 212 | 213 | Item 2 (): The project that was added. 214 | 215 | 216 | 217 | 218 | 219 | 220 | Represents a single project found in the SLNX file. 221 | 222 | 223 | 224 | 225 | Checks if the project file exists. 226 | 227 | 228 | 229 | 230 | Path to the project file. 231 | 232 | 233 | 234 | 235 | Type of the project. 236 | 237 | 238 | 239 | 240 | Extension of the project file. If the extension isn't present, the 241 | value is null. 242 | 243 | 244 | 245 | 246 | Checks if the property has an extension. 247 | 248 | 249 | 250 | 251 | Gets the type of the project. This can be a string (for example, a string "Classic C#"), 252 | or a GUID. 253 | 254 | 255 | 256 | 257 | Descendant configuration. 258 | 259 | 260 | 261 | 262 | Initializes a new instance of the class. 263 | 264 | Path to the project file. 265 | The project type. 266 | The configuration. 267 | 268 | 269 | 270 | Changes the descendant configuration of this project to and returns it. 271 | 272 | The descendant project configuration. 273 | Current instance of with the given configuration. 274 | 275 | 276 | 277 | Represents the type of a project file. 278 | 279 | 280 | 281 | 282 | C# Project (*.csproj) 283 | 284 | 285 | 286 | 287 | Visual C++ Project (*.vcxproj) 288 | 289 | 290 | 291 | 292 | VB.NET Project (*.vbproj) 293 | 294 | 295 | 296 | 297 | F# Project (*.fsproj) 298 | 299 | 300 | 301 | 302 | Microsoft Installer Project (*.vdproj) 303 | 304 | 305 | 306 | 307 | Shared Project (*.shproj) 308 | 309 | 310 | 311 | 312 | Database Project (*.dbproj) 313 | 314 | 315 | 316 | 317 | Docker Compose Project (*.dcproj) 318 | 319 | 320 | 321 | 322 | SQL Project (*.sqlproj) 323 | 324 | 325 | 326 | 327 | JavaScript Application/ECMAScript Project (*.esproj) 328 | 329 | 330 | 331 | 332 | .NET Core 2015 Project (*.xproj) 333 | 334 | 335 | 336 | 337 | Common MSBuild Project (*.msbuildproj) 338 | 339 | 340 | 341 | 342 | Project (*.proj) 343 | 344 | 345 | 346 | 347 | Any project type which is not defined. 348 | 349 | 350 | 351 | 352 | Extension methods for . 353 | 354 | 355 | 356 | 357 | Returns the extension for the project type. 358 | 359 | Type of the project. 360 | A file extension for that project type, excluding the period. 361 | 362 | 363 | 364 | Returns the project type for the project extension. 365 | 366 | Extension of the project, excluding the period. 367 | A type of the project from the extension. 368 | 369 | 370 | 371 | Represents a <Property> element. 372 | 373 | 374 | 375 | 376 | Name of the property. 377 | 378 | 379 | 380 | 381 | Property value. 382 | 383 | 384 | 385 | 386 | Initializes a new instance of the class. 387 | 388 | Name of the property. 389 | Property value. 390 | 391 | 392 | 393 | Checks if the name of the property signifies an MSBuild Shared Items import. 394 | 395 | 396 | Example: 397 | 398 | Path\To\ProjItems\File.projitems*{AnyGuidHere}*SharedItemsImports 399 | 400 | 401 | 402 | 403 | 404 | Represents the <Properties> element. 405 | 406 | 407 | 408 | 409 | Name of the property collection (if present). Example: SharedMSBuildProjectFiles. 410 | 411 | 412 | 413 | 414 | Scope of the property collection (if present). Example: PreLoad. 415 | 416 | 417 | 418 | 419 | All descendant properties. 420 | 421 | 422 | 423 | 424 | Initializes a new instance of the . 425 | 426 | Name of the property collection (if present). 427 | Scope of the property collection (if present). 428 | All descendant properties. 429 | 430 | 431 | 432 | Builds an SLNX file. 433 | 434 | 435 | 436 | 437 | All folders. 438 | 439 | 440 | 441 | 442 | All projects. 443 | 444 | 445 | 446 | 447 | All <Properties> elements. 448 | 449 | 450 | 451 | 452 | Initializes a new instance of the class. 453 | 454 | 455 | 456 | 457 | Converts this instance of into . 458 | 459 | An instance of . 460 | 461 | 462 | 463 | Adds a new folder to the SLNX file. 464 | 465 | The folder to add. 466 | Current instance of after adding a folder. 467 | 468 | This will also return a copy of the current instance, which could introduce additional 469 | memory allocation. 470 | 471 | 472 | 473 | 474 | Adds a new project to the SLNX file. 475 | 476 | The project to add. 477 | Current instance of after adding a project. 478 | 479 | This will also return a copy of the current instance, which could introduce additional 480 | memory allocation. 481 | 482 | 483 | 484 | 485 | Represents information about an SLNX file. 486 | 487 | 488 | 489 | 490 | If you pass to , this is 491 | the instance that will use. 492 | 493 | 494 | 495 | 496 | Gets all projects in the same level as the SLNX. 497 | 498 | 499 | 500 | 501 | Gets all folders in the same level as the SLNX. 502 | 503 | 504 | 505 | 506 | Gets all property collections in the same level as the SLNX. These can only appear 507 | in the same level as the SLNX - they cannot appear inside folder or project elements. 508 | 509 | 510 | 511 | 512 | Initializes a new instance of the class. 513 | 514 | A list of projects. 515 | A list of folders. 516 | A list of property collections. 517 | 518 | 519 | 520 | Loads and parses a SLNX file. 521 | 522 | The string contents of the SLNX file. Use . 523 | Information about that SLNX file. 524 | 525 | 526 | 527 | Stores the data of this instance of into a SLNX file. 528 | 529 | 530 | Output file. Can be anything but it is recommended for the 531 | extension to end with .slnx. 532 | 533 | Settings for the style of the output XML. If the value is , default parameters are used. 534 | 535 | The output file will be attempted to be deleted if it exists. 536 | 537 | 538 | 539 | 540 | Converts this instance of to the string representation 541 | of the SLNX file with same projects and folders. 542 | 543 | Settings for the style of the output XML. If the value is , default parameters are used. 544 | A string that represents the SLNX file with projects, folders, and other metadata from this instance of . 545 | 546 | 547 | 548 | Stores the data of this instance of into a SLNX file. 549 | 550 | 551 | Output file. Can be anything but it is recommended for the 552 | extension to end with .slnx. 553 | 554 | Settings for the style of the output XML. If the value is , default parameters are used. 555 | 556 | The output file will be attempted to be deleted if it exists. 557 | 558 | 559 | 560 | 561 | Converts this instance of to the string representation 562 | of the SLNX file with same projects and folders. 563 | 564 | Settings for the style of the output XML. If the value is , default parameters are used. 565 | A string that represents the SLNX file with projects, folders, and other metadata from this instance of . 566 | 567 | 568 | 569 | Represents an error related to SLNX syntax or its infrastructure. 570 | 571 | 572 | 573 | 574 | Initializes a new instance of the class. 575 | 576 | Exception message. 577 | 578 | 579 | 580 | Initializes a new instance of the class. 581 | 582 | Exception message. 583 | An inner exception. 584 | 585 | 586 | 587 | -------------------------------------------------------------------------------- /benchmark/Slnx.ReadBenchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using BenchmarkDotNet.Running; 3 | using Slnx; 4 | 5 | BenchmarkRunner.Run(); 6 | 7 | [MemoryDiagnoser] 8 | public class Benchie 9 | { 10 | private const string Input = """ 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | """; 23 | 24 | [Benchmark] 25 | public void ReadBenchie() 26 | { 27 | _ = SlnxModel.Load(Input); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /benchmark/Slnx.ReadBenchmark/Slnx.ReadBenchmark.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /benchmark/Slnx.WriteBenchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using BenchmarkDotNet.Running; 3 | using Slnx; 4 | 5 | BenchmarkRunner.Run(); 6 | 7 | [MemoryDiagnoser] 8 | public class Benchie 9 | { 10 | [Benchmark] 11 | public void WriteBenchie() 12 | { 13 | var factory = new SlnxFactory(); 14 | 15 | var folder = new Folder("Solution Items"); 16 | folder.AddProjectWithPathOnly("./CSharp/CSharp.csproj"); 17 | folder.AddProjectWithPathOnly("./VB.NET/VB.NET.vbproj"); 18 | folder.AddProject(new Project("./DockerCompose/DockerCompose.dcproj", type: null, config: new(solution: "*|*", project: "*|*|Deploy"))); 19 | var moreFolders = new Folder("C++"); 20 | moreFolders.AddFiles(["util.cpp", "util.h", "data.cc", "data.h"]); 21 | folder.AddFiles(["File1.cs", "File2.cs"]); 22 | 23 | factory.AddFolder(folder); 24 | factory.AddProjectWithPathOnly("Slnx/Slnx.csproj"); 25 | factory.AddProjectWithPathOnly("App/App.shproj"); 26 | 27 | _ = factory.AsModel().Store(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /benchmark/Slnx.WriteBenchmark/Slnx.WriteBenchmark.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Slnx/DescendantConfiguration.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents a "<Configuration>" like tag. 5 | /// 6 | public class DescendantConfiguration 7 | { 8 | /// 9 | /// Solution where this configuration applies to. 10 | /// 11 | public string? Solution { get; init; } 12 | 13 | /// 14 | /// Project where this configuration applies to. 15 | /// 16 | public string? Project { get; init; } 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// Solution where this configuration applies to. 22 | /// Project where this configuration applies to. 23 | public DescendantConfiguration(string? solution, string? project) 24 | { 25 | Solution = solution; 26 | Project = project; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Slnx/Folder.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents a folder in the same level as the solution. 5 | /// 6 | public class Folder 7 | { 8 | /// 9 | /// Gets an instance of with no content and name "(Folder Name)". 10 | /// 11 | public static Folder Empty => new("(Folder Name)", Array.Empty(), Array.Empty(), Array.Empty()); 12 | 13 | /// 14 | /// Name of the folder. 15 | /// 16 | public string Name { get; set; } 17 | 18 | /// 19 | /// Nested projects in the folder. If this is , then there 20 | /// are no descendant projects. 21 | /// 22 | public List? DescendantProjects { get; set; } 23 | 24 | /// 25 | /// Nested folders in the folder. If this is , then there 26 | /// are no descendant folders. 27 | /// 28 | public List? DescendantFolders { get; set; } 29 | 30 | /// 31 | /// Files in the folder. If this is , then there 32 | /// are no descendant files. 33 | /// 34 | public List? DescendantFiles { get; set; } 35 | 36 | /// 37 | /// Initializes a new instance of the class. 38 | /// 39 | /// Folder name. 40 | /// All projects. 41 | /// All folders. 42 | /// All files. 43 | public Folder(string name, Project[] projects, Folder[] folders, string[] files) 44 | { 45 | Name = name; 46 | DescendantProjects = projects.Length == 0 ? null : new List(projects); 47 | DescendantFolders = folders.Length == 0 ? null : new List(folders); 48 | DescendantFiles = files.Length == 0 ? null : new List(files); 49 | } 50 | 51 | /// 52 | /// Initializes a new instance of the class. 53 | /// 54 | /// The name of the folder. 55 | /// 56 | /// Properties , , 57 | /// and will be . 58 | /// 59 | public Folder(string name) : this(name, Array.Empty(), Array.Empty(), Array.Empty()) 60 | { 61 | } 62 | 63 | /// 64 | /// Initializes a new instance of the class. 65 | /// 66 | /// The name of the folder. 67 | /// All projects. 68 | /// 69 | /// Properties and 70 | /// will be . 71 | /// 72 | public Folder(string name, Project[] projects) : this(name, projects, Array.Empty(), Array.Empty()) 73 | { 74 | } 75 | 76 | /// 77 | /// Initializes a new instance of the class. 78 | /// 79 | /// The name of the folder. 80 | /// All folders. 81 | /// 82 | /// Properties and 83 | /// will be . 84 | /// 85 | public Folder(string name, Folder[] folders) : this(name, Array.Empty(), folders, Array.Empty()) 86 | { 87 | } 88 | 89 | /// 90 | /// Initializes a new instance of the class. 91 | /// 92 | /// The name of the folder. 93 | /// All files. 94 | /// 95 | /// Properties and 96 | /// will be . 97 | /// 98 | public Folder(string name, string[] files) : this(name, Array.Empty(), Array.Empty(), files) 99 | { 100 | } 101 | 102 | /// 103 | /// Initializes a new instance of the class. 104 | /// 105 | /// The name of the folder. 106 | /// All folders. 107 | /// All files. 108 | /// 109 | /// Property will be . 110 | /// 111 | public Folder(string name, Folder[] folders, string[] files) : this(name, Array.Empty(), folders, files) 112 | { 113 | } 114 | 115 | /// 116 | /// Initializes a new instance of the class. 117 | /// 118 | /// The name of the folder. 119 | /// All folders. 120 | /// All projects. 121 | /// 122 | /// Property will be . 123 | /// 124 | public Folder(string name, Folder[] folders, Project[] projects) : this(name, projects, folders, Array.Empty()) 125 | { 126 | } 127 | 128 | /// 129 | /// Initializes a new instance of the class. 130 | /// 131 | /// The name of the folder. 132 | /// All projects. 133 | /// All files. 134 | /// 135 | /// Property will be . 136 | /// 137 | public Folder(string name, Project[] project, string[] files) : this(name, project, Array.Empty(), files) 138 | { 139 | } 140 | 141 | /// 142 | /// Initializes a new instance of the class. 143 | /// 144 | /// The name of the folder. 145 | /// All projects. 146 | /// All folders. 147 | /// 148 | /// Property will be . 149 | /// 150 | public Folder(string name, Project[] project, Folder[] folders) : this(name, project, folders, Array.Empty()) 151 | { 152 | } 153 | 154 | /// 155 | /// Adds a given array of projects to this instance of . 156 | /// 157 | /// An array of projects to add. 158 | /// Current instance of after adding the given projects. 159 | public Folder AddProjects(Project[] projects) 160 | { 161 | DescendantProjects ??= new List(); 162 | 163 | foreach (Project project in projects) 164 | { 165 | DescendantProjects.Add(project); 166 | } 167 | 168 | return this; 169 | } 170 | 171 | /// 172 | /// Adds a single project to this instance of . 173 | /// 174 | /// A project that will be added. 175 | /// Current instance of after adding the given project. 176 | public Folder AddProject(Project project) 177 | { 178 | DescendantProjects ??= new List(); 179 | DescendantProjects.Add(project); 180 | return this; 181 | } 182 | 183 | /// 184 | /// Adds a given array of files to this instance of . 185 | /// 186 | /// An array of files to add. 187 | /// Current instance of after adding the given files. 188 | public Folder AddFiles(string[] files) 189 | { 190 | DescendantFiles ??= new List(); 191 | foreach (string file in files) 192 | { 193 | DescendantFiles.Add(file); 194 | } 195 | return this; 196 | } 197 | 198 | /// 199 | /// Adds a single file to this instance of . 200 | /// 201 | /// A file that will be added. 202 | /// Current instance of after adding the given file. 203 | public Folder AddFile(string file) 204 | { 205 | DescendantFiles ??= new List(); 206 | DescendantFiles.Add(file); 207 | return this; 208 | } 209 | 210 | /// 211 | /// Adds a given array of folders to this instance of . 212 | /// 213 | /// An array of folders to add. 214 | /// Current instance of after adding the given folder. 215 | public Folder AddFolders(Folder[] folders) 216 | { 217 | DescendantFolders ??= new List(); 218 | foreach (Folder folder in folders) 219 | { 220 | DescendantFolders.Add(folder); 221 | } 222 | return this; 223 | } 224 | 225 | /// 226 | /// Adds a single folder to this instance of . 227 | /// 228 | /// A folder that will be added. 229 | /// Current instance of after adding the given folder. 230 | public Folder AddFolder(Folder folder) 231 | { 232 | DescendantFolders ??= new List(); 233 | DescendantFolders.Add(folder); 234 | return this; 235 | } 236 | 237 | /// 238 | /// Adds a new project without any configuration to this instance of 239 | /// 240 | /// The path to the project. 241 | /// 242 | /// A tuple that contains two elements: 243 | /// 244 | /// 245 | /// Item 1 (): Current instance of 246 | /// after adding the project. 247 | /// 248 | /// 249 | /// Item 2 (): The project that was added. 250 | /// 251 | /// 252 | /// 253 | public (Folder, Project) AddProjectWithPathOnly(string path) 254 | { 255 | var project = new Project(path); 256 | AddProject(project); 257 | return (this, project); 258 | } 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/Slnx/Project.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents a single project found in the SLNX file. 5 | /// 6 | public class Project 7 | { 8 | /// 9 | /// Checks if the project file exists. 10 | /// 11 | public bool Exists => File.Exists(System.IO.Path.Combine(Directory.GetCurrentDirectory(), Path)); 12 | 13 | /// 14 | /// Path to the project file. 15 | /// 16 | public string Path { get; set; } 17 | 18 | /// 19 | /// Type of the project. 20 | /// 21 | public ProjectType Type { get; set; } 22 | 23 | /// 24 | /// Extension of the project file. If the extension isn't present, the 25 | /// value is null. 26 | /// 27 | public string? Extension { get; set; } 28 | 29 | /// 30 | /// Checks if the property has an extension. 31 | /// 32 | public bool HasExtension => Extension != null; 33 | 34 | /// 35 | /// Gets the type of the project. This can be a string (for example, a string "Classic C#"), 36 | /// or a GUID. 37 | /// 38 | public string? ProjectType { get; set; } 39 | 40 | /// 41 | /// Descendant configuration. 42 | /// 43 | public DescendantConfiguration? Configuration { get; set; } 44 | 45 | /// 46 | /// Initializes a new instance of the class. 47 | /// 48 | /// Path to the project file. 49 | /// The project type. 50 | /// The configuration. 51 | public Project(string path, string? type = null, DescendantConfiguration? config = null) 52 | { 53 | Path = path; 54 | 55 | string typeString = (path.Contains('/') ? path.Split('/').Last() 56 | : path.Contains('\\') ? path.Split('\\').Last() 57 | : path).TrimStart('.'); 58 | typeString = typeString.Contains('.') ? typeString.Split('.').Last() : typeString; 59 | 60 | Type = typeString.FromExtension(); 61 | Extension = path.Contains('.') ? path.Split('.').Last() : null; 62 | ProjectType = type; 63 | Configuration = config; 64 | } 65 | 66 | /// 67 | /// Changes the descendant configuration of this project to and returns it. 68 | /// 69 | /// The descendant project configuration. 70 | /// Current instance of with the given configuration. 71 | public Project WithConfiguration(DescendantConfiguration? configuration) 72 | { 73 | Configuration = configuration; 74 | return this; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Slnx/ProjectType.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents the type of a project file. 5 | /// 6 | public enum ProjectType 7 | { 8 | /// 9 | /// C# Project (*.csproj) 10 | /// 11 | CSharp = 0, 12 | 13 | /// 14 | /// Visual C++ Project (*.vcxproj) 15 | /// 16 | VC = 1, 17 | 18 | /// 19 | /// VB.NET Project (*.vbproj) 20 | /// 21 | VisualBasic = 2, 22 | 23 | /// 24 | /// F# Project (*.fsproj) 25 | /// 26 | FSharp = 3, 27 | 28 | /// 29 | /// Microsoft Installer Project (*.vdproj) 30 | /// 31 | Installer = 4, 32 | 33 | /// 34 | /// Shared Project (*.shproj) 35 | /// 36 | Shared = 5, 37 | 38 | /// 39 | /// Database Project (*.dbproj) 40 | /// 41 | Database = 6, 42 | 43 | /// 44 | /// Docker Compose Project (*.dcproj) 45 | /// 46 | DockerCompose = 7, 47 | 48 | /// 49 | /// SQL Project (*.sqlproj) 50 | /// 51 | Sql = 8, 52 | 53 | /// 54 | /// JavaScript Application/ECMAScript Project (*.esproj) 55 | /// 56 | JavaScript = 9, 57 | 58 | /// 59 | /// .NET Core 2015 Project (*.xproj) 60 | /// 61 | NetCore2015 = 10, 62 | 63 | /// 64 | /// Common MSBuild Project (*.msbuildproj) 65 | /// 66 | Common = 11, 67 | 68 | /// 69 | /// Project (*.proj) 70 | /// 71 | Project = 12, 72 | 73 | /// 74 | /// Any project type which is not defined. 75 | /// 76 | Unknown = 0xFF 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Slnx/ProjectTypeExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Extension methods for . 5 | /// 6 | public static class ProjectTypeExtensions 7 | { 8 | /// 9 | /// Returns the extension for the project type. 10 | /// 11 | /// Type of the project. 12 | /// A file extension for that project type, excluding the period. 13 | public static string GetExtension(this ProjectType projectType) 14 | => (byte)projectType switch 15 | { 16 | 0 => "csproj", 17 | 1 => "vcxproj", 18 | 2 => "vbproj", 19 | 3 => "fsproj", 20 | 4 => "vdproj", 21 | 5 => "shproj", 22 | 6 => "dbproj", 23 | 7 => "dcproj", 24 | 8 => "sqlproj", 25 | 9 => "esproj", 26 | 10 => "xproj", 27 | 11 => "msbuildproj", 28 | 12 => "proj", 29 | _ => string.Empty 30 | }; 31 | 32 | /// 33 | /// Returns the project type for the project extension. 34 | /// 35 | /// Extension of the project, excluding the period. 36 | /// A type of the project from the extension. 37 | public static ProjectType FromExtension(this string extension) 38 | => (ProjectType)(byte)(extension.ToLowerInvariant() switch 39 | { 40 | "csproj" => 0, 41 | "vcxproj" => 1, 42 | "vbproj" => 2, 43 | "fsproj" => 3, 44 | "vdproj" => 4, 45 | "shproj" => 5, 46 | "dbproj" => 6, 47 | "dcproj" => 7, 48 | "sqlproj" => 8, 49 | "esproj" => 9, 50 | "xproj" => 10, 51 | "msbuildproj" => 11, 52 | "proj" => 12, 53 | _ => 0xFF 54 | }); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Slnx/Property.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents a <Property> element. 5 | /// 6 | public sealed class Property 7 | { 8 | /// 9 | /// Name of the property. 10 | /// 11 | public string? Name { get; set; } 12 | 13 | /// 14 | /// Property value. 15 | /// 16 | public string? Value { get; set; } 17 | 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// Name of the property. 22 | /// Property value. 23 | public Property(string? name, string? value) 24 | { 25 | Name = name; 26 | Value = value; 27 | } 28 | 29 | /// 30 | /// Checks if the name of the property signifies an MSBuild Shared Items import. 31 | /// 32 | /// 33 | /// Example: 34 | /// 35 | /// Path\To\ProjItems\File.projitems*{AnyGuidHere}*SharedItemsImports 36 | /// 37 | /// 38 | public bool IsMSBuildSharedItemsImports 39 | { 40 | get 41 | { 42 | // Example: 43 | // Path\To\ProjItems\File.projitems*{AnyGuidHere}*SharedItemsImports 44 | 45 | if (Name?.Contains('*') != true) 46 | { 47 | return false; 48 | } 49 | 50 | string[] splitted = Name.Split('*'); 51 | bool isGuid = Guid.TryParse(splitted[1], out _); 52 | 53 | if (!isGuid) 54 | { 55 | return false; 56 | } 57 | 58 | if (!splitted[2].Equals("SharedItemsImports", StringComparison.CurrentCultureIgnoreCase)) 59 | { 60 | return false; 61 | } 62 | 63 | return true; 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Slnx/PropertyCollection.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents the <Properties> element. 5 | /// 6 | public sealed class PropertyCollection 7 | { 8 | /// 9 | /// Name of the property collection (if present). Example: SharedMSBuildProjectFiles. 10 | /// 11 | public string? Name { get; set; } 12 | 13 | /// 14 | /// Scope of the property collection (if present). Example: PreLoad. 15 | /// 16 | public string? Scope { get; set; } 17 | 18 | /// 19 | /// All descendant properties. 20 | /// 21 | public List Properties { get; set; } 22 | 23 | /// 24 | /// Initializes a new instance of the . 25 | /// 26 | /// Name of the property collection (if present). 27 | /// Scope of the property collection (if present). 28 | /// All descendant properties. 29 | public PropertyCollection(string? name, string? scope, List properties) 30 | { 31 | Name = name; 32 | Scope = scope; 33 | Properties = properties; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Slnx/Slnx.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | Slnx.dll 8 | 3.0.1.0 9 | winscripter 10 | True 11 | Slnx 12 | winscripter 13 | 14 | SLNX Parser: Reader and Writer for the (currently) in-preview Visual Studio 15 | XML-based solution format found in Visual Studio 2022 17.10 Preview 3. 16 | 17 | Copyright (c) winscripter, 2024 18 | https://github.com/winscripter/Slnx 19 | https://github.com/winscripter/Slnx 20 | MIT 21 | True 22 | README.md 23 | VisualStudio, Solution, Sln, Slnx 24 | 3.0.1 25 | Removed internal unused class named "TextBuilderInternal" 26 | 27 | 28 | 29 | 30 | True 31 | \ 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/Slnx/SlnxFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Builds an SLNX file. 5 | /// 6 | public class SlnxFactory 7 | { 8 | /// 9 | /// All folders. 10 | /// 11 | public List Folders { get; init; } 12 | 13 | /// 14 | /// All projects. 15 | /// 16 | public List Projects { get; init; } 17 | 18 | /// 19 | /// All <Properties> elements. 20 | /// 21 | public List Properties { get; init; } 22 | 23 | /// 24 | /// Initializes a new instance of the class. 25 | /// 26 | public SlnxFactory() 27 | { 28 | Folders = new List(); 29 | Projects = new List(); 30 | Properties = new List(); 31 | } 32 | 33 | /// 34 | /// Converts this instance of into . 35 | /// 36 | /// An instance of . 37 | public SlnxModel AsModel() => new(Projects, Folders, Properties); 38 | 39 | /// 40 | /// Adds a new folder to the SLNX file. 41 | /// 42 | /// The folder to add. 43 | /// Current instance of after adding a folder. 44 | /// 45 | /// This will also return a copy of the current instance, which could introduce additional 46 | /// memory allocation. 47 | /// 48 | public SlnxFactory AddFolder(Folder folder) 49 | { 50 | Folders.Add(folder); 51 | return this; 52 | } 53 | 54 | /// 55 | /// Adds a new project to the SLNX file. 56 | /// 57 | /// The project to add. 58 | /// Current instance of after adding a project. 59 | /// 60 | /// This will also return a copy of the current instance, which could introduce additional 61 | /// memory allocation. 62 | /// 63 | public SlnxFactory AddProjectWithPathOnly(string path) 64 | { 65 | Projects.Add(new Project(path)); 66 | return this; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Slnx/SlnxModel.StoreAsync.cs: -------------------------------------------------------------------------------- 1 | // This C# file contains .StoreAsync methods. 2 | 3 | using System.Text; 4 | using System.Xml; 5 | 6 | namespace Slnx 7 | { 8 | public partial class SlnxModel 9 | { 10 | /// 11 | /// Stores the data of this instance of into a SLNX file. 12 | /// 13 | /// 14 | /// Output file. Can be anything but it is recommended for the 15 | /// extension to end with .slnx. 16 | /// 17 | /// Settings for the style of the output XML. If the value is , default parameters are used. 18 | /// 19 | /// The output file will be attempted to be deleted if it exists. 20 | /// 21 | public async Task StoreAsync(string outputFile, XmlWriterSettings? settings = null) 22 | { 23 | settings ??= DefaultXmlSettings; 24 | 25 | if (File.Exists(outputFile)) 26 | { 27 | File.Delete(outputFile); 28 | } 29 | 30 | string result = await StoreAsync(settings); 31 | File.AppendAllText(outputFile, result.ToString()); 32 | } 33 | 34 | /// 35 | /// Converts this instance of to the string representation 36 | /// of the SLNX file with same projects and folders. 37 | /// 38 | /// Settings for the style of the output XML. If the value is , default parameters are used. 39 | /// A string that represents the SLNX file with projects, folders, and other metadata from this instance of . 40 | public async Task StoreAsync(XmlWriterSettings? xmlWriterSettings = null) 41 | { 42 | if (xmlWriterSettings is null) 43 | { 44 | xmlWriterSettings ??= DefaultXmlSettings; 45 | xmlWriterSettings.Async = true; 46 | } 47 | 48 | var output = new StringBuilder(); 49 | using XmlWriter xmlWriter = XmlWriter.Create(output, xmlWriterSettings); 50 | await xmlWriter.WriteStartElementAsync(null, "Solution", null); 51 | 52 | foreach (Folder folder in TopLevelFolders ?? _emptyFolderList) 53 | { 54 | await WriteFolderAndDescendantsAsync(folder); 55 | } 56 | 57 | foreach (Project project in TopLevelProjects ?? _emptyProjectList) 58 | { 59 | await WriteProjectAsync(project); 60 | } 61 | 62 | foreach (PropertyCollection propertyCollection in PropertyCollections) 63 | { 64 | await xmlWriter.WriteStartElementAsync(null, "Properties", null); 65 | 66 | if (propertyCollection.Name is string propertyCollectionName) 67 | { 68 | await xmlWriter.WriteAttributeStringAsync(null, "Name", null, propertyCollectionName); 69 | } 70 | 71 | if (propertyCollection.Scope is string propertyCollectionScope) 72 | { 73 | await xmlWriter.WriteAttributeStringAsync(null, "Scope", null, propertyCollectionScope); 74 | } 75 | 76 | foreach (Property property in propertyCollection.Properties) 77 | { 78 | await xmlWriter.WriteStartElementAsync(null, "Property", null); 79 | 80 | if (property.Name is string propertyName) 81 | { 82 | await xmlWriter.WriteAttributeStringAsync(null, "Name", null, propertyName); 83 | } 84 | 85 | if (property.Value is string propertyValue) 86 | { 87 | await xmlWriter.WriteAttributeStringAsync(null, "Value", null, propertyValue); 88 | } 89 | 90 | await xmlWriter.WriteEndElementAsync(); 91 | } 92 | 93 | await xmlWriter.WriteEndElementAsync(); 94 | } 95 | 96 | await xmlWriter.WriteEndElementAsync(); 97 | await xmlWriter.FlushAsync(); 98 | 99 | return output.ToString(); 100 | 101 | async Task WriteFolderAndDescendantsAsync(Folder folder) 102 | { 103 | await xmlWriter.WriteStartElementAsync(null, "Folder", null); 104 | await xmlWriter.WriteAttributeStringAsync(null, "Name", null, folder.Name); 105 | 106 | foreach (string file in folder.DescendantFiles ?? _emptyStringList) 107 | { 108 | await xmlWriter.WriteStartElementAsync(null, "File", null); 109 | await xmlWriter.WriteAttributeStringAsync(null, "Path", null, file); 110 | await xmlWriter.WriteEndElementAsync(); 111 | } 112 | 113 | foreach (Project project in folder.DescendantProjects ?? _emptyProjectList) 114 | { 115 | await WriteProjectAsync(project); 116 | } 117 | 118 | foreach (Folder nestedFolder in folder.DescendantFolders ?? _emptyFolderList) 119 | { 120 | await WriteFolderAndDescendantsAsync(nestedFolder); 121 | } 122 | 123 | await xmlWriter.WriteEndElementAsync(); 124 | } 125 | 126 | async Task WriteProjectAsync(Project project) 127 | { 128 | await xmlWriter.WriteStartElementAsync(null, "Project", null); 129 | 130 | if (project.ProjectType is string projectType) 131 | { 132 | await xmlWriter.WriteAttributeStringAsync(null, "Type", null, projectType); 133 | } 134 | 135 | if (project.Path is string path) 136 | { 137 | await xmlWriter.WriteAttributeStringAsync(null, "Path", null, path); 138 | } 139 | 140 | if (project.Configuration is DescendantConfiguration configuration) 141 | { 142 | await xmlWriter.WriteStartElementAsync(null, "Configuration", null); 143 | 144 | if (configuration.Project is string configurationProject) 145 | { 146 | await xmlWriter.WriteAttributeStringAsync(null, "Project", null, configurationProject); 147 | } 148 | 149 | if (configuration.Solution is string configurationSolution) 150 | { 151 | await xmlWriter.WriteAttributeStringAsync(null, "Solution", null, configurationSolution); 152 | } 153 | 154 | await xmlWriter.WriteEndElementAsync(); 155 | } 156 | 157 | await xmlWriter.WriteEndElementAsync(); 158 | } 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/Slnx/SlnxModel.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using System.Xml; 3 | 4 | namespace Slnx 5 | { 6 | /// 7 | /// Represents information about an SLNX file. 8 | /// 9 | public partial class SlnxModel 10 | { 11 | // Can't use collection expressions because this is C# 10 at least 12 | private static readonly List _emptyFolderList = new(); 13 | private static readonly List _emptyStringList = new(); 14 | private static readonly List _emptyProjectList = new(); 15 | 16 | /// 17 | /// If you pass to , this is 18 | /// the instance that will use. 19 | /// 20 | public static readonly XmlWriterSettings DefaultXmlSettings = new() 21 | { 22 | Indent = true, 23 | IndentChars = " ", // 4 spaces 24 | NewLineChars = "\r\n", 25 | NewLineHandling = NewLineHandling.Replace, 26 | Encoding = new UTF8Encoding(false), 27 | OmitXmlDeclaration = true 28 | }; 29 | 30 | /// 31 | /// Gets all projects in the same level as the SLNX. 32 | /// 33 | public List? TopLevelProjects { get; set; } 34 | 35 | /// 36 | /// Gets all folders in the same level as the SLNX. 37 | /// 38 | public List? TopLevelFolders { get; set; } 39 | 40 | /// 41 | /// Gets all property collections in the same level as the SLNX. These can only appear 42 | /// in the same level as the SLNX - they cannot appear inside folder or project elements. 43 | /// 44 | public List PropertyCollections { get; set; } 45 | 46 | /// 47 | /// Initializes a new instance of the class. 48 | /// 49 | /// A list of projects. 50 | /// A list of folders. 51 | /// A list of property collections. 52 | public SlnxModel(List projects, List folders, List propertyCollections) 53 | { 54 | TopLevelProjects = projects.Count == 0 ? null : projects; 55 | TopLevelFolders = folders.Count == 0 ? null : folders; 56 | PropertyCollections = propertyCollections; 57 | } 58 | 59 | /// 60 | /// Loads and parses a SLNX file. 61 | /// 62 | /// The string contents of the SLNX file. Use . 63 | /// Information about that SLNX file. 64 | public static SlnxModel Load(string slnxContent) => SlnxParser.ParseSlnx(slnxContent); 65 | 66 | /// 67 | /// Stores the data of this instance of into a SLNX file. 68 | /// 69 | /// 70 | /// Output file. Can be anything but it is recommended for the 71 | /// extension to end with .slnx. 72 | /// 73 | /// Settings for the style of the output XML. If the value is , default parameters are used. 74 | /// 75 | /// The output file will be attempted to be deleted if it exists. 76 | /// 77 | public void Store(string outputFile, XmlWriterSettings? settings = null) 78 | { 79 | settings ??= DefaultXmlSettings; 80 | 81 | if (File.Exists(outputFile)) 82 | { 83 | File.Delete(outputFile); 84 | } 85 | 86 | string result = Store(settings); 87 | File.AppendAllText(outputFile, result.ToString()); 88 | } 89 | 90 | /// 91 | /// Converts this instance of to the string representation 92 | /// of the SLNX file with same projects and folders. 93 | /// 94 | /// Settings for the style of the output XML. If the value is , default parameters are used. 95 | /// A string that represents the SLNX file with projects, folders, and other metadata from this instance of . 96 | public string Store(XmlWriterSettings? xmlWriterSettings = null) 97 | { 98 | xmlWriterSettings ??= DefaultXmlSettings; 99 | 100 | var output = new StringBuilder(); 101 | using XmlWriter xmlWriter = XmlWriter.Create(output, xmlWriterSettings); 102 | xmlWriter.WriteStartElement("Solution"); 103 | 104 | foreach (Folder folder in TopLevelFolders ?? _emptyFolderList) 105 | { 106 | WriteFolderAndDescendants(folder); 107 | } 108 | 109 | foreach (Project project in TopLevelProjects ?? _emptyProjectList) 110 | { 111 | WriteProject(project); 112 | } 113 | 114 | foreach (PropertyCollection propertyCollection in PropertyCollections) 115 | { 116 | xmlWriter.WriteStartElement("Properties"); 117 | 118 | if (propertyCollection.Name is string propertyCollectionName) 119 | { 120 | xmlWriter.WriteAttributeString("Name", propertyCollectionName); 121 | } 122 | 123 | if (propertyCollection.Scope is string propertyCollectionScope) 124 | { 125 | xmlWriter.WriteAttributeString("Scope", propertyCollectionScope); 126 | } 127 | 128 | foreach (Property property in propertyCollection.Properties) 129 | { 130 | xmlWriter.WriteStartElement("Property"); 131 | 132 | if (property.Name is string propertyName) 133 | { 134 | xmlWriter.WriteAttributeString("Name", propertyName); 135 | } 136 | 137 | if (property.Value is string propertyValue) 138 | { 139 | xmlWriter.WriteAttributeString("Value", propertyValue); 140 | } 141 | 142 | xmlWriter.WriteEndElement(); 143 | } 144 | 145 | xmlWriter.WriteEndElement(); 146 | } 147 | 148 | xmlWriter.WriteEndElement(); 149 | xmlWriter.Flush(); 150 | 151 | return output.ToString(); 152 | 153 | void WriteFolderAndDescendants(Folder folder) 154 | { 155 | xmlWriter.WriteStartElement("Folder"); 156 | xmlWriter.WriteAttributeString("Name", folder.Name); 157 | 158 | foreach (string file in folder.DescendantFiles ?? _emptyStringList) 159 | { 160 | xmlWriter.WriteStartElement("File"); 161 | xmlWriter.WriteAttributeString("Path", file); 162 | xmlWriter.WriteEndElement(); 163 | } 164 | 165 | foreach (Project project in folder.DescendantProjects ?? _emptyProjectList) 166 | { 167 | WriteProject(project); 168 | } 169 | 170 | foreach (Folder nestedFolder in folder.DescendantFolders ?? _emptyFolderList) 171 | { 172 | WriteFolderAndDescendants(nestedFolder); 173 | } 174 | 175 | xmlWriter.WriteEndElement(); 176 | } 177 | 178 | void WriteProject(Project project) 179 | { 180 | xmlWriter.WriteStartElement("Project"); 181 | 182 | if (project.ProjectType is string projectType) 183 | { 184 | xmlWriter.WriteAttributeString("Type", projectType); 185 | } 186 | 187 | if (project.Path is string path) 188 | { 189 | xmlWriter.WriteAttributeString("Path", path); 190 | } 191 | 192 | if (project.Configuration is DescendantConfiguration configuration) 193 | { 194 | xmlWriter.WriteStartElement("Configuration"); 195 | 196 | if (configuration.Project is string configurationProject) 197 | { 198 | xmlWriter.WriteAttributeString("Project", configurationProject); 199 | } 200 | 201 | if (configuration.Solution is string configurationSolution) 202 | { 203 | xmlWriter.WriteAttributeString("Solution", configurationSolution); 204 | } 205 | 206 | xmlWriter.WriteEndElement(); 207 | } 208 | 209 | xmlWriter.WriteEndElement(); 210 | } 211 | } 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/Slnx/SlnxParser.cs: -------------------------------------------------------------------------------- 1 | using System.Xml.Linq; 2 | 3 | namespace Slnx 4 | { 5 | internal static class SlnxParser 6 | { 7 | public static SlnxModel ParseSlnx(string slnxContent) 8 | { 9 | var doc = XDocument.Parse(slnxContent); 10 | var root = doc.Root ?? throw new SolutionException("Root tag is missing."); 11 | 12 | var folders = new List(); 13 | var projects = new List(); 14 | var propertyCollections = new List(); 15 | 16 | foreach (var childTag in root.Elements()) 17 | { 18 | switch (childTag.Name.LocalName) 19 | { 20 | case "Folder": 21 | folders.Add(ParseFolder(childTag)); 22 | break; 23 | case "Project": 24 | projects.Add(ParseProject(childTag)); 25 | break; 26 | case "Properties": 27 | propertyCollections.Add(ParsePropertyCollection(childTag)); 28 | break; 29 | case "File": 30 | throw new SolutionException("A File element cannot be at the root level. Please add it into the Solution Items folder first."); 31 | default: 32 | throw new SolutionException($"Unrecognized tag \"{childTag.Name.LocalName}\""); 33 | } 34 | } 35 | 36 | return new SlnxModel(projects, folders, propertyCollections); 37 | } 38 | 39 | private static PropertyCollection ParsePropertyCollection(XElement propertiesElement) 40 | { 41 | string? name = propertiesElement.Attribute("Name")?.Value; 42 | string? scope = propertiesElement.Attribute("Scope")?.Value; 43 | 44 | var properties = new List(); 45 | 46 | foreach (XElement property in propertiesElement.Elements()) 47 | { 48 | if (property.Name.LocalName != "Property") 49 | { 50 | throw new SolutionException(" element can only have descendants"); 51 | } 52 | 53 | string? descendantName = property.Attribute("Name")?.Value; 54 | string? descendantValue = property.Attribute("Value")?.Value; 55 | 56 | properties.Add(new Property(descendantName, descendantValue)); 57 | } 58 | 59 | return new PropertyCollection(name, scope, properties); 60 | } 61 | 62 | private static Folder ParseFolder(XElement folderElement) 63 | { 64 | var name = folderElement.Attribute("Name")?.Value ?? throw new SolutionException("Missing attribute \"Name\""); 65 | var projects = folderElement.Elements("Project").Select(ParseProject).ToArray(); 66 | var folders = folderElement.Elements("Folder").Select(ParseFolder).ToArray(); 67 | var files = folderElement.Elements("File").Select(ParseFile).ToArray(); 68 | 69 | return new Folder(name, projects, folders, files); 70 | } 71 | 72 | private static string ParseFile(XElement fileElement) 73 | { 74 | var path = fileElement.Attribute("Path")?.Value ?? throw new SolutionException("Missing attribute \"Path\""); 75 | 76 | return path; 77 | } 78 | 79 | private static Project ParseProject(XElement projectElement) 80 | { 81 | var path = projectElement.Attribute("Path")?.Value ?? throw new SolutionException("Missing attribute \"Path\""); 82 | 83 | var configElement = projectElement.Element("Configuration"); 84 | var config = configElement != null ? ParseConfiguration(configElement) : null; 85 | var type = projectElement.Attribute("Type")?.Value; 86 | 87 | return new Project(path, type, config); 88 | } 89 | 90 | private static DescendantConfiguration ParseConfiguration(XElement configElement) 91 | { 92 | var solution = configElement.Attribute("Solution")?.Value; 93 | var project = configElement.Attribute("Project")?.Value; 94 | 95 | return new DescendantConfiguration(solution, project); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Slnx/SolutionException.cs: -------------------------------------------------------------------------------- 1 | namespace Slnx 2 | { 3 | /// 4 | /// Represents an error related to SLNX syntax or its infrastructure. 5 | /// 6 | internal class SolutionException : Exception 7 | { 8 | /// 9 | /// Initializes a new instance of the class. 10 | /// 11 | /// Exception message. 12 | public SolutionException(string message) : base(message) 13 | { 14 | } 15 | 16 | /// 17 | /// Initializes a new instance of the class. 18 | /// 19 | /// Exception message. 20 | /// An inner exception. 21 | public SolutionException(string message, Exception innerException) : base(message, innerException) 22 | { 23 | } 24 | } 25 | } 26 | --------------------------------------------------------------------------------