1+ Param (
2+ [Parameter (Mandatory = $true )][string ] $SourcesDirectory , # Directory where source files live; if using a Localize directory it should live in here
3+ [string ] $LanguageSet = ' VS_Main_Languages' , # Language set to be used in the LocProject.json
4+ [switch ] $UseCheckedInLocProjectJson , # When set, generates a LocProject.json and compares it to one that already exists in the repo; otherwise just generates one
5+ [switch ] $CreateNeutralXlfs # Creates neutral xlf files. Only set to false when running locally
6+ )
7+
8+ # Generates LocProject.json files for the OneLocBuild task. OneLocBuildTask is described here:
9+ # https://ceapex.visualstudio.com/CEINTL/_wiki/wikis/CEINTL.wiki/107/Localization-with-OneLocBuild-Task
10+
11+ Set-StrictMode - Version 2.0
12+ $ErrorActionPreference = " Stop"
13+ . $PSScriptRoot \tools.ps1
14+
15+ Import-Module - Name (Join-Path $PSScriptRoot ' native\CommonLibrary.psm1' )
16+
17+ $exclusionsFilePath = " $SourcesDirectory \Localize\LocExclusions.json"
18+ $exclusions = @ { Exclusions = @ () }
19+ if (Test-Path - Path $exclusionsFilePath )
20+ {
21+ $exclusions = Get-Content " $exclusionsFilePath " | ConvertFrom-Json
22+ }
23+
24+ Push-Location " $SourcesDirectory " # push location for Resolve-Path -Relative to work
25+
26+ # Template files
27+ $jsonFiles = @ ()
28+ $jsonFiles += Get-ChildItem - Recurse - Path " $SourcesDirectory " | Where-Object { $_.FullName -Match " \.template\.config\\localize\\en\..+\.json" } # .NET templating pattern
29+ $jsonFiles += Get-ChildItem - Recurse - Path " $SourcesDirectory " | Where-Object { $_.FullName -Match " en\\strings\.json" } # current winforms pattern
30+
31+ $xlfFiles = @ ()
32+
33+ $allXlfFiles = Get-ChildItem - Recurse - Path " $SourcesDirectory \*\*.xlf"
34+ $langXlfFiles = @ ()
35+ if ($allXlfFiles ) {
36+ $null = $allXlfFiles [0 ].FullName -Match " \.([\w-]+)\.xlf" # matches '[langcode].xlf'
37+ $firstLangCode = $Matches.1
38+ $langXlfFiles = Get-ChildItem - Recurse - Path " $SourcesDirectory \*\*.$firstLangCode .xlf"
39+ }
40+ $langXlfFiles | ForEach-Object {
41+ $null = $_.Name -Match " (.+)\.[\w-]+\.xlf" # matches '[filename].[langcode].xlf'
42+
43+ $destinationFile = " $ ( $_.Directory.FullName ) \$ ( $Matches.1 ) .xlf"
44+ $xlfFiles += Copy-Item " $ ( $_.FullName ) " - Destination $destinationFile - PassThru
45+ }
46+
47+ $locFiles = $jsonFiles + $xlfFiles
48+
49+ $locJson = @ {
50+ Projects = @ (
51+ @ {
52+ LanguageSet = $LanguageSet
53+ LocItems = @ (
54+ $locFiles | ForEach-Object {
55+ $outputPath = " $ ( ($_.DirectoryName | Resolve-Path - Relative) + " \" ) "
56+ $continue = $true
57+ foreach ($exclusion in $exclusions.Exclusions ) {
58+ if ($outputPath.Contains ($exclusion ))
59+ {
60+ $continue = $false
61+ }
62+ }
63+ $sourceFile = ($_.FullName | Resolve-Path - Relative)
64+ if (! $CreateNeutralXlfs -and $_.Extension -eq ' .xlf' ) {
65+ Remove-Item - Path $sourceFile
66+ }
67+ if ($continue )
68+ {
69+ if ($_.Directory.Name -eq ' en' -and $_.Extension -eq ' .json' ) {
70+ return @ {
71+ SourceFile = $sourceFile
72+ CopyOption = " LangIDOnPath"
73+ OutputPath = " $ ( $_.Directory.Parent.FullName | Resolve-Path - Relative) \"
74+ }
75+ }
76+ else {
77+ return @ {
78+ SourceFile = $sourceFile
79+ CopyOption = " LangIDOnName"
80+ OutputPath = $outputPath
81+ }
82+ }
83+ }
84+ }
85+ )
86+ }
87+ )
88+ }
89+
90+ $json = ConvertTo-Json $locJson - Depth 5
91+ Write-Host " LocProject.json generated:`n`n $json `n`n "
92+ Pop-Location
93+
94+ if (! $UseCheckedInLocProjectJson ) {
95+ New-Item " $SourcesDirectory \Localize\LocProject.json" - Force # Need this to make sure the Localize directory is created
96+ Set-Content " $SourcesDirectory \Localize\LocProject.json" $json
97+ }
98+ else {
99+ New-Item " $SourcesDirectory \Localize\LocProject-generated.json" - Force # Need this to make sure the Localize directory is created
100+ Set-Content " $SourcesDirectory \Localize\LocProject-generated.json" $json
101+
102+ if ((Get-FileHash " $SourcesDirectory \Localize\LocProject-generated.json" ).Hash -ne (Get-FileHash " $SourcesDirectory \Localize\LocProject.json" ).Hash) {
103+ Write-PipelineTelemetryError - Category " OneLocBuild" - Message " Existing LocProject.json differs from generated LocProject.json. Download LocProject-generated.json and compare them."
104+
105+ exit 1
106+ }
107+ else {
108+ Write-Host " Generated LocProject.json and current LocProject.json are identical."
109+ }
110+ }
0 commit comments