2021-03-18 13:00:01 +00:00
Param (
[ Parameter ( Mandatory = $true ) ] [ string ] $SourcesDirectory , # Directory where source files live; if using a Localize directory it should live in here
[ string ] $LanguageSet = 'VS_Main_Languages' , # Language set to be used in the LocProject.json
[ switch ] $UseCheckedInLocProjectJson , # When set, generates a LocProject.json and compares it to one that already exists in the repo; otherwise just generates one
[ switch ] $CreateNeutralXlfs # Creates neutral xlf files. Only set to false when running locally
)
# Generates LocProject.json files for the OneLocBuild task. OneLocBuildTask is described here:
# https://ceapex.visualstudio.com/CEINTL/_wiki/wikis/CEINTL.wiki/107/Localization-with-OneLocBuild-Task
Set-StrictMode -Version 2.0
$ErrorActionPreference = " Stop "
2022-07-07 12:38:55 +00:00
. $PSScriptRoot \ pipeline-logging -functions . ps1
2021-03-18 13:00:01 +00:00
2021-05-22 12:59:44 +00:00
$exclusionsFilePath = " $SourcesDirectory \eng\Localize\LocExclusions.json "
2021-03-18 13:00:01 +00:00
$exclusions = @ { Exclusions = @ ( ) }
if ( Test-Path -Path $exclusionsFilePath )
{
$exclusions = Get-Content " $exclusionsFilePath " | ConvertFrom-Json
}
Push-Location " $SourcesDirectory " # push location for Resolve-Path -Relative to work
# Template files
$jsonFiles = @ ( )
2021-06-20 13:07:26 +00:00
$jsonTemplateFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory " | Where-Object { $_ . FullName -Match " \.template\.config\\localize\\.+\.en\.json " } # .NET templating pattern
$jsonTemplateFiles | ForEach-Object {
$null = $_ . Name -Match " (.+)\.[\w-]+\.json " # matches '[filename].[langcode].json
2022-07-07 12:38:55 +00:00
2021-06-20 13:07:26 +00:00
$destinationFile = " $( $_ . Directory . FullName ) \ $( $Matches . 1 ) .json "
$jsonFiles + = Copy-Item " $( $_ . FullName ) " -Destination $destinationFile -PassThru
}
$jsonWinformsTemplateFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory " | Where-Object { $_ . FullName -Match " en\\strings\.json " } # current winforms pattern
2021-03-18 13:00:01 +00:00
2022-08-17 12:42:25 +00:00
$wxlFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory " | Where-Object { $_ . FullName -Match " \\.+\.wxl " -And -Not ( $_ . Directory . Name -Match " \d{4} " ) } # localized files live in four digit lang ID directories; this excludes them
2023-01-18 10:18:03 +00:00
if ( -not $wxlFiles ) {
$wxlEnFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory " | Where-Object { $_ . FullName -Match " \\1033\\.+\.wxl " } # pick up en files (1033 = en) specifically so we can copy them to use as the neutral xlf files
if ( $wxlEnFiles ) {
$wxlFiles = @ ( )
$wxlEnFiles | ForEach-Object {
$destinationFile = " $( $_ . Directory . Parent . FullName ) \ $( $_ . Name ) "
$wxlFiles + = Copy-Item " $( $_ . FullName ) " -Destination $destinationFile -PassThru
}
}
}
$macosHtmlEnFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory " | Where-Object { $_ . FullName -Match " en\.lproj\\.+\.html " } # add installer HTML files
$macosHtmlFiles = @ ( )
if ( $macosHtmlEnFiles ) {
$macosHtmlEnFiles | ForEach-Object {
$destinationFile = " $( $_ . Directory . Parent . FullName ) \ $( $_ . Name ) "
$macosHtmlFiles + = Copy-Item " $( $_ . FullName ) " -Destination $destinationFile -PassThru
}
}
2022-08-17 12:42:25 +00:00
2021-03-18 13:00:01 +00:00
$xlfFiles = @ ( )
$allXlfFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory \*\*.xlf "
$langXlfFiles = @ ( )
if ( $allXlfFiles ) {
$null = $allXlfFiles [ 0 ] . FullName -Match " \.([\w-]+)\.xlf " # matches '[langcode].xlf'
$firstLangCode = $Matches . 1
$langXlfFiles = Get-ChildItem -Recurse -Path " $SourcesDirectory \*\*. $firstLangCode .xlf "
}
$langXlfFiles | ForEach-Object {
2021-03-26 13:54:15 +00:00
$null = $_ . Name -Match " (.+)\.[\w-]+\.xlf " # matches '[filename].[langcode].xlf
2022-07-07 12:38:55 +00:00
2021-03-18 13:00:01 +00:00
$destinationFile = " $( $_ . Directory . FullName ) \ $( $Matches . 1 ) .xlf "
$xlfFiles + = Copy-Item " $( $_ . FullName ) " -Destination $destinationFile -PassThru
}
2021-06-20 13:07:26 +00:00
$locFiles = $jsonFiles + $jsonWinformsTemplateFiles + $xlfFiles
2021-03-18 13:00:01 +00:00
$locJson = @ {
Projects = @ (
@ {
LanguageSet = $LanguageSet
LocItems = @ (
$locFiles | ForEach-Object {
2022-07-07 12:38:55 +00:00
$outputPath = " $( ( $_ . DirectoryName | Resolve-Path -Relative ) + " \ " ) "
2021-03-18 13:00:01 +00:00
$continue = $true
foreach ( $exclusion in $exclusions . Exclusions ) {
2022-08-25 18:01:42 +00:00
if ( $_ . FullName . Contains ( $exclusion ) )
2021-03-18 13:00:01 +00:00
{
$continue = $false
}
}
$sourceFile = ( $_ . FullName | Resolve-Path -Relative )
if ( ! $CreateNeutralXlfs -and $_ . Extension -eq '.xlf' ) {
Remove-Item -Path $sourceFile
}
if ( $continue )
{
2021-04-20 13:10:00 +00:00
if ( $_ . Directory . Name -eq 'en' -and $_ . Extension -eq '.json' ) {
return @ {
SourceFile = $sourceFile
CopyOption = " LangIDOnPath "
OutputPath = " $( $_ . Directory . Parent . FullName | Resolve-Path -Relative ) \ "
}
2022-08-17 12:42:25 +00:00
} else {
2021-04-20 13:10:00 +00:00
return @ {
SourceFile = $sourceFile
CopyOption = " LangIDOnName "
OutputPath = $outputPath
}
2021-03-18 13:00:01 +00:00
}
}
}
)
2022-08-17 12:42:25 +00:00
} ,
@ {
2022-09-13 12:25:17 +00:00
LanguageSet = $LanguageSet
2022-08-17 12:42:25 +00:00
CloneLanguageSet = " WiX_CloneLanguages "
LssFiles = @ ( " wxl_loc.lss " )
LocItems = @ (
$wxlFiles | ForEach-Object {
$outputPath = " $( $_ . Directory . FullName | Resolve-Path -Relative ) \ "
$continue = $true
foreach ( $exclusion in $exclusions . Exclusions ) {
2023-01-18 10:18:03 +00:00
if ( $_ . FullName . Contains ( $exclusion ) ) {
2022-08-17 12:42:25 +00:00
$continue = $false
}
}
$sourceFile = ( $_ . FullName | Resolve-Path -Relative )
if ( $continue )
{
return @ {
SourceFile = $sourceFile
CopyOption = " LangIDOnPath "
OutputPath = $outputPath
}
}
}
)
2023-01-18 10:18:03 +00:00
} ,
@ {
LanguageSet = $LanguageSet
CloneLanguageSet = " VS_macOS_CloneLanguages "
LocItems = @ (
$macosHtmlFiles | ForEach-Object {
$outputPath = " $( $_ . Directory . FullName | Resolve-Path -Relative ) \ "
$continue = $true
foreach ( $exclusion in $exclusions . Exclusions ) {
if ( $_ . FullName . Contains ( $exclusion ) ) {
$continue = $false
}
}
$sourceFile = ( $_ . FullName | Resolve-Path -Relative )
if ( $continue ) {
return @ {
SourceFile = $sourceFile
CopyOption = " LangIDOnPath "
OutputPath = $outputPath
}
}
}
)
2021-03-18 13:00:01 +00:00
}
)
}
$json = ConvertTo-Json $locJson -Depth 5
2021-03-25 18:13:25 +00:00
Write-Host " LocProject.json generated: `n `n $json `n `n "
2021-03-18 13:00:01 +00:00
Pop-Location
if ( ! $UseCheckedInLocProjectJson ) {
2021-05-22 12:59:44 +00:00
New-Item " $SourcesDirectory \eng\Localize\LocProject.json " -Force # Need this to make sure the Localize directory is created
Set-Content " $SourcesDirectory \eng\Localize\LocProject.json " $json
2021-03-18 13:00:01 +00:00
}
else {
2021-05-22 12:59:44 +00:00
New-Item " $SourcesDirectory \eng\Localize\LocProject-generated.json " -Force # Need this to make sure the Localize directory is created
Set-Content " $SourcesDirectory \eng\Localize\LocProject-generated.json " $json
2021-03-18 13:00:01 +00:00
2021-05-22 12:59:44 +00:00
if ( ( Get-FileHash " $SourcesDirectory \eng\Localize\LocProject-generated.json " ) . Hash -ne ( Get-FileHash " $SourcesDirectory \eng\Localize\LocProject.json " ) . Hash ) {
2021-03-25 18:13:25 +00:00
Write-PipelineTelemetryError -Category " OneLocBuild " -Message " Existing LocProject.json differs from generated LocProject.json. Download LocProject-generated.json and compare them. "
2022-07-07 12:38:55 +00:00
2021-03-18 13:00:01 +00:00
exit 1
}
else {
Write-Host " Generated LocProject.json and current LocProject.json are identical. "
}
2022-07-07 12:38:55 +00:00
}