/// <summary> /// Adds an exiting project to this package. /// </summary> /// <param name="pathToMsproj">The path to msproj.</param> /// <param name="logger">The logger.</param> public void AddExitingProject(UFile pathToMsproj, LoggerResult logger) { if (pathToMsproj == null) throw new ArgumentNullException("pathToMsproj"); if (logger == null) throw new ArgumentNullException("logger"); if (!pathToMsproj.IsAbsolute) throw new ArgumentException("Expecting relative path", "pathToMsproj"); try { // Load a project without specifying a platform to make sure we get the correct platform type var msProject = VSProjectHelper.LoadProject(pathToMsproj, platform: "NoPlatform"); var projectType = VSProjectHelper.GetProjectTypeFromProject(msProject); if (!projectType.HasValue) { logger.Error("This project is not a project created with the editor"); } else { var platformType = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared; var projectReference = new ProjectReference() { Id = VSProjectHelper.GetProjectGuid(msProject), Location = pathToMsproj.MakeRelative(RootDirectory), Type = projectType.Value }; // Add the ProjectReference only for the compatible profiles (same platform or no platform) foreach (var profile in Profiles.Where(profile => platformType == profile.Platform)) { profile.ProjectReferences.Add(projectReference); } } } catch (Exception ex) { logger.Error("Unexpected exception while loading project [{0}]", ex, pathToMsproj); } }
public void TestMakeRelativeWithDrive() { UPath assetPath2 = null; UPath newAssetPath2 = null; var dir1 = new UDirectory("C:/a/b/c"); // Test direct relative assetPath2 = new UFile("C:/a/b/c/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("test.txt", newAssetPath2.FullPath); // Test direct relative + subdir assetPath2 = new UFile("C:/a/b/c/test/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("test/test.txt", newAssetPath2.FullPath); // Test relative 1 assetPath2 = new UFile("C:/a/b/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../test.txt", newAssetPath2.FullPath); // Test relative 2 assetPath2 = new UFile("C:/a/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../test.txt", newAssetPath2.FullPath); // Test relative 3 assetPath2 = new UFile("C:/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../../test.txt", newAssetPath2.FullPath); // Test already relative assetPath2 = new UFile("../test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../test.txt", newAssetPath2.FullPath); // Test no path in common assetPath2 = new UFile("E:/e/f/g/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("E:/e/f/g/test.txt", newAssetPath2.FullPath); // Test no root path single file assetPath2 = new UFile("E:/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("E:/test.txt", newAssetPath2.FullPath); }
/// <summary> /// This method is running in a separate thread and process file events received from <see cref="Core.IO.DirectoryWatcher"/> /// in order to generate the appropriate list of <see cref="AssetFileChangedEvent"/>. /// </summary> private void RunChangeWatcher() { // Only check every minute while (true) { if (threadWatcherEvent.WaitOne(TrackingSleepTime)) break; // Use a working copy in order to limit the locking fileEventsWorkingCopy.Clear(); lock (fileEvents) { fileEventsWorkingCopy.AddRange(fileEvents); fileEvents.Clear(); } if (fileEventsWorkingCopy.Count == 0 || isTrackingPaused) continue; var assetEvents = new List<AssetFileChangedEvent>(); // If this an asset belonging to a package lock (ThisLock) { var packages = Packages; // File event foreach (var fileEvent in fileEventsWorkingCopy) { var file = new UFile(fileEvent.FullPath); // When the session is being saved, we should not process events are they are false-positive alerts // So we just skip the file if (assetsBeingSaved.Contains(file.FullPath)) { continue; } // 1) Check if this is related to imported assets // Asset imports are handled slightly differently as we need to compute the // hash of the source file if (mapInputDependencyToAssets.ContainsKey(file.FullPath)) { // Prepare the hash of the import file in advance for later re-import FileVersionManager.Instance.ComputeFileHashAsync(file.FullPath, SourceImportFileHashCallback, tokenSourceForImportHash.Token); continue; } // 2) else check that the file is a supported extension if (!AssetRegistry.IsAssetFileExtension(file.GetFileExtension())) { continue; } // Find the parent package of the file that has been updated UDirectory parentPackagePath = null; Package parentPackage = null; foreach (var package in packages) { var rootDirectory = package.RootDirectory; if (rootDirectory == null) continue; if (rootDirectory.Contains(file) && (parentPackagePath == null || parentPackagePath.FullPath.Length < rootDirectory.FullPath.Length)) { parentPackagePath = rootDirectory; parentPackage = package; } } // If we found a parent package, create an associated asset event if (parentPackage != null) { var relativeLocation = file.MakeRelative(parentPackagePath); var item = parentPackage.Assets.Find(relativeLocation); AssetFileChangedEvent evt = null; switch (fileEvent.ChangeType) { case FileEventChangeType.Created: evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Added, relativeLocation); break; case FileEventChangeType.Deleted: evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Deleted, relativeLocation); break; case FileEventChangeType.Changed: evt = new AssetFileChangedEvent(parentPackage, AssetFileChangedType.Updated, relativeLocation); break; } if (evt != null) { if (item != null) { evt.AssetId = item.Id; } assetEvents.Add(evt); } } } // After all events have been processed, we // remove the file assetBeingSaved foreach (var fileEvent in fileEventsWorkingCopy) { var file = new UFile(fileEvent.FullPath); // When the session is being saved, we should not process events are they are false-positive alerts // So we just skip the file if (assetsBeingSaved.Remove(file)) { if (assetsBeingSaved.Count == 0) { break; } } } // If we have any new events, copy them back if (assetEvents.Count > 0 && !isTrackingPaused) { currentAssetFileChangedEvents.AddRange(assetEvents); } } } }
public void TestMakeRelative() { UPath assetPath2 = null; UPath newAssetPath2 = null; var dir1 = new UDirectory("/a/b/c"); var assetDir2 = new UDirectory("/a/b/c"); newAssetPath2 = dir1.MakeRelative(assetDir2); Assert.AreEqual(".", newAssetPath2.FullPath); var assetDir3 = new UDirectory("/a/b"); newAssetPath2 = dir1.MakeRelative(assetDir3); Assert.AreEqual("c", newAssetPath2.FullPath); var assetDir4 = new UDirectory("/a/b/c/d"); newAssetPath2 = dir1.MakeRelative(assetDir4); Assert.AreEqual("..", newAssetPath2.FullPath); // Test direct relative assetPath2 = new UFile("/a/b/c/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("test.txt", newAssetPath2.FullPath); // Test direct relative + subdir assetPath2 = new UFile("/a/b/c/test/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("test/test.txt", newAssetPath2.FullPath); // Test relative 1 assetPath2 = new UFile("/a/b/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../test.txt", newAssetPath2.FullPath); // Test relative 2 assetPath2 = new UFile("/a/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../test.txt", newAssetPath2.FullPath); // Test relative 3 assetPath2 = new UFile("/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../../test.txt", newAssetPath2.FullPath); // Test already relative assetPath2 = new UFile("../test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../test.txt", newAssetPath2.FullPath); // Test only root path in common assetPath2 = new UFile("/e/f/g/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../../e/f/g/test.txt", newAssetPath2.FullPath); // Test only root path in common with single file assetPath2 = new UFile("/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.AreEqual("../../../test.txt", newAssetPath2.FullPath); }