private static void RemoveDirectoryEvenIfNotEmpty(FtpConnection ftpConnection, string startDirectory) { if (!startDirectory.EndsWith("/")) startDirectory += "/"; List<string> directories = new List<string>(); directories.Add(startDirectory); int index = 0; while (index < directories.Count) { string currentDirectory = directories[index]; directories.AddRange(ftpConnection.GetDirectories(currentDirectory).Select(dir => currentDirectory + dir.Name + "/")); index++; } directories.Reverse(); List<string> fileNames = new List<string>(); foreach (var directory in directories) { foreach (var ftpFile in ftpConnection.GetFiles(directory)) { string fullPath = directory + ftpFile.Name; if (ftpConnection.FileExists(fullPath)) ftpConnection.RemoveFile(fullPath); } foreach (var ftpDir in ftpConnection.GetDirectories(directory)) { string fullPath = directory + ftpDir.Name; if (ftpConnection.DirectoryExists(fullPath)) ftpConnection.RemoveDirectory(fullPath); } } if (startDirectory != "./" && ftpConnection.DirectoryExists(startDirectory)) ftpConnection.RemoveDirectory(startDirectory); }
public void RecursiveDeleteFTP(string path, bool RemFromLog, ref FtpConnection ftpcon) { ftpcon.SetCurrentDirectory(path); foreach (FtpFileInfo fi in ftpcon.GetFiles()) { string fpath = string.Format("{0}/{1}", path, fi.Name); ftpcon.RemoveFile(fpath); Log.Write("Gon'delete: {0}", fpath); if (RemFromLog) RemoveFromLog(fpath); } foreach (FtpDirectoryInfo di in ftpcon.GetDirectories()) { if (di.Name != "." && di.Name != "..") { string fpath = string.Format("{0}/{1}", noSlashes(path), di.Name); Log.Write("Gon'delete files in: {0}", fpath); RecursiveDeleteFTP(fpath, RemFromLog, ref ftpcon); } } ftpcon.RemoveDirectory(path); if (RemFromLog) RemoveFromLog(path); }
public void PublishToGitFTP(DeploymentModel model) { if (model.GitDeployment) { Logger.Debug("Executing git add"); var addProcess = new Process(); var addProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" add -A" }; addProcess.StartInfo = addProcessStartInfo; addProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data); addProcess.ErrorDataReceived += (sender, args) => Logger.Debug(args.Data); addProcess.Start(); addProcess.BeginOutputReadLine(); addProcess.BeginErrorReadLine(); addProcess.WaitForExit(); Logger.Debug("git add process to exited"); Logger.Debug("Executing git email config process"); var emailProcess = new Process(); var emailProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" config user.email \"[email protected]\"" }; emailProcess.StartInfo = emailProcessStartInfo; emailProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data); emailProcess.ErrorDataReceived += (sender, args) => Logger.Debug(args.Data); emailProcess.Start(); emailProcess.BeginOutputReadLine(); emailProcess.BeginErrorReadLine(); emailProcess.WaitForExit(); Logger.Debug("git email config process to exited"); Logger.Debug("Executing git name config process"); var userProcess = new Process(); var userProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" config user.name \"barbato\"" }; userProcess.StartInfo = userProcessStartInfo; userProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data); userProcess.ErrorDataReceived += (sender, args) => Logger.Debug(args.Data); userProcess.Start(); userProcess.BeginOutputReadLine(); userProcess.BeginErrorReadLine(); userProcess.WaitForExit(); Logger.Debug("git name config process to exited"); Logger.Debug("Executing git commit"); var commitProcess = new Process(); var commitProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" commit -a -m \"Static Content Regenerated\"" }; commitProcess.StartInfo = commitProcessStartInfo; commitProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data); commitProcess.ErrorDataReceived += (sender, args) => Logger.Debug(args.Data); commitProcess.Start(); commitProcess.BeginOutputReadLine(); commitProcess.BeginErrorReadLine(); commitProcess.WaitForExit(); Logger.Debug("git commit process to exited"); Logger.Debug("Executing git push"); var pushProcess = new Process(); var pushProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, Arguments = " --git-dir=\"" + fullPublishGitPath + "\" push -f origin master" }; pushProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data); pushProcess.ErrorDataReceived += (sender, args) => Logger.Debug(args.Data); pushProcess.StartInfo = pushProcessStartInfo; pushProcess.Start(); pushProcess.BeginOutputReadLine(); pushProcess.BeginErrorReadLine(); pushProcess.WaitForExit(); Logger.Debug("git push process to exited"); } else { using (ftp = new FtpConnection(model.FTPServer, model.FTPUsername, model.FTPPassword)) { try { ftp.Open(); ftp.Login(); if (!string.IsNullOrWhiteSpace(model.FTPPath)) { var parrentDirectory = String.Format("/{0}", Path.GetDirectoryName(model.FTPPath).Replace(Path.DirectorySeparatorChar, '/')); /* Get name of the directory */ var checkingDirectory = String.Format("{0}", Path.GetFileName(model.FTPPath)).ToLower(); /* Get all child directories info of the parent directory */ var ftpDirectories = ftp.GetDirectories(parrentDirectory); /* check if the given directory exists in the returned result */ var exists = ftpDirectories.Any(d => d.Name.ToLower() == checkingDirectory); if (!exists) { ftp.CreateDirectory(model.FTPPath); } ftp.SetCurrentDirectory(model.FTPPath); } FtpBlogFiles(publishGitPath, model.FTPPath); } catch (Exception ex) { } } } }