-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathCompileChm.ps1
203 lines (178 loc) · 3.78 KB
/
CompileChm.ps1
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
#CompileChm.ps1
Param(
[Parameter(Mandatory)]
[string]$HtmlHelpProject,
[Parameter(Mandatory)]
[string]$Destination
)
# ファイル存在チェック
# Path Is Existing と読めるように作成
function Is-Existing
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Path
)
return Test-Path -Path $Path
}
# ファイル不存在チェック
# Path Is Missing と読めるように作成
function Is-Missing
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Path
)
return -not($Path | Is-Existing)
}
# ファイルロックチェック
# Path Is Locked と読めるように作成
function Is-Locked
{
[CmdletBinding()]
Param(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Path
)
if ($Path | Is-Missing) {
return $false
}
try
{
$File = New-Object System.IO.FileInfo $Path
$Stream = $File.Open(
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::None)
return $false
}
catch [System.IO.IOException]
{
echo "file is locked by a process."
return $true
}
finally
{
if ($Stream -ne $null)
{
$Stream.Close()
}
}
}
# コンパイル済みHTMLのコピー処理
# Azure Pipelinesがたまにコピー失敗する対策として作成
function Copy-Chm
{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[string]$Destination
)
if ($Path | Is-Missing)
{
return $false
}
if ($Destination.EndsWith([System.IO.Path]::DirectorySeparatorChar))
{
if ($Destination | Is-Missing)
{
New-Item -Path $Destination -ItemType Directory
}
$Destination = [System.IO.Path]::Combine($Destination, [System.IO.Path]::GetFileName($Path))
}
if ($Path -like $Destination)
{
echo "Copy is not required."
return $true
}
try
{
echo "`$CompiledHelp is: $Path"
echo "`$Destination is: $Destination"
Copy-Item -Path $Path -Destination $Destination
}
catch #[System.IO.IOException]
{
echo "Copy Chm error [$($PSItem.Exception)]: $($PSItem.ToString())"
return $false
}
return $true
}
$HtmlHelpProject = Convert-Path $HtmlHelpProject
if (-not($HtmlHelpProject -imatch '\.hhp$'))
{
throw [System.ArgumentException]::new("Bad filename of `$HtmlHelpProject: $HtmlHelpProject")
}
if ([string]::IsNullOrEmpty($env:CMD_HHC) -or ($env:CMD_HHC | Is-Missing))
{
$env:CMD_HHC = 'C:\Program Files (x86)\HTML Help Workshop\hhc.exe'
}
$hhc = $env:CMD_HHC -replace '(.+)', '""$1""'
$CompiledHelp = $HtmlHelpProject -ireplace '\.hhp$', '.chm'
$CompileLog = "$([System.IO.Path]::GetDirectoryName($HtmlHelpProject))\Compile.Log"
if ($CompileLog | Is-Existing)
{
rm $CompileLog
}
if ($CompiledHelp | Is-Existing)
{
rm $CompiledHelp
}
if ($Destination | Is-Existing)
{
rm $Destination
}
while ($true)
{
try
{
if ([string]::IsNullOrEmpty($env:CMD_LEPROC))
{
#kick cmd.exe for a legacy command.
Start-Process $env:CMD_HHC $HtmlHelpProject
}
else
{
#kick LEProc.exe for a legacy command.
Start-Process $env:CMD_LEPROC "$env:COMSPEC /C `"$hhc $HtmlHelpProject`""
}
#wait for complete
for ($count = 0; $count -lt 30; $count++)
{
if ($CompiledHelp | Is-Missing)
{
echo "`$CompiledHelp is missing: $CompiledHelp"
}
elseif ($CompiledHelp | Is-Locked)
{
echo "`$CompiledHelp is still locked: $CompiledHelp"
}
elseif ($CompileLog | Is-Missing)
{
echo "`$CompileLog is missing: $CompileLog"
}
elseif ($CompileLog | Is-Locked)
{
echo "`$CompileLog is still locked: $CompileLog"
}
elseif (Copy-Chm $CompiledHelp $Destination)
{
return
}
else
{
echo "Copy `$CompiledHelp has failed"
break
}
Start-Sleep -Second 1
}
}
catch [System.AccessViolationException]
{
echo "LEProc.exe has crashed"
}
}