Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit e723a51

Browse files
committed
Merge .targets files in build script
We can only have one .targets file in a nupkg that gets auto-imported into the project (it must be named nuget-package-id.targets), and since some of our packages already were using this file, we need to merge the generated targets for Xamarin.Build.Download with these files. This means the custom build tasks must be built before the nuget-setup task runs. This changes the build script to build the existing custom tasks, copy their .targets files, and then detect them when generated the .targets files for Xamarin.Build.Download, and merge them together.
1 parent 2107578 commit e723a51

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

build.cake

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Task ("component-setup").Does (() =>
324324
});
325325

326326

327-
Task ("nuget-setup").Does (() => {
327+
Task ("nuget-setup").IsDependentOn ("buildtasks").Does (() => {
328328
var templateText = FileReadText ("./template.targets");
329329

330330
if (FileExists ("./generated.targets"))
@@ -365,9 +365,31 @@ Task ("nuget-setup").Does (() => {
365365
foreach (var kvp in items)
366366
targetsText = targetsText.Replace (kvp.Key, kvp.Value);
367367

368-
var targetsFile = string.Format ("{0}/nuget/{1}.targets", aar.Path, aar.NugetId);
368+
var targetsFile = new FilePath(string.Format ("{0}/nuget/{1}.targets", aar.Path, aar.NugetId));
369369
FileWriteText (targetsFile, targetsText);
370370

371+
// Check for an existing .targets file in this nuget package
372+
// we need to merge the generated one with it if it exists
373+
// nuget only allows one automatic .targets file in the build/ folder
374+
// of the nuget package, which must be named {nuget-package-id}.targets
375+
// so we need to merge them all into one
376+
var mergeFile = new FilePath (aar.Path + "/nuget/merge.targets");
377+
378+
if (FileExists (mergeFile)) {
379+
Information ("merge.targets found, merging into generated file...");
380+
381+
// Load the doc to append to, and the doc to append
382+
var xOrig = System.Xml.Linq.XDocument.Load (MakeAbsolute(targetsFile).FullPath);
383+
System.Xml.Linq.XNamespace nsOrig = xOrig.Root.Name.Namespace;
384+
var xMerge = System.Xml.Linq.XDocument.Load (MakeAbsolute(mergeFile).FullPath);
385+
System.Xml.Linq.XNamespace nsMerge = xMerge.Root.Name.Namespace;
386+
// Add all the elements under <Project> into the existing file's <Project> node
387+
foreach (var xItemToAdd in xMerge.Element (nsMerge + "Project").Elements ())
388+
xOrig.Element (nsOrig + "Project").Add (xItemToAdd);
389+
390+
xOrig.Save (MakeAbsolute (targetsFile).FullPath);
391+
}
392+
371393
// Merge each generated targets file into one main one
372394
// this makes one file to import into our actual binding projects
373395
// which is much easier/less maintenance
@@ -377,7 +399,7 @@ Task ("nuget-setup").Does (() => {
377399
// Load the doc to append to, and the doc to append
378400
var xFileRoot = System.Xml.Linq.XDocument.Load ("./generated.targets");
379401
System.Xml.Linq.XNamespace nsRoot = xFileRoot.Root.Name.Namespace;
380-
var xFileChild = System.Xml.Linq.XDocument.Load (targetsFile);
402+
var xFileChild = System.Xml.Linq.XDocument.Load (MakeAbsolute(targetsFile).FullPath);
381403
System.Xml.Linq.XNamespace nsChild = xFileRoot.Root.Name.Namespace;
382404

383405
// Add all the elements under <Project> into the existing file's <Project> node
@@ -480,7 +502,7 @@ Task ("component-docs").Does (() =>
480502
}
481503
});
482504

483-
Task ("libs").IsDependentOn ("genapi").IsDependentOn ("nuget-setup");
505+
Task ("libs").IsDependentOn ("nuget-setup").IsDependentOn ("genapi").IsDependentOn ("libs-base");
484506

485507
Task ("genapi").IsDependentOn ("libs-base").IsDependentOn ("externals").Does (() => {
486508

@@ -522,6 +544,15 @@ Task ("genapi").IsDependentOn ("libs-base").IsDependentOn ("externals").Does (()
522544
CopyFile ("./v4/source/bin/Release/Xamarin.Android.Support.v4.dll", "./output/Xamarin.Android.Support.v4.dll");
523545
});
524546

547+
Task ("buildtasks").Does (() =>
548+
{
549+
NuGetRestore ("./vector-drawable/buildtask/Vector-Drawable-BuildTasks.csproj");
550+
551+
DotNetBuild ("./vector-drawable/buildtask/Vector-Drawable-BuildTasks.csproj", c => c.Configuration = "Release");
552+
553+
CopyFile ("./vector-drawable/buildtask/bin/Release/Xamarin.Android.Support.Tasks.VectorDrawable.targets", "./vector-drawable/nuget/merge.targets");
554+
});
555+
525556
SetupXamarinBuildTasks (buildSpec, Tasks, Task);
526557

527558
RunTarget (TARGET);

vector-drawable/nuget/Xamarin.Android.Support.Vector.Drawable.nuspec

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
</dependencies>
2020
</metadata>
2121
<files>
22-
<!-- Include .targets file to setup AndroidResGenExtraArgs property to add -no-version-vectors to the aapt.exe call -->
23-
<file src="vector-drawable/buildtask/bin/Release/Xamarin.Android.Support.Tasks.VectorDrawable.targets" target="build" />
2422
<file src="vector-drawable/buildtask/bin/Release/Xamarin.Android.Support.Tasks.VectorDrawable.dll" target="build" />
25-
2623
<file src="vector-drawable/nuget/Xamarin.Android.Support.Vector.Drawable.targets" target="build" />
2724

2825
<file src="output/Xamarin.Android.Support.Vector.Drawable.dll" target="lib/MonoAndroid70" />

0 commit comments

Comments
 (0)