/// <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;
}
}