forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateSatelliteAssemblies.cs
94 lines (77 loc) · 3.15 KB
/
GenerateSatelliteAssemblies.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
// 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.Collections.Generic;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using System.Reflection;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Generates Satellite Assemblies
/// </summary>
public class GenerateSatelliteAssemblies : TaskBase
{
[Required]
public ITaskItem[] EmbedResources { get; set; }
[Output]
[Required]
public ITaskItem OutputAssembly { get; set; }
[Required]
public string[] References { get; set; }
public bool DelaySign { get; set; }
public bool PublicSign { get; set; }
public string KeyContainer { get; set; }
public string KeyFile { get; set; }
[Required]
public string AssemblyInfoFile { get; set; }
protected override void ExecuteCore()
{
var resourceDescriptions = new List<ResourceDescription>();
foreach (var input in EmbedResources)
{
var resourceName = input.GetMetadata("ManifestResourceName") + input.GetMetadata("Extension");
var fullPath = input.GetMetadata("FullPath");
var fileInfo = new FileInfo(fullPath);
resourceDescriptions.Add(new ResourceDescription(resourceName, () => fileInfo.OpenRead(), true));
}
if (KeyFile != null)
{
KeyFile = Path.GetFullPath(KeyFile);
}
var compilationOptions = new CSharpCompilationOptions(
outputKind: OutputKind.DynamicallyLinkedLibrary,
cryptoKeyContainer: KeyContainer,
cryptoKeyFile: KeyFile,
delaySign: DelaySign,
publicSign: PublicSign,
deterministic: true);
var compilation = CSharpCompilation.Create(OutputAssembly.GetMetadata("Filename"),
references: References.Select(reference => MetadataReference.CreateFromFile(reference)),
options: compilationOptions);
var cs = File.ReadAllText(AssemblyInfoFile);
compilation = compilation.AddSyntaxTrees(new[]
{
CSharpSyntaxTree.ParseText(cs)
});
var satelliteAssembly = OutputAssembly.GetMetadata("FullPath");
using (var outputStream = File.Create(satelliteAssembly))
{
var result = compilation.Emit(outputStream, manifestResources: resourceDescriptions);
if (!result.Success)
{
Log.LogError(Strings.ErrorsOccurredWhenEmittingSatelliteAssembly, satelliteAssembly);
foreach (var diagnostic in result.Diagnostics)
{
Log.LogError(diagnostic.ToString());
}
}
}
}
}
}