-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathXamarinProject.cs
More file actions
439 lines (382 loc) · 15.8 KB
/
XamarinProject.cs
File metadata and controls
439 lines (382 loc) · 15.8 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.Build.Construction;
using System.Diagnostics;
using System.Text;
using System.Xml.Linq;
namespace Xamarin.ProjectTools
{
public abstract class XamarinProject
{
string debugConfigurationName;
string releaseConfigurationName;
public virtual ProjectLanguage Language { get; set; }
public string ProjectName { get; set; }
public string ProjectGuid { get; set; }
public string AssemblyName { get; set; }
public abstract string ProjectTypeGuid { get; }
public IList<Property> DebugProperties { get; private set; }
public IList<Property> ReleaseProperties { get; private set; }
public IList<Property> CommonProperties { get; private set; }
public IList<IList<BuildItem>> ItemGroupList { get; private set; }
public IList<PropertyGroup> PropertyGroups { get; private set; }
public IList<BuildItem> References { get; private set; }
public IList<Package> PackageReferences { get; private set; }
public string GlobalPackagesFolder { get; set; } = FileSystemUtils.FindNugetGlobalPackageFolder ();
public IList<string> ExtraNuGetConfigSources { get; set; } = new List<string> ();
public virtual bool ShouldRestorePackageReferences => PackageReferences?.Count > 0;
/// <summary>
/// If true, the ProjectDirectory will be deleted and populated on the first build
/// </summary>
public virtual bool ShouldPopulate { get; set; } = true;
public IList<Import> Imports { get; private set; }
PropertyGroup common, debug, release;
bool isRelease;
/// <summary>
/// Configures projects with Configuration=Debug or Release
/// * Directly in the `.csproj` file for legacy projects
/// * Uses `Directory.Build.props` for .NET 5+ projects
/// </summary>
public bool IsRelease {
get => isRelease;
set {
isRelease = value;
Touch ("Directory.Build.props");
}
}
public string Configuration => IsRelease ? releaseConfigurationName : debugConfigurationName;
public XamarinProject (string debugConfigurationName = "Debug", string releaseConfigurationName = "Release")
{
this.debugConfigurationName = debugConfigurationName;
this.releaseConfigurationName = releaseConfigurationName;
References = new List<BuildItem> ();
PackageReferences = new List<Package> ();
ItemGroupList = new List<IList<BuildItem>> ();
PropertyGroups = new List<PropertyGroup> ();
CommonProperties = new List<Property> ();
common = new PropertyGroup (null, CommonProperties);
DebugProperties = new List<Property> ();
ReleaseProperties = new List<Property> ();
debug = new PropertyGroup ($"'$(Configuration)' == '{debugConfigurationName}'", DebugProperties);
release = new PropertyGroup ($"'$(Configuration)' == '{releaseConfigurationName}'", ReleaseProperties);
PropertyGroups.Add (common);
PropertyGroups.Add (debug);
PropertyGroups.Add (release);
Imports = new List<Import> ();
//NOTE: for SDK-style projects, we need $(Configuration) set before Microsoft.NET.Sdk.targets
Imports.Add (new Import ("Directory.Build.props") {
TextContent = () =>
$@"<Project>
<PropertyGroup>
<Configuration>{Configuration}</Configuration>
<DisableTransitiveFrameworkReferenceDownloads>true</DisableTransitiveFrameworkReferenceDownloads>
</PropertyGroup>
</Project>"
});
}
/// <summary>
/// Adds a reference to another project. The optional include path uses a relative path and ProjectName if omitted.
/// </summary>
public void AddReference (XamarinProject other, string include = null)
{
if (string.IsNullOrEmpty (include)) {
include = $"..\\{other.ProjectName}\\{other.ProjectName}.csproj";
}
References.Add (new BuildItem.ProjectReference (include, other.ProjectName, other.ProjectGuid));
}
public string TargetFramework {
get { return GetProperty ("TargetFramework"); }
set { SetProperty ("TargetFramework", value); }
}
public string TargetFrameworks {
get { return GetProperty ("TargetFrameworks"); }
set { SetProperty ("TargetFrameworks", value); }
}
public string GetProperty (string name)
{
return GetProperty (CommonProperties, name);
}
public string GetProperty (IList<Property> group, string name)
{
var prop = group.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
return prop != null ? prop.Value () : null;
}
public void SetProperty (string name, string value, string condition = null)
{
SetProperty (name, () => value, condition);
}
public void SetProperty (IList<Property> group, string name, bool value, string condition = null)
{
SetProperty (group, name, () => value.ToString (), condition);
}
public void SetProperty (IList<Property> group, string name, string value, string condition = null)
{
SetProperty (group, name, () => value, condition);
}
public bool RemoveProperty (string name)
{
return RemoveProperty (CommonProperties, name);
}
public bool RemoveProperty (IList<Property> group, string name)
{
var prop = group.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
if (prop == null)
return false;
group.Remove (prop);
return true;
}
public void SetProperty (string name, Func<string> value, string condition = null)
{
SetProperty (CommonProperties, name, value);
}
public void SetProperty (IList<Property> group, string name, Func<string> value, string condition = null)
{
var prop = group.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
if (prop == null)
group.Add (new Property (condition, name, value));
else {
prop.Condition = condition;
prop.Value = value;
}
}
public BuildItem GetItem (string include)
{
return ItemGroupList.SelectMany (g => g).FirstOrDefault (i => i.Include ().Equals (include, StringComparison.OrdinalIgnoreCase));
}
public Import GetImport (string include)
{
return Imports.FirstOrDefault (i => i.Project ().Equals (include, StringComparison.OrdinalIgnoreCase));
}
public void Touch (params string [] itemPaths)
{
foreach (var item in itemPaths) {
var buildItem = GetItem (item);
if (buildItem != null) {
buildItem.Timestamp = DateTime.UtcNow;
continue;
}
var import = GetImport (item);
if (import != null) {
import.Timestamp = DateTime.UtcNow;
continue;
}
throw new InvalidOperationException ($"Path `{item}` not found in project!");
}
}
string project_file_path;
public string ProjectFilePath {
get { return project_file_path ?? ProjectName + Language.DefaultProjectExtension; }
set { project_file_path = value; }
}
public string AssemblyInfo { get; set; }
public string RootNamespace { get; set; }
public virtual string SaveProject ()
{
return string.Empty;
}
public virtual BuildOutput CreateBuildOutput (ProjectBuilder builder)
{
return new BuildOutput (this) { Builder = builder };
}
ProjectResource project;
public virtual List<ProjectResource> Save (bool saveProject = true)
{
var list = new List<ProjectResource> ();
if (saveProject) {
if (project == null) {
project = new ProjectResource {
Path = ProjectFilePath,
Encoding = System.Text.Encoding.UTF8,
};
}
// Clear the Timestamp if the project changed
var contents = SaveProject ();
if (contents != project.Content) {
project.Timestamp = null;
project.Content = contents;
}
list.Add (project);
}
foreach (var ig in ItemGroupList)
list.AddRange (ig.Select (s => new ProjectResource () {
Timestamp = s.Timestamp,
Path = s.Include?.Invoke () ?? s.Update?.Invoke (),
Content = s.TextContent == null ? null : s.TextContent (),
BinaryContent = s.BinaryContent == null ? null : s.BinaryContent (),
Encoding = s.Encoding,
Deleted = s.Deleted,
Attributes = s.Attributes,
}));
foreach (var import in Imports)
list.Add (new ProjectResource () {
Timestamp = import.Timestamp,
Path = import.Project (),
Content = import.TextContent == null ? null : import.TextContent (),
Encoding = System.Text.Encoding.UTF8,
});
return list;
}
public void Populate (string directory)
{
Populate (directory, Save ());
}
public string Root {
get {
return XABuildPaths.TestOutputDirectory;
}
}
public void Populate (string directory, IEnumerable<ProjectResource> projectFiles)
{
directory = directory.Replace ('\\', '/').Replace ('/', Path.DirectorySeparatorChar);
if (File.Exists (directory) || Directory.Exists (directory))
throw new InvalidOperationException ("Path '" + directory + "' already exists. Cannot create a project at this state.");
Directory.CreateDirectory (Path.Combine (Root, directory));
UpdateProjectFiles (directory, projectFiles);
}
public virtual void UpdateProjectFiles (string directory, IEnumerable<ProjectResource> projectFiles, bool doNotCleanup = false)
{
directory = Path.Combine (Root, directory.Replace ('\\', '/').Replace ('/', Path.DirectorySeparatorChar));
if (!Directory.Exists (directory))
throw new InvalidOperationException ("Path '" + directory + "' does not exist.");
foreach (var p in projectFiles) {
// Skip empty paths or wildcards
if (string.IsNullOrEmpty (p.Path) || p.Path.Contains ("*"))
continue;
var path = Path.Combine (directory, p.Path.Replace ('\\', '/').Replace ('/', Path.DirectorySeparatorChar));
if (p.Deleted) {
if (File.Exists (path))
File.Delete (path);
continue;
}
string filedir = directory;
if (path.Contains ($"{Path.DirectorySeparatorChar}")) {
filedir = Path.GetDirectoryName (path);
if (!Directory.Exists (filedir) && (p.Content != null || p.BinaryContent != null))
Directory.CreateDirectory (filedir);
}
// LastWriteTime is inaccurate without milliseconds, but neither mono FileSystemInfo nor UnixFileSystemInfo (in Mono.Posix)
// handles this precisely. And that causes comparison problem.
// To avoid this issue, we compare time after removing milliseconds field here.
//
// Mono.Posix after mono 367d417 will handle file time precisely.
//
/*
// The code below debug prints time comparison. Current code could still result in unwanted comparison,
// so in case of doubtful results, use this to get comparison results.
if (File.Exists (path) && p.Timestamp != null) {
var ft = new DateTimeOffset (new FileInfo (path).LastWriteTime);
string cmp = string.Format ("{0} {1} {2}", p.Timestamp.Value.TimeOfDay.TotalMilliseconds, ft.TimeOfDay.TotalMilliseconds, p.Timestamp.Value > ft);
Console.Error.WriteLine (cmp);
}
*/
var needsUpdate = (!File.Exists (path) || p.Timestamp == null || p.Timestamp.Value > new DateTimeOffset (new FileInfo (path).LastWriteTimeUtc));
if (p.Content != null && needsUpdate) {
if (File.Exists (path)) File.SetAttributes (path, FileAttributes.Normal);
File.WriteAllText (path, p.Content, p.Encoding ?? Encoding.UTF8);
File.SetLastWriteTimeUtc (path, p.Timestamp != null ? p.Timestamp.Value.UtcDateTime : DateTime.UtcNow);
File.SetAttributes (path, p.Attributes);
p.Timestamp = new DateTimeOffset (new FileInfo (path).LastWriteTimeUtc);
} else if (p.BinaryContent != null && needsUpdate) {
using (var f = File.Create (path))
f.Write (p.BinaryContent, 0, p.BinaryContent.Length);
File.SetLastWriteTimeUtc (path, p.Timestamp != null ? p.Timestamp.Value.UtcDateTime : DateTime.UtcNow);
File.SetAttributes (path, p.Attributes);
p.Timestamp = new DateTimeOffset (new FileInfo (path).LastWriteTimeUtc);
}
}
// finally clean up files that do not exist in project anymore.
if (!doNotCleanup) {
var dirFullPath = Path.GetFullPath (directory) + '/';
foreach (var fi in new DirectoryInfo (directory).GetFiles ("*", SearchOption.AllDirectories)) {
var subname = fi.FullName.Substring (dirFullPath.Length).Replace ('\\', '/');
if (subname.StartsWith ("bin", StringComparison.OrdinalIgnoreCase) || subname.StartsWith ("obj", StringComparison.OrdinalIgnoreCase))
continue;
if (subname.Equals ("NuGet.config", StringComparison.OrdinalIgnoreCase))
continue;
if (subname.EndsWith (".log", StringComparison.OrdinalIgnoreCase) || subname.EndsWith (".binlog", StringComparison.OrdinalIgnoreCase))
continue;
if (!projectFiles.Any (p => p.Path != null && p.Path.Replace ('\\', '/').Equals (subname))) {
fi.Delete ();
}
}
}
}
public virtual string ProcessSourceTemplate (string source)
{
return source.Replace ("${ROOT_NAMESPACE}", RootNamespace ?? ProjectName).Replace ("${PROJECT_NAME}", ProjectName);
}
public void CopyNuGetConfig (string relativeDirectory)
{
// Copy our solution's NuGet.config
var repoNuGetConfig = Path.Combine (XABuildPaths.TopDirectory, "NuGet.config");
var projNugetConfig = Path.Combine (Root, relativeDirectory, "NuGet.config");
if (File.Exists (repoNuGetConfig) && !File.Exists (projNugetConfig)) {
Directory.CreateDirectory (Path.GetDirectoryName (projNugetConfig));
File.Copy (repoNuGetConfig, projNugetConfig, overwrite: true);
AddNuGetConfigSources (projNugetConfig);
// Set a local PackageReference installation folder if specified
if (!string.IsNullOrEmpty (GlobalPackagesFolder)) {
var doc = XDocument.Load (projNugetConfig);
XElement gpfElement = doc.Descendants ().FirstOrDefault (c => c.Name.LocalName.ToLowerInvariant () == "add"
&& c.Attributes ().Any (a => a.Name.LocalName.ToLowerInvariant () == "key" && a.Value.ToLowerInvariant () == "globalpackagesfolder"));
if (gpfElement != default (XElement)) {
gpfElement.SetAttributeValue ("value", GlobalPackagesFolder);
} else {
var configElement = new XElement ("add");
configElement.SetAttributeValue ("key", "globalPackagesFolder");
configElement.SetAttributeValue ("value", GlobalPackagesFolder);
XElement configParentElement = doc.Descendants ().FirstOrDefault (c => c.Name.LocalName.ToLowerInvariant () == "config");
if (configParentElement != default (XElement)) {
configParentElement.Add (configElement);
} else {
configParentElement = new XElement ("config");
configParentElement.Add (configElement);
doc.Root.Add (configParentElement);
}
}
doc.Save (projNugetConfig);
}
}
// Copy packages folder
var repoPackages = Path.Combine (XABuildPaths.TopDirectory, "packages");
var projPackages = Path.Combine (Root, relativeDirectory, "packages");
if (Directory.Exists (repoPackages)) {
Directory.CreateDirectory (projPackages);
foreach (var package in Directory.GetFiles (repoPackages, "*.nupkg")) {
var destination = Path.Combine (projPackages, Path.GetFileName (package));
File.Copy (package, destination, overwrite: true);
}
}
}
/// <summary>
/// Updates a NuGet.config based on sources in ExtraNuGetConfigSources
/// </summary>
protected void AddNuGetConfigSources (string nugetConfigPath)
{
XDocument doc;
if (File.Exists (nugetConfigPath))
doc = XDocument.Load (nugetConfigPath);
else
doc = new XDocument (new XElement ("configuration"));
const string elementName = "packageSources";
XElement pkgSourcesElement = doc.Root?.Elements ().FirstOrDefault (d => string.Equals (d.Name.LocalName, elementName, StringComparison.OrdinalIgnoreCase));
if (pkgSourcesElement == null) {
doc.Root.Add (pkgSourcesElement = new XElement (elementName));
}
if (ExtraNuGetConfigSources == null) {
ExtraNuGetConfigSources = new List<string> ();
}
int sourceIndex = 0;
foreach (var source in ExtraNuGetConfigSources.Distinct ()) {
var sourceElement = new XElement ("add");
sourceElement.SetAttributeValue ("key", $"testsource{++sourceIndex}");
sourceElement.SetAttributeValue ("value", source);
pkgSourcesElement.Add (sourceElement);
}
doc.Save (nugetConfigPath);
}
}
}