-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaToolTask.cs
More file actions
134 lines (113 loc) · 4.47 KB
/
Copy pathJavaToolTask.cs
File metadata and controls
134 lines (113 loc) · 4.47 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace Tvl.Java.BuildTasks
{
using System;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Microsoft.Win32;
using Directory = System.IO.Directory;
using File = System.IO.File;
using Path = System.IO.Path;
using SecurityException = System.Security.SecurityException;
public abstract class JavaToolTask : ToolTask
{
[Required]
public string JvmRegistryBase
{
get;
set;
}
[Required]
public string Platform
{
get;
set;
}
protected override Encoding ResponseFileEncoding
{
get
{
return new UTF8Encoding(false);
}
}
protected override string GenerateFullPathToTool()
{
if (File.Exists(ToolPath))
return Path.GetFullPath(ToolPath);
return FindJavaBinary(ToolName, true);
}
protected override string GenerateCommandLineCommands()
{
CommandLineBuilderExtension commandLine = new CommandLineBuilderExtension();
AddCommandLineCommands(commandLine);
return commandLine.ToString().Replace('\\', '/');
}
protected override string GenerateResponseFileCommands()
{
CommandLineBuilderExtension commandLine = new CommandLineBuilderExtension();
AddResponseFileCommands(commandLine);
return commandLine.ToString().Replace('\\', '/');
}
protected virtual void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
{
}
protected virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
{
}
protected virtual string FindJavaBinary(string fileName, bool developmentKit)
{
string vendorBase = JvmRegistryBase;
bool allow64bit = Platform.Equals("X64", StringComparison.OrdinalIgnoreCase) || Platform.Equals("AnyCPU", StringComparison.OrdinalIgnoreCase);
bool allow32bit = Platform.Equals("X86", StringComparison.OrdinalIgnoreCase) || Platform.Equals("AnyCPU", StringComparison.OrdinalIgnoreCase);
string softwareRegKey = @"SOFTWARE\";
string installation = developmentKit ? "Java Development Kit" : "Java Runtime Environment";
if (allow64bit)
{
string registryRoot = softwareRegKey + JvmRegistryBase + @"\" + installation;
string path = FindJavaPath(RegistryView.Registry64, registryRoot, fileName);
if (!string.IsNullOrEmpty(path))
return path;
}
if (allow32bit)
{
string registryRoot = softwareRegKey + JvmRegistryBase + @"\" + installation;
string path = FindJavaPath(RegistryView.Registry32, registryRoot, fileName);
if (!string.IsNullOrEmpty(path))
return path;
}
return null;
}
private static string FindJavaPath(RegistryView view, string registryRoot, string fileName)
{
try
{
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view))
using (RegistryKey jdk = baseKey.OpenSubKey(registryRoot, RegistryKeyPermissionCheck.ReadSubTree))
{
if (jdk == null)
return null;
string currentVersion = jdk.GetValue("CurrentVersion") as string;
if (currentVersion == null)
return null;
using (RegistryKey jdkVersion = jdk.OpenSubKey(currentVersion, RegistryKeyPermissionCheck.ReadSubTree))
{
if (jdkVersion == null)
return null;
string javaHome = jdkVersion.GetValue("JavaHome") as string;
if (!Directory.Exists(javaHome))
return null;
string javac = Path.Combine(javaHome, "bin", fileName);
if (!File.Exists(javac))
return null;
return javac;
}
}
}
catch (SecurityException)
{
return null;
}
}
}
}