Skip to content
Permalink
Browse files

[Assets] Allow creation of samples (as long as they don't use externa…

…l packages)
  • Loading branch information
xen2 committed Sep 3, 2018
1 parent e21932d commit c444d02c5ce5a0a8f86147f3e0a74b6bdcb4ea6e
@@ -68,10 +68,11 @@ public enum PackageType
[NonIdentifiableCollectionItems]
[AssetDescription(PackageFileExtension)]
[DebuggerDisplay("Id: {Id}, Name: {Meta.Name}, Version: {Meta.Version}, Assets [{Assets.Count}]")]
[AssetFormatVersion("Assets", PackageFileVersion)]
[AssetFormatVersion("Assets", PackageFileVersion, "0.0.0.4")]
[AssetUpgrader("Assets", "0.0.0.4", "3.1.0.0", typeof(MovePackageInsideProject))]
public sealed partial class Package : IIdentifiable, IFileSynchronizable, IAssetFinder
{
private const int PackageFileVersion = 4;
private const string PackageFileVersion = "3.1.0.0";

private Guid id;

@@ -185,7 +186,7 @@ public Guid Id
/// </summary>
/// <value>The profiles.</value>
[DataMember(50)]
public PackageProfile Profile { get; } = PackageProfile.NewShared();
public PackageProfile Profile { get; set; } = PackageProfile.NewShared();

/// <summary>
/// Gets or sets the list of folders that are explicitly created but contains no assets.
@@ -762,6 +763,18 @@ public static Guid GetPackageIdFromFile(string filePath)
public static Package Load(ILogger log, string filePath, PackageLoadParameters loadParametersArg = null)
{
var package = LoadRaw(log, filePath);

// TODO CSPROJ=XKPKG temp code until Package loading supports csproj format natively
var projectFullPath = Path.ChangeExtension(package.FullPath, ".csproj");
if (File.Exists(projectFullPath))
{
var project = new VisualStudio.Project(package.Id, VisualStudio.KnownProjectTypeGuid.CSharp, Path.GetFileNameWithoutExtension(projectFullPath), projectFullPath, Guid.Empty, null, null, null);
package.ProjectFullPath = project.FullPath;
package.Type = PackageType.ProjectAndPackage;
package.ProjectType = Core.Assets.ProjectType.Library;
package.VSProject = project;
}

if (package != null)
{
if (!package.LoadAssembliesAndAssets(log, loadParametersArg))
@@ -805,8 +818,8 @@ internal static Package LoadRaw(ILogger log, string filePath)
? AssetFileSerializer.Load<Package>(new MemoryStream(packageFile.AssetContent), filePath, log)
: AssetFileSerializer.Load<Package>(filePath, log);
var package = loadResult.Asset;
package.FullPath = filePath;
package.previousPackagePath = package.FullPath;
package.FullPath = packageFile.FilePath;
package.previousPackagePath = packageFile.OriginalFilePath;
package.IsDirty = packageFile.AssetContent != null || loadResult.AliasOccurred;

return package;
@@ -1424,5 +1437,36 @@ private static void FindAssetsInProject(ICollection<PackageLoadingAssetFile> lis
}
}
}

private class MovePackageInsideProject : AssetUpgraderBase
{
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint)
{
if (asset.Profiles != null)
{
var profiles = asset.Profiles;

foreach (var profile in profiles)
{
if (profile.Platform == "Shared" && profile.ProjectReferences.Count == 1)
{
var projectLocation = (UFile)(string)profile.ProjectReferences[0].Location;
assetFile.FilePath = UPath.Combine(assetFile.OriginalFilePath.GetFullDirectory(), (UFile)(projectLocation.GetFullPathWithoutExtension() + PackageFileExtension));

for (int i = 0; i < profile.AssetFolders.Count; ++i)
{
var assetPath = UPath.Combine(assetFile.OriginalFilePath.GetFullDirectory(), (UDirectory)(string)profile.AssetFolders[i].Path);
assetPath = assetPath.MakeRelative(assetFile.FilePath.GetFullDirectory());
profile.AssetFolders[i].Path = (string)assetPath;
}

asset.Profile = profile;
}
}

asset.Profiles = DynamicYamlEmpty.Default;
}
}
}
}
}
@@ -3,7 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xenko.Core.Annotations;
using Xenko.Core.IO;
using Xenko.Core.Serialization.Contents;
using Xenko.Core.Yaml;
@@ -15,9 +15,11 @@ namespace Xenko.Core.Assets
/// </summary>
public class PackageLoadingAssetFile
{
public readonly UFile FilePath;
public readonly UDirectory SourceFolder;

[NotNull] public UFile OriginalFilePath { get; }
[NotNull] public UFile FilePath { get; set; }

public long CachedFileSize;

// If asset has been created or upgraded in place during package upgrade phase, it will be stored here
@@ -32,9 +34,10 @@ public class PackageLoadingAssetFile
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="sourceFolder">The source folder.</param>
public PackageLoadingAssetFile(UFile filePath, UDirectory sourceFolder)
public PackageLoadingAssetFile([NotNull] UFile filePath, UDirectory sourceFolder)
{
FilePath = filePath;
OriginalFilePath = FilePath;
SourceFolder = sourceFolder;
}

@@ -45,13 +48,14 @@ public PackageLoadingAssetFile(UFile filePath, UDirectory sourceFolder)
/// <param name="filePath">The relative file path (from default asset folder).</param>
/// <param name="sourceFolder">The source folder (optional, can be null).</param>
/// <exception cref="System.ArgumentException">filePath must be relative</exception>
public PackageLoadingAssetFile(Package package, UFile filePath, UDirectory sourceFolder)
public PackageLoadingAssetFile(Package package, [NotNull] UFile filePath, UDirectory sourceFolder)
{
if (filePath.IsAbsolute)
throw new ArgumentException("filePath must be relative", filePath);

SourceFolder = UPath.Combine(package.RootDirectory, sourceFolder ?? package.GetDefaultAssetFolder());
FilePath = UPath.Combine(SourceFolder, filePath);
OriginalFilePath = FilePath;
}

public IReference ToReference()
@@ -881,6 +881,16 @@ private Package PreLoadPackage(ILogger log, string filePath, bool isSystemPackag
var package = Package.LoadRaw(log, filePath);
package.IsSystem = isSystemPackage;

if (project == null)
{
// TODO CSPROJ=XKPKG temp code until Package loading supports csproj format natively
var projectFullPath = Path.ChangeExtension(package.FullPath, ".csproj");
if (File.Exists(projectFullPath))
{
project = new VisualStudio.Project(package.Id, KnownProjectTypeGuid.CSharp, Path.GetFileNameWithoutExtension(projectFullPath), projectFullPath, Guid.Empty, null, null, null);
}
}

// TODO CSPROJ=XKPKG temp code until Package loading supports csproj format natively
if (project != null)
{
@@ -124,7 +124,7 @@ protected override bool Generate(SessionTemplateGeneratorParameters parameters)
}

// We are going to replace all projects/packages id by new ids
var idsToReplace = package.Session.Packages.Select(x => x.Id).Distinct().ToDictionary(guid => guid.ToString("D"), guid => Guid.NewGuid(), StringComparer.OrdinalIgnoreCase);
var idsToReplace = new Dictionary<string, Guid>(StringComparer.OrdinalIgnoreCase);
idsToReplace.Add(package.Id.ToString("D"), Guid.NewGuid());

var guidRegexPattern = new StringBuilder();
@@ -351,26 +351,27 @@ protected override async Task<bool> AfterSave(SessionTemplateGeneratorParameters
// We want to force regeneration of Windows platform in case the sample .csproj is outdated
UpdatePlatformsTemplateGenerator.SetForcePlatformRegeneration(updateParameters, true);

var updateTemplate = UpdatePlatformsTemplateGenerator.Default;
if (!await updateTemplate.PrepareForRun(updateParameters) || !updateTemplate.Run(updateParameters))
{
// Remove the created project
var path = Path.GetDirectoryName(parameters.Session.SolutionPath.ToWindowsPath());
try
{
Directory.Delete(path ?? "", true);
}
catch (IOException ex)
{
parameters.Logger.Error("Error when removing generated project.", ex);
}
catch (UnauthorizedAccessException ex)
{
parameters.Logger.Error("Error when removing generated project.", ex);
}
// Notify cancellation
return false;
}
// TODO CSPROJ=XKPKG
//var updateTemplate = UpdatePlatformsTemplateGenerator.Default;
//if (!await updateTemplate.PrepareForRun(updateParameters) || !updateTemplate.Run(updateParameters))
//{
// // Remove the created project
// var path = Path.GetDirectoryName(parameters.Session.SolutionPath.ToWindowsPath());
// try
// {
// Directory.Delete(path ?? "", true);
// }
// catch (IOException ex)
// {
// parameters.Logger.Error("Error when removing generated project.", ex);
// }
// catch (UnauthorizedAccessException ex)
// {
// parameters.Logger.Error("Error when removing generated project.", ex);
// }
// // Notify cancellation
// return false;
//}

// Save again post update
SaveSession(parameters);
@@ -495,10 +495,10 @@ public override void Destroy()

private void AutoSelectCurrentPackage()
{
var currentProject = LocalPackages.SelectMany(x => x.Content).OfType<ProjectViewModel>().FirstOrDefault(x => x.Type == ProjectType.Executable && x.Profile.Platform == PlatformType.Windows);
var currentProject = LocalPackages.FirstOrDefault();
if (currentProject != null)
{
SetCurrentPackage(currentProject.Package);
SetCurrentPackage(currentProject);
}
}

@@ -1059,7 +1059,7 @@ private PackageViewModel CreatePackageViewModel(Package package, bool packageAlr
private void SetCurrentPackage(object selectedItem)
{
var package = selectedItem as PackageViewModel;
if (package == null || !package.Profile.HasExecutables)
if (package == null)
{
// Editor.MessageBox(Resources.Strings.SessionViewModel.SelectExecutableAsCurrentProject, MessageBoxButton.OK, MessageBoxImage.Information);
return;

0 comments on commit c444d02

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