Skip to content
Permalink
Browse files

[Assets] Compiler: can now properly compile existing projects

  • Loading branch information
xen2 committed Oct 24, 2018
1 parent 051a811 commit 4c6ca70b399fc77bb0439916bbd0f240350e13f8
Showing with 169 additions and 534 deletions.
  1. +0 −14 samples/Tests/SampleTestsData.cs
  2. +38 −31 sources/assets/Xenko.Core.Assets.CompilerApp/NuGetAssemblyResolver.cs
  3. +0 −7 sources/assets/Xenko.Core.Assets.CompilerApp/Program.cs
  4. +2 −0 sources/assets/Xenko.Core.Assets.CompilerApp/Xenko.Core.Assets.CompilerApp.csproj
  5. +1 −7 sources/assets/Xenko.Core.Assets.Tests/TestPackageStore.cs
  6. +0 −19 sources/assets/Xenko.Core.Assets/DirectoryHelper.cs
  7. +1 −1 sources/assets/Xenko.Core.Assets/Package.cs
  8. +50 −48 sources/assets/Xenko.Core.Assets/PackageSession.Dependencies.cs
  9. +0 −5 sources/assets/Xenko.Core.Assets/PackageSession.cs
  10. +2 −167 sources/assets/Xenko.Core.Assets/PackageStore.cs
  11. +1 −1 sources/assets/Xenko.Core.Assets/PackageUpgraderAttribute.cs
  12. +1 −1 sources/assets/Xenko.Core.Assets/Templates/TemplateManager.cs
  13. +19 −152 sources/assets/Xenko.Core.Packages/NugetStore.cs
  14. +4 −1 sources/core/Xenko.Core/build/Xenko.Core.targets
  15. +9 −3 sources/editor/Xenko.Assets.Presentation/Templates/Core/ProjectLibrary.Game/$ProjectName$.csproj.t4
  16. +9 −3 sources/editor/Xenko.Assets.Presentation/Templates/Core/ProjectLibrary/$ProjectName$.csproj.t4
  17. +0 −11 sources/editor/Xenko.GameStudio/Program.cs
  18. +2 −1 sources/editor/Xenko.GameStudio/View/AboutPage.xaml.cs
  19. +24 −3 sources/engine/Xenko.Assets/XenkoPackageUpgrader.cs
  20. +0 −25 sources/launcher/Xenko.Launcher/Launcher.cs
  21. +0 −1 sources/launcher/Xenko.Launcher/LauncherArguments.cs
  22. +0 −22 sources/launcher/Xenko.Launcher/LauncherInstance.cs
  23. +2 −1 sources/launcher/Xenko.Launcher/Services/SelfUpdater.cs
  24. +1 −1 sources/launcher/Xenko.Launcher/ViewModels/LauncherViewModel.cs
  25. +0 −2 sources/launcher/Xenko.Launcher/ViewModels/PackageVersionViewModel.cs
  26. +3 −0 sources/tools/Xenko.ConnectionRouter/RouterHelper.cs
  27. +0 −7 sources/tools/Xenko.VisualStudio.Package.Tests/IntegrationTests.cs
@@ -22,19 +22,5 @@ class SampleTestsData
#else
public const PlatformType TestPlatform = PlatformType.Windows;
#endif

[ModuleInitializer]
public static void Initialize()
{
// Override search path since we are in a unit test directory
DirectoryHelper.PackageDirectoryOverride = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..");

// Running first time? If yes, create nuget redirect package.
var packageVersion = new PackageVersion(XenkoVersion.NuGetVersion);
if (PackageStore.Instance.IsDevelopmentStore)
{
PackageStore.Instance.CheckDeveloperTargetRedirects("Xenko", packageVersion, PackageStore.Instance.InstallationPath).Wait();
}
}
}
}
@@ -5,18 +5,18 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

namespace Xenko.Core.Assets.CompilerApp
{
@@ -38,46 +38,53 @@ internal static void __Initialize__()
var installPath = SettingsUtility.GetGlobalPackagesFolder(settings);
var assemblies = new List<string>();

var logger = new Logger();
var specPath = Path.Combine("TestProject", "project.json");
var spec = new PackageSpec()
{
var logger = new Logger();
var configJson = JObject.Parse(@"
Name = "TestProject", // make sure this package never collides with a dependency
FilePath = specPath,
Dependencies = new List<LibraryDependency>()
{
""dependencies"": {
""Xenko.Core.Assets.CompilerApp"": ""3.1.0.1-dev""
},
""frameworks"": {
""net462"": { }
new LibraryDependency
{
LibraryRange = new LibraryRange(Assembly.GetEntryAssembly().GetName().Name, new VersionRange(new NuGetVersion(XenkoVersion.NuGetVersion)), LibraryDependencyTarget.Package),
}
}");
},
TargetFrameworks =
{
new TargetFrameworkInformation
{
FrameworkName = NuGetFramework.Parse("net462"),
}
},
};

var specPath = Path.Combine("TestProject", "project.json");
var spec = JsonPackageSpecReader.GetPackageSpec(configJson.ToString(), "TestProject", specPath);
using (var context = new SourceCacheContext())
{
context.IgnoreFailedSources = true;

using (var context = new SourceCacheContext())
var provider = RestoreCommandProviders.Create(installPath, new List<string>(), sourceRepositoryProvider.GetRepositories(), context, new LocalPackageFileCache(), logger);
var request = new RestoreRequest(spec, provider, context, logger)
{
context.IgnoreFailedSources = true;

var provider = RestoreCommandProviders.Create(installPath, new List<string>(), sourceRepositoryProvider.GetRepositories(), context, new LocalPackageFileCache(), logger);
var request = new RestoreRequest(spec, provider, context, logger)
{
LockFilePath = "project.lock.json",
};
LockFilePath = "project.lock.json",
};

var command = new RestoreCommand(request);
var command = new RestoreCommand(request);

Console.WriteLine("Restoring");
// Act
var result = command.ExecuteAsync().Result;
Console.WriteLine("Restoring done");
Console.WriteLine("Restoring");
// Act
var result = command.ExecuteAsync().Result;
Console.WriteLine("Restoring done");
result.CommitAsync(logger, CancellationToken.None).Wait();

foreach (var library in result.LockFile.Libraries)
foreach (var library in result.LockFile.Libraries)
{
foreach (var file in library.Files)
{
foreach (var file in library.Files)
if (file.StartsWith("lib/net"))
{
if (file.StartsWith("lib/net"))
{
assemblies.Add(Path.Combine(installPath, library.Path, file));
}
assemblies.Add(Path.Combine(installPath, library.Path, file));
}
}
}
@@ -12,13 +12,6 @@ private static int Main(string[] args)
{
try
{
// Override search path since we are in a unit test directory
DirectoryHelper.PackageDirectoryOverride = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\..\..");

// Set the XenkoDir environment variable
var installDir = DirectoryHelper.GetInstallationDirectory("Xenko");
Environment.SetEnvironmentVariable("XenkoDir", installDir);

// Running first time? If yes, create nuget redirect package.
//var packageVersion = new PackageVersion(XenkoVersion.NuGetVersion);
//if (PackageStore.Instance.IsDevelopmentStore)
@@ -62,12 +62,14 @@

<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeExtraAssemblies</TargetsForTfmSpecificBuildOutput>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.config</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<Target Name="IncludeExtraAssemblies">
<ItemGroup>
<BuildOutputInPackage Include="$(OutputPath)System*.dll" />
<BuildOutputInPackage Include="$(OutputPath)NuGet*.dll" />
<BuildOutputInPackage Include="$(OutputPath)Newtonsoft.Json.dll" />
<BuildOutputInPackage Include="$(OutputPath)$(TargetFileName).config" />
</ItemGroup>
</Target>
</Project>
@@ -17,17 +17,11 @@ public void TestDefault()
// Initialize a default package manager that will use the
var packageManager = PackageStore.Instance;

// Build output is Bin\Windows\Tests\Xenko.Core.Assets.Tests, so need to go to parent 4 times
var installationPath = (UDirectory)Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\.."));

Assert.Equal(installationPath, packageManager.InstallationPath);

var packageFileName = packageManager.GetPackageWithFileName(packageManager.DefaultPackageName);
var packageFileName = packageManager.GetPackageWithFileName("Xenko");

Assert.True(File.Exists(packageFileName), "Unable to find default package file [{0}]".ToFormat(packageFileName));
}


//[Fact]
//public void TestRemote()
//{
@@ -14,22 +14,6 @@ public static class DirectoryHelper
private const string XenkoNuspec = @"xenko.nuspec";
private static string packageDirectoryOverride;

/// <summary>
/// If not null, the location where to find the package directory and the installation directory, overriding the default locations.
/// It can only be set once.
/// </summary>
public static string PackageDirectoryOverride {
get
{
return packageDirectoryOverride;
}
set
{
if (packageDirectoryOverride != null) throw new NotSupportedException("Cannot set more than once the directory override!");
packageDirectoryOverride = value;
}
}

/// <summary>
/// Gets the directory of the package from which the <see cref="Xenko.Core.Assets"/> assembly has been loaded.
/// </summary>
@@ -38,9 +22,6 @@ public static class DirectoryHelper
/// <exception cref="InvalidOperationException">The package from which the <see cref="Xenko.Core.Assets"/> assembly has been loaded does not match the <paramref name="packageName"/>.</exception>
public static string GetPackageDirectory(string packageName)
{
if (PackageDirectoryOverride != null)
return PackageDirectoryOverride;

var appDomain = AppDomain.CurrentDomain;
var baseDirectory = new DirectoryInfo(appDomain.BaseDirectory);
var defaultPackageDirectoryTemp = baseDirectory.Parent?.Parent;
@@ -897,7 +897,7 @@ private void LoadAsset(AssetMigrationContext context, PackageLoadingAssetFile as
// the loop
try
{
AssetMigration.MigrateAssetIfNeeded(context, assetFile, PackageStore.Instance.DefaultPackageName);
AssetMigration.MigrateAssetIfNeeded(context, assetFile, "Xenko");

// Try to load only if asset is not already in the package or assetRef.Asset is null
var assetPath = assetFile.AssetLocation;
@@ -89,39 +89,66 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje

log.Verbose($"Process dependencies for {project.Name}...");

var restoreGraph = await GenerateRestoreGraph(log, project.Name, project.FullPath);
var packageReferences = new Dictionary<string, PackageVersionRange>();

// Load some informations about the project
try
{
// Load a project without specifying a platform to make sure we get the correct platform type
var msProject = VSProjectHelper.LoadProject(project.FullPath, platform: "NoPlatform");
try
{

var projectIsExecutable = msProject.GetPropertyValue("XenkoIsExecutable");
project.Type = projectIsExecutable.ToLowerInvariant() == "true" ? ProjectType.Executable : ProjectType.Library;
if (project.Type == ProjectType.Executable)
project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;

foreach (var packageReference in msProject.GetItems("PackageReference").ToList())
{
if (packageReference.HasMetadata("Version") && PackageVersionRange.TryParse(packageReference.GetMetadataValue("Version"), out var packageRange))
packageReferences[packageReference.EvaluatedInclude] = packageRange;
}
}
finally
{
msProject.ProjectCollection.UnloadAllProjects();
msProject.ProjectCollection.Dispose();
}
}
catch (Exception ex)
{
log.Error($"Unexpected exception while loading project [{project.FullPath.ToWindowsPath()}]", ex);
}

// Check if there is any package upgrade to do
var pendingPackageUpgrades = new List<PendingPackageUpgrade>();
pendingPackageUpgradesPerPackage.Add(package, pendingPackageUpgrades);

foreach (var item in restoreGraph.Flattened)
foreach (var packageReference in packageReferences)
{
if (item.Key.Type == LibraryType.Package)
{
var dependencyName = item.Key.Name;
var dependencyVersion = item.Key.Version.ToPackageVersion();
var dependencyName = packageReference.Key;
var dependencyVersion = packageReference.Value;

var packageUpgrader = AssetRegistry.GetPackageUpgrader(dependencyName);
if (packageUpgrader != null)
var packageUpgrader = AssetRegistry.GetPackageUpgrader(dependencyName);
if (packageUpgrader != null)
{
// Check if upgrade is necessary
if (dependencyVersion.MinVersion >= packageUpgrader.Attribute.UpdatedVersionRange.MinVersion)
{
// Check if upgrade is necessary
if (dependencyVersion >= packageUpgrader.Attribute.UpdatedVersionRange.MinVersion)
{
continue;
}
continue;
}

// Check if upgrade is allowed
if (dependencyVersion < packageUpgrader.Attribute.PackageMinimumVersion)
{
// Throw an exception, because the package update is not allowed and can't be done
throw new InvalidOperationException($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] is not supported (supported only from version [{packageUpgrader.Attribute.PackageMinimumVersion}]");
}
// Check if upgrade is allowed
if (dependencyVersion.MinVersion < packageUpgrader.Attribute.PackageMinimumVersion)
{
// Throw an exception, because the package update is not allowed and can't be done
throw new InvalidOperationException($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] is not supported (supported only from version [{packageUpgrader.Attribute.PackageMinimumVersion}]");
}

log.Info($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] will be required");
log.Info($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] will be required");

pendingPackageUpgrades.Add(new PendingPackageUpgrade(packageUpgrader, new PackageDependency(dependencyName, new PackageVersionRange(dependencyVersion)), null));
}
pendingPackageUpgrades.Add(new PendingPackageUpgrade(packageUpgrader, new PackageDependency(dependencyName, dependencyVersion), null));
}
}

@@ -157,8 +184,7 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
var msbuildProject = VSProjectHelper.LoadProject(projectFile.ToWindowsPath());
var isProjectDirty = false;

var packageReferences = msbuildProject.GetItems("PackageReference").ToList();
foreach (var packageReference in packageReferences)
foreach (var packageReference in msbuildProject.GetItems("PackageReference").ToList())
{
if (packageReference.EvaluatedInclude == pendingPackageUpgrade.Dependency.Name && packageReference.GetMetadataValue("Version") != expectedVersion)
{
@@ -256,30 +282,6 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
projectDependency.Package = loadedPackage;
}

// Load some informations about the project
try
{
// Load a project without specifying a platform to make sure we get the correct platform type
var msProject = VSProjectHelper.LoadProject(project.FullPath, platform: "NoPlatform");
try
{

var projectIsExecutable = msProject.GetPropertyValue("XenkoIsExecutable");
project.Type = projectIsExecutable.ToLowerInvariant() == "true" ? ProjectType.Executable : ProjectType.Library;
if (project.Type == ProjectType.Executable)
project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;
}
finally
{
msProject.ProjectCollection.UnloadAllProjects();
msProject.ProjectCollection.Dispose();
}
}
catch (Exception ex)
{
log.Error($"Unexpected exception while loading project [{project.FullPath.ToWindowsPath()}]", ex);
}

// 2. Load local packages
/*foreach (var packageReference in package.LocalDependencies)
{
@@ -422,8 +422,6 @@ public PackageSession()
VSSolution = new VisualStudio.Solution();
VSSolution.Headers.Add(PackageSessionHelper.SolutionHeader);

constraintProvider.AddConstraint(PackageStore.Instance.DefaultPackageName, new PackageVersionRange(PackageStore.Instance.DefaultPackageVersion));

Projects = new ProjectCollection();
Projects.CollectionChanged += ProjectsCollectionChanged;

@@ -1381,9 +1379,6 @@ private bool TryLoadAssets(PackageSession session, ILogger log, Package package,

// Default package version override
newLoadParameters.ExtraCompileProperties = new Dictionary<string, string>();
var defaultPackageOverride = NugetStore.GetPackageVersionVariable(PackageStore.Instance.DefaultPackageName) + "Override";
var defaultPackageVersion = PackageStore.Instance.DefaultPackageVersion.Version;
newLoadParameters.ExtraCompileProperties.Add(defaultPackageOverride, new Version(defaultPackageVersion.Major, defaultPackageVersion.Minor).ToString());
if (loadParameters.ExtraCompileProperties != null)
{
foreach (var property in loadParameters.ExtraCompileProperties)

0 comments on commit 4c6ca70

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