99 Will NOT handle deduplication of annotations, so if the target CSDL file already has annotations, they will be duplicated.
1010 This script is intended to be used in the context of the msgraph-metadata repository.
1111. 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
12+ ./scripts/copy-annotations-to-csdl.ps1 -sourceCsdlDirectoryPath C:/github/msgraph-metadata/schemas/annotated-v1.0-Prod.csdl -targetCsdlPath C:/github/msgraph-metadata/schemas/annotated-v1.0-Prod.csdl
1313 ./scripts/run-metadata-validation.ps1 -repoDirectory C:/github/msgraph-metadata -version "v1.0"
1414. Example
1515. Parameter repoDirectory
1616 Full path the the root directory of msgraph-metadata checkout.
1717#>
1818
1919param (
20- [Parameter (Mandatory = $true )][string ]$sourceCsdlPath ,
20+ [Parameter (Mandatory = $true )][string ]$sourceCsdlDirectoryPath ,
2121 [Parameter (Mandatory = $true )][string ]$targetCsdlPath
2222)
2323
24- if (-not (Test-Path $sourceCsdlPath )) {
25- throw " Source CSDL file does not exist: $sourceCsdlPath "
24+ if (-not (Test-Path $sourceCsdlDirectoryPath )) {
25+ throw " Source CSDL directory does not exist: $sourceCsdlDirectoryPath "
2626}
2727if (-not (Test-Path $targetCsdlPath )) {
2828 throw " Target CSDL file does not exist: $targetCsdlPath "
2929}
30- # Load the source CSDL file
31- $sourceCsdl = [xml ](Get-Content $sourceCsdlPath )
30+ $sourceCsdlPaths = Get-ChildItem - Path $sourceCsdlDirectoryPath - Filter " *.csdl" - File
31+ if ($sourceCsdlPaths.Count -eq 0 ) {
32+ throw " No CSDL files found in the source directory: $sourceCsdlDirectoryPath "
33+ }
3234# Load the target CSDL file
3335$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" )
3836# load the namespaces for the target CSDL file
3937$targetNsMgr = New-Object System.Xml.XmlNamespaceManager($targetCsdl.NameTable )
4038$targetNsMgr.AddNamespace (" edmx" , " http://docs.oasis-open.org/odata/ns/edmx" )
4139$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
40+
41+ foreach ($sourceCsdlPath in $sourceCsdlPaths ) {
42+ if (-not (Test-Path $sourceCsdlPath.FullName )) {
43+ throw " Source CSDL file does not exist: $ ( $sourceCsdlPath.FullName ) "
44+ }
45+ # Load the source CSDL file
46+ $sourceCsdl = [xml ](Get-Content $sourceCsdlPath.FullName )
47+ # load the namespaces for the source CSDL file
48+ $sourceNsMgr = New-Object System.Xml.XmlNamespaceManager($sourceCsdl.NameTable )
49+ $sourceNsMgr.AddNamespace (" edmx" , " http://docs.oasis-open.org/odata/ns/edmx" )
50+ $sourceNsMgr.AddNamespace (" edm" , " http://docs.oasis-open.org/odata/ns/edm" )
51+ # Get the schema node from the source CSDL
52+ $sourceSchemas = $sourceCsdl.DocumentElement.SelectNodes (" //edmx:Edmx/edmx:DataServices/edm:Schema" , $sourceNsMgr )
53+ foreach ($sourceSchema in $sourceSchemas ) {
54+ # get the namespace of the source schema
55+ $sourceNamespace = $sourceSchema.GetAttribute (" Namespace" )
56+ # get the target schema node by namespace
57+ $targetSchema = $targetCsdl.DocumentElement.SelectSingleNode (" //edmx:Edmx/edmx:DataServices/edm:Schema[@Namespace='$sourceNamespace ']" , $targetNsMgr )
58+ # Get all annotations from the source CSDL
59+ $sourceAnnotations = $sourceCsdl.DocumentElement.SelectNodes (" //edmx:Edmx/edmx:DataServices/edm:Schema[@Namespace='$sourceNamespace ']/edm:Annotations" , $sourceNsMgr )
60+ # Iterate through each annotation in the source CSDL
61+ foreach ($annotations in $sourceAnnotations ) {
62+ # Create a new annotation element
63+ $newAnnotation = $targetCsdl.CreateElement (" Annotations" , $annotations.NamespaceURI )
64+ # Copy attributes from the source annotation to the new annotation
65+ foreach ($attribute in $annotations.Attributes ) {
66+ $newAnnotation.SetAttribute ($attribute.Name , $attribute.Value )
67+ }
68+ # Copy the children of the annotations element
69+ foreach ($child in $annotations.ChildNodes ) {
70+ $importedChild = $targetCsdl.ImportNode ($child , $true )
71+ $newAnnotation.AppendChild ($importedChild ) | Out-Null
72+ }
73+ # Append the new annotation to the target CSDL
74+ $targetSchema.AppendChild ($newAnnotation ) | Out-Null
6375 }
64- # Append the new annotation to the target CSDL
65- $targetSchema.AppendChild ($newAnnotation ) | Out-Null
6676 }
6777}
6878# Save the modified target CSDL file
6979$targetCsdl.Save ($targetCsdlPath )
70- Write-Host " Annotations copied from $sourceCsdlPath to $targetCsdlPath " - ForegroundColor Green
80+ Write-Host " Annotations copied from $sourceCsdlDirectoryPath to $targetCsdlPath " - ForegroundColor Green
0 commit comments