Skip to content

Commit e8cc5d3

Browse files
committed
Make generated index.pidx schema compliant
1 parent 478960e commit e8cc5d3

File tree

6 files changed

+56
-42
lines changed

6 files changed

+56
-42
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ concurrency:
2222
permissions:
2323
contents: read
2424

25+
2526
jobs:
2627
build-and-verify:
27-
uses: Open-CMSIS-Pack/workflows-and-actions-collection/.github/workflows/build-and-verify.yml@v1.0.1
28+
uses: Open-CMSIS-Pack/workflows-and-actions-collection/.github/workflows/build-and-verify.yml@print-test-logs
2829
secrets:
2930
QLTY_COVERAGE_TOKEN: ${{ secrets.QLTY_COVERAGE_TOKEN }}
3031
with:

cmd/cli_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,17 @@ func TestCli(t *testing.T) {
8383
t.Fatal(err)
8484
}
8585

86-
expected := `<index>
87-
<vendor>testing_vendor_index</vendor>
88-
<url>file://XX/test-continue-despite-errors.xml</url>
89-
<>
90-
<pindex>
91-
<pdsc vendor="TheVendor" url="test/" name="ThePack2" version="1.1.0" timestamp=""></pdsc>
92-
<pdsc vendor="TheVendor" url="test/" name="ThePack1" version="1.2.3" timestamp=""></pdsc>
93-
<pdsc vendor="TheOtherVendor" url="test/" name="TheOtherPack" version="1.0.50" timestamp=""></pdsc>
94-
<pdsc vendor="TheVendor" url="non-existing-path/" name="ThePack" version="1.0.1" timestamp=""></pdsc>
95-
</pindex>
86+
expected := `<?xml version="1.0" encoding="UTF-8"?>
87+
<index schemaVersion="1.1.1" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
88+
<vendor>testing_vendor_index</vendor>
89+
<url>file:///XX/test-continue-despite-errors.xml</url>
90+
<>
91+
<pindex>
92+
<pdsc vendor="TheVendor" url="test/" name="ThePack2" version="1.1.0"></pdsc>
93+
<pdsc vendor="TheVendor" url="test/" name="ThePack1" version="1.2.3"></pdsc>
94+
<pdsc vendor="TheOtherVendor" url="test/" name="TheOtherPack" version="1.0.50"></pdsc>
95+
<pdsc vendor="TheVendor" url="non-existing-path/" name="ThePack" version="1.0.1"></pdsc>
96+
</pindex>
9697
</index>`
9798
wd, _ := os.Getwd()
9899
wd = filepath.ToSlash(wd)

cmd/pidx.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import (
1616
// PidxXML maps the PIDX file format.
1717
// Ref: https://github.com/ARM-software/CMSIS_5/blob/develop/CMSIS/Utilities/PackIndex.xsd
1818
type PidxXML struct {
19-
XMLName xml.Name `xml:"index"`
20-
Vendor string `xml:"vendor"`
21-
URL string `xml:"url"`
22-
Timestamp string `xml:"timestamp"`
19+
XMLName xml.Name `xml:"index"`
20+
Vendor string `xml:"vendor"`
21+
URL string `xml:"url"`
22+
Timestamp string `xml:"timestamp"`
23+
SchemaVersion string `xml:"schemaVersion,attr"`
24+
XsiSchemaLocation string `xml:"xs:noNamespaceSchemaLocation,attr"`
25+
XmlnsXs string `xml:"xmlns:xs,attr"`
2326

2427
Pindex struct {
2528
XMLName xml.Name `xml:"pindex"`
@@ -32,12 +35,11 @@ type PidxXML struct {
3235

3336
// PdscTag maps a <pdsc> tag that goes in PIDX files.
3437
type PdscTag struct {
35-
XMLName xml.Name `xml:"pdsc"`
36-
Vendor string `xml:"vendor,attr"`
37-
URL string `xml:"url,attr"`
38-
Name string `xml:"name,attr"`
39-
Version string `xml:"version,attr"`
40-
Timestamp string `xml:"timestamp,attr"`
38+
XMLName xml.Name `xml:"pdsc"`
39+
Vendor string `xml:"vendor,attr"`
40+
URL string `xml:"url,attr"`
41+
Name string `xml:"name,attr"`
42+
Version string `xml:"version,attr"`
4143

4244
err error
4345
}
@@ -136,10 +138,15 @@ func (p *PidxXML) Update(vidx *VidxXML, vidxFileName string, outputFileName stri
136138
filename := filepath.Base(vidxFileName)
137139
p.Vendor = strings.TrimSuffix(filename, filepath.Ext(filename))
138140
p.URL, _ = filepath.Abs(outputFileName)
139-
p.URL = "file://" + filepath.ToSlash(p.URL)
141+
p.URL = "file:///" + filepath.ToSlash(p.URL)
140142
t := time.Now()
141143
p.Timestamp = t.Format("2006-01-02T15:04:05")
142144

145+
// Set schemaVersion and schema location attributes as required by PackIndex.xsd
146+
p.SchemaVersion = "1.1.1"
147+
p.XsiSchemaLocation = "PackIndex.xsd"
148+
p.XmlnsXs = "http://www.w3.org/2001/XMLSchema-instance"
149+
143150
var wg sync.WaitGroup
144151

145152
// Process package index first
@@ -154,7 +161,6 @@ func (p *PidxXML) Update(vidx *VidxXML, vidxFileName string, outputFileName stri
154161
// Now process package descriptors (vendors without pidx files)
155162
offset := vidx.PidxLength()
156163
for i, pdsc := range vidx.ListPdsc() {
157-
158164
errs[i+offset] = make([]error, 1)
159165
errs[i+offset][0] = p.addPdsc(pdsc)
160166
}

cmd/pidx_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ func TestPidxXML_Update(t *testing.T) {
274274
Logger.SetFile(output)
275275
Logger.SetLevel(DEBUG)
276276

277-
xml := `<index>
277+
xml := `<?xml version="1.0" encoding="UTF-8"?>
278+
<index>
278279
<vendor>TheVendor</vendor>
279280
<url>http://vendor.com/</url>
280281
<timestamp></timestamp>
@@ -327,18 +328,19 @@ func TestPidxXML_Update(t *testing.T) {
327328
t.Fatal(err)
328329
}
329330

330-
expected := `<index>
331-
<vendor>vv</vendor>
332-
<url>file:///uu</url>
333-
<>
334-
<pindex>
335-
<pdsc vendor="TheVendor" url="http://vendor.com/" name="ThePack" version="1.2.3" timestamp=""></pdsc>
336-
<pdsc vendor="TheVendor" url="http://vendor.com/" name="TheOtherPack" version="1.1.0" timestamp=""></pdsc>
337-
<pdsc vendor="TheOtherVendor" url="http://other-vendor.com/" name="ThePackage" version="0.0.1" timestamp=""></pdsc>
338-
</pindex>
331+
expected := `<?xml version="1.0" encoding="UTF-8"?>
332+
<index schemaVersion="1.1.1" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
333+
<vendor>vv</vendor>
334+
<url>file:///uu</url>
335+
<>
336+
<pindex>
337+
<pdsc vendor="TheVendor" url="http://vendor.com/" name="ThePack" version="1.2.3"></pdsc>
338+
<pdsc vendor="TheVendor" url="http://vendor.com/" name="TheOtherPack" version="1.1.0"></pdsc>
339+
<pdsc vendor="TheOtherVendor" url="http://other-vendor.com/" name="ThePackage" version="0.0.1"></pdsc>
340+
</pindex>
339341
</index>`
340342
if runtime.GOOS == "windows" {
341-
expected = strings.ReplaceAll(expected, "///uu", "//C:/uu")
343+
expected = strings.ReplaceAll(expected, "///uu", "///C:/uu")
342344
}
343345
s := string(out)
344346
sa, se, _ := strings.Cut(s, "timestamp") // cut out time, cannot compare

cmd/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ func ReadXML(path string, targetStruct interface{}) error {
100100

101101
// WriteXML marshals the XML info from "targetStruct" and save it to "path".
102102
func WriteXML(path string, targetStruct interface{}) error {
103-
output, err := xml.MarshalIndent(targetStruct, "", " ")
103+
body, err := xml.MarshalIndent(targetStruct, "", " ")
104104
if err != nil {
105105
return err
106106
}
107107

108+
output := append([]byte(xml.Header), body...)
109+
108110
if path == "" || path == "-" {
109111
os.Stdout.Write(output)
110112
return nil

cmd/utils_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@ func TestWriteXML(t *testing.T) {
323323
t.Fatalf("Can't open file %s to test if XML got actually written: %s", fileName, err2)
324324
}
325325

326-
AssertEqual(t, written, []byte(`<dummyXML>
327-
<dummy></dummy>
328-
<contents>dummy content</contents>
326+
AssertEqual(t, written, []byte(`<?xml version="1.0" encoding="UTF-8"?>
327+
<dummyXML>
328+
<dummy></dummy>
329+
<contents>dummy content</contents>
329330
</dummyXML>`))
330331
err = os.Remove(fileName)
331332
if err != nil {
@@ -343,10 +344,11 @@ func ExampleWriteXML() {
343344
xml.Contents = "dummy content"
344345
ExitOnError(WriteXML("", xml))
345346
// Output:
346-
// <dummyXML>
347-
// <dummy></dummy>
348-
// <contents>dummy content</contents>
349-
// </dummyXML>
347+
//<?xml version="1.0" encoding="UTF-8"?>
348+
//<dummyXML>
349+
// <dummy></dummy>
350+
// <contents>dummy content</contents>
351+
//</dummyXML>
350352
}
351353

352354
func TestEnsureDir(t *testing.T) {

0 commit comments

Comments
 (0)