nove verzije

This commit is contained in:
David Štaleker
2024-08-02 11:00:59 +02:00
parent edab522e0e
commit ca229fbd89
8 changed files with 491 additions and 157 deletions

View File

@@ -21,6 +21,7 @@ using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using Microsoft.Data.SqlClient;
using Path = System.IO.Path;
namespace InfosysPublisher
@@ -28,7 +29,7 @@ namespace InfosysPublisher
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public partial class WinMain : Window
{
#region Classes
@@ -39,25 +40,24 @@ namespace InfosysPublisher
}
#endregion
private Settings.Application? _application;
private string _projectPath;
private List<Project> _projects;
private Project? _selectedProject = null;
private string _selectedProjectSubPath = "";
private string _selectedProjectPublishLocation = "";
private string _selectedProjectVersion = "";
private string _selectedProjectGuid = "";
private const string SftpArchivePath = "Archive";
public MainWindow()
public WinMain()
{
InitializeComponent();
var version = typeof(App).Assembly.GetName().Version;
this.Title += " " + version;
_application = WinSettings.GetSettings();
App._application = WinSettings.GetSettings();
TbProjectsPath.LostFocus += TbProjectsPath_LostFocus;
CbProjects.SelectionChanged += CbProjects_SelectionChanged;
@@ -66,20 +66,14 @@ namespace InfosysPublisher
BtnSettings.Click += BtnSettings_Click;
Closing += MainWindow_Closing;
if (_application == null)
{
OpenSettings();
return;
}
TbProjectsPath.Text = _application.LastFolder;
TbProjectsPath.Text = App._application.LastFolder;
LoadProjects();
}
private void BtnSettings_Click(object sender, RoutedEventArgs e)
{
OpenSettings();
App.OpenSettings();
}
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
@@ -91,15 +85,7 @@ namespace InfosysPublisher
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)
{
@@ -162,43 +148,105 @@ namespace InfosysPublisher
.Where(x => x.Name is "App.xaml.cs" or "Program.cs")
.ToList();
if (projectFileInfos.Count <= 0)
{
projectFileInfos = new DirectoryInfo(projectSubPath)
.GetDirectories("Properties")
.SelectMany(x => x.GetFiles("AssemblyInfo.cs"))
.ToList();
};
if (projectFileInfos.Count <= 0) return;
var csLines = File.ReadLines(projectFileInfos[0].FullName).ToList();
var tmpSelectedProjectVersion = "";
var tmpSelectedProjectSubPath = "";
var tmpSelectedProjectPublishLocation = "";
var tmpSelectedProjectGuid = "";
csLines.Where(csLine => csLine.StartsWith("[assembly: AssemblyVersion("))
.ToList()
.ForEach(csLine =>
{
_selectedProjectVersion = csLine.Replace("[assembly: AssemblyVersion(\"", "").Replace("\")]", "");
_selectedProjectSubPath = projectSubPath;
tmpSelectedProjectVersion = csLine.Replace("[assembly: AssemblyVersion(\"", "").Replace("\")]", "");
tmpSelectedProjectSubPath = projectSubPath;
System.Diagnostics.Debug.WriteLine(_selectedProjectVersion);
System.Diagnostics.Debug.WriteLine(tmpSelectedProjectVersion);
});
if (_selectedProjectSubPath != "")
if (tmpSelectedProjectSubPath != "")
{
csLines.Where(csLine => csLine.StartsWith("//Publish location:"))
.ToList()
.ForEach(csLine =>
{
_selectedProjectPublishLocation = csLine.Replace("//Publish location:", "");
tmpSelectedProjectPublishLocation = csLine.Replace("//Publish location:", "");
System.Diagnostics.Debug.WriteLine(_selectedProjectPublishLocation);
System.Diagnostics.Debug.WriteLine(tmpSelectedProjectPublishLocation);
});
//Guid za paket vnos dela, tiste ko ga imajo v program cd
csLines.Where(csLine => csLine.Contains("public static string GuidAplikacije"))
.ToList()
.ForEach(csLine =>
{
tmpSelectedProjectGuid = csLine.Split('"')[1];
System.Diagnostics.Debug.WriteLine(tmpSelectedProjectGuid);
});
}
if (tmpSelectedProjectVersion == ""
|| tmpSelectedProjectSubPath == ""
|| tmpSelectedProjectPublishLocation == "") return;
//ce se ni guidja pogledam v nlog
if (tmpSelectedProjectGuid == "")
{
var projectNlogConfig= new DirectoryInfo(projectSubPath)
.GetFiles("NLog.config")
.ToList();
if (projectFileInfos.Count <= 0) return;
try
{
var xmlDocument = new XmlDocument();
xmlDocument.Load(projectNlogConfig[0].FullName);
var node = xmlDocument
.GetElementsByTagName("variable")
.Cast<XmlNode>()
.FirstOrDefault(x => x.Attributes != null
&& x.Attributes.Cast<XmlAttribute>().Any(y => y.Name == "name" && y.Value == "AplikacijaGuid"));
if (node != null)
tmpSelectedProjectGuid = node.Attributes["value"].Value;
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
if (tmpSelectedProjectGuid == "") return;
_selectedProjectVersion = tmpSelectedProjectVersion;
_selectedProjectSubPath = tmpSelectedProjectSubPath;
_selectedProjectPublishLocation = tmpSelectedProjectPublishLocation;
_selectedProjectGuid = tmpSelectedProjectGuid;
});
if (_selectedProjectVersion == "" || _selectedProjectSubPath == "" || _selectedProjectPublishLocation == "")
if (_selectedProjectVersion == "" || _selectedProjectSubPath == "" || _selectedProjectPublishLocation == "" || _selectedProjectGuid == "")
return;
LblProjectInfo.Content = $"Verzija: {_selectedProjectVersion} Pot: {_selectedProjectPublishLocation}";
LblProjectInfo.Content = $"Verzija: {_selectedProjectVersion} Pot: {_selectedProjectPublishLocation} Guid: {_selectedProjectGuid}";
BtnPublish.IsEnabled = true;
}
private async void BtnPublish_Click(object sender, RoutedEventArgs e)
{
if (_selectedProject == null || _application == null)
if (_selectedProject == null || App._application == null)
return;
var releaseFolder = Path.Combine(_selectedProjectSubPath, @"bin\Release");
@@ -208,6 +256,9 @@ namespace InfosysPublisher
return;
}
TbOutput.Text = $"Publish start {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
TbOutput.Text += $"\nClean {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
GridProgress.Visibility = Visibility.Visible;
LblLoading.Content = $"Čiščenje mape {releaseFolder}";
await Task.Run(() =>
@@ -225,6 +276,7 @@ namespace InfosysPublisher
});
LblLoading.Content = $"Rebuild {_selectedProject.Path}";
TbOutput.Text += $"\nRebuild {_selectedProject.Path} {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
var output = "";
await Task.Run(() =>
{
@@ -241,17 +293,18 @@ namespace InfosysPublisher
//cmd.StandardInput.WriteLine("echo Oscar");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit(_application.BuildSeconds * 1000);
cmd.WaitForExit(App._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;
TbOutput.Text += "\n" + output;
var zipDirectory = new DirectoryInfo(releaseFolder).Parent?.FullName ?? "";
var zipPath = Path.Combine(zipDirectory, "Package.zip");
LblLoading.Content = $"Zip {zipPath}";
TbOutput.Text += $"\nZip {zipPath} {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
if (File.Exists(zipPath))
File.Delete(zipPath);
@@ -260,129 +313,189 @@ namespace InfosysPublisher
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();
UploadSftp(zipDirectory, zipPath);
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 = $@"<?xml version=""1.0""?>
<Update>
<Version>{_selectedProjectVersion}</Version>
</Update>";
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);
}
}
TbOutput.Text += $"\nVersion {_selectedProjectVersion} {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
WriteVersion();
GridProgress.Visibility = Visibility.Hidden;
LblLoading.Content = "";
TbOutput.Text += $"\nEnd {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
}
private async void UploadSftp(string zipDirectory, string zipPath)
{
if (ChbCreateOnlyZip.IsChecked != null && (bool)ChbCreateOnlyZip.IsChecked)
{
Process.Start("explorer.exe", zipDirectory);
return;
}
//InfosysUpdate
//v&H6c$wTbTkgSgdWvL*8k$st3#z5X
LblLoading.Content = $"Upload to SFTP";
TbOutput.Text += $"\nUpload to SFTP {DateTime.Now:dd.MM.yyyy HH:mm:ss}";
var error = "";
await Task.Run(() =>
{
try
{
using var sftpClient = new SftpClient(App._application.SftpServerAddress, App._application.SftpPort,
App._application.SftpUsername, App._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 = $@"<?xml version=""1.0""?>
<Update>
<Version>{_selectedProjectVersion}</Version>
</Update>";
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);
}
var sftpDirVersion = _selectedProjectPublishLocation + "/" + _selectedProjectVersion;
if (!sftpClient.Exists(sftpDirVersion))
sftpClient.CreateDirectory(sftpDirVersion);
using (var fileStream = new FileStream(zipPath, FileMode.Open))
{
sftpClient.UploadFile(fileStream, sftpDirVersion + "/" + "Package.zip",
true);
}
File.Delete(tmpXmlFileUpload);
sftpClient.Disconnect();
}
catch (Exception ex)
{
error += "\n\n";
error += ex.ToString();
}
});
if (error != "")
{
MessageBox.Show(error);
return;
}
}
private void WriteVersion()
{
using var sqlConnection = new SqlConnection(App.SqlConnectionString);
sqlConnection.Open();
var sqlCommandString = @"
DECLARE @idVerzija INT = NULL
SELECT TOP 1 @idVerzija = ID_Verzija
FROM AplikacijaVerzija
WHERE GUID_Aplikacija_FK = @guid
AND Verzija = @verzija
IF @idVerzija IS NULL
BEGIN
CREATE TABLE #tmpId(
ID INT
);
INSERT INTO AplikacijaVerzija (GUID_Aplikacija_FK, Verzija)
OUTPUT inserted.ID_Verzija INTO #tmpId
VALUES (@guid, @verzija)
SELECT TOP 1 @idVerzija = Id FROM #tmpId
END
INSERT INTO AplikacijaVerzijaRevizija (ID_AplikacijaVerzija_FK, ID_Uporabnik_FK)
VALUES (@idVerzija, @idUporabnik);";
using var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
sqlCommand.Parameters.AddWithValue("guid", _selectedProjectGuid);
sqlCommand.Parameters.AddWithValue("verzija", _selectedProjectVersion);
sqlCommand.Parameters.AddWithValue("idUporabnik", App.User.Id);
sqlCommand.ExecuteNonQuery();
}
}
}