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.Equal(".", newAssetPath2.FullPath); var assetDir3 = new UDirectory("/a/b"); newAssetPath2 = dir1.MakeRelative(assetDir3); Assert.Equal("c", newAssetPath2.FullPath); var assetDir4 = new UDirectory("/a/b/c/d"); newAssetPath2 = dir1.MakeRelative(assetDir4); Assert.Equal("..", newAssetPath2.FullPath); // Test direct relative assetPath2 = new UFile("/a/b/c/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("test.txt", newAssetPath2.FullPath); // Test direct relative + subdir assetPath2 = new UFile("/a/b/c/test/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("test/test.txt", newAssetPath2.FullPath); // Test relative 1 assetPath2 = new UFile("/a/b/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("../test.txt", newAssetPath2.FullPath); // Test relative 2 assetPath2 = new UFile("/a/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("../../test.txt", newAssetPath2.FullPath); // Test relative 3 assetPath2 = new UFile("/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("../../../test.txt", newAssetPath2.FullPath); // Test already relative assetPath2 = new UFile("../test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("../test.txt", newAssetPath2.FullPath); // Test only root path in common assetPath2 = new UFile("/e/f/g/test.txt"); newAssetPath2 = assetPath2.MakeRelative(dir1); Assert.Equal("../../../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.Equal("../../../test.txt", newAssetPath2.FullPath); }
/// <summary> /// Sync <paramref name="sourceDir"/> with <paramref name="destDir"/> /// </summary> /// <param name="connectInfo">Credentials to access host where synchronization takes place.</param> /// <param name="sourceDir">Source directory on the local host.</param> /// <param name="destDir">Destination directory on the remote host</param> /// <param name="logger">Logging facility</param> /// <returns><c>true</c> if successful; otherwise, <c>false</c>.</returns> private static bool SyncTo(ConnectionInfo connectInfo, UDirectory sourceDir, UDirectory destDir, LoggerResult logger) { // Copy files over using (var sftp = new SftpClient(connectInfo)) { try { sftp.Connect(); if (!sftp.IsConnected) { return(false); } } catch { logger.Error("Cannot connect"); return(false); } // Perform recursive copy of all the folders under `sourceDir`. This is required // as the sftp client only synchronize directories at their level only, no // subdirectory. var dirs = new Queue <DirectoryInfo>(); dirs.Enqueue(new DirectoryInfo(sourceDir)); var parentPath = sourceDir; while (dirs.Count != 0) { var currentDir = dirs.Dequeue(); var currentPath = new UDirectory(currentDir.FullName); foreach (var subdir in currentDir.EnumerateDirectories()) { dirs.Enqueue(subdir); } // Get the destination path by adding to `Location` the relative path of `sourceDir` to `currentDir`. var destination = UPath.Combine(destDir, currentPath.MakeRelative(parentPath)); logger.Info("Synchronizing " + currentPath + " with " + destination.FullPath); // Try to create a remote directory. If it throws an exception, we will assume // for now that the directory already exists. See https://github.com/sshnet/SSH.NET/issues/25 try { sftp.CreateDirectory(destination.FullPath); logger.Info("Creating remote directory " + destination.FullPath); } catch (SshException) { // Do nothing, as this is when the directory already exists } // Synchronize files. foreach (var file in sftp.SynchronizeDirectories(currentPath.FullPath, destination.FullPath, "*")) { logger.Info("Updating " + file.Name); // Some of our files needs executable rights, however we do not know in advance which one // need it. For now all files will be rwxr.xr.x (0755 in octal but written in decimal for readability). sftp.ChangePermissions(destination.FullPath + "/" + file.Name, 755); } } return(true); } }
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); }
/// <summary> /// Sync output of current compilation to <paramref name="dir"/> /// </summary> /// <param name="dir"></param> /// <returns></returns> private bool SyncTo(string dir) { // Copy files over using (var sftp = new SftpClient(Machine, Port, Username, Password)) { sftp.Connect(); if (!sftp.IsConnected) { return(false); } // Perform recursive copy of all the folders under `dir`. This is required // as the sftp client only synchronize directories at their level only, no // subdirectory. var dirs = new Queue <DirectoryInfo>(); dirs.Enqueue(new DirectoryInfo(dir)); var parentPath = new UDirectory(dir); while (dirs.Count != 0) { var currentDir = dirs.Dequeue(); var currentPath = new UDirectory(currentDir.FullName); foreach (var subdir in currentDir.EnumerateDirectories()) { dirs.Enqueue(subdir); } // Get the destination path by adding to `Location` the relative path of `dir` to `currentDir`. var destination = UPath.Combine(new UDirectory(Location.ItemSpec), currentPath.MakeRelative(parentPath)); Log.LogMessage("Synchronizing " + currentPath + " with " + destination.FullPath); // Try to create a remote directory. If it throws an exception, we will assume // for now that the directory already exists. See https://github.com/sshnet/SSH.NET/issues/25 try { sftp.CreateDirectory(destination.FullPath); Log.LogMessage("Creating remote directory " + destination.FullPath); } catch (SshException) { // Do nothing, as this is when the directory already exists } // Synchronize files. foreach (var file in sftp.SynchronizeDirectories(currentPath.FullPath, destination.FullPath, "*")) { Log.LogMessage("Updating " + file.Name); } } return(true); } }