1+ # Copyright (c) Microsoft Corporation. All rights reserved.
2+ # Licensed under the MIT License.
3+
4+ <#
5+ . Synopsis
6+ Imports a the annotations from a source CSDL file into the target CSDL file.
7+ . Description
8+ Leverages basic XML parsing capabilities to import annotations from a source CSDL file into the target CSDL file.
9+ Will NOT handle deduplication of annotations, so if the target CSDL file already has annotations, they will be duplicated.
10+ This script is intended to be used in the context of the msgraph-metadata repository.
11+ . Example
12+ ./scripts/copy-annotations-to-csdl.ps1 -sourceCsdlPath C:/github/msgraph-metadata/schemas/annotated-v1.0-Prod.csdl -targetCsdlPath C:/github/msgraph-metadata/schemas/annotated-v1.0-Prod.csdl
13+ ./scripts/run-metadata-validation.ps1 -repoDirectory C:/github/msgraph-metadata -version "v1.0"
14+ . Example
15+ . Parameter repoDirectory
16+ Full path the the root directory of msgraph-metadata checkout.
17+ #>
18+
19+ param (
20+ [Parameter (Mandatory = $true )][string ]$sourceCsdlPath ,
21+ [Parameter (Mandatory = $true )][string ]$targetCsdlPath
22+ )
23+
24+ if (-not (Test-Path $sourceCsdlPath )) {
25+ throw " Source CSDL file does not exist: $sourceCsdlPath "
26+ }
27+ if (-not (Test-Path $targetCsdlPath )) {
28+ throw " Target CSDL file does not exist: $targetCsdlPath "
29+ }
30+ # Load the source CSDL file
31+ $sourceCsdl = [xml ](Get-Content $sourceCsdlPath )
32+ # Load the target CSDL file
33+ $targetCsdl = [xml ](Get-Content $targetCsdlPath )
34+ # load the namespaces for the source CSDL file
35+ $sourceNsMgr = New-Object System.Xml.XmlNamespaceManager($sourceCsdl.NameTable )
36+ $sourceNsMgr.AddNamespace (" edmx" , " http://docs.oasis-open.org/odata/ns/edmx" )
37+ $sourceNsMgr.AddNamespace (" edm" , " http://docs.oasis-open.org/odata/ns/edm" )
38+ # load the namespaces for the target CSDL file
39+ $targetNsMgr = New-Object System.Xml.XmlNamespaceManager($targetCsdl.NameTable )
40+ $targetNsMgr.AddNamespace (" edmx" , " http://docs.oasis-open.org/odata/ns/edmx" )
41+ $targetNsMgr.AddNamespace (" edm" , " http://docs.oasis-open.org/odata/ns/edm" )
42+ # Get the schema node from the source CSDL
43+ $sourceSchemas = $sourceCsdl.DocumentElement.SelectNodes (" //edmx:Edmx/edmx:DataServices/edm:Schema" , $sourceNsMgr )
44+ foreach ($sourceSchema in $sourceSchemas ) {
45+ # get the namespace of the source schema
46+ $sourceNamespace = $sourceSchema.GetAttribute (" Namespace" )
47+ # get the target schema node by namespace
48+ $targetSchema = $targetCsdl.DocumentElement.SelectSingleNode (" //edmx:Edmx/edmx:DataServices/edm:Schema[@Namespace='$sourceNamespace ']" , $targetNsMgr )
49+ # Get all annotations from the source CSDL
50+ $sourceAnnotations = $sourceCsdl.DocumentElement.SelectNodes (" //edmx:Edmx/edmx:DataServices/edm:Schema[@Namespace='$sourceNamespace ']/edm:Annotations" , $sourceNsMgr )
51+ # Iterate through each annotation in the source CSDL
52+ foreach ($annotations in $sourceAnnotations ) {
53+ # Create a new annotation element
54+ $newAnnotation = $targetCsdl.CreateElement (" Annotations" , $annotations.NamespaceURI )
55+ # Copy attributes from the source annotation to the new annotation
56+ foreach ($attribute in $annotations.Attributes ) {
57+ $newAnnotation.SetAttribute ($attribute.Name , $attribute.Value )
58+ }
59+ # Copy the children of the annotations element
60+ foreach ($child in $annotations.ChildNodes ) {
61+ $importedChild = $targetCsdl.ImportNode ($child , $true )
62+ $newAnnotation.AppendChild ($importedChild ) | Out-Null
63+ }
64+ # Append the new annotation to the target CSDL
65+ $targetSchema.AppendChild ($newAnnotation ) | Out-Null
66+ }
67+ }
68+ # Save the modified target CSDL file
69+ $targetCsdl.Save ($targetCsdlPath )
70+ Write-Host " Annotations copied from $sourceCsdlPath to $targetCsdlPath " - ForegroundColor Green
0 commit comments