Skip to content

Commit d151457

Browse files
committed
cleanup
1 parent 972487f commit d151457

File tree

15 files changed

+129
-181
lines changed

15 files changed

+129
-181
lines changed

scripts/copy-templates.js

Lines changed: 92 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -3,178 +3,117 @@
33
const fs = require('fs');
44
const path = require('path');
55

6-
// Version configurations
7-
const VERSION_CONFIGS = {
8-
'13': {
9-
libpgQueryTag: '13-2.2.0',
10-
useEmscriptenPatch: true
11-
},
12-
'14': {
13-
libpgQueryTag: '14-3.0.0',
14-
useEmscriptenPatch: false
15-
},
16-
'15': {
17-
libpgQueryTag: '15-4.2.4',
18-
useEmscriptenPatch: false
19-
},
20-
'16': {
21-
libpgQueryTag: '16-5.2.0',
22-
useEmscriptenPatch: false
23-
},
24-
'17': {
25-
libpgQueryTag: '17-6.1.0',
26-
useEmscriptenPatch: false
27-
}
28-
};
29-
30-
// Headers for different file types
31-
const HEADERS = {
32-
// JavaScript/TypeScript/C style comment
33-
default: `/**
6+
const HEADER = `/**
347
* DO NOT MODIFY MANUALLY — this is generated from the templates dir
358
*
369
* To make changes, edit the files in the templates/ directory and run:
3710
* npm run copy:templates
3811
*/
3912
40-
`,
41-
// Makefile style comment
42-
makefile: `# DO NOT MODIFY MANUALLY — this is generated from the templates dir
13+
`;
14+
15+
const MAKEFILE_HEADER = `# DO NOT MODIFY MANUALLY — this is generated from the templates dir
4316
#
4417
# To make changes, edit the files in the templates/ directory and run:
4518
# npm run copy:templates
4619
47-
`
48-
};
49-
50-
// File extensions that should get headers
51-
const HEADER_EXTENSIONS = ['.ts', '.js', '.c'];
52-
const MAKEFILE_NAMES = ['Makefile', 'makefile'];
20+
`;
5321

54-
/**
55-
* Process template content with simple mustache-like syntax
56-
* @param {string} content - Template content
57-
* @param {object} config - Configuration object
58-
* @returns {string} Processed content
59-
*/
60-
function processTemplate(content, config) {
61-
// Replace simple variables
62-
content = content.replace(/\{\{LIBPG_QUERY_TAG\}\}/g, config.libpgQueryTag);
63-
64-
// Handle conditional blocks
65-
// {{#USE_EMSCRIPTEN_PATCH}}...{{/USE_EMSCRIPTEN_PATCH}}
66-
const conditionalRegex = /\{\{#(\w+)\}\}([\s\S]*?)\{\{\/\1\}\}/g;
67-
68-
content = content.replace(conditionalRegex, (match, flag, blockContent) => {
69-
if (flag === 'USE_EMSCRIPTEN_PATCH' && config.useEmscriptenPatch) {
70-
return blockContent;
71-
}
72-
return '';
73-
});
74-
75-
return content;
76-
}
77-
78-
/**
79-
* Add header to file content if applicable
80-
* @param {string} filePath - Path to the file
81-
* @param {string} content - File content
82-
* @returns {string} Content with header if applicable
83-
*/
84-
function addHeaderIfNeeded(filePath, content) {
85-
const basename = path.basename(filePath);
86-
const ext = path.extname(filePath);
87-
88-
// Check if it's a Makefile
89-
if (MAKEFILE_NAMES.includes(basename)) {
90-
return HEADERS.makefile + content;
91-
}
92-
93-
// Check if it's a source file that needs a header
94-
if (HEADER_EXTENSIONS.includes(ext)) {
95-
return HEADERS.default + content;
22+
// Version-specific configurations
23+
const VERSION_CONFIGS = {
24+
'13': {
25+
tag: '13-2.2.0',
26+
hasEmscriptenPatch: true
27+
},
28+
'14': {
29+
tag: '14-3.0.0',
30+
hasEmscriptenPatch: false
31+
},
32+
'15': {
33+
tag: '15-4.2.4',
34+
hasEmscriptenPatch: false
35+
},
36+
'16': {
37+
tag: '16-5.2.0',
38+
hasEmscriptenPatch: false
39+
},
40+
'17': {
41+
tag: '17-6.1.0',
42+
hasEmscriptenPatch: false
9643
}
97-
98-
return content;
99-
}
44+
};
10045

101-
/**
102-
* Copy a file from template to destination with processing
103-
* @param {string} templatePath - Source template path
104-
* @param {string} destPath - Destination path
105-
* @param {object} config - Version configuration
106-
*/
107-
function copyTemplate(templatePath, destPath, config) {
108-
const content = fs.readFileSync(templatePath, 'utf8');
109-
const processedContent = processTemplate(content, config);
110-
const finalContent = addHeaderIfNeeded(destPath, processedContent);
111-
112-
// Ensure destination directory exists
113-
const destDir = path.dirname(destPath);
114-
if (!fs.existsSync(destDir)) {
115-
fs.mkdirSync(destDir, { recursive: true });
116-
}
117-
118-
fs.writeFileSync(destPath, finalContent);
119-
}
46+
// Files to copy from templates
47+
const TEMPLATE_FILES = [
48+
{ src: 'LICENSE', dest: 'LICENSE', header: false },
49+
{ src: 'wasm_wrapper.c', dest: 'src/wasm_wrapper.c', header: HEADER },
50+
{ src: 'libpg-query.d.ts', dest: 'src/libpg-query.d.ts', header: HEADER },
51+
{ src: 'index.ts', dest: 'src/index.ts', header: HEADER }
52+
];
12053

121-
/**
122-
* Copy all templates for a specific version
123-
* @param {string} version - Version number
124-
* @param {object} config - Version configuration
125-
*/
126-
function copyTemplatesForVersion(version, config) {
54+
function copyTemplates() {
12755
const templatesDir = path.join(__dirname, '..', 'templates');
128-
const versionDir = path.join(__dirname, '..', 'versions', version);
56+
const versionsDir = path.join(__dirname, '..', 'versions');
12957

130-
// Check if version directory exists
131-
if (!fs.existsSync(versionDir)) {
132-
console.warn(`Warning: Directory ${versionDir} does not exist. Skipping...`);
133-
return;
134-
}
135-
136-
// Files to copy
137-
const filesToCopy = [
138-
'LICENSE',
139-
'Makefile',
140-
'src/index.ts',
141-
'src/libpg-query.d.ts',
142-
'src/wasm_wrapper.c'
143-
];
144-
145-
filesToCopy.forEach(file => {
146-
const templatePath = path.join(templatesDir, file);
147-
const destPath = path.join(versionDir, file);
58+
// Process each version
59+
for (const [version, config] of Object.entries(VERSION_CONFIGS)) {
60+
const versionDir = path.join(versionsDir, version);
61+
console.log(`\nProcessing version ${version}...`);
14862

149-
if (!fs.existsSync(templatePath)) {
150-
console.error(`Error: Template file ${templatePath} does not exist!`);
151-
return;
63+
// Copy template files
64+
for (const file of TEMPLATE_FILES) {
65+
const srcPath = path.join(templatesDir, file.src);
66+
const destPath = path.join(versionDir, file.dest);
67+
68+
// Ensure destination directory exists
69+
const destDir = path.dirname(destPath);
70+
if (!fs.existsSync(destDir)) {
71+
fs.mkdirSync(destDir, { recursive: true });
72+
}
73+
74+
// Read template content
75+
let content = fs.readFileSync(srcPath, 'utf8');
76+
77+
// Add header if specified
78+
if (file.header) {
79+
content = file.header + content;
80+
}
81+
82+
// Write to destination
83+
fs.writeFileSync(destPath, content);
84+
console.log(` ✓ Copied ${file.src} to ${file.dest}`);
15285
}
15386

154-
copyTemplate(templatePath, destPath, config);
155-
});
156-
157-
console.log(`✓ Version ${version} completed`);
158-
}
159-
160-
/**
161-
* Main function
162-
*/
163-
function main() {
164-
console.log('Copying template files to version directories...\n');
165-
166-
// Process each version
167-
Object.entries(VERSION_CONFIGS).forEach(([version, config]) => {
168-
console.log(`Processing version ${version}...`);
169-
copyTemplatesForVersion(version, config);
170-
});
87+
// Process Makefile template
88+
const makefileTemplate = fs.readFileSync(path.join(templatesDir, 'Makefile.template'), 'utf8');
89+
let makefileContent = makefileTemplate.replace(/{{VERSION_TAG}}/g, config.tag);
90+
91+
// Handle the USE_EMSCRIPTEN_PATCH placeholder
92+
if (config.hasEmscriptenPatch) {
93+
// For version 13, keep the patch block (remove only the placeholders)
94+
makefileContent = makefileContent.replace(
95+
/{{#USE_EMSCRIPTEN_PATCH}}\n?/g,
96+
''
97+
);
98+
makefileContent = makefileContent.replace(
99+
/{{\/USE_EMSCRIPTEN_PATCH}}\n?/g,
100+
''
101+
);
102+
} else {
103+
// For other versions, remove the entire block including placeholders
104+
makefileContent = makefileContent.replace(
105+
/{{#USE_EMSCRIPTEN_PATCH}}[\s\S]*?{{\/USE_EMSCRIPTEN_PATCH}}\n?/g,
106+
''
107+
);
108+
}
109+
110+
// Write Makefile with header
111+
fs.writeFileSync(path.join(versionDir, 'Makefile'), MAKEFILE_HEADER + makefileContent);
112+
console.log(` ✓ Generated Makefile with tag ${config.tag}`);
113+
}
171114

172-
console.log('\nAll versions processed successfully!');
173-
}
174-
175-
// Run if called directly
176-
if (require.main === module) {
177-
main();
115+
console.log('\n✅ Template copying completed!');
178116
}
179117

180-
module.exports = { processTemplate, copyTemplatesForVersion };
118+
// Run the script
119+
copyTemplates();

templates/Makefile renamed to templates/Makefile.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ WASM_OUT_DIR := wasm
22
WASM_OUT_NAME := libpg-query
33
WASM_MODULE_NAME := PgQueryModule
44
LIBPG_QUERY_REPO := https://github.com/pganalyze/libpg_query.git
5-
LIBPG_QUERY_TAG := {{LIBPG_QUERY_TAG}}
5+
LIBPG_QUERY_TAG := {{VERSION_TAG}}
66

77
CACHE_DIR := .cache
88

templates/README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ This directory contains template files that are shared across all PostgreSQL ver
44

55
## Files
66

7-
- `LICENSE` - The MIT license file
8-
- `Makefile` - The build configuration with placeholders for version-specific values
9-
- `src/index.ts` - TypeScript entry point
10-
- `src/libpg-query.d.ts` - TypeScript type definitions
11-
- `src/wasm_wrapper.c` - C wrapper for WASM compilation
7+
- `LICENSE` - The BSD 3-Clause license file
8+
- `Makefile.template` - The build configuration with placeholders for version-specific values
9+
- `index.ts` - TypeScript entry point
10+
- `libpg-query.d.ts` - TypeScript type definitions
11+
- `wasm_wrapper.c` - C wrapper for WASM compilation
1212

1313
## Usage
1414

@@ -22,17 +22,28 @@ This script will:
2222
1. Copy all template files to each version directory
2323
2. Replace placeholders with version-specific values
2424
3. Add a header comment to source files indicating they are auto-generated
25-
4. Handle special cases (e.g., the patch command for version 13)
25+
4. Handle special cases (e.g., the emscripten patch for version 13)
2626

27-
## Placeholders and Flags
27+
## Placeholders
2828

2929
The following placeholders are used in template files:
3030

31-
- `{{LIBPG_QUERY_TAG}}` - The libpg_query version tag (e.g., "14-3.0.0")
31+
- `{{VERSION_TAG}}` - The libpg_query version tag (e.g., "14-3.0.0")
3232
- `{{#USE_EMSCRIPTEN_PATCH}}...{{/USE_EMSCRIPTEN_PATCH}}` - Conditional block for version-specific patches (currently only used in version 13)
3333

34+
## Version-Specific Configurations
35+
36+
The `scripts/copy-templates.js` script contains version-specific configurations:
37+
38+
- **Version 13**: Uses tag `13-2.2.0` and requires emscripten patch
39+
- **Version 14**: Uses tag `14-3.0.0`
40+
- **Version 15**: Uses tag `15-4.2.4`
41+
- **Version 16**: Uses tag `16-5.2.0`
42+
- **Version 17**: Uses tag `17-6.1.0`
43+
3444
## Important Notes
3545

3646
- DO NOT edit files directly in the `versions/*/` directories for these common files
3747
- Always edit the templates and run the copy script
38-
- The script preserves version-specific configurations while maintaining consistency
48+
- The script preserves version-specific configurations while maintaining consistency
49+
- Generated files will have a header warning about manual modifications

templates/src/index.ts renamed to templates/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ export interface SqlErrorFormatOptions {
2323
maxQueryLength?: number; // Max query length to display (default: no limit)
2424
}
2525

26-
// Helper function to create enhanced error with SQL details
27-
function createSqlError(message: string, details: SqlErrorDetails): Error {
28-
const error = new Error(message);
29-
// Attach error details as properties
30-
Object.defineProperty(error, 'sqlDetails', {
31-
value: details,
32-
enumerable: true,
33-
configurable: true
34-
});
35-
return error;
26+
export class SqlError extends Error {
27+
sqlDetails?: SqlErrorDetails;
28+
29+
constructor(message: string, details?: SqlErrorDetails) {
30+
super(message);
31+
this.name = 'SqlError';
32+
this.sqlDetails = details;
33+
}
3634
}
3735

36+
37+
3838
// Helper function to classify error source
3939
function getErrorSource(filename: string | null): string {
4040
if (!filename) return 'unknown';
@@ -220,7 +220,7 @@ export const parse = awaitInit(async (query: string) => {
220220
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
221221
};
222222

223-
throw createSqlError(message, errorDetails);
223+
throw new SqlError(message, errorDetails);
224224
}
225225

226226
if (!parseTreePtr) {
@@ -289,7 +289,7 @@ export function parseSync(query: string) {
289289
context: contextPtr ? wasmModule.UTF8ToString(contextPtr) : undefined
290290
};
291291

292-
throw createSqlError(message, errorDetails);
292+
throw new SqlError(message, errorDetails);
293293
}
294294

295295
if (!parseTreePtr) {
File renamed without changes.
File renamed without changes.

versions/13/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ endif
4545
$(LIBPG_QUERY_DIR):
4646
mkdir -p $(CACHE_DIR)
4747
git clone -b $(LIBPG_QUERY_TAG) --single-branch $(LIBPG_QUERY_REPO) $(LIBPG_QUERY_DIR)
48-
4948
ifdef EMSCRIPTEN
5049
cd $(LIBPG_QUERY_DIR); patch -p1 < $(shell pwd)/patches/emscripten_disable_spinlocks.patch
5150
endif
52-
5351

5452
$(LIBPG_QUERY_HEADER): $(LIBPG_QUERY_DIR)
5553

0 commit comments

Comments
 (0)