@platforma-sdk/tengo-builder 2.0.2 → 2.1.0

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.
@@ -7,49 +7,96 @@ import {
7
7
  fullNameWithoutTypeToString,
8
8
  parseArtefactNameAndVersion,
9
9
  } from './package';
10
- import type { TemplateData } from '@milaboratories/pl-model-backend';
10
+ import type { CompiledTemplateV3 } from '@milaboratories/pl-model-backend';
11
11
  import {
12
12
  parseTemplate,
13
13
  serializeTemplate,
14
14
  } from '@milaboratories/pl-model-backend';
15
15
 
16
- export class Template {
17
- public readonly data: TemplateData;
18
- public readonly content: Uint8Array;
19
-
20
- constructor(
21
- public readonly compileMode: CompileMode,
22
- public readonly fullName: FullArtifactName,
23
- body: {
24
- data?: TemplateData;
25
- content?: Uint8Array;
26
- },
27
- ) {
28
- let { data, content } = body;
29
- if (data === undefined && content === undefined)
30
- throw new Error('Neither data nor content is provided for template constructor');
31
- if (data !== undefined && content !== undefined)
32
- throw new Error('Both data and content are provided for template constructor');
33
-
34
- if (data === undefined) data = parseTemplate(content!);
35
- if (content === undefined) content = serializeTemplate(data);
36
-
37
- const nameFromData: FullArtifactNameWithoutType = parseArtefactNameAndVersion(data);
38
-
39
- if (
40
- nameFromData.pkg !== fullName.pkg
41
- || nameFromData.id !== fullName.id
42
- || nameFromData.version !== fullName.version
43
- )
44
- throw new Error(
45
- `Compiled template name don't match it's package and file names: ${fullNameWithoutTypeToString(nameFromData)} != ${fullNameWithoutTypeToString(fullName)}`,
46
- );
47
-
48
- this.data = data;
49
- this.content = content;
50
- }
16
+ /** Just a holder for template data, compilation options, full name and source code.
17
+ * It mimics ArtifactSource interface.
18
+ */
19
+ export type TemplateWithSource = {
20
+ readonly compileMode: CompileMode;
21
+ readonly fullName: FullArtifactName;
22
+ readonly source: string;
23
+ readonly data: CompiledTemplateV3;
24
+ };
25
+
26
+ export function newTemplateWithSource(
27
+ compileMode: CompileMode,
28
+ fullName: FullArtifactName,
29
+ data: CompiledTemplateV3,
30
+ source: string,
31
+ ): TemplateWithSource {
32
+ validateTemplateName(fullName, data);
33
+
34
+ return {
35
+ compileMode,
36
+ fullName,
37
+ data,
38
+ source,
39
+ };
40
+ }
41
+
42
+ export type Template = {
43
+ readonly compileMode: CompileMode;
44
+ readonly fullName: FullArtifactName;
45
+ readonly data: CompiledTemplateV3;
46
+ readonly content: Uint8Array;
47
+ };
48
+
49
+ export function newTemplateFromData(
50
+ compileMode: CompileMode,
51
+ fullName: FullArtifactName,
52
+ data: CompiledTemplateV3,
53
+ ): Template {
54
+ validateTemplateName(fullName, data);
55
+ return {
56
+ compileMode,
57
+ fullName,
58
+ data,
59
+ content: serializeTemplate(data),
60
+ };
61
+ }
51
62
 
52
- toJSON() {
53
- return { compileMode: this.compileMode, fullName: this.fullName, data: this.data };
63
+ export function newTemplateFromContent(
64
+ compileMode: CompileMode,
65
+ fullName: FullArtifactName,
66
+ content: Uint8Array,
67
+ ): Template {
68
+ const data = parseTemplate(content);
69
+ if (data.type !== 'pl.tengo-template.v3') {
70
+ throw new Error('malformed v3 template');
54
71
  }
72
+
73
+ validateTemplateName(fullName, data);
74
+ return {
75
+ compileMode,
76
+ fullName,
77
+ data,
78
+ content,
79
+ };
80
+ }
81
+
82
+ export function templateToSource(tpl: Template): TemplateWithSource {
83
+ return {
84
+ compileMode: tpl.compileMode,
85
+ fullName: tpl.fullName,
86
+ data: tpl.data,
87
+ source: tpl.data.hashToSource[tpl.data.template.sourceHash],
88
+ };
89
+ }
90
+ function validateTemplateName(fullName: FullArtifactName, data: CompiledTemplateV3) {
91
+ const nameFromData: FullArtifactNameWithoutType = parseArtefactNameAndVersion(data.template);
92
+
93
+ if (
94
+ nameFromData.pkg !== fullName.pkg
95
+ || nameFromData.id !== fullName.id
96
+ || nameFromData.version !== fullName.version
97
+ )
98
+ throw new Error(
99
+ `Compiled template name don't match it's package and file names: `
100
+ + `${fullNameWithoutTypeToString(nameFromData)} != ${fullNameWithoutTypeToString(fullName)}`,
101
+ );
55
102
  }
@@ -202,6 +202,16 @@ export const testPackage1Soft1Src = `
202
202
  some software contents. Template builder should pass it 'as-is'
203
203
  `;
204
204
 
205
+ export const testPackage1Asset1Name: FullArtifactName = {
206
+ type: 'asset',
207
+ pkg: 'current-package',
208
+ id: 'asset-1',
209
+ version: '1.2.3',
210
+ };
211
+ export const testPackage1Asset1Src = `
212
+ some asset contents. Template builder should pass it 'as-is'
213
+ `;
214
+
205
215
  export const testPackage1Lib2Name: FullArtifactName = {
206
216
  type: 'library',
207
217
  pkg: 'package1',