public static void Clear(this SftpClient client, string path) { foreach (var file in client.ListDirectory(path)) { if (file.Name == "." || file.Name == "..") { continue; } if (file.IsDirectory) { client.Clear($"{path}/{file.Name}"); } file.Delete(); } }
public int StartBuild(IVsOutputWindowPane outputPane, uint dwOptions) { var buildHost = this[MonoPropertyPage.BuildHostProperty]; var dteProject = GetDTEProject(project); var projectFolder = Path.GetDirectoryName(dteProject.FullName); outputPane.Log($"Starting build of {projectFolder}..."); // If using Windows Bash... if (string.IsNullOrEmpty(buildHost)) { var bash = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Sysnative", "bash.exe"); if (!File.Exists(bash)) { // outputPane.LogError(dteProject.FullName, $"Error: You must set up a build server on the 'Mono' project property page."); outputPane.Log(VsLogSeverity.Error, dteProject.UniqueName, dteProject.FullName, "Error: You must set up a build server on the 'Mono' project property page."); UpdateBuildStatus(0); return(VSConstants.S_FALSE); } var bashProjectFolder = ConvertToUnixPath(projectFolder); var outputFile = Path.GetTempFileName(); var script = $@"cd ""{bashProjectFolder}"" xbuild > ""{ConvertToUnixPath(outputFile)}"" exit ".Replace("\r\n", "\n"); var scriptFile = Path.GetTempFileName(); var arguments = $"/C {bash} --init-file {ConvertToUnixPath(scriptFile)}"; File.WriteAllText(scriptFile, script); var process = new System.Diagnostics.Process { StartInfo = new ProcessStartInfo("CMD", arguments) { WindowStyle = ProcessWindowStyle.Hidden } }; process.Start(); process.WaitForExit(); outputPane.Log(File.ReadAllText(outputFile)); try { File.Delete(scriptFile); File.Delete(outputFile); } catch (Exception e) { outputPane.Log(e.ToString()); } } else { outputPane.Log("Uploading project to the build server..."); var buildUsername = this[MonoPropertyPage.BuildUsernameProperty]; var buildPassword = this[MonoPropertyPage.BuildPasswordProperty]; var buildFolder = this[MonoPropertyPage.BuildFolderProperty]; using (var client = new SftpClient(buildHost, buildUsername, buildPassword)) { client.Connect(); client.CreateFullDirectory(buildFolder); client.ChangeDirectory(buildFolder); outputPane.Log($"Clearing out the contents of the build folder: {buildFolder}"); client.Clear(); // Upload project var createdDirectories = new HashSet <string>(); var projectFile = Path.GetFileName(dteProject.FullName); client.Upload(projectFolder, projectFile, createdDirectories); outputPane.Log($"Uploaded {projectFile}"); var projectItems = new Queue <ProjectItem>(dteProject.ProjectItems.Cast <ProjectItem>()); while (projectItems.Any()) { var projectItem = projectItems.Dequeue(); for (short i = 1; i <= projectItem.FileCount; i++) { var fileName = projectItem.FileNames[i]; if (File.Exists(fileName)) { fileName = FileUtils.ToRelativePath(projectFolder, fileName); client.Upload(projectFolder, fileName, createdDirectories); outputPane.Log($"Uploaded {fileName}"); } } foreach (ProjectItem childItem in projectItem.ProjectItems) { projectItems.Enqueue(childItem); } } using (var ssh = new SshClient(buildHost, buildUsername, buildPassword)) { outputPane.Log("Starting xbuild to build the project"); ssh.Connect(); var exitCode = ssh.RunCommand($@"cd {buildFolder}; xbuild /p:Configuration={dteProject.ConfigurationManager.ActiveConfiguration.ConfigurationName} > xbuild.output; exitcode=$?; cat xbuild.output; rm xbuild.output; exit ""$exitcode""", outputPane, dteProject.UniqueName); if (exitCode != 0) { UpdateBuildStatus(0); return(VSConstants.S_FALSE); } } var projectConfiguration = dteProject.ConfigurationManager.ActiveConfiguration; var outputFolder = Path.GetDirectoryName(Path.Combine(projectFolder, projectConfiguration.Properties.Item("OutputPath").Value.ToString())); outputPane.Log($"Copying build artifacts to the output folder: {outputFolder}"); var buildServerOutputPath = buildFolder + "/" + FileUtils.ToRelativePath(projectFolder, outputFolder).Replace("\\", "/"); client.CreateFullDirectory(buildServerOutputPath); client.ChangeDirectory(buildServerOutputPath); foreach (var file in client.ListDirectory(".").Where(x => x.IsRegularFile)) { using (var output = new FileStream(Path.Combine(outputFolder, file.Name), FileMode.Create, FileAccess.Write)) { client.DownloadFile(file.FullName, output); outputPane.Log($"Copied {file.Name}"); } } client.Disconnect(); } } UpdateBuildStatus(1); return(VSConstants.S_OK); }