-
Notifications
You must be signed in to change notification settings - Fork 55
Package xml improvements #1872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Package xml improvements #1872
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env ts-node | ||
|
||
import { writeClientPackageXml, ClientPackageXML } from "../src/package-xml-v2"; | ||
import { getWidgetInfo } from "../src/package-info"; | ||
import { readPropertiesFile } from "../src/package-xml-v2/properties-xml"; | ||
import path from "node:path"; | ||
import { existsSync } from "node:fs"; | ||
|
||
async function generatePackageXml(): Promise<void> { | ||
const widgetDir = process.cwd(); | ||
|
||
// Read package.json info | ||
const packageInfo = await getWidgetInfo(widgetDir); | ||
|
||
const srcDir = path.join(widgetDir, "src"); | ||
|
||
// Create src directory if it doesn't exist | ||
if (!existsSync(srcDir)) { | ||
throw new Error(`Src folder not found: ${srcDir}`); | ||
} | ||
|
||
// Get properties file name from package.json (mxpackage.name + ".xml") | ||
const propertiesFileName = packageInfo.mxpackage.name + ".xml"; | ||
const propertiesFilePath = path.join(srcDir, propertiesFileName); | ||
|
||
// Properties file must exist | ||
if (!existsSync(propertiesFilePath)) { | ||
throw new Error(`Properties file not found: ${propertiesFilePath}`); | ||
} | ||
|
||
// Read properties file and extract widget ID | ||
const propertiesXml = await readPropertiesFile(propertiesFilePath); | ||
const widgetId = propertiesXml.widget["@_id"]; | ||
|
||
// Generate ClientPackageXML structure | ||
const clientPackageXml: ClientPackageXML = { | ||
name: packageInfo.mxpackage.name, | ||
version: packageInfo.version, | ||
widgetFiles: [packageInfo.mxpackage.name + ".xml"], | ||
files: [widgetId.split(".").slice(0, -1).join("/") + "/"] | ||
}; | ||
|
||
// Write the generated package.xml | ||
const packageXmlPath = path.join(srcDir, "package.xml"); | ||
await writeClientPackageXml(packageXmlPath, clientPackageXml); | ||
} | ||
|
||
async function main() { | ||
try { | ||
await generatePackageXml(); | ||
} catch (error) { | ||
console.error("Error generating package.xml:", error instanceof Error ? error.message : String(error)); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
main(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { XMLBuilder, XMLParser } from "fast-xml-parser"; | ||
import { readFile, writeFile } from "fs/promises"; | ||
import { Version, VersionString } from "../version"; | ||
import { ClientModulePackageFile } from "./schema"; | ||
|
||
export function xmlTextToXmlJson(xmlText: string | Buffer): unknown { | ||
const parser = new XMLParser({ ignoreAttributes: false }); | ||
return parser.parse(xmlText); | ||
} | ||
|
||
export function xmlJsonToXmlText(xmlObject: any): string { | ||
const builder = new XMLBuilder({ | ||
ignoreAttributes: false, | ||
format: true, | ||
indentBy: " ", | ||
suppressEmptyNode: true | ||
}); | ||
return builder | ||
.build(xmlObject) | ||
.replaceAll(/(<[^>]*?)\/>/g, "$1 />") // Add space before /> in self-closing tags | ||
.replaceAll(/(<\?[^>]*?)\?>/g, "$1 ?>"); // Add space before ?> in XML declarations | ||
} | ||
|
||
export interface ClientPackageXML { | ||
name: string; | ||
version: Version; | ||
widgetFiles: string[]; | ||
files: string[]; | ||
} | ||
|
||
export async function readClientPackageXml(path: string): Promise<ClientPackageXML> { | ||
return parseClientPackageXml(ClientModulePackageFile.passthrough().parse(xmlTextToXmlJson(await readFile(path)))); | ||
} | ||
|
||
export async function writeClientPackageXml(path: string, data: ClientPackageXML): Promise<void> { | ||
await writeFile(path, xmlJsonToXmlText(buildClientPackageXml(data))); | ||
} | ||
|
||
function parseClientPackageXml(xmlJson: ClientModulePackageFile): ClientPackageXML { | ||
const clientModule = xmlJson?.package?.clientModule ?? {}; | ||
const widgetFilesNode = clientModule.widgetFiles !== "" ? clientModule.widgetFiles?.widgetFile : undefined; | ||
const filesNode = clientModule.files !== "" ? clientModule.files?.file : undefined; | ||
|
||
const extractPaths = (node: any): string[] => { | ||
if (!node) return []; | ||
if (Array.isArray(node)) { | ||
return node.map((item: any) => item["@_path"]); | ||
} | ||
return [node["@_path"]]; | ||
}; | ||
|
||
const name = clientModule["@_name"] ?? ""; | ||
const versionString = clientModule["@_version"] ?? "1.0.0"; | ||
|
||
return { | ||
name, | ||
version: Version.fromString(versionString as VersionString), | ||
widgetFiles: extractPaths(widgetFilesNode), | ||
files: extractPaths(filesNode) | ||
}; | ||
} | ||
|
||
function buildClientPackageXml(clientPackage: ClientPackageXML): ClientModulePackageFile { | ||
const toXmlNode = <T extends "file" | "widgetFile">(arr: string[], tag: T) => { | ||
if (arr.length === 0) return ""; | ||
if (arr.length === 1) { | ||
return { [tag]: { "@_path": arr[0] } }; | ||
} | ||
return { [tag]: arr.map(path => ({ "@_path": path })) }; | ||
}; | ||
|
||
return { | ||
"?xml": { | ||
"@_version": "1.0", | ||
"@_encoding": "utf-8" | ||
}, | ||
package: { | ||
clientModule: { | ||
widgetFiles: toXmlNode( | ||
clientPackage.widgetFiles, | ||
"widgetFile" | ||
) as ClientModulePackageFile["package"]["clientModule"]["widgetFiles"], | ||
files: toXmlNode( | ||
clientPackage.files, | ||
"file" | ||
) as ClientModulePackageFile["package"]["clientModule"]["files"], | ||
"@_name": clientPackage.name, | ||
"@_version": clientPackage.version.format(), | ||
"@_xmlns": "http://www.mendix.com/clientModule/1.0/" | ||
}, | ||
"@_xmlns": "http://www.mendix.com/package/1.0/" | ||
} | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from "zod"; | ||
import { xmlTextToXmlJson } from "./index"; | ||
import { readFile } from "node:fs/promises"; | ||
|
||
export const PropertiesXMLFile = z.object({ | ||
"?xml": z.object({ | ||
"@_version": z.literal("1.0"), | ||
"@_encoding": z.literal("utf-8") | ||
}), | ||
widget: z.object({ | ||
"@_id": z.string(), | ||
"@_xmlns": z.literal("http://www.mendix.com/widget/1.0/"), | ||
"@_xmlns:xsi": z.literal("http://www.w3.org/2001/XMLSchema-instance") | ||
}) | ||
}); | ||
|
||
type PropertiesXMLFile = z.infer<typeof PropertiesXMLFile>; | ||
|
||
export async function readPropertiesFile(filePath: string): Promise<PropertiesXMLFile> { | ||
return PropertiesXMLFile.passthrough().parse(xmlTextToXmlJson(await readFile(filePath, "utf-8"))); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { z } from "zod"; | ||
|
||
const FileTag = z.object({ | ||
"@_path": z.string() | ||
}); | ||
|
||
const FileNode = z.union([FileTag, FileTag.array()]); | ||
|
||
export const ModelerProjectPackageFile = z.object({ | ||
"?xml": z.object({ | ||
"@_version": z.literal("1.0"), | ||
"@_encoding": z.literal("utf-8") | ||
}), | ||
package: z.object({ | ||
"@_xmlns": z.literal("http://www.mendix.com/package/1.0/"), | ||
modelerProject: z.object({ | ||
"@_xmlns": z.literal("http://www.mendix.com/modelerProject/1.0/"), | ||
module: z.object({ | ||
"@_name": z.string() | ||
}), | ||
projectFile: z.object({ | ||
"@_path": z.string() | ||
}), | ||
files: z.union([ | ||
z.literal(""), | ||
z.object({ | ||
file: FileNode | ||
}) | ||
]) | ||
}) | ||
}) | ||
}); | ||
|
||
export type ModelerProjectPackageFile = z.infer<typeof ModelerProjectPackageFile>; | ||
|
||
export const ClientModulePackageFile = z.object({ | ||
"?xml": z.object({ | ||
"@_version": z.literal("1.0"), | ||
"@_encoding": z.literal("utf-8") | ||
}), | ||
package: z.object({ | ||
"@_xmlns": z.literal("http://www.mendix.com/package/1.0/"), | ||
clientModule: z.object({ | ||
"@_name": z.string({ | ||
required_error: "name attribute is required" | ||
}), | ||
"@_version": z.string({ | ||
required_error: "version attribute is required" | ||
}), | ||
"@_xmlns": z.literal("http://www.mendix.com/clientModule/1.0/"), | ||
|
||
files: z.union([ | ||
z.literal(""), | ||
z.object({ | ||
file: FileNode | ||
}) | ||
]), | ||
|
||
widgetFiles: z.union([ | ||
z.literal(""), | ||
z.object({ | ||
widgetFile: FileNode | ||
}) | ||
]) | ||
}) | ||
}) | ||
}); | ||
|
||
export type ClientModulePackageFile = z.infer<typeof ClientModulePackageFile>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/pluggableWidgets/barcode-scanner-web/src/package.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<package xmlns="http://www.mendix.com/package/1.0/"> | ||
<clientModule name="Barcode Scanner" version="2.4.2" xmlns="http://www.mendix.com/clientModule/1.0/"> | ||
<clientModule name="BarcodeScanner" version="2.4.2" xmlns="http://www.mendix.com/clientModule/1.0/"> | ||
<widgetFiles> | ||
<widgetFile path="BarcodeScanner.xml" /> | ||
</widgetFiles> | ||
<files> | ||
<file path="com/mendix/widget/web/barcodescanner" /> | ||
<file path="com/mendix/widget/web/barcodescanner/" /> | ||
</files> | ||
</clientModule> | ||
</package> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.