forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInfoExtensions.cs
More file actions
57 lines (47 loc) · 1.63 KB
/
Copy pathFileInfoExtensions.cs
File metadata and controls
57 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
namespace Microsoft.DotNet.Interactive.CSharpProject.Build;
internal static class FileInfoExtensions
{
public static string GetTargetFramework(this FileInfo project)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
if (!project.Exists)
{
throw new FileNotFoundException("Project file not found", project.FullName);
}
var dom = XElement.Parse(File.ReadAllText(project.FullName));
var targetFramework = dom.XPathSelectElement("//TargetFramework");
return targetFramework?.Value ?? string.Empty;
}
public static void SetLanguageVersion(this FileInfo project, string version)
{
if (project is null)
{
throw new ArgumentNullException(nameof(project));
}
if (!project.Exists)
{
throw new FileNotFoundException();
}
var dom = XElement.Parse(File.ReadAllText(project.FullName));
var langElement = dom.XPathSelectElement("//LangVersion");
if (langElement != null)
{
langElement.Value = version;
}
else
{
var propertyGroup = dom.XPathSelectElement("//PropertyGroup");
propertyGroup?.Add(new XElement("LangVersion", version));
}
File.WriteAllText(project.FullName, dom.ToString());
}
}