diff --git a/.vscode/launch.json b/.vscode/launch.json index d935fa8..e78c56d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,15 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "PowerShell: Launch Ganan(Test Mode)", - "type": "PowerShell", - "request": "launch", - "script": "${workspaceFolder}/src/main/powershell/ganan.ps1", - "args": ["-Test","-Debug", "-Verbose", "-ErrorAction Break"], - "cwd": "${workspaceFolder}" - }, - { - "name": "PowerShell: Launch Ganan(Normal Mode)", + "name": "PowerShell: Launch Ganan", "type": "PowerShell", "request": "launch", "script": "${workspaceFolder}/src/main/powershell/ganan.ps1", diff --git a/src/main/powershell/DocumentGenerator.psm1 b/src/main/powershell/DocumentGenerator.ps1 similarity index 88% rename from src/main/powershell/DocumentGenerator.psm1 rename to src/main/powershell/DocumentGenerator.ps1 index 80dba62..4e28ae2 100644 --- a/src/main/powershell/DocumentGenerator.psm1 +++ b/src/main/powershell/DocumentGenerator.ps1 @@ -7,6 +7,31 @@ using namespace System.Text.RegularExpressions using namespace System.Linq using namespace System.Xml.XPath +[CmdletBinding()] +param( + [string] $FileTemplate, + [string] $FileXml, + [string] $FileDocument +) + +$global:config = @{} +# 対象となる行数 +$global:config.searchLines = 128 +# 対象となる列数 +$global:config.searchColumns = 64 + +$global:const = @{} +# xlWorksheet定数 +$global:const.xlWorksheet = -4167 +# xlShiftDown定数 +$global:const.xlShiftDown = -4121 +# xlToRight定数 +$global:const.xlToRight = -4161 + +# メッセージ定義の読み込み +& (Join-Path -Path $PSScriptRoot -ChildPath '.\Messages.ps1' -Resolve) +Import-LocalizedData -BindingVariable 'messages' -FileName 'Messages' + # ドキュメント生成 class DocumentGenerator: System.IDisposable { @@ -24,10 +49,11 @@ class DocumentGenerator: System.IDisposable { # XPath hidden [XPathNavigator] $xpath - DocumentGenerator([string] $fileTemplate) { + DocumentGenerator() { + $this.excel.Visible = $true # テンプレートを開く - $this.bookTemplate = $this.excel.Workbooks.Open($fileTemplate, 0, $true) + $this.bookTemplate = $this.excel.Workbooks.Open($script:FileTemplate, 0, $true) $this.ParseTemplate() } @@ -41,10 +67,10 @@ class DocumentGenerator: System.IDisposable { $this.excel = $null } - [void] GenerateDocument([string] $fileXml, [string] $fileExcel) { + [void] GenerateDocument() { # コード解析XMLファイルを開く - $this.xml = [System.Xml.XmlDocument](Get-Content $fileXml) + $this.xml = [System.Xml.XmlDocument](Get-Content $script:FileXml) $this.xpath = $this.xml.CreateNavigator() # ドキュメントを生成する @@ -52,7 +78,7 @@ class DocumentGenerator: System.IDisposable { $this.MakeDocument() # 保存する - $this.bookDocument.SaveAs($fileExcel) + $this.bookDocument.SaveAs($script:FileExcel) $this.bookDocument.Close($false) } @@ -228,3 +254,9 @@ class DocumentGenerator: System.IDisposable { } } + +Write-Debug "[DocumentGenerator] $FileTemplate, $FileXml, $FileDocument" + +$generator = [DocumentGenerator]::new() +$generator.GenerateDocument() +$generator.Dispose() diff --git a/src/main/powershell/ganan.ps1 b/src/main/powershell/ganan.ps1 index 351d516..3078111 100644 --- a/src/main/powershell/ganan.ps1 +++ b/src/main/powershell/ganan.ps1 @@ -7,47 +7,23 @@ .NOTES This project was released under the MIT Lincense. #> -using module '.\DocumentGenerator.psm1' - -using namespace System.IO [CmdletBinding()] param( # Sharonで出力したXMLファイルを指定します。 # ディレクトリを指定した場合、配下のXMLを対象とします。 - [Parameter(Mandatory=$true, ParameterSetName="Normal")] - [Parameter(Mandatory=$false, ParameterSetName="Test")] + [Parameter(Mandatory=$true)] [string[]] $Path, # Excelテンプレートファイルを指定します。 - [Parameter(Mandatory=$true, ParameterSetName="Normal")] - [Parameter(Mandatory=$false, ParameterSetName="Test")] + [Parameter(Mandatory=$true)] [string] $Template, # 出力先ディレクトリを指定します。 - [Parameter(Mandatory=$true, ParameterSetName="Normal")] - [Parameter(Mandatory=$false, ParameterSetName="Test")] - [string] $OutputDirectory, - - [Parameter(Mandatory=$false, ParameterSetName="Normal")] - [Parameter(Mandatory=$true, ParameterSetName="Test")] - [Switch] $Test = $false + [Parameter(Mandatory=$true)] + [string] $OutputDirectory ) -$global:config = @{} -# 対象となる行数 -$global:config.searchLines = 128 -# 対象となる列数 -$global:config.searchColumns = 64 - -$global:const = @{} -# xlWorksheet定数 -$global:const.xlWorksheet = -4167 -# xlShiftDown定数 -$global:const.xlShiftDown = -4121 -# xlToRight定数 -$global:const.xlToRight = -4161 - # メッセージ定義の読み込み & (Join-Path -Path $PSScriptRoot -ChildPath '.\Messages.ps1' -Resolve) Import-LocalizedData -BindingVariable 'messages' -FileName 'Messages' @@ -63,16 +39,20 @@ class GananApplication { # ファイルリスト [string[]] $files = @() - [void] Initialize() { + GananApplication() { + Write-Debug "[GananApplication] begin..." + + $this.template = [System.IO.Path]::GetFullPath($script:Template) + $this.outputDir = [System.IO.Path]::GetFullPath($script:OutputDirectory) # テンプレートチェック - if (![File]::Exists($script:Template)) { + if (![System.IO.File]::Exists($this.template)) { throw (New-Object -TypeName 'System.ArgumentException' ` -ArgumentList ("$($global:messages.E005001) Template:$($script:Template)")) } # 出力先ディレクトリチェック - if (![Directory]::Exists($script:OutputDirectory)) { + if (![System.IO.Directory]::Exists($this.outputDir)) { throw (New-Object -TypeName 'System.ArgumentException' ` -ArgumentList ("$($global:messages.E005002) OutputDirectory:$($script:OutputDirectory)")) } @@ -80,12 +60,14 @@ class GananApplication { # 生成対象XMLファイル列挙 $list = [System.Collections.Generic.List[string]]::new() foreach ($i in $script:Path) { - if ([File]::Exists($i)) { - [void]$list.Add($i) + $fullpath = [System.IO.Path]::GetFullPath($i) + + if ([System.IO.File]::Exists($fullpath)) { + [void]$list.Add($fullpath) } - elseif ([Directory]::Exists($i)) { + elseif ([System.IO.Directory]::Exists($fullpath)) { # ディレクトリ内のXMLファイルを対象とする - [void]$list.AddRange([Directory]::EnumerateFiles($i, '*.xml')) + [void]$list.AddRange([System.IO.Directory]::EnumerateFiles($fullpath, '*.xml')) } else { throw (New-Object -TypeName 'System.ArgumentException' ` @@ -93,34 +75,40 @@ class GananApplication { } } - $this.template = $script:Template - $this.outputDir = $script:OutputDirectory $this.files = $list.ToArray() - } - [void] GenerateDocuments() { - # TODO + Write-Debug "[GananApplication] end." } - [void] Test() { - - $projectRoot = (Convert-Path "$PSScriptRoot\\..\\..\\..") + [void] GenerateDocuments() { + Write-Debug "[GenerateDocuments] begin..." + + $jobs = $this.files | ForEach-Object { + $fileTemplate = $this.template + $fileXml = $_ + $fileDocument = [System.IO.Path]::Combine($this.outputDir, [System.IO.Path]::GetFileNameWithoutExtension($_) + ".xlsx") + + Start-ThreadJob { + # 出力設定を引き継ぐ + $ConfirmPreference = $using:ConfirmPreference + $DebugPreference = $using:DebugPreference + $VerbosePreference = $using:VerbosePreference + $WarningPreference = $using:WarningPreference + $ErrorActionPreference = $using:ErrorActionPreference + + & (Join-Path -Path $using:PSScriptRoot -ChildPath '.\DocumentGenerator.ps1' -Resolve) -FileTemplate "$using:fileTemplate" -FileXml "$using:fileXml" -FileDocument "$using:fileDocument" + } + } - $generator = [DocumentGenerator]::new("$projectRoot\\template\\test.xlsm") - $generator.excel.Visible = $true + do { + Receive-Job -Job $jobs + } while ($null -eq (Wait-Job -Job $jobs -Timeout 1)) + Receive-Job -Job $jobs - $generator.GenerateDocument("$projectRoot\\test.xml", "$projectRoot\\test\\test.xlsx") - $generator.Dispose() + Write-Debug "[GenerateDocuments] end." } } $app = [GananApplication]::new() - -if ($Test) { - Write-Host 'テストモード' - #$app.Test() -} -else { - $app.Initialize() -} +$app.GenerateDocuments()