using InfosysPublisher.Classes; using Newtonsoft.Json; using Renci.SshNet; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Runtime.InteropServices.ComTypes; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; using Path = System.IO.Path; namespace InfosysPublisher { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { #region Classes public class Project { public string Name { get; set; } public string Path { get; set; } } #endregion private Settings.Application? _application; private string _projectPath; private List _projects; private Project? _selectedProject = null; private string _selectedProjectSubPath = ""; private string _selectedProjectPublishLocation = ""; private string _selectedProjectVersion = ""; private const string SftpArchivePath = "Archive"; public MainWindow() { InitializeComponent(); var version = typeof(App).Assembly.GetName().Version; this.Title += " " + version; _application = WinSettings.GetSettings(); TbProjectsPath.LostFocus += TbProjectsPath_LostFocus; CbProjects.SelectionChanged += CbProjects_SelectionChanged; BtnPublish.Click += BtnPublish_Click; BtnSettings.Click += BtnSettings_Click; Closing += MainWindow_Closing; if (_application == null) { OpenSettings(); return; } TbProjectsPath.Text = _application.LastFolder; LoadProjects(); } private void BtnSettings_Click(object sender, RoutedEventArgs e) { OpenSettings(); } private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e) { var settings = WinSettings.GetSettings(); if (settings == null) return; settings.LastFolder = TbProjectsPath.Text; WinSettings.SaveSettings(settings); } private void OpenSettings() { var win = new WinSettings(); win.ShowDialog(); if (win.DialogResult != null && (bool)win.DialogResult) { _application = WinSettings.GetSettings(); } } private void CbProjects_SelectionChanged(object sender, SelectionChangedEventArgs e) { _selectedProject = (Project)CbProjects.SelectedItem; LoadProject(); } private void TbProjectsPath_LostFocus(object sender, RoutedEventArgs e) { LoadProjects(); } private void LoadProjects() { _projectPath = TbProjectsPath.Text; if (!Directory.Exists(_projectPath)) { MessageBox.Show($"Pot {_projectPath} ne obstaja!"); return; } _projects = new List(); var dirInfo = new DirectoryInfo(_projectPath); dirInfo.GetDirectories() .ToList() .ForEach(directoryInfo => directoryInfo.GetFiles("*.sln") .ToList() .ForEach(x => _projects.Add(new Project { Name = $"{directoryInfo.Name}/{x.Name}", Path = x.FullName }))); CbProjects.ItemsSource = _projects; } private void LoadProject() { TbOutput.Text = ""; LblProjectInfo.Content = ""; BtnPublish.IsEnabled = false; if (_selectedProject == null) return; _selectedProjectVersion = ""; _selectedProjectSubPath = ""; _selectedProjectPublishLocation = ""; var lines = File.ReadLines(_selectedProject.Path); lines.Where(line => line.StartsWith("Project")) .ToList() .ForEach(line => { var projectTitle = line.Split(",")[1].Replace(" ", "").Replace("\"", "").Split(@"\")[0]; var projectSubPath = Path.Combine(new FileInfo(_selectedProject.Path)?.DirectoryName ?? "", projectTitle); var projectFileInfos = new DirectoryInfo(projectSubPath) .GetFiles("*.cs") .Where(x => x.Name is "App.xaml.cs" or "Program.cs") .ToList(); if (projectFileInfos.Count <= 0) return; var csLines = File.ReadLines(projectFileInfos[0].FullName).ToList(); csLines.Where(csLine => csLine.StartsWith("[assembly: AssemblyVersion(")) .ToList() .ForEach(csLine => { _selectedProjectVersion = csLine.Replace("[assembly: AssemblyVersion(\"", "").Replace("\")]", ""); _selectedProjectSubPath = projectSubPath; System.Diagnostics.Debug.WriteLine(_selectedProjectVersion); }); if (_selectedProjectSubPath != "") { csLines.Where(csLine => csLine.StartsWith("//Publish location:")) .ToList() .ForEach(csLine => { _selectedProjectPublishLocation = csLine.Replace("//Publish location:", ""); System.Diagnostics.Debug.WriteLine(_selectedProjectPublishLocation); }); } }); if (_selectedProjectVersion == "" || _selectedProjectSubPath == "" || _selectedProjectPublishLocation == "") return; LblProjectInfo.Content = $"Verzija: {_selectedProjectVersion} Pot: {_selectedProjectPublishLocation}"; BtnPublish.IsEnabled = true; } private async void BtnPublish_Click(object sender, RoutedEventArgs e) { if (_selectedProject == null || _application == null) return; var releaseFolder = Path.Combine(_selectedProjectSubPath, @"bin\Release"); if (!Directory.Exists(releaseFolder)) { MessageBox.Show($"Mapa {releaseFolder} ne obstaja!"); return; } GridProgress.Visibility = Visibility.Visible; LblLoading.Content = $"Čiščenje mape {releaseFolder}"; await Task.Run(() => { var diReleaseFolder = new DirectoryInfo(releaseFolder); foreach (var file in diReleaseFolder.GetFiles()) { file.Delete(); } foreach (var dir in diReleaseFolder.GetDirectories()) { dir.Delete(true); } }); LblLoading.Content = $"Rebuild {_selectedProject.Path}"; var output = ""; await Task.Run(() => { var command = $"devenv {_selectedProject.Path} /Rebuild \"Release\""; var cmd = new Process(); cmd.StartInfo.FileName = "cmd.exe"; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.UseShellExecute = false; cmd.Start(); cmd.StandardInput.WriteLine(command); //cmd.StandardInput.WriteLine("echo Oscar"); cmd.StandardInput.Flush(); cmd.StandardInput.Close(); cmd.WaitForExit(_application.BuildSeconds * 1000); output = cmd.StandardOutput.ReadToEnd(); Debug.WriteLine("\n\n\n\n"); Debug.WriteLine(output); //System.Diagnostics.Process.Start("CMD.exe", ""); }); TbOutput.Text = output; var zipDirectory = new DirectoryInfo(releaseFolder).Parent?.FullName ?? ""; var zipPath = Path.Combine(zipDirectory, "Package.zip"); LblLoading.Content = $"Zip {zipPath}"; if (File.Exists(zipPath)) File.Delete(zipPath); await Task.Run(() => { ZipFile.CreateFromDirectory(releaseFolder, zipPath, CompressionLevel.Optimal, false); }); if (ChbPripraviSamoZip.IsChecked != null && (bool) ChbPripraviSamoZip.IsChecked) { Process.Start("explorer.exe", zipDirectory); } else { //InfosysUpdate //v&H6c$wTbTkgSgdWvL*8k$st3#z5X LblLoading.Content = $"Upload to SFTP"; var error = ""; await Task.Run(() => { try { using var sftpClient = new SftpClient(_application.SftpServerAddress, _application.SftpPort, _application.SftpUsername, _application.SftpPassword); sftpClient.Connect(); if (!sftpClient.Exists(_selectedProjectPublishLocation)) sftpClient.CreateDirectory(_selectedProjectPublishLocation); if (!sftpClient.Exists(SftpArchivePath)) sftpClient.CreateDirectory(SftpArchivePath); var files = sftpClient.ListDirectory(_selectedProjectPublishLocation).ToList(); var xmlVersion = ""; //arhiv foreach (var file in files) { if (!file.Name.ToLower().EndsWith(".xml")) continue; var tmpXmlFile = Path.GetTempFileName(); using (var tmpFile = File.OpenWrite(tmpXmlFile)) { sftpClient.DownloadFile(file.FullName, tmpFile); } try { var xmlContent = File.ReadAllText(tmpXmlFile); if (!string.IsNullOrEmpty(xmlContent)) { //Oddaljena verzije var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlContent); var xmlNode = xmlDocument.SelectSingleNode("/Update"); xmlVersion = xmlNode["Version"].InnerText; } } catch (Exception exception) { error += "\n\n"; error += exception.ToString(); } File.Delete(tmpXmlFile); } if (xmlVersion != "") { var projectArchive = SftpArchivePath + "/" + _selectedProjectPublishLocation; if (!sftpClient.Exists(projectArchive)) sftpClient.CreateDirectory(projectArchive); var archiveFolderWithoutIndex = projectArchive + "/" + xmlVersion.Replace(".", "_"); var archiveFolder = archiveFolderWithoutIndex; var index = 1; while (sftpClient.Exists(archiveFolder)) { archiveFolder = archiveFolderWithoutIndex + "_" + index; index++; } sftpClient.CreateDirectory(archiveFolder); foreach (var file in files) { if (!file.IsRegularFile) continue; sftpClient.Get(file.FullName).MoveTo(archiveFolder + "/" + file.Name); } } //Upload var tmpXmlFileUpload = Path.GetTempFileName(); string xml = $@" {_selectedProjectVersion} "; File.WriteAllText(tmpXmlFileUpload, xml); using (var fileStream = new FileStream(tmpXmlFileUpload, FileMode.Open)) { sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Update.xml", true); } using (var fileStream = new FileStream(zipPath, FileMode.Open)) { sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Package.zip", true); } File.Delete(tmpXmlFileUpload); sftpClient.Disconnect(); } catch (Exception ex) { error += "\n\n"; error += ex.ToString(); } }); if (error != "") { MessageBox.Show(error); } } GridProgress.Visibility = Visibility.Hidden; LblLoading.Content = ""; } } }