2025-03-28 09:49:56 +08:00

524 lines
24 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace SuperMap.RealEstate.Launcher.Deploy
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Compress7z.SeventZPrcessPath = "Core";
CreateDefaultDirectory(
"Core|7z压缩工具目录",
"AppRuns|可运行的程序等同于ClickOnce",
"Install|驱动程序安装",
"AppRuns_Deploy|AppRuns的7z压缩部署",
"Install_Deploy|Install的7z压缩部署");
string AppPath = AppDomain.CurrentDomain.BaseDirectory;
//保存7z资源文件
Save7zResouceFile(AppPath, "7z.dll", "7z.exe");//, "Launcher.Core.dll", "Launcher.Install.exe");
//初始化AppRuns配置
CreateIniFile_AppRuns(AppPath);
//初始化Install配置
CreateIniFile_Install(AppPath);
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonBuild_Click(object sender, EventArgs e)
{
string AppPath = AppDomain.CurrentDomain.BaseDirectory;
//7z的发布部署
Deploy7zFileCache(AppPath, "7z.dll", "7z.exe", "Launcher.Core.dll", "Launcher.Install.exe", "Launcher.Register.exe", "Launcher.Loading.exe");
//发布AppRuns
Build_AppRuns(AppPath);
//发布Install
Build_Install(AppPath);
MessageBox.Show(this,"OK");
}
#region Build_Install
private void Build_Install(string AppPath)
{
string _KeyName = "Install";
string _AppPath = AppPath + _KeyName + "\\";
StringBuilder _StringBuilderAll = new StringBuilder();
using (StreamReader _StreamReader = File.OpenText(_AppPath + "Config.ini"))
{
string _SourcePath, _LineText, _Name, _Version, _SourceText, _DeployFile, _DeployPath;
Dictionary<string, string> _Dictionary = new Dictionary<string, string>();
while (!_StreamReader.EndOfStream)
{
_LineText = _StreamReader.ReadLine();
if (string.IsNullOrEmpty(_LineText))
continue;
//Name
if (!_LineText.StartsWith("[" + _KeyName + "]"))
continue;
StringBuilder _StringBuilder = new StringBuilder();
//_StringBuilder.AppendLine("[Launcher]");
//_StringBuilder.AppendLine("Version:" + GetFileVersion(AppPath + "Launcher.exe"));
//_StringBuilder.AppendLine("[Core]");
//_StringBuilder.AppendLine("File:Core\\7z.exe");
//_StringBuilder.AppendLine("Deploy:Core/7z.exe.Deploy");
//_StringBuilder.AppendLine("File:Core\\7z.dll");
//_StringBuilder.AppendLine("Deploy:Core/7z.dll.Deploy");
_StringBuilder.AppendLine("[" + _KeyName + "]");
//Group
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
//Name
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
_Name = _LineText.Split(':')[1].Trim();
//Desc
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
//Path
_SourcePath = _LineText = _StreamReader.ReadLine();
_SourcePath = _SourcePath.Split(':')[1];
_StringBuilder.AppendLine(_LineText);
//File
_SourceText = _LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
_SourceText = _SourceText.Split(':')[1];
//Version
_LineText = _StreamReader.ReadLine();
_Version = _LineText.Split(':')[1];
if (_Version.Equals("auto", StringComparison.CurrentCultureIgnoreCase))
_Version = GetFileVersion(_AppPath + _SourceText);
if (_Version=="无版本")
{
MessageBox.Show(this,"配置的文件无版本请修改auto为自定义版本\r\n\r\n类型" + _KeyName + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
_StringBuilder.AppendLine(_LineText.Split(':')[0] + ":" +_Version);
_DeployPath = _KeyName + "_Deploy\\" + _SourcePath + "\\" + _Version.Replace(".", "_") + "\\";
_DeployFile = _DeployPath + _SourcePath + ".7z.Deploy";
_StringBuilder.AppendLine("Deploy:" + _DeployFile.Replace("\\", "/"));
_DeployFile = AppPath + _DeployFile;
_SourcePath = _AppPath + _SourcePath + "\\";
_SourceText = _AppPath + _SourceText;
//列出所有的文件
if (!Directory.Exists(_SourcePath))
{
MessageBox.Show(this, "配置的目录不存在!\r\n\r\n类型" + _KeyName + "\r\n配置" + _Name + "\r\n目录" +
_SourcePath.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
if (!File.Exists(_SourceText))
{
MessageBox.Show(this, "配置的文件不存在!\r\n\r\n类型" + _KeyName + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
if (!Compress7z.Zip(_DeployFile, _SourcePath))
{
MessageBox.Show(this, "压缩7z文件失败\r\n\r\n类型" + _KeyName + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
//PlatformTarget
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
//Parameters
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
//下组
_StringBuilderAll.AppendLine(_StringBuilder.ToString());
//_LineText = _StreamReader.ReadLine();
}
_StreamReader.Close();
using (StreamWriter _StreamWriter = File.CreateText(Directory.GetParent(_AppPath).FullName + "_Deploy\\Install.ini.Deploy"))
{
_StreamWriter.Write(_StringBuilderAll.ToString());
_StreamWriter.Close();
}
}
//return _StringBuilder.ToString();
}
#endregion
#region Save7zResouceFile
public static void Save7zResouceFile(string Path,params string[] resourceFileList)
{
string _BaseDirectory = Path + "Core\\";
if (!Directory.Exists(_BaseDirectory))
Directory.CreateDirectory(_BaseDirectory);
foreach (string resourceFile in resourceFileList)
{
if (File.Exists(_BaseDirectory + resourceFile))
File.Delete(_BaseDirectory + resourceFile);
Stream _Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"SuperMap.RealEstate.Launcher.Deploy.Resources." + resourceFile);
byte[] _ByteCollection = new byte[_Stream.Length];
_Stream.Read(_ByteCollection, 0, _ByteCollection.Length);
FileStream _FileStream = new FileStream(_BaseDirectory + resourceFile, FileMode.OpenOrCreate, FileAccess.Write);
_FileStream.Write(_ByteCollection, 0, _ByteCollection.Length);
_FileStream.Close();
}
}
#endregion
#region CreateDefaultDirectory
void CreateDefaultDirectory(params string[] PathList)
{
String _BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string[] _KeyValue;
ListViewItem _Item;
foreach (string _Path in PathList)
{
_KeyValue = _Path.Split('|');
if (!Directory.Exists(_BaseDirectory + _KeyValue[0]))
Directory.CreateDirectory(_BaseDirectory + _KeyValue[0]);
_Item = new ListViewItem(_KeyValue[0]);
_Item.SubItems.Add(_KeyValue[1]);
listView1.Items.Add(_Item);
}
listView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
}
#endregion
#region Deploy7zFileCache
void Deploy7zFileCache(string AppPath, params string[] AppKey)
{
string _Deploy = AppPath + "Core\\";
StringBuilder _StringBuilder = new StringBuilder();
_StringBuilder.AppendLine("[Core]");
foreach (string _fileName in AppKey)
{
if (File.Exists(_Deploy + _fileName + ".Deploy"))
File.Delete(_Deploy + _fileName + ".Deploy");
File.Copy(_Deploy + _fileName, _Deploy + _fileName + ".Deploy", true);
_StringBuilder.AppendLine("File:Core\\" + _fileName);
_StringBuilder.AppendLine("Version:" + GetFileVersion("Core\\" + _fileName));
_StringBuilder.AppendLine("Deploy:Core/" + _fileName + ".Deploy");
}
_StringBuilder.AppendLine("");
using (StreamWriter _StreamWriter = File.CreateText("Core\\Version.ini.Deploy" ))
{
_StreamWriter.Write(_StringBuilder.ToString());
_StreamWriter.Close();
}
//MessageBox.Show(_StringBuilder.ToString());
}
#endregion
#region CreateIniFile_AppRuns
void CreateIniFile_AppRuns(string AppPath)
{
string _FileName;
string _KeyName = "AppRuns";
_FileName = AppPath + _KeyName + "\\Config.ini";
if (!Directory.Exists(Path.GetDirectoryName(_FileName)))
Directory.CreateDirectory(Path.GetDirectoryName(_FileName));
if (File.Exists(_FileName))
return;
StringBuilder _StringBuilder = new StringBuilder();
_StringBuilder.AppendLine("[" + _KeyName + "]");
_StringBuilder.AppendLine("Name:唯一标识");
_StringBuilder.AppendLine("Desc:中文名字");
_StringBuilder.AppendLine("Path:程序目录");
_StringBuilder.AppendLine("File:程序目录\\程序名字.exe");
_StringBuilder.AppendLine("Version:程序版本[如1.0]");
_StringBuilder.AppendLine("ServerFile:程序目录\\每次检查MD5的文件[可配置多行]");
_StringBuilder.AppendLine("ServerFile:程序目录\\每次检查MD5的文件[可配置多行]");
using (StreamWriter _StreamWriter = File.CreateText(_FileName))
{
_StreamWriter.Write(_StringBuilder.ToString());
_StreamWriter.Close();
}
}
#endregion
#region CreateIniFile_Install
void CreateIniFile_Install(string AppPath)
{
string _FileName;
string _KeyName = "Install";
_FileName = AppPath + _KeyName + "\\Config.ini";
if (!Directory.Exists(Path.GetDirectoryName(_FileName)))
Directory.CreateDirectory(Path.GetDirectoryName(_FileName));
if (File.Exists(_FileName))
return;
StringBuilder _StringBuilder = new StringBuilder();
_StringBuilder.AppendLine("[" + _KeyName + "]");
_StringBuilder.AppendLine("Group:驱动分类");
_StringBuilder.AppendLine("Name:唯一标识");
_StringBuilder.AppendLine("Desc:中文名字");
_StringBuilder.AppendLine("Path:程序目录");
_StringBuilder.AppendLine("File:程序目录\\安装程序.exe");
_StringBuilder.AppendLine("Version:程序版本[如1.0如果写为auto则取安装程序.exe的版本]");
_StringBuilder.AppendLine("PlatformTarget:运行平台[x86或x64]");
_StringBuilder.AppendLine("Parameters:启动参数[如/passive /li setup.log]");
using (StreamWriter _StreamWriter = File.CreateText(_FileName))
{
_StreamWriter.Write(_StringBuilder.ToString());
_StreamWriter.Close();
}
}
#endregion
#region Build_AppRuns
private void Build_AppRuns(string AppPath)
{
//更新AppRuns文件版本
UpdateVersion_AppRuns(AppPath);
//更新AppRuns文件缓存
DeployFileCache_AppRuns(AppPath);
}
#endregion
#region UpdateVersion_AppRuns
void UpdateVersion_AppRuns(string AppPath)
{
string _FileName;
string _KeyName = "AppRuns";
_FileName = AppPath + _KeyName + "\\Config.ini";
if (!File.Exists(_FileName))
return;
StringBuilder _StringBuilder = new StringBuilder();
using (StreamReader _StreamReader = File.OpenText(_FileName))
{
string _LineText, _LineFile;
while (!_StreamReader.EndOfStream)
{
_LineText = _StreamReader.ReadLine();
if (string.IsNullOrEmpty(_LineText))
continue;
_StringBuilder.AppendLine(_LineText);
if (_LineText.StartsWith("File:"))
{
_LineFile = AppPath + _KeyName + "\\" + _LineText.Split(':')[1];
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText.Split(':')[0] + ":" +
GetFileVersion(_LineFile));
_LineText = _StreamReader.ReadLine();
while (_LineText != null && _LineText.StartsWith("ServerFile:"))
{
_StringBuilder.AppendLine(_LineText);
_LineText = _StreamReader.ReadLine();
}
_StringBuilder.AppendLine("");
continue;
}
}
_StreamReader.Close();
}
if (File.Exists(_FileName))
File.Delete(_FileName);
using (StreamWriter _StreamWriter = File.CreateText(_FileName))
{
_StreamWriter.Write(_StringBuilder.ToString());
_StreamWriter.Close();
}
//}
}
#endregion
#region DeployFileCache_AppRuns
void DeployFileCache_AppRuns(string AppPath)
{
string typeStr = "AppRuns";
string _AppPath = AppPath + typeStr + "\\";
using (StreamReader _StreamReader = File.OpenText(_AppPath + "Config.ini"))
{
string _SourcePath, _LineText, _Name, _Version, _SourceText, _DeployFile, _DeployPath;
Dictionary<string, string> _Dictionary = new Dictionary<string, string>();
while (!_StreamReader.EndOfStream)
{
_LineText = _StreamReader.ReadLine();
if (string.IsNullOrEmpty(_LineText))
continue;
//Name
if (!_LineText.StartsWith("Name:"))
continue;
StringBuilder _StringBuilder = new StringBuilder();
//_StringBuilder.AppendLine("[Launcher]");
//_StringBuilder.AppendLine("Version:" + GetFileVersion(AppPath + "Launcher.exe"));
//_StringBuilder.AppendLine("[Core]");
//_StringBuilder.AppendLine("File:Core\\7z.exe");
//_StringBuilder.AppendLine("Deploy:Core/7z.exe.Deploy");
//_StringBuilder.AppendLine("File:Core\\7z.dll");
//_StringBuilder.AppendLine("Deploy:Core/7z.dll.Deploy");
_StringBuilder.AppendLine("[" + typeStr + "]");
_StringBuilder.AppendLine(_LineText);
_Name = _LineText.Split(':')[1].Trim();
//Desc
_LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
//Path
_SourcePath = _LineText = _StreamReader.ReadLine();
_SourcePath = _SourcePath.Split(':')[1];
_StringBuilder.AppendLine(_LineText);
//File
_SourceText = _LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
_SourceText = _SourceText.Split(':')[1];
//Version
_Version = _LineText = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineText);
_Version = _Version.Split(':')[1];
_DeployPath = typeStr + "_Deploy\\" + _SourcePath + "\\" + _Version.Replace(".", "_") + "\\";
_DeployFile = _DeployPath + _SourcePath + ".7z.Deploy";
_StringBuilder.AppendLine("Deploy:" + _DeployFile.Replace("\\", "/"));
_DeployFile = AppPath + _DeployFile;
_SourcePath = _AppPath + _SourcePath + "\\";
_SourceText = _AppPath + _SourceText;
//列出所有的文件
if (!Directory.Exists(_SourcePath))
{
MessageBox.Show(this, "配置目录不存在!\r\n\r\n类型" + typeStr + "\r\n配置" + _Name + "\r\n目录" +
_SourcePath.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
if (!File.Exists(_SourceText))
{
MessageBox.Show(this, "配置文件不存在!\r\n\r\n类型" + typeStr + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
if (!Compress7z.Zip(_DeployFile, _SourcePath))
{
MessageBox.Show(this, "压缩7z文件失败\r\n\r\n类型" + typeStr + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
_LineText = _StreamReader.ReadLine();
_Dictionary.Clear();
while (_LineText != null && _LineText.StartsWith("ServerFile:"))
{
_LineText = _LineText.Split(':')[1];
_DeployFile = _AppPath + _LineText;
if (!File.Exists(_DeployFile))
{
MessageBox.Show(this, "配置Update的文件不存在\r\n\r\n类型" + typeStr + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (!_Dictionary.ContainsKey(_LineText))
{
_Dictionary.Add(_LineText, GetMD5HashFromFile(_DeployFile));
}
}
_LineText = _StreamReader.ReadLine();
}
if (_Dictionary.Count > 0)
{
_StringBuilder.AppendLine("[ServerFileCollection]");
int _Index = 0;
foreach (KeyValuePair<string, string> _KeyValue in _Dictionary)
{
_Index++;
_LineText = "ServerFile_" + _Index.ToString().PadLeft(2, '0') + ".7z.Deploy";
_StringBuilder.AppendLine(_KeyValue.Key + ":" + _KeyValue.Value + ":" +
(_DeployPath + _LineText).Replace("\\", "/"));
_DeployFile = AppPath + _DeployPath + _LineText;
if (!Compress7z.Zip(_DeployFile, _AppPath + _KeyValue.Key))
{
MessageBox.Show(this, "压缩7z更新包文件失败\r\n\r\n类型" + typeStr + "\r\n配置" + _Name + "\r\n文件" +
_SourceText.Replace(_AppPath, ""), "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
using (StreamWriter _StreamWriter = File.CreateText(Directory.GetParent(_AppPath).FullName + "_Deploy\\" + _Name + ".ini.Deploy"))
{
_StreamWriter.Write(_StringBuilder.ToString());
_StreamWriter.Close();
}
}
_StreamReader.Close();
}
//return _StringBuilder.ToString();
}
#endregion
#region GetFileVersion
private string GetFileVersion(string filePath)
{
if (!File.Exists(filePath))
return "不存在";
FileVersionInfo _FileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
if (String.IsNullOrEmpty(_FileVersionInfo.FileVersion))
return "无版本";
else
return _FileVersionInfo.FileVersion.Split(' ')[0];
}
#endregion
#region GetMD5HashFromFile
private string GetMD5HashFromFile(string fileName)
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
}
}
#endregion
private void buttonAppRuns_Click(object sender, EventArgs e)
{
using (frmAppRunsConfig _frmAppRuns=new frmAppRunsConfig())
{
this.Visible = false;
_frmAppRuns.ShowDialog();
this.Visible = true;
}
}
}
}