-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathMSBuildHost.cs
65 lines (58 loc) · 1.63 KB
/
MSBuildHost.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
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using System.Diagnostics;
using System.Reflection;
namespace React.MSBuild
{
/// <summary>
/// Handles initialisation of the MSBuild environment.
/// </summary>
internal static class MSBuildHost
{
/// <summary>
/// Hack to use Lazy{T} for thread-safe, once-off initialisation :)
/// </summary>
private readonly static Lazy<bool> _initializer = new Lazy<bool>(Initialize);
/// <summary>
/// Ensures the environment has been initialised.
/// </summary>
public static bool EnsureInitialized()
{
return _initializer.Value;
}
/// <summary>
/// Actually perform the initialisation of the environment.
/// </summary>
/// <returns></returns>
private static bool Initialize()
{
AssemblyBindingRedirect.Enable();
// All "per-request" registrations should be singletons in MSBuild, since there's no
// such thing as a "request"
Initializer.Initialize(requestLifetimeRegistration: registration => registration.AsSingleton());
return true;
}
/// <summary>
/// Determines if the current process is MSBuild
/// </summary>
/// <returns><c>true</c> if we are currently in MSBuild</returns>
public static bool IsInMSBuild()
{
try
{
return Process.GetCurrentProcess().ProcessName.StartsWith("MSBuild");
}
catch (Exception)
{
return false;
}
}
}
}