This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProjectInstaller.cs
207 lines (174 loc) · 5.96 KB
/
ProjectInstaller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Authors:
* 钟峰(Popeye Zhong) <[email protected]>
*
* Copyright (C) 2013 Zongsoft Corporation <http://www.zongsoft.com>
*
* This file is part of Zongsoft.Daemon.Launcher.
*
* Zongsoft.Daemon.Launcher is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Zongsoft.Daemon.Launcher is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Zongsoft.Daemon.Launcher; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Text;
using Zongsoft.Plugins;
using Zongsoft.Services;
namespace Zongsoft.Daemon.Launcher
{
[RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer
{
#region 构造函数
public ProjectInstaller()
{
InitializeComponent();
}
#endregion
#region 初始化器
private void InitializeComponent()
{
this.Installers.Add(new ServiceProcessInstaller()
{
Account = ServiceAccount.LocalSystem,
Password = null,
Username = null,
});
var installers = GetServiceInstallers();
if(installers != null && installers.Length > 0)
this.Installers.AddRange(installers);
}
#endregion
#region 内部方法
internal ServiceInstaller[] GetServiceInstallers()
{
string servicesPath = this.GetServicesPath();
if(string.IsNullOrWhiteSpace(servicesPath))
servicesPath = @"/Workbench/Startup";
else
{
if(servicesPath[0] != '/')
servicesPath = '/' + servicesPath;
}
//创建服务安装器列表
List<ServiceInstaller> installers = new List<ServiceInstaller>();
//确保插件树被加载
ApplicationContext.Current.PluginContext.PluginTree.Load();
//获取服务挂载的根节点
var node = ApplicationContext.Current.PluginContext.PluginTree.Find(servicesPath);
if(node == null)
{
string message = Zongsoft.Resources.ResourceUtility.GetString("Text.InstallFailed.Message", this.GetType().Assembly, ApplicationContext.Current.PluginContext.Settings.PluginsDirectory, servicesPath);
this.WriteLog(message);
}
else
{
//从挂载在启动路径下的工作者并将其装包在服务包装器中
this.GetServiceInstallers(installers, node);
}
return installers.ToArray();
}
#endregion
#region 私有方法
private void WriteLog(string message)
{
if(string.IsNullOrEmpty(message))
return;
var directoryPath = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);
var filePath = System.IO.Path.Combine(directoryPath, "Install.log");
using(var file = new System.IO.FileStream(filePath, System.IO.FileMode.Append, System.IO.FileAccess.Write))
{
var data = Encoding.UTF8.GetBytes(message + Environment.NewLine);
file.Write(data, 0, data.Length);
}
}
private string GetServicesPath()
{
var args = Environment.GetCommandLineArgs();
foreach(string arg in args)
{
if(arg.Length > 0 && (arg[0] == '/' || arg[0] == '-'))
{
var parts = arg.Split('=');
if(parts.Length == 0)
parts = arg.Split(':');
if(parts.Length == 2)
{
string name = parts[0].Substring(1);
if(string.Equals(name, "ServicesPath", StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, "Path", StringComparison.OrdinalIgnoreCase))
{
return parts[1];
}
}
}
}
return string.Empty;
}
private void GetServiceInstallers(IList<ServiceInstaller> installers, PluginTreeNode node)
{
if(installers == null || node == null)
return;
if(node.NodeType != PluginTreeNodeType.Empty)
{
Type serviceType = null;
bool disabled = false;
string serviceName = node.Name;
if(node.NodeType == PluginTreeNodeType.Custom)
{
var worker = node.UnwrapValue<Zongsoft.Services.IWorker>(ObtainMode.Auto, this, null);
if(worker != null)
{
serviceName = worker.Name;
serviceType = worker.GetType();
disabled = !worker.Enabled;
}
}
else
{
if(typeof(IWorker).IsAssignableFrom(node.ValueType))
serviceType = node.ValueType;
disabled = ((Builtin)node.Value).Properties.GetValue<bool>("disabled", false);
}
if(serviceType != null)
{
ServiceInstaller installer = new ServiceInstaller();
installer.DelayedAutoStart = false;
installer.ServiceName = serviceName;
installer.StartType = (disabled ? ServiceStartMode.Disabled : ServiceStartMode.Automatic);
//设置安装服务的描述文本
var descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(serviceType, typeof(DescriptionAttribute));
if(descriptionAttribute != null)
installer.Description = Resources.ResourceUtility.GetString(descriptionAttribute.Description, serviceType.Assembly);
//设置安装服务的显示名称
var displayAttribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(serviceType, typeof(DisplayNameAttribute));
if(displayAttribute != null)
installer.DisplayName = Resources.ResourceUtility.GetString(displayAttribute.DisplayName, serviceType.Assembly) + " (" + serviceName + ")";
installers.Add(installer);
}
}
foreach(PluginTreeNode child in node.Children)
this.GetServiceInstallers(installers, child);
}
#endregion
}
}