-
Notifications
You must be signed in to change notification settings - Fork 184
/
ExtensionsCsprojGenerator.cs
96 lines (79 loc) · 3.4 KB
/
ExtensionsCsprojGenerator.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Microsoft.Azure.Functions.Worker.Sdk
{
internal class ExtensionsCsprojGenerator
{
private const string ExtensionsProjectName = "WorkerExtensions.csproj";
private readonly IDictionary<string, string> _extensions;
private readonly string _outputPath;
private readonly string _targetFramework;
public ExtensionsCsprojGenerator(IDictionary<string, string> extensions, string outputPath, string targetFramework)
{
_extensions = extensions ?? throw new ArgumentNullException(nameof(extensions));
_outputPath = outputPath ?? throw new ArgumentNullException(nameof(outputPath));
_targetFramework = targetFramework ?? throw new ArgumentNullException(nameof(targetFramework));
}
public void Generate()
{
var extensionsCsprojFilePath = Path.Combine(_outputPath, ExtensionsProjectName);
RecreateDirectory(_outputPath);
WriteExtensionsCsProj(extensionsCsprojFilePath);
}
private void RecreateDirectory(string directoryPath)
{
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath, recursive: true);
}
Directory.CreateDirectory(directoryPath);
}
private void WriteExtensionsCsProj(string filePath)
{
string csprojContent = GetCsProjContent();
File.WriteAllText(filePath, csprojContent);
}
internal string GetCsProjContent()
{
string extensionReferences = GetExtensionReferences();
return $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{_targetFramework}</TargetFramework>
<LangVersion>preview</LangVersion>
<Configuration>Release</Configuration>
<AssemblyName>Microsoft.Azure.Functions.Worker.Extensions</AssemblyName>
<RootNamespace>Microsoft.Azure.Functions.Worker.Extensions</RootNamespace>
<MajorMinorProductVersion>1.0</MajorMinorProductVersion>
<Version>$(MajorMinorProductVersion).0</Version>
<AssemblyVersion>$(MajorMinorProductVersion).0.0</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.NETCore.Targets"" Version=""3.0.0"" PrivateAssets=""all"" />
<PackageReference Include=""Microsoft.NET.Sdk.Functions"" Version=""3.0.11"" />
{extensionReferences}
</ItemGroup>
</Project>
";
}
private string GetExtensionReferences()
{
var packages = new StringBuilder();
foreach (KeyValuePair<string, string> extensionPair in _extensions)
{
packages.AppendLine(GetPackageReferenceFromExtension(name: extensionPair.Key, version: extensionPair.Value));
}
return packages.ToString();
}
private static string GetPackageReferenceFromExtension(string name, string version)
{
return $@"<PackageReference Include=""{name}"" Version=""{version}"" />";
}
}
}