-
Notifications
You must be signed in to change notification settings - Fork 537
/
Copy pathJavaSourceUtils.cs
184 lines (146 loc) · 5.3 KB
/
JavaSourceUtils.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2012 Xamarin, Inc. All rights reserved.
using System;
using System.Linq;
using System.IO;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks
{
public class JavaSourceUtils : AndroidToolTask
{
public override string TaskPrefix => "JSU";
[Required]
public string JavaSourceUtilsJar { get; set; }
[Required]
public string JavaSdkDirectory { get; set; }
[Required]
public ITaskItem[] InputFiles { get; set; }
public ITaskItem[] References { get; set; }
public ITaskItem[] BootClassPath { get; set; }
public ITaskItem JavadocCopyrightFile { get; set; }
public string JavadocUrlPrefix { get; set; }
public string JavadocUrlStyle { get; set; }
public string JavadocDocRootUrl { get; set; }
public string JavaOptions { get; set; }
public string JavaMaximumHeapSize { get; set; }
public ITaskItem OutputJavadocXml { get; set; }
string responseFilePath;
public override bool RunTask ()
{
if (InputFiles == null || InputFiles.Count () == 0) {
Log.LogCodedError ("XA1020", Properties.Resources.XA1020);
return false;
}
if (References != null)
foreach (var path in References)
if (!Directory.Exists (path.ItemSpec) && !File.Exists (path.ItemSpec))
Log.LogCodedError ("XA1022", Properties.Resources.XA1022, path.ItemSpec);
if (Log.HasLoggedErrors)
return false;
try {
return base.RunTask ();
}
finally {
File.Delete (responseFilePath);
}
}
static string GetOutputFileName (ITaskItem[] items) =>
Files.HashString (
string.Join ("\n", items.Select (item => Path.GetFullPath (item.ItemSpec).Replace ('\\', '/'))));
protected override string GenerateCommandLineCommands ()
{
responseFilePath = CreateResponseFile ();
var cmd = new CommandLineBuilder ();
// Add the JavaOptions if they are not null
// These could be any of the additional options
if (!string.IsNullOrEmpty (JavaOptions)) {
cmd.AppendSwitch (JavaOptions);
}
if (!string.IsNullOrEmpty (JavaMaximumHeapSize)) {
cmd.AppendSwitchIfNotNull("-Xmx", JavaMaximumHeapSize);
}
// Arguments sent to java.exe
cmd.AppendSwitchIfNotNull ("-jar ", JavaSourceUtilsJar);
cmd.AppendSwitch ($"@{responseFilePath}");
return cmd.ToString ();
}
string CreateResponseFile ()
{
var responseFile = Path.GetTempFileName ();
using var response = new StreamWriter (responseFile, append: false, encoding: Files.UTF8withoutBOM);
Log.LogDebugMessage ("[java-source-utils] response file contents: {0}", responseFile);
if (BootClassPath != null && BootClassPath.Any ()) {
var classpath = string.Join (Path.PathSeparator.ToString (), BootClassPath.Select (p => Path.GetFullPath (p.ItemSpec)));
AppendArg (response, "--bootclasspath");
AppendArg (response, classpath);
}
if (References != null && References.Any ()) {
foreach (var r in References) {
if (Directory.Exists (r.ItemSpec)) {
AppendArg (response, "--source");
AppendArg (response, Path.GetFullPath (r.ItemSpec));
continue;
}
if (!File.Exists (r.ItemSpec)) {
Log.LogCodedError ("XA1022", Properties.Resources.XA1022, r.ItemSpec);
continue;
}
if (r.ItemSpec.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)) {
AppendArg (response, "--jar");
AppendArg (response, Path.GetFullPath (r.ItemSpec));
continue;
}
if (r.ItemSpec.EndsWith (".aar", StringComparison.OrdinalIgnoreCase)) {
AppendArg (response, "--aar");
AppendArg (response, Path.GetFullPath (r.ItemSpec));
continue;
}
Log.LogError ($"Unsupported @(Reference) item: {r.ItemSpec}");
}
}
AppendArg (response, "--output-javadoc");
AppendArg (response, OutputJavadocXml.ItemSpec);
if (!string.IsNullOrEmpty (JavadocCopyrightFile?.ItemSpec)) {
AppendArg (response, "--doc-copyright");
AppendArg (response, Path.GetFullPath (JavadocCopyrightFile.ItemSpec));
}
if (!string.IsNullOrEmpty (JavadocUrlPrefix)) {
AppendArg (response, "--doc-url-prefix");
AppendArg (response, JavadocUrlPrefix);
}
if (!string.IsNullOrEmpty (JavadocUrlStyle)) {
AppendArg (response, "--doc-url-style");
AppendArg (response, JavadocUrlStyle);
}
if (!string.IsNullOrEmpty (JavadocDocRootUrl)) {
AppendArg (response, "--doc-root-url");
AppendArg (response, JavadocDocRootUrl);
}
var inputs = InputFiles.Select (p => Path.GetFullPath (p.ItemSpec))
.Distinct (StringComparer.OrdinalIgnoreCase);
foreach (var path in inputs) {
AppendArg (response, path);
}
return responseFile;
void AppendArg (TextWriter writer, string line)
{
writer.WriteLine (line);
Log.LogDebugMessage (line);
}
}
protected override string ToolName {
get => "java-source-utils";
}
public override string ToolExe {
get { return OS.IsWindows ? "java.exe" : "java"; }
set { base.ToolExe = value; }
}
protected override string GenerateFullPathToTool ()
{
return Path.Combine (JavaSdkDirectory, "bin", ToolExe);
}
}
}