Skip to content

Commit 81a8dfa

Browse files
authored
Merge pull request #413 from intersystems-community/map-files
Ability to add file mapping
2 parents e277f12 + 1c64c55 commit 81a8dfa

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,37 @@
746746
],
747747
"default": false
748748
},
749+
"objectscript.export.map": {
750+
"markdownDescription": "Map file names before export, with regexp pattern as a key and replacement as a value (e.g. `{ \"%(.*)\": \"_$1\" }` to make % classes or routines use underscore prefix instead).",
751+
"type": "object",
752+
"default": {},
753+
"examples": [
754+
{
755+
"%(.*)": "_$1"
756+
}
757+
],
758+
"patternProperties": {
759+
"\\(.*\\)": {
760+
"type": "string",
761+
"pattern": "\\$1"
762+
}
763+
},
764+
"properties": {
765+
"%(.*)": {
766+
"description": "Catch any % items. Set value to `_$1` to replace `%` prefix with `_`",
767+
"type": "string",
768+
"pattern": "\\$1",
769+
"default": "_$1"
770+
},
771+
"(.*)": {
772+
"description": "Match everything. Set value to `$1` to leave unchanged",
773+
"type": "string",
774+
"pattern": "\\$1",
775+
"default": "$1"
776+
}
777+
},
778+
"additionalProperties": false
779+
},
749780
"objectscript.export.atelier": {
750781
"description": "Export source code as Atelier did it, with packages as subfolders.",
751782
"type": "boolean",

src/commands/export.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ const getCategory = (fileName: string, addCategory: any | boolean): string => {
3636
}
3737
};
3838

39-
export const getFileName = (folder: string, name: string, split: boolean, addCategory: boolean): string => {
39+
export const getFileName = (
40+
folder: string,
41+
name: string,
42+
split: boolean,
43+
addCategory: boolean,
44+
map: {
45+
[key: string]: string;
46+
}
47+
): string => {
48+
if (map) {
49+
for (const pattern of Object.keys(map)) {
50+
if (new RegExp(`^${pattern}$`).test(name)) {
51+
name = name.replace(new RegExp(`^${pattern}$`), map[pattern]);
52+
break;
53+
}
54+
}
55+
}
4056
const fileNameArray: string[] = name.split(".");
4157
const fileExt = fileNameArray.pop().toLowerCase();
4258
const cat = addCategory ? getCategory(name, addCategory) : null;
@@ -142,15 +158,15 @@ export async function exportList(files: string[], workspaceFolder: string, names
142158
if (!files || !files.length) {
143159
vscode.window.showWarningMessage("Nothing to export");
144160
}
145-
const { atelier, folder, addCategory } = config("export", workspaceFolder);
161+
const { atelier, folder, addCategory, map } = config("export", workspaceFolder);
146162

147163
const root = [workspaceFolderUri(workspaceFolder).fsPath, typeof folder === "string" && folder.length ? folder : null]
148164
.filter(notNull)
149165
.join(path.sep);
150166
const run = async (fileList) => {
151167
const errors = [];
152168
for (const file of fileList) {
153-
await exportFile(workspaceFolder, namespace, file, getFileName(root, file, atelier, addCategory)).catch(
169+
await exportFile(workspaceFolder, namespace, file, getFileName(root, file, atelier, addCategory, map)).catch(
154170
(error) => {
155171
errors.push(`${file} - ${error}`);
156172
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
849849
const { apiTarget } = connectionTarget(uri);
850850
if (typeof apiTarget === "string") {
851851
// It was a file-type uri, so find its document (we hope it is open)
852-
const docs = vscode.workspace.textDocuments.filter((doc) => doc.uri === uri);
852+
const docs = vscode.workspace.textDocuments.filter((doc) => doc.uri.toString() === uri.toString());
853853
let fileName = "";
854854
if (docs.length === 1) {
855855
// Found it, so work out the corresponding server-side name

src/providers/DocumentContentProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class DocumentContentProvider implements vscode.TextDocumentContentProvid
1414
}
1515

1616
public static getAsFile(name: string, workspaceFolder: string): string {
17-
const { atelier, folder, addCategory } = config("export", workspaceFolder);
17+
const { atelier, folder, addCategory, map } = config("export", workspaceFolder);
1818

1919
const root = [workspaceFolderUri(workspaceFolder).fsPath, folder].join(path.sep);
20-
const fileName = getFileName(root, name, atelier, addCategory);
20+
const fileName = getFileName(root, name, atelier, addCategory, map);
2121
if (fs.existsSync(fileName)) {
2222
return fs.realpathSync.native(fileName);
2323
}

0 commit comments

Comments
 (0)