/// <summary> /// Maps a directory to the project file /// </summary> /// <param name="prj">The active project</param> /// <param name="localPath">The local path</param> /// <param name="remotePath">The remote path</param> private void MapDirectoryAbsolutePath(Project prj, string localPath, string remotePath) { String rPth = prj.Connection.Data.RootDir + remotePath; MappedPath path = MappingUtils.GetMappedPath(prj, localPath, remotePath); this.MapDirectory(prj, path); }
/// <summary> /// Downloads a file /// </summary> /// <param name="credentials">The ssh site credentials</param> /// <param name="fileMap">The mapping file to download</param> /// <param name="replace">True if the downloaded file will be replaced</param> public static void Download(this SiteCredentials credentials, MappedPath fileMap, Boolean replace) { FileInfo sC, lC; AuraSftpClient.SSHTransactionVoid(credentials, (AuraSftpClient c) => { sC = new FileInfo(fileMap.GetFullServerCopy()); lC = new FileInfo(fileMap.GetFullProjectCopy()); DownloadFile(c, fileMap.GetFullRemotePath(), sC, lC, replace); }); }
/// <summary> /// Maps a directory to the project file /// </summary> /// <param name="prj">The project file</param> /// <param name="path">The mapped directory data</param> private void MapDirectory(Project prj, MappedPath path) { String error_msg = null; var result = AuraSftpClient.SFTPTransactionGen <MappedPath> (prj.Connection.Data, (RenCiSftpClient client) => { String rPth = path.GetFullRemotePath(); if (client.Exists(rPth)) { var entry = client.Get(rPth); path.RemoteVersion = entry.LastAccessTime; Boolean mapExist = prj.Data.Map.Directories.FirstOrDefault(x => x.GetFullRemotePath() == entry.FullName) != null; if (entry.IsDirectory && !mapExist) { if (!Directory.Exists(path.GetFullProjectCopy())) { Directory.CreateDirectory(path.GetFullProjectCopy()); } if (!Directory.Exists(path.GetFullServerCopy())) { Directory.CreateDirectory(path.GetFullServerCopy()); } } else if (!entry.IsDirectory) { error_msg = String.Format(MSG_ERR_MAP_REM_PTH_NOT_DIR, rPth, HelpCommand, "dir"); } else if (mapExist) { error_msg = String.Format(MSG_ERR_MAP_AlREADY_MAPPED, rPth); } } else { error_msg = String.Format(MSG_ERR_MAP_REM_PTH, rPth); } return(path); }); if (result != null && error_msg == null) { prj.Data.Map.Directories = prj.Data.Map.Directories.Union(new MappedPath[] { result }).ToArray(); prj.SaveProject(this.ConfigFile); Console.WriteLine(String.Format(MSG_INF_MAP_CREATED, result.GetFullRemotePath(), result.GetFullProjectCopy())); } else if (error_msg != null) { Console.WriteLine(error_msg); } }
/// <summary> /// Gets the mapped path /// </summary> /// <param name="prj">The active project</param> /// <param name="localPath">The relative path to the local path in windows format</param> /// <param name="remotePath">The relative path to the remote path in linux format</param> /// <returns>The mapped path</returns> public static MappedPath GetMappedPath(Project prj, String localPath, String remotePath) { remotePath = ValidatePath(remotePath, false); localPath = ValidatePath(localPath, true); MappedPath pth = new MappedPath() { ProjectCopy = GetPath(ValidatePath(prj.Data.ProjectCopy, true), localPath.Split('\\'), true), ServerCopy = GetPath(ValidatePath(prj.Data.ServerCopy, true), localPath.Split('\\'), true), RemotePath = GetPath(ValidatePath(prj.Connection.Data.RootDir, false), remotePath.Split('/'), false), RemoteVersion = DateTime.Now, LocalVersion = DateTime.Now }; return(pth); }
public new void Dispose() { if (IsMapped) { UnmapDrive(mappedChar); } if (MappedPath != null && MappedPath.Exists) { try { MappedPath.Delete(true); } catch { /*ignore*/ } } IsMapped = false; MappedPath = null; base.Dispose(); }
/// <summary> /// Downloads a directory /// </summary> /// <param name="client">The Sftp client</param> /// <param name="file">The directory used to download its files</param> /// <param name="filter">The download filter</param> /// <param name="silentDownload">Download the files without listing everything</param> public static void Download(this RenciSftpClient client, SiteCredentials credentials, MappedPath dir, SftpFilter filter, Boolean replace, Boolean silentDownload = false) { var files = SftpUtils.ListFiles(client, dir.GetFullRemotePath(), filter, silentDownload); string fileName, serverCopy; FileInfo cC, wC; AuraSftpClient.SSHTransactionVoid(credentials, (Action <AuraSftpClient>)((AuraSftpClient c) => { foreach (String remoteFile in files) { fileName = remoteFile.Replace((string)dir.GetFullRemotePath(), "").Substring(1).Replace("/", "\\"); serverCopy = Path.Combine((string)dir.GetFullServerCopy(), fileName); //Cache copy cC = new FileInfo(serverCopy); //The path to the working copy wC = new FileInfo(Path.Combine((string)dir.GetFullProjectCopy(), fileName)); DownloadFile(c, remoteFile, cC, wC, replace, silentDownload); } })); }
/// <summary> /// Gets the mapped file paths from the given directory, using the project filtering /// The result is saved in the files List /// </summary> /// <param name="files">The list of mapped files</param> /// <param name="prj">The current project</param> /// <param name="dir">The mapped path directory</param> private void GetPaths(ref List <MappedPath> files, DirectoryInfo dirInfo, Project prj, MappedPath dirMap) { String servercopy = prj.Data.ServerCopy, fileInServerCopy; foreach (var file in dirInfo.GetFiles()) { //Files must exist in server copy and shouldn't be already added fileInServerCopy = file.FullName.Replace(MappingUtils.ValidatePath(prj.Data.ProjectCopy, true), MappingUtils.ValidatePath(prj.Data.ServerCopy, true)) + ".copy"; if (File.Exists(fileInServerCopy) && files.Count(x => file.FullName == x.GetFullServerCopy()) == 0) { files.Add(SftpUtils.GetMappedPath(file, dirMap.ProjectCopy, dirMap.RemotePath, dirMap.ServerCopy)); } } foreach (var dir in dirInfo.GetDirectories()) { GetPaths(ref files, dir, prj, dirMap); } }
/// <summary> /// Adds a file or a directory to the server /// </summary> /// <param name="prj">The current project</param> /// <param name="localPath">The local path</param> /// <param name="remotePath">The remote path</param> private void AddToServer(Project prj, string localPath, string remotePath) { String prjPath = Environment.CurrentDirectory; String configFile = Path.Combine(prjPath, ".ssh", "config.json"); localPath = Path.Combine(prj.Data.ProjectCopy, localPath); remotePath = Path.Combine(prj.Connection.Data.RootDir, remotePath); if (File.Exists(localPath)) //is a file { var mapped = prj.Data.Map.Files.FirstOrDefault(x => x.ProjectCopy == localPath); if (mapped == null) { // File.Copy (localPath, localPath.Replace (prj.Data.ProjectCopy, prj.Data.ServerCopy), true); MappedPath newMap = new MappedPath() { ProjectCopy = localPath, ServerCopy = localPath.Replace(prj.Data.ProjectCopy, prj.Data.ServerCopy), RemotePath = remotePath, RemoteVersion = DateTime.Now, LocalVersion = DateTime.Now }; SftpUtils.UploadFiles(new MappedPath[] { newMap }, prj, true); prj.Data.Map.Files = prj.Data.Map.Files.Union(new MappedPath[] { newMap }).ToArray(); prj.SaveProject(configFile); Console.WriteLine(String.Format(MSG_INF_MAP_CREATED, localPath, remotePath)); } else { throw new Exception(String.Format(MSG_ERR_MAP_AlREADY_MAPPED, localPath)); } } else if (Directory.Exists(localPath)) { var mapped = prj.Data.Map.Files.FirstOrDefault(x => x.ProjectCopy == localPath); if (mapped == null) { var files = prj.Filter.FilesInDirectory(localPath); foreach (String file in files) { File.Copy(file, file.Replace(prj.Data.ProjectCopy, prj.Data.ServerCopy + ".copy")); } SftpUtils.UploadFiles(files.Select(x => new MappedPath() { ProjectCopy = x, ServerCopy = x.Replace(prj.Data.ProjectCopy, prj.Data.ServerCopy), RemotePath = remotePath + "/" + x.Substring(x.LastIndexOf('\\')), RemoteVersion = DateTime.Now, LocalVersion = DateTime.Now } ), prj); MappedPath newMap = new MappedPath() { ProjectCopy = localPath, ServerCopy = localPath.Replace(prj.Data.ProjectCopy, prj.Data.ServerCopy), RemotePath = remotePath, RemoteVersion = DateTime.Now, LocalVersion = DateTime.Now }; prj.Data.Map.Directories = prj.Data.Map.Directories.Union(new MappedPath[] { newMap }).ToArray(); prj.SaveProject(configFile); Console.WriteLine(String.Format(MSG_INF_MAP_CREATED, localPath, remotePath)); } } else { throw new Exception(MSG_ERR_PRJ_BAD_LOC_PTH); } }
/// <summary> /// Maps a file absolute to the project file, /// using as base path the project path /// </summary> /// <param name="prj">The active project</param> /// <param name="localPath">The relative path using simple slash as separator</param> private void MapFileAbsolutePath(Project prj, string localFilePath, string remoteFilePath) { MappedPath path = MappingUtils.GetMappedPath(prj, localFilePath, remoteFilePath); this.MapFile(prj, path); }