• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # Copyright (c) 2025 Huawei Device Co., Ltd.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 
14 # This script will replace the old paths with the new ones in all files within the testcases directory.
15 
16 $ErrorActionPreference = "Stop"
17 
18 $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
19 
20 # Check if the 'ets' directory exists in ScriptDir
21 if (-not (Test-Path -Path "$ScriptDir\ets" -PathType Container)) {
22     Write-Error "Error: 'ets' directory not found in $ScriptDir."
23     Write-Error "Please make sure the 'ets' directory exists before running bindings test."
24     exit 1
25 }
26 
27 $RestoreMode = 0
28 if ($args.Count -gt 0 -and $args[0] -eq "--restore") {
29     $RestoreMode = 1
30 }
31 
32 $OldPath = "path/to/bindings/test"
33 
34 $TestcasesDir = "$ScriptDir\testcases"
35 
36 if ($RestoreMode -eq 1) {
37     Write-Host "Restoring path '$ScriptDir' back to '$OldPath' in files..."
38 }
39 else {
40     Write-Host "Replacing path '$OldPath' with '$ScriptDir' in files..."
41 }
42 
Process-Directorynull43 function Process-Directory {
44     param (
45         [string] $directory
46     )
47 
48     if (-not (Test-Path -Path $directory -PathType Container)) {
49         Write-Host "Directory $directory does not exist. Skipping."
50         return
51     }
52 
53     Write-Host "Processing directory: $directory"
54 
55     $jsonFiles = Get-ChildItem -Path $directory -Filter "*.json" -File -Recurse
56 
57     foreach ($file in $jsonFiles) {
58         Write-Host "Processing file: $($file.FullName)"
59         $content = Get-Content -Path $file.FullName -Raw
60 
61         $scriptDirJson = $ScriptDir -replace '\\', '/'
62 
63         if ($RestoreMode -eq 1) {
64             $newContent = $content -replace [regex]::Escape($scriptDirJson), $OldPath
65         }
66         else {
67             $newContent = $content -replace [regex]::Escape($OldPath), $scriptDirJson
68         }
69 
70         Set-Content -Path $file.FullName -Value $newContent -NoNewline
71     }
72 }
73 
74 Process-Directory -directory $TestcasesDir
75 
76 if ($RestoreMode -eq 1) {
77     if (Test-Path -Path "$ScriptDir\..\ets2panda") {
78         Remove-Item -Path "$ScriptDir\..\ets2panda" -Recurse -Force
79         Write-Host "Removed '$ScriptDir\..\ets2panda' directory."
80     }
81     Write-Host "Path restoration completed."
82 }
83 else {
84     $sourceDir = "$ScriptDir\ets\ets1.2\build-tools\ets2panda"
85     $destinationDir = "$ScriptDir\..\ets2panda"
86 
87     # Check if source directory exists
88     if (-not (Test-Path -Path $sourceDir -PathType Container)) {
89         Write-Error "Source directory '$sourceDir' does not exist."
90         exit 1
91     }
92 
93     # Remove destination directory if it exists
94     if (Test-Path -Path $destinationDir) {
95         Remove-Item -Path $destinationDir -Recurse -Force
96     }
97 
98     # Copy directory
99     try {
100         Copy-Item -Path $sourceDir -Destination $destinationDir -Recurse -Force
101         Write-Host "Directory copied successfully from '$sourceDir' to '$destinationDir'."
102         Write-Host "Path replacement completed."
103     }
104     catch {
105         Write-Error "Failed to copy directory."
106         Write-Error $_.Exception.Message
107         exit 1
108     }
109 }
110 
111 exit 0