This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
forked from Squirrel/Squirrel.Windows
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathPlatformModeDetector.cs
80 lines (69 loc) · 2.38 KB
/
PlatformModeDetector.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
#pragma warning disable CS1591
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
#if SILVERLIGHT
using System.Windows;
#elif NETFX_CORE
using Windows.ApplicationModel;
#endif
namespace Squirrel.SimpleSplat
{
public class PlatformModeDetector : IModeDetector
{
public bool? InUnitTestRunner()
{
var testAssemblies = new[] {
"CSUNIT",
"NUNIT",
"XUNIT",
"MBUNIT",
"NBEHAVE",
};
try {
return searchForAssembly(testAssemblies);
} catch (Exception) {
return null;
}
}
public bool? InDesignMode()
{
#if SILVERLIGHT
if (Application.Current.RootVisual != null) {
return System.ComponentModel.DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual);
}
return false;
#elif NETFX_CORE
return DesignMode.DesignModeEnabled;
#else
var designEnvironments = new[] {
"BLEND.EXE",
"XDESPROC.EXE",
};
var exeName = (new FileInfo(SquirrelRuntimeInfo.EntryExePath)).Name.ToUpperInvariant();
if (designEnvironments.Any(x => x.Contains(exeName))) {
return true;
}
return false;
#endif
}
static bool searchForAssembly(IEnumerable<string> assemblyList)
{
#if SILVERLIGHT
return Deployment.Current.Parts.Any(x => assemblyList.Any(name => x.Source.ToUpperInvariant().Contains(name)));
#elif NETFX_CORE
var depPackages = Package.Current.Dependencies.Select(x => x.Id.FullName);
if (depPackages.Any(x => assemblyList.Any(name => x.ToUpperInvariant().Contains(name)))) return true;
var fileTask = Task.Factory.StartNew(async () => {
var files = await Package.Current.InstalledLocation.GetFilesAsync();
return files.Select(x => x.Path).ToArray();
}, TaskCreationOptions.HideScheduler).Unwrap();
return fileTask.Result.Any(x => assemblyList.Any(name => x.ToUpperInvariant().Contains(name)));
#else
return AppDomain.CurrentDomain.GetAssemblies()
.Any(x => assemblyList.Any(name => x.FullName.ToUpperInvariant().Contains(name)));
#endif
}
}
}