Skip to content

Commit

Permalink
並列処理の実装
Browse files Browse the repository at this point in the history
refs #10
  • Loading branch information
love2hina-net committed Dec 18, 2021
1 parent a1397c8 commit 9cbb279
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 69 deletions.
10 changes: 1 addition & 9 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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()
}

Expand All @@ -41,18 +67,18 @@ 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()

# ドキュメントを生成する
$this.bookDocument = $this.excel.Workbooks.Add($global:const.xlWorksheet)
$this.MakeDocument()

# 保存する
$this.bookDocument.SaveAs($fileExcel)
$this.bookDocument.SaveAs($script:FileExcel)
$this.bookDocument.Close($false)
}

Expand Down Expand Up @@ -228,3 +254,9 @@ class DocumentGenerator: System.IDisposable {
}

}

Write-Debug "[DocumentGenerator] $FileTemplate, $FileXml, $FileDocument"

$generator = [DocumentGenerator]::new()
$generator.GenerateDocument()
$generator.Dispose()
98 changes: 43 additions & 55 deletions src/main/powershell/ganan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -63,64 +39,76 @@ 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)"))
}

# 生成対象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' `
-ArgumentList ("$($global:messages.E005003) Path:$i"))
}
}

$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()

0 comments on commit 9cbb279

Please sign in to comment.