-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartupLoader.ms
274 lines (246 loc) · 8.83 KB
/
StartupLoader.ms
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*! © 2022 imaoki | MIT License | https://github.com/imaoki */
/*- @var <Struct:StartupLoaderStruct> */
global startupLoader
/*-
スタートアップスクリプトを管理する。
*/
struct StartupLoaderStruct (
/*- @prop <BooleanClass> シャットダウンプロセスが開始されたかどうか。既定値は`false`。 */
private hasSystemShutdownStarted = false,
/*- @prop <Array[<BooleanClass>]> スクリプトファイルをインストーラとして評価するかどうか。既定値は`#()`。 */
private installerFlags = #(),
/*- @prop <Array[<String>]> 登録スクリプトファイル。既定値は`#()`。 */
private scriptFiles = #(),
/*- @prop <String> スタートアップファイルのパス。既定値は`""`。 */
private startupFilePath = "",
/*- @prop <DotNetClass:System.DateTime> */
private dateTimeClass = DotNetClass "System.DateTime",
/*- @prop <DotNetClass:System.IO.File> */
private fileClass = DotNetClass "System.IO.File",
/*- @prop <String> 自己のインスタンスを格納するグローバル変数の名前。既定値は`"startupLoader"`。 */
private globalVarName = "startupLoader",
/*- @prop <DotNetClass:System.IO.Path> */
private pathClass = DotNetClass "System.IO.Path",
/*- @prop <DotNetObject:System.Text.UTF8Encoding> */
private utf8withoutBOM = DotNetObject "System.Text.UTF8Encoding" false,
/*
public fn GetInstallerFlags = (),
public fn GetScriptFiles = (),
public fn GetStartupFilePath = (),
public fn Install = (),
public fn OnPreSystemShutdown = (),
public fn RegisterFile scriptFilePath installer:false = (),
public fn Uninstall = (),
public fn UnregisterFile scriptFilePath = (),
public fn Write = (),
private fn deleteStartupFile = (),
private fn existsStartupFile = (),
private fn initializeStartupFilePath = (),
private fn registerPreSystemShutdown = (),
private fn unregisterPreSystemShutdown = (),
*/
/*-
スクリプトファイルをインストーラとして評価するかどうかを取得する。
@returns <Array[<BooleanClass>]>
*/
public fn GetInstallerFlags = (
this.installerFlags
),
/*-
登録スクリプトファイルを取得する。
@returns <Array[<String>]>
*/
public fn GetScriptFiles = (
this.scriptFiles
),
/*-
スタートアップファイルのパスを取得する。
@returns <Array[<String>]>
*/
public fn GetStartupFilePath = (
this.startupFilePath
),
/*-
インストールする。
@returns <OkClass>
@remarks 静的メソッド。
*/
public fn Install = (
::StartupLoaderStruct()
format "[install]::startupLoader\n"
ok
),
/*-
#PreSystemShutdownコールバックによって呼び出されるメソッド。
*/
public fn OnPreSystemShutdown = (
this.Write()
this.hasSystemShutdownStarted = true
ok
),
/*-
スクリプトファイルを登録する。
@param scriptFilePath <String>
@param installer: <BooleanClass> ファイルをインストーラとして評価する場合は`true`、通常のスクリプトとして評価する場合は`false`。既定値は`false`。
@returns <BooleanClass> 成功した場合(既に登録済みの場合も)は`true`、失敗した場合は`false`。
@remarks 登録したスクリプトファイルをインストーラとして評価するには以下の条件を満たす必要がある。
* スクリプトファイルの評価結果が構造体定義であること
* 引数を持たない静的メソッド`Install`を持っていること
*/
public fn RegisterFile scriptFilePath installer:false = (
if findItem this.scriptFiles scriptFilePath == 0 do (
append this.installerFlags installer
append this.scriptFiles scriptFilePath
)
findItem this.scriptFiles scriptFilePath > 0
),
/*-
アンインストールする。
@returns <OkClass>
*/
public fn Uninstall = (
this.unregisterPreSystemShutdown()
this.deleteStartupFile()
::startupLoader = undefined
format "[uninstall]::startupLoader\n"
ok
),
/*-
スクリプトファイルを登録解除する。
@param scriptFilePath <String>
@returns <BooleanClass> 成功した場合(元から登録されていない場合も)は`true`、失敗した場合は`false`。
@remarks ツールの終了処理によって状態が変更されることを防ぐため、シャットダウンプロセス中は処理を行わない。
*/
public fn UnregisterFile scriptFilePath = (
if not this.hasSystemShutdownStarted do (
local index = findItem this.scriptFiles scriptFilePath
if index > 0 do (
deleteItem this.installerFlags index
deleteItem this.scriptFiles index
)
)
findItem this.scriptFiles scriptFilePath == 0
),
/*-
内部状態をスタートアップファイルに書き出す。
@returns <OkClass>
*/
public fn Write = (
if not this.hasSystemShutdownStarted do (
local content = StringStream ""
local now = this.dateTimeClass.Now.ToString "yyyy/MM/dd HH:mm:ss K"
format "/* Generated by StartupLoader % at % */\n" this.version now to:content
format "fileIn @\"%\"\n" (getSourceFileName()) to:content
for i = 1 to this.scriptFiles.Count do (
local scriptFilePath = this.scriptFiles[i]
if this.installerFlags[i] then (
format "(fileIn @\"%\").Install()\n" scriptFilePath to:content
)
else (
format "fileIn @\"%\"\n" scriptFilePath to:content
)
)
format "::%.Write()\n" this.globalVarName to:content
content = content as String
this.fileClass.WriteAllText this.startupFilePath content this.utf8withoutBOM
format "[write]@\"%\"\n" this.startupFilePath
)
ok
),
/*-
スタートアップファイルを削除する。
@returns <BooleanClass> 成功した場合は`true`、失敗した場合は`false`。
*/
private fn deleteStartupFile = (
local result = not this.existsStartupFile()
if not result do (
this.fileClass.Delete this.startupFilePath
result = not this.existsStartupFile()
)
if result do (
format "[delete]@\"%\"\n" this.startupFilePath
)
result
),
/*-
スタートアップファイルが存在するかどうかを判定する。
@returns <BooleanClass>
*/
private fn existsStartupFile = (
this.fileClass.Exists this.startupFilePath
),
/*-
スタートアップファイルのファイル名を初期化する。
@returns <OkClass>
*/
private fn initializeStartupFilePath = (
local userStartupScriptsDir = SymbolicPaths.GetPathValue "$UserStartupScripts"
this.startupFilePath = userStartupScriptsDir + @"\StartupLoader.ms"
ok
),
/*-
#PreSystemShutdownコールバックを登録する。
@returns <OkClass>
*/
private fn registerPreSystemShutdown = (
Callbacks.AddScript #PreSystemShutdown this.OnPreSystemShutdown id:#StartupLoader
ok
),
/*-
#PreSystemShutdownコールバックを登録解除する。
@returns <OkClass>
*/
private fn unregisterPreSystemShutdown = (
Callbacks.RemoveScripts #PreSystemShutdown id:#StartupLoader
ok
),
/*- @prop <Array[<Integer>]> バージョン番号。メジャー、マイナー、パッチの順。 */
private version = #(1, 3, 0),
/*- @returns <Name> */
public fn StructName = #StartupLoaderStruct,
/*-
@param indent: <String>
@param out: <FileStream|StringStream|WindowStream> 出力先。既定値は`listener`。
@returns <OkClass>
*/
public fn Dump indent:"" out:listener = (
format "%StartupLoaderStruct.Dump\n" indent to:out
format "% globalVarName:\"%\"\n" indent this.globalVarName to:out
format "% hasSystemShutdownStarted:%\n" indent this.hasSystemShutdownStarted to:out
format "% startupFilePath:@\"%\"\n" indent this.startupFilePath to:out
format "% installerFlags:%\n" indent this.installerFlags.Count to:out
format "% scriptFiles:%\n" indent this.scriptFiles.Count to:out
for i = 1 to this.scriptFiles.Count do (
format "% [%]:\n" indent i to:out
format "% installer:%\n" indent this.installerFlags[i] to:out
format "% path:@\"%\"\n" indent this.scriptFiles[i] to:out
)
ok
),
/*-
@param obj <Any>
@returns <BooleanClass>
@remarks 大文字と小文字を区別する。
*/
public fn Equals obj = (
local isEqualStructName = isStruct obj \
and isProperty obj #StructName \
and classOf obj.StructName == MAXScriptFunction \
and obj.StructName() == this.StructName()
local isEqualProperties = true
isEqualStructName and isEqualProperties
),
/*-
@returns <Array[<Integer>]>
*/
public fn GetVersion = (
this.version
),
on Create do (
::startupLoader = this
this.initializeStartupFilePath()
this.registerPreSystemShutdown()
)
)
::StartupLoaderStruct.Install()
::StartupLoaderStruct