Skip to content
Permalink
Browse files

[Build] Use PackAssets as an exe rather than a Tasks so that MSBuild …

…doesn't lock the files (annoying when rebuilding)
  • Loading branch information
xen2 committed Nov 22, 2018
1 parent 76b3c85 commit aae4951f4dae45192ed079d3b4810c9a34715057
@@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xenko.Core.Assets;
using Xenko.Core.Diagnostics;
using Xenko.Core.IO;
@@ -27,16 +28,29 @@ public class PackAssets : Task

public override bool Execute()
{
var result = new RedirectLog(Log);
var package = Package.Load(result, ProjectFile.ItemSpec, new PackageLoadParameters()
var generatedItems = new List<(string SourcePath, string PackagePath)>();
var result = Run(new RedirectLog(Log), ProjectFile.ItemSpec, IntermediatePackagePath.ItemSpec, generatedItems);

GeneratedItems = generatedItems.Select(x =>
{
var generatedItem = new TaskItem(x.SourcePath);
generatedItem.SetMetadata("Pack", "true");
generatedItem.SetMetadata("PackagePath", x.PackagePath);
return generatedItem;
}).ToArray();
return result;
}

public static bool Run(Diagnostics.Logger logger, string projectFile, string intermediatePackagePath, List<(string SourcePath, string PackagePath)> generatedItems)
{
var package = Package.Load(logger, projectFile, new PackageLoadParameters()
{
AutoCompileProjects = false,
LoadAssemblyReferences = false,
AutoLoadTemporaryAssets = false,
});

var generatedItems = new List<ITaskItem>();
var outputPath = new UDirectory(new FileInfo(IntermediatePackagePath.ItemSpec).FullName);
var outputPath = new UDirectory(new FileInfo(intermediatePackagePath).FullName);
var newPackage = new Package
{
Meta = package.Meta,
@@ -49,10 +63,7 @@ public override bool Execute()

void RegisterItem(UFile targetFilePath)
{
var generatedItem = new TaskItem(targetFilePath.ToWindowsPath());
generatedItem.SetMetadata("Pack", "true");
generatedItem.SetMetadata("PackagePath", UPath.Combine("xenko", targetFilePath.MakeRelative(outputPath)).ToWindowsPath());
generatedItems.Add(generatedItem);
generatedItems.Add((targetFilePath.ToWindowsPath(), UPath.Combine("xenko", targetFilePath.MakeRelative(outputPath)).ToWindowsPath()));
}

void TryCopyDirectory(UDirectory sourceDirectory, UDirectory targetDirectory)
@@ -73,7 +84,7 @@ void TryCopyResource(UFile resourceFilePath, UFile targetFilePath)

if (resourcesTargetToSource.TryGetValue(targetFilePath, out var otherResourceFilePath))
{
result.Error($"Could not copy resource file [{targetFilePath.MakeRelative(resourceOutputPath)}] because it exists in multiple locations: [{resourceFilePath.ToWindowsPath()}] and [{otherResourceFilePath.ToWindowsPath()}]");
logger.Error($"Could not copy resource file [{targetFilePath.MakeRelative(resourceOutputPath)}] because it exists in multiple locations: [{resourceFilePath.ToWindowsPath()}] and [{otherResourceFilePath.ToWindowsPath()}]");
}
else
{
@@ -88,7 +99,7 @@ void TryCopyResource(UFile resourceFilePath, UFile targetFilePath)
}
catch (Exception e)
{
result.Error($"Could not copy resource file from [{resourceFilePath.ToWindowsPath()}] to [{targetFilePath.MakeRelative(resourceOutputPath)}]", e);
logger.Error($"Could not copy resource file from [{resourceFilePath.ToWindowsPath()}] to [{targetFilePath.MakeRelative(resourceOutputPath)}]", e);
}
}
}
@@ -102,7 +113,7 @@ void TryCopyResource(UFile resourceFilePath, UFile targetFilePath)
}

var assetOutputPath = UPath.Combine(outputPath, (UDirectory)"Assets");
var assets = Package.ListAssetFiles(result, package, true, true, null);
var assets = Package.ListAssetFiles(logger, package, true, true, null);
if (assets.Count > 0)
{
newPackage.AssetFolders.Add(new AssetFolder(assetOutputPath));
@@ -183,7 +194,7 @@ void TryCopyResource(UFile resourceFilePath, UFile targetFilePath)
}
catch (Exception e)
{
result.Error($"Could not process asset [{asset.FilePath}]", e);
logger.Error($"Could not process asset [{asset.FilePath}]", e);
}
}
}
@@ -231,13 +242,11 @@ void TryCopyResource(UFile resourceFilePath, UFile targetFilePath)
{
// Make sure we have a standalone package
var standalonePackage = new StandalonePackage(newPackage);
standalonePackage.Save(result);
standalonePackage.Save(logger);
RegisterItem(newPackage.FullPath);
}

GeneratedItems = generatedItems.ToArray();

return !result.HasErrors;
return !logger.HasErrors;
}

class RedirectLog : Core.Diagnostics.Logger
@@ -2,10 +2,12 @@
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Build.Locator;
using Mono.Options;
using Xenko.Core.Diagnostics;

namespace Xenko.Core.Tasks
{
@@ -37,6 +39,7 @@ public static int RealMain(string[] args)
"=== Commands ===",
string.Empty,
" locate-devenv <MSBuildPath>: returns devenv path",
" pack-assets <csprojFile> <intermediatePackagePath>: copy and adjust assets for nupkg packaging",
string.Empty,
"=== Options ===",
string.Empty,
@@ -71,6 +74,29 @@ public static int RealMain(string[] args)
Console.WriteLine(devenvPath);
break;
}
case "pack-assets":
{
if (commandArgs.Count != 3)
throw new OptionException("Need two extra arguments", "");

var csprojFile = commandArgs[1];
var intermediatePackagePath = commandArgs[2];
var generatedItems = new List<(string SourcePath, string PackagePath)>();
var logger = new LoggerResult();
if (!PackAssets.Run(logger, csprojFile, intermediatePackagePath, generatedItems))
{
foreach (var message in logger.Messages)
{
Console.WriteLine(message);
}
return 1;
}
foreach (var generatedItem in generatedItems)
{
Console.WriteLine($"{generatedItem.SourcePath}|{generatedItem.PackagePath}");
}
break;
}
}
}
catch (Exception e)
@@ -150,10 +150,24 @@
</PropertyGroup>

<!-- Pack target -->
<UsingTask TaskName="Xenko.Core.Tasks.PackAssets" AssemblyFile="$(MSBuildThisFileDirectory)..\core\Xenko.Core.Tasks\bin\Debug\net472\Xenko.Core.Tasks.dll" />
<!--<UsingTask TaskName="Xenko.Core.Tasks.PackAssets" AssemblyFile="$(MSBuildThisFileDirectory)..\core\Xenko.Core.Tasks\bin\$(Configuration)\net472\Xenko.Core.Tasks.exe" />-->
<Target Name="PrepareXenkoAssetsForPack" BeforeTargets="_GetPackageFiles" Condition="'$(XenkoPackAssets)' == 'true'">
<PackAssets ProjectFile="$(MSBuildProjectFile)" IntermediatePackagePath="$(IntermediateOutputPath)\xenko">
<!-- We use exec version rather than task version, otherwise file will be locked during rebuild -->
<!--<PackAssets ProjectFile="$(MSBuildProjectFile)" IntermediatePackagePath="$(IntermediateOutputPath)\xenko">
<Output TaskParameter="GeneratedItems" ItemName="None"/>
</PackAssets>
</PackAssets>-->
<Exec Command="&quot;$(MSBuildThisFileDirectory)..\core\Xenko.Core.Tasks\bin\$(Configuration)\net472\Xenko.Core.Tasks.exe&quot; pack-assets &quot;$(MSBuildProjectFile)&quot; &quot;$(IntermediateOutputPath)\xenko&quot;" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="PackAssetsLine" />
</Exec>
<ItemGroup>
<PackAssetsLine Update="@(PackAssetsLine)">
<SourcePath>$([System.String]::new('%(Identity)').Split('|')[0])</SourcePath>
<PackagePath>$([System.String]::new('%(Identity)').Split('|')[1])</PackagePath>
</PackAssetsLine>
<None Include="%(PackAssetsLine.SourcePath)">
<Pack>true</Pack>
<PackagePath>%(PackAssetsLine.PackagePath)</PackagePath>
</None>
</ItemGroup>
</Target>
</Project>

0 comments on commit aae4951

Please sign in to comment.
You can’t perform that action at this time.