-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddedResourceHelper.cs
More file actions
54 lines (43 loc) · 2.13 KB
/
EmbeddedResourceHelper.cs
File metadata and controls
54 lines (43 loc) · 2.13 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
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
using PostSharp.Engineering.BuildTools.Build;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PostSharp.Engineering.BuildTools.Utilities;
internal static class EmbeddedResourceHelper
{
public static void ExtractScript( BuildContext context, string fileName, string targetDirectory )
{
var product = context.Product;
var replacements = new Dictionary<string, string>();
replacements.Add( "<ENG_PATH>", product.EngineeringDirectory );
// Combine standard environment variables with product-specific additional ones
var allEnvironmentVariables = EnvironmentVariableNames.All
.Concat( product.AdditionalDockerEnvironmentVariables )
.OrderBy( x => x );
replacements.Add( "<ENVIRONMENT_VARIABLES>", string.Join( ",", allEnvironmentVariables ) );
replacements.Add( "<PRODUCT_NAME>", product.ProductNameWithoutDot );
ExtractResource( context, fileName, targetDirectory, replacements );
}
public static void ExtractResource(
BuildContext context,
string fileName,
string targetDirectory,
IReadOnlyDictionary<string, string>? replacements = null )
{
var targetPath = Path.Combine( context.RepoDirectory, targetDirectory, fileName );
using var resource = typeof(EmbeddedResourceHelper).Assembly.GetManifestResourceStream( $"PostSharp.Engineering.BuildTools.Resources.{fileName}" )
?? throw new InvalidOperationException( $"Cannot find the resource {fileName}." );
using var reader = new StreamReader( resource );
var text = reader.ReadToEnd();
if ( replacements != null )
{
foreach ( var replacement in replacements )
{
text = text.Replace( replacement.Key, replacement.Value, StringComparison.Ordinal );
}
}
TextFileHelper.WriteIfDifferent( targetPath, text, context );
}
}