@kosdev-code/kos-codegen-core 0.1.0-next.792 → 0.1.0-next.796

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/index.d.ts +2 -2
  2. package/index.d.ts.map +1 -1
  3. package/index.js +395 -0
  4. package/index.js.map +1 -1
  5. package/index.mjs +395 -0
  6. package/index.mjs.map +1 -1
  7. package/lib/generators/ci-sync.d.ts +26 -0
  8. package/lib/generators/ci-sync.d.ts.map +1 -0
  9. package/lib/generators/generate-polyglot-workspace.d.ts +29 -0
  10. package/lib/generators/generate-polyglot-workspace.d.ts.map +1 -0
  11. package/lib/generators/index.d.ts +5 -0
  12. package/lib/generators/index.d.ts.map +1 -1
  13. package/lib/generators/java-workspace.d.ts +33 -0
  14. package/lib/generators/java-workspace.d.ts.map +1 -0
  15. package/lib/generators/kab-targets.d.ts +40 -0
  16. package/lib/generators/kab-targets.d.ts.map +1 -0
  17. package/lib/generators/release-version-script.d.ts +10 -0
  18. package/lib/generators/release-version-script.d.ts.map +1 -0
  19. package/package.json +2 -2
  20. package/templates/polyglot-workspace/build/build-java.sh.template +18 -0
  21. package/templates/polyglot-workspace/build/build-release.sh.template +11 -0
  22. package/templates/polyglot-workspace/build/build-ui.sh.template +16 -0
  23. package/templates/polyglot-workspace/build/docker-build.sh.template +49 -0
  24. package/templates/polyglot-workspace/build/jdkw.sh.template +93 -0
  25. package/templates/polyglot-workspace/build/nodew.sh.template +73 -0
  26. package/templates/polyglot-workspace/build/release_version_prebuild.sh.template +31 -0
  27. package/templates/polyglot-workspace/github/build-java.json.template +6 -0
  28. package/templates/polyglot-workspace/github/build-release.json.template +14 -0
  29. package/templates/polyglot-workspace/github/build-ui.json.template +13 -0
  30. package/templates/polyglot-workspace/github/workflows/develop-java.yml.template +35 -0
  31. package/templates/polyglot-workspace/github/workflows/develop-ui.yml.template +38 -0
  32. package/templates/polyglot-workspace/github/workflows/release.yml.template +37 -0
  33. package/templates/polyglot-workspace/java/README.md.template +36 -0
  34. package/templates/polyglot-workspace/root/.gitignore.template +5 -0
  35. package/templates/polyglot-workspace/root/README.md.template +69 -0
package/index.mjs CHANGED
@@ -515,6 +515,294 @@ function generateInit(codegenFs, options) {
515
515
  };
516
516
  writeJson(codegenFs, "nx.json", nxConfig);
517
517
  }
518
+ const BUILD_SCRIPTS = [
519
+ "build/build-ui.sh",
520
+ "build/build-java.sh",
521
+ "build/build-release.sh",
522
+ "build/release_version_prebuild.sh",
523
+ "build/docker-build.sh"
524
+ ];
525
+ function generatePolyglotWorkspace(codegenFs, templateDir, options) {
526
+ const normalized = normalizeAllValues({ name: options.name });
527
+ const vars = {
528
+ ...normalized,
529
+ keyset: options.keyset || "prod.kos",
530
+ appUiName: `${normalized.nameDashCase}-ui`,
531
+ nodeVersion: options.nodeVersion || process.version.replace(/^v/, ""),
532
+ jdkMajor: options.jdkMajor || "17",
533
+ mavenVersion: options.mavenVersion || "3.9.9"
534
+ };
535
+ generateFilesFromTemplates(
536
+ codegenFs,
537
+ path.join(templateDir, "root"),
538
+ ".",
539
+ vars
540
+ );
541
+ generateFilesFromTemplates(
542
+ codegenFs,
543
+ path.join(templateDir, "build"),
544
+ "build",
545
+ vars
546
+ );
547
+ generateFilesFromTemplates(
548
+ codegenFs,
549
+ path.join(templateDir, "github"),
550
+ ".github",
551
+ vars
552
+ );
553
+ generateFilesFromTemplates(
554
+ codegenFs,
555
+ path.join(templateDir, "java"),
556
+ "java",
557
+ vars
558
+ );
559
+ return { executablePaths: [...BUILD_SCRIPTS] };
560
+ }
561
+ const UI_PROJECT_DIRS = [
562
+ "apps",
563
+ "libs",
564
+ "plugins",
565
+ "splash",
566
+ "themes",
567
+ "content",
568
+ "translations"
569
+ ];
570
+ function joinArtifactPath(outputPath, fileName) {
571
+ return `${outputPath.replace(/\/+$/, "")}/${fileName}`;
572
+ }
573
+ function discoverUiArtifacts(codegenFs) {
574
+ const artifacts = [];
575
+ for (const dir of UI_PROJECT_DIRS) {
576
+ for (const file of codegenFs.listFiles(`ui/${dir}`)) {
577
+ if (!file.endsWith("project.json")) continue;
578
+ const raw = codegenFs.read(file);
579
+ if (raw === null) continue;
580
+ let project;
581
+ try {
582
+ project = JSON.parse(raw);
583
+ } catch {
584
+ continue;
585
+ }
586
+ const name = project.name;
587
+ if (!name) continue;
588
+ const kabOptions = project.targets?.kab?.options;
589
+ if (kabOptions?.outputPath && kabOptions?.kabName) {
590
+ artifacts.push({
591
+ id: name,
592
+ filename: `ui/${joinArtifactPath(kabOptions.outputPath, kabOptions.kabName)}`
593
+ });
594
+ } else if (project.targets?.splash) {
595
+ artifacts.push({
596
+ id: name,
597
+ filename: `ui/dist/archives/packages/${name}/${name}.kab`,
598
+ layer: 1
599
+ });
600
+ }
601
+ }
602
+ }
603
+ for (const file of codegenFs.listFiles("ui/external")) {
604
+ if (!file.endsWith(".kab")) continue;
605
+ const base = file.slice(file.lastIndexOf("/") + 1, -".kab".length);
606
+ artifacts.push({ id: base, filename: file });
607
+ }
608
+ return artifacts;
609
+ }
610
+ function discoverJavaArtifacts(codegenFs) {
611
+ const aggregator = codegenFs.read("java/pom.xml");
612
+ if (aggregator === null) return [];
613
+ const artifacts = [];
614
+ for (const match of aggregator.matchAll(/<module>([^<]+)<\/module>/g)) {
615
+ const moduleDir = match[1];
616
+ const pom = codegenFs.read(`java/${moduleDir}/pom.xml`);
617
+ if (pom === null || !pom.includes("kos-kab-maven-plugin")) continue;
618
+ const withoutParent = pom.replace(/<parent>[\s\S]*?<\/parent>/, "");
619
+ const artifactId = withoutParent.match(
620
+ /<artifactId>([^<]+)<\/artifactId>/
621
+ )?.[1];
622
+ if (!artifactId) continue;
623
+ artifacts.push({
624
+ id: moduleDir,
625
+ // eslint-disable-next-line no-template-curly-in-string
626
+ filename: `java/${moduleDir}/target/${artifactId}-\${KOS_STD_VERSION_REGEX}.kab`
627
+ });
628
+ }
629
+ return artifacts;
630
+ }
631
+ function syncManifest(codegenFs, manifestPath, discovered, prune) {
632
+ const raw = codegenFs.read(manifestPath);
633
+ if (raw === null) return null;
634
+ const manifest = JSON.parse(raw);
635
+ const existing = manifest.artifacts ?? [];
636
+ const byFilename = new Map(discovered.map((d) => [d.filename, d]));
637
+ const kept = [];
638
+ const stale = [];
639
+ const pruned = [];
640
+ for (const entry of existing) {
641
+ if (byFilename.has(entry.filename)) {
642
+ kept.push(entry);
643
+ byFilename.delete(entry.filename);
644
+ } else if (prune) {
645
+ pruned.push(entry.id ?? entry.filename);
646
+ } else {
647
+ kept.push(entry);
648
+ stale.push(entry.id ?? entry.filename);
649
+ }
650
+ }
651
+ const added = [];
652
+ for (const artifact of byFilename.values()) {
653
+ kept.push({
654
+ id: artifact.id,
655
+ filename: artifact.filename,
656
+ artifactstore: "kos-cdn",
657
+ marketplace: 1,
658
+ ...artifact.layer !== void 0 ? { layer: artifact.layer } : {}
659
+ });
660
+ added.push(artifact.id);
661
+ }
662
+ if (added.length > 0 || pruned.length > 0) {
663
+ manifest.artifacts = kept;
664
+ codegenFs.write(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
665
+ }
666
+ return { added, stale, pruned };
667
+ }
668
+ function syncCiManifests(codegenFs, options = {}) {
669
+ const prune = options.prune ?? false;
670
+ const ui = discoverUiArtifacts(codegenFs);
671
+ const java = discoverJavaArtifacts(codegenFs);
672
+ const manifests = {};
673
+ const plan = [
674
+ [".github/build-ui.json", ui],
675
+ [".github/build-java.json", java],
676
+ [".github/build-release.json", [...ui, ...java]]
677
+ ];
678
+ for (const [manifestPath, discovered] of plan) {
679
+ const result = syncManifest(codegenFs, manifestPath, discovered, prune);
680
+ if (result) {
681
+ manifests[manifestPath] = result;
682
+ }
683
+ }
684
+ return { manifests, discovered: { ui, java } };
685
+ }
686
+ function ensureJavaAggregatorPom(codegenFs, options) {
687
+ const pomPath = "java/pom.xml";
688
+ const existing = codegenFs.read(pomPath);
689
+ if (existing === null) {
690
+ codegenFs.write(
691
+ pomPath,
692
+ `<?xml version="1.0" encoding="UTF-8"?>
693
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
694
+ <modelVersion>4.0.0</modelVersion>
695
+ <groupId>${options.groupId}</groupId>
696
+ <artifactId>${options.artifactId}</artifactId>
697
+ <version>0.0.0-SNAPSHOT</version>
698
+ <packaging>pom</packaging>
699
+ <modules>
700
+ <module>${options.moduleName}</module>
701
+ </modules>
702
+ </project>
703
+ `
704
+ );
705
+ return;
706
+ }
707
+ if (existing.includes(`<module>${options.moduleName}</module>`)) {
708
+ return;
709
+ }
710
+ codegenFs.write(
711
+ pomPath,
712
+ existing.replace(
713
+ "</modules>",
714
+ ` <module>${options.moduleName}</module>
715
+ </modules>`
716
+ )
717
+ );
718
+ }
719
+ function finalizeArchetypeModule(codegenFs, options) {
720
+ const { moduleName, kosVersion, kabPluginVersion, apiInfoVersion } = options;
721
+ const notes = [];
722
+ const pomPath = `java/${moduleName}/pom.xml`;
723
+ let pom = codegenFs.read(pomPath);
724
+ if (pom === null) {
725
+ throw new Error(`Generated module pom not found: ${pomPath}`);
726
+ }
727
+ if (pom.includes("<kos.version>0.0.0-SNAPSHOT</kos.version>")) {
728
+ if (kosVersion) {
729
+ pom = pom.replace(
730
+ "<kos.version>0.0.0-SNAPSHOT</kos.version>",
731
+ `<kos.version>${kosVersion}</kos.version>`
732
+ );
733
+ } else {
734
+ notes.push(
735
+ "kos.version is 0.0.0-SNAPSHOT — set it to a released kos-bom version before building"
736
+ );
737
+ }
738
+ }
739
+ if (pom.includes("${kos-kab-maven-plugin.version}") && !pom.includes("<kos-kab-maven-plugin.version>")) {
740
+ if (kabPluginVersion) {
741
+ pom = pom.replace(
742
+ /(<kos\.version>[^<]*<\/kos\.version>)/,
743
+ `$1
744
+ <kos-kab-maven-plugin.version>${kabPluginVersion}</kos-kab-maven-plugin.version>`
745
+ );
746
+ } else {
747
+ notes.push(
748
+ "the ${kos-kab-maven-plugin.version} property is referenced but undefined — add it before building"
749
+ );
750
+ }
751
+ }
752
+ const unversionedApiInfo = /<artifactId>api-info<\/artifactId>(\s*)<\/dependency>/;
753
+ if (unversionedApiInfo.test(pom)) {
754
+ if (apiInfoVersion) {
755
+ pom = pom.replace(
756
+ unversionedApiInfo,
757
+ `<artifactId>api-info</artifactId>$1 <version>${apiInfoVersion}</version>$1</dependency>`
758
+ );
759
+ } else {
760
+ notes.push(
761
+ "api-info has no version and is not managed by the kos-bom — add one before building"
762
+ );
763
+ }
764
+ }
765
+ codegenFs.write(pomPath, pom);
766
+ const githubDir = `java/${moduleName}/github`;
767
+ if (codegenFs.exists(githubDir)) {
768
+ for (const file of codegenFs.listFiles(githubDir)) {
769
+ codegenFs.delete(file);
770
+ }
771
+ }
772
+ const gitignore = codegenFs.read(`java/${moduleName}/gitignore`);
773
+ if (gitignore !== null) {
774
+ codegenFs.write(`java/${moduleName}/.gitignore`, gitignore);
775
+ codegenFs.delete(`java/${moduleName}/gitignore`);
776
+ }
777
+ return notes;
778
+ }
779
+ function addJavaArtifactToManifests(codegenFs, options) {
780
+ const { moduleName } = options;
781
+ for (const manifestPath of [
782
+ ".github/build-java.json",
783
+ ".github/build-release.json"
784
+ ]) {
785
+ const raw = codegenFs.read(manifestPath);
786
+ if (raw === null) {
787
+ continue;
788
+ }
789
+ const manifest = JSON.parse(raw);
790
+ manifest.artifacts = manifest.artifacts ?? [];
791
+ if (manifest.artifacts.some(
792
+ (artifact) => artifact.id === moduleName
793
+ )) {
794
+ continue;
795
+ }
796
+ manifest.artifacts.push({
797
+ id: moduleName,
798
+ // eslint-disable-next-line no-template-curly-in-string
799
+ filename: `java/${moduleName}/target/${moduleName}-\${KOS_STD_VERSION_REGEX}.kab`,
800
+ artifactstore: "kos-cdn",
801
+ marketplace: 1
802
+ });
803
+ codegenFs.write(manifestPath, JSON.stringify(manifest, null, 2) + "\n");
804
+ }
805
+ }
518
806
  function normalizeOptions(codegenFs, options, projects) {
519
807
  const toNormalize = {
520
808
  name: options.name
@@ -560,6 +848,103 @@ function normalizeOptions(codegenFs, options, projects) {
560
848
  template: ""
561
849
  };
562
850
  }
851
+ const UPDATE_RELEASE_VERSION_SCRIPT_PATH = "tools/scripts/update-release-version.mjs";
852
+ function buildKabTargets(options) {
853
+ const {
854
+ name,
855
+ archiveDir = "packages",
856
+ descriptorDir,
857
+ buildTarget = "build"
858
+ } = options;
859
+ const outputPath = `dist/archives/${archiveDir}/${name}/`;
860
+ const targets = {
861
+ kab: {
862
+ command: `node tools/scripts/kabtool.mjs build ${name} && node tools/scripts/kabtool.mjs list ${name} `,
863
+ options: {
864
+ outputPath,
865
+ zipName: "ui.zip",
866
+ kabName: `${name}.kab`
867
+ },
868
+ dependsOn: ["zip"]
869
+ },
870
+ zip: {
871
+ command: `node tools/scripts/archiver.js ${name}`,
872
+ options: {
873
+ outputPath,
874
+ zipName: "ui.zip"
875
+ },
876
+ dependsOn: descriptorDir ? [buildTarget, "descriptor"] : [buildTarget]
877
+ }
878
+ };
879
+ if (descriptorDir) {
880
+ targets.descriptor = {
881
+ command: `node tools/scripts/descriptor.mjs ${name}`,
882
+ options: {
883
+ outputPath: `dist/${descriptorDir}`,
884
+ fileName: "descriptor.json"
885
+ },
886
+ dependsOn: ["build"]
887
+ };
888
+ }
889
+ targets.version = {
890
+ command: `node ${UPDATE_RELEASE_VERSION_SCRIPT_PATH} ${name} {args.ver}`,
891
+ options: {},
892
+ dependsOn: []
893
+ };
894
+ return targets;
895
+ }
896
+ const UPDATE_RELEASE_VERSION_SCRIPT = `import devkit from "@nx/devkit";
897
+ import { resolve } from "path";
898
+ import { readFileSync, writeFileSync } from "fs";
899
+ import prettier from "prettier";
900
+
901
+ // KOS artifact versioning: stamps the project's .kos.json "version" field
902
+ // (which kabtool bakes into the KAB). Never touches package.json.
903
+ // Driven by tag-based releases:
904
+ // nx run-many --target=version --args=--ver=$KOSBUILD_VERSION
905
+
906
+ const { readCachedProjectGraph } = devkit;
907
+ const [, , name, versionArg] = process.argv;
908
+
909
+ // "{args.ver}" arrives literally when the target runs without --args=--ver=<v>;
910
+ // treat that (or a missing arg) as "report current version, change nothing".
911
+ const version =
912
+ versionArg && !versionArg.startsWith("{args") ? versionArg : undefined;
913
+
914
+ if (!name) {
915
+ console.error("usage: update-release-version.mjs <project> <version>");
916
+ process.exit(1);
917
+ }
918
+
919
+ const graph = readCachedProjectGraph();
920
+ const project = graph.nodes[name];
921
+ if (!project) {
922
+ console.error("Unknown project: " + name);
923
+ process.exit(1);
924
+ }
925
+
926
+ const kosJsonPath = resolve(process.cwd(), project.data.root, ".kos.json");
927
+ let kosJson;
928
+ try {
929
+ kosJson = JSON.parse(readFileSync(kosJsonPath, "utf8"));
930
+ } catch {
931
+ console.error("Missing or invalid .kos.json: " + kosJsonPath);
932
+ process.exit(1);
933
+ }
934
+
935
+ if (!version) {
936
+ console.log(name + ": " + kosJson.version + " (no --ver given; unchanged)");
937
+ process.exit(0);
938
+ }
939
+
940
+ const prettierOptions = await prettier.resolveConfig(kosJsonPath);
941
+ const output = await prettier.format(
942
+ JSON.stringify({ ...kosJson, version }, null, 2),
943
+ { ...prettierOptions, parser: "json" }
944
+ );
945
+ writeFileSync(kosJsonPath, output);
946
+ console.log(name + ": version -> " + version);
947
+ `;
563
948
  function appendBarrelExport(codegenFs, indexPath, exportPath) {
564
949
  const exportLine = `export * from '${exportPath}'`;
565
950
  const content = codegenFs.read(indexPath) ?? "";
@@ -2985,6 +3370,8 @@ export {
2985
3370
  PLUGIN_TYPES,
2986
3371
  PluginHandlerFactory,
2987
3372
  TrackingFileSystem,
3373
+ UPDATE_RELEASE_VERSION_SCRIPT,
3374
+ UPDATE_RELEASE_VERSION_SCRIPT_PATH,
2988
3375
  ValidationError,
2989
3376
  addChildToModel,
2990
3377
  addComputedToModel,
@@ -2992,17 +3379,23 @@ export {
2992
3379
  addContainerSupportToModel,
2993
3380
  addDependencyToModel,
2994
3381
  addFutureToModel,
3382
+ addJavaArtifactToManifests,
2995
3383
  addKosModelConfiguration,
2996
3384
  addModelEffectToModel,
2997
3385
  addPropertyToModel,
2998
3386
  addServiceRequestToModel,
2999
3387
  addTopicHandlerToModel,
3000
3388
  appendBarrelExport,
3389
+ buildKabTargets,
3001
3390
  camelCase,
3002
3391
  constantCase,
3003
3392
  dashCase,
3004
3393
  describeModel,
3394
+ discoverJavaArtifacts,
3005
3395
  discoverProjects,
3396
+ discoverUiArtifacts,
3397
+ ensureJavaAggregatorPom,
3398
+ finalizeArchetypeModule,
3006
3399
  findProjectByName,
3007
3400
  findProjectForPath,
3008
3401
  formatFiles,
@@ -3014,6 +3407,7 @@ export {
3014
3407
  generateHook,
3015
3408
  generateInit,
3016
3409
  generateModel,
3410
+ generatePolyglotWorkspace,
3017
3411
  generateSplashProject,
3018
3412
  getCodegenLogger,
3019
3413
  getCurrentDirectoryName,
@@ -3031,6 +3425,7 @@ export {
3031
3425
  readNxJson,
3032
3426
  resolveModelFilePath,
3033
3427
  setCodegenLogger,
3428
+ syncCiManifests,
3034
3429
  updateJson,
3035
3430
  updateModelIndex,
3036
3431
  validateModel,