Skip to content

Commit 2ebb328

Browse files
committed
import and compile entire folder
1 parent d0f9b55 commit 2ebb328

File tree

5 files changed

+104
-45
lines changed

5 files changed

+104
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- added Server actions menu, by clicking on server info from status bar. Open Management portal, Class Reference and toggle connection.
77
- Class Suggestion in ##class, Extends, As, CompileAfter, DependsOn, PropertyClass
88
- \$SYSTEM suggestion by Classes from %SYSTEM
9+
- Import and compile folder or file by context menu in File explorer
910

1011
## [0.7.10]
1112

commands/compile.ts

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import vscode = require('vscode');
22
import fs = require('fs');
3+
import path = require('path');
4+
import glob = require('glob');
35
import { AtelierAPI } from '../api';
46
import { currentFile, CurrentFile, outputChannel } from '../utils';
57
import { documentContentProvider, config } from '../extension';
@@ -13,7 +15,7 @@ async function compileFlags(): Promise<string> {
1315
});
1416
}
1517

16-
async function importFile(file: CurrentFile, flags: string): Promise<any> {
18+
async function importFile(file: CurrentFile): Promise<any> {
1719
const api = new AtelierAPI();
1820
return api
1921
.putDoc(
@@ -23,13 +25,7 @@ async function importFile(file: CurrentFile, flags: string): Promise<any> {
2325
content: file.content.split(/\r?\n/)
2426
},
2527
true
26-
)
27-
.then(() => compile(file, flags))
28-
.catch((error: Error) => {
29-
outputChannel.appendLine(error.message);
30-
outputChannel.show(true);
31-
vscode.window.showErrorMessage(error.message);
32-
});
28+
);
3329
}
3430

3531
function updateOthers(others: string[]) {
@@ -39,30 +35,42 @@ function updateOthers(others: string[]) {
3935
});
4036
}
4137

42-
async function loadChanges(file: CurrentFile): Promise<any> {
38+
async function loadChanges(files: CurrentFile[]): Promise<any> {
4339
const api = new AtelierAPI();
44-
return api.getDoc(file.name).then(data => {
45-
fs.writeFileSync(file.fileName, (data.result.content || []).join('\n'));
46-
api
47-
.actionIndex([file.name])
48-
.then(data => data.result.content[0].others)
49-
.then(updateOthers);
50-
});
40+
return Promise.all(files.map(file =>
41+
api.getDoc(file.name).then(data => {
42+
fs.writeFileSync(file.fileName, (data.result.content || []).join('\n'));
43+
return api
44+
.actionIndex([file.name])
45+
.then(data => data.result.content[0].others)
46+
.then(updateOthers);
47+
})
48+
));
5149
}
5250

53-
async function compile(file: CurrentFile, flags: string): Promise<any> {
51+
async function compile(docs: CurrentFile[], flags?: string): Promise<any> {
52+
flags = flags || config().compileFlags;
5453
const api = new AtelierAPI();
5554
return api
56-
.actionCompile([file.name], flags)
55+
.actionCompile(docs.map(el => el.name), flags)
5756
.then(data => {
57+
let info = docs.length > 1 ? '' : `${docs[0].name}: `;
5858
if (data.status && data.status.errors && data.status.errors.length) {
59-
throw new Error(`${file.name}: Compile error`);
59+
throw new Error(`${info}Compile error`);
6060
} else {
61-
vscode.window.showInformationMessage(`${file.name}: Compile successed`);
61+
vscode.window.showInformationMessage(`${info}Compile successed`, 'Hide');
6262
}
63-
return file;
63+
return docs;
6464
})
65-
.then(loadChanges);
65+
.then(loadChanges)
66+
.catch((error: Error) => {
67+
outputChannel.appendLine(error.message);
68+
outputChannel.show(true);
69+
vscode.window.showErrorMessage(error.message, 'Show details')
70+
.then(data => {
71+
outputChannel.show(true);
72+
});
73+
});
6674
}
6775

6876
export async function importAndCompile(askFLags = false): Promise<any> {
@@ -76,15 +84,15 @@ export async function importAndCompile(askFLags = false): Promise<any> {
7684

7785
const defaultFlags = config().compileFlags;
7886
const flags = askFLags ? await compileFlags() : defaultFlags;
79-
return importFile(file, flags).catch(error => {
87+
return importFile(file).catch(error => {
8088
console.error(error);
81-
});
89+
}).then(() => compile([file], flags));
8290
}
8391

8492
// Compiles all files types in the namespace
8593
export async function namespaceCompile(askFLags = false): Promise<any> {
8694
const api = new AtelierAPI();
87-
const fileTypes = ["*.CLS", "*.MAC", "*.INC", "*.BAS"]
95+
const fileTypes = ['*.CLS', '*.MAC', '*.INC', '*.BAS'];
8896
if (!config('conn').active) {
8997
throw new Error(`No Active Connection`);
9098
}
@@ -94,21 +102,58 @@ export async function namespaceCompile(askFLags = false): Promise<any> {
94102
// User cancelled
95103
return;
96104
}
97-
vscode.window.withProgress({
98-
location: vscode.ProgressLocation.Notification,
99-
title: `Compiling Namespace: ${api.ns}`,
100-
cancellable: false
101-
}, async () => {
102-
const data = await api
103-
.actionCompile(fileTypes, flags);
104-
if (data.status && data.status.errors && data.status.errors.length) {
105-
console.error(data.status.summary);
106-
throw new Error(`Compiling Namespace: ${api.ns} Error`);
107-
}
108-
else {
109-
vscode.window.showInformationMessage(`Compiling Namespace: ${api.ns} Success`);
105+
vscode.window.withProgress(
106+
{
107+
location: vscode.ProgressLocation.Notification,
108+
title: `Compiling Namespace: ${api.ns}`,
109+
cancellable: false
110+
},
111+
async () => {
112+
const data = await api.actionCompile(fileTypes, flags);
113+
if (data.status && data.status.errors && data.status.errors.length) {
114+
console.error(data.status.summary);
115+
throw new Error(`Compiling Namespace: ${api.ns} Error`);
116+
} else {
117+
vscode.window.showInformationMessage(`Compiling Namespace: ${api.ns} Success`);
118+
}
119+
const file = currentFile();
120+
return loadChanges([file]);
110121
}
111-
const file = currentFile();
112-
return loadChanges(file);
113-
});
122+
);
123+
}
124+
125+
function importFiles(files) {
126+
return Promise.all<CurrentFile>(
127+
files.map(file =>
128+
vscode.workspace
129+
.openTextDocument(file)
130+
.then(currentFile)
131+
.then(file =>
132+
Promise.resolve(file)
133+
.then(importFile)
134+
.then(data => {
135+
outputChannel.appendLine('Imported file: ' + file.fileName)
136+
return file;
137+
})
138+
)
139+
)
140+
)
141+
.then(compile);
142+
}
143+
144+
export async function importFolder(uri: vscode.Uri): Promise<any> {
145+
let folder = uri.path;
146+
if (fs.lstatSync(folder).isFile()) {
147+
return importFiles([folder]);
148+
}
149+
glob(
150+
'**/*.{cls,inc,mac,int}',
151+
{
152+
cwd: folder,
153+
nocase: true
154+
},
155+
(error, files) => importFiles(
156+
files.map(name => path.join(folder, name))
157+
)
158+
);
114159
}

extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const OBJECTSCRIPT_FILE_SCHEMA = 'objectscript';
44
export const OBJECTSCRIPTXML_FILE_SCHEMA = 'objectscriptxml';
55

66
import { viewOthers } from './commands/viewOthers';
7-
import { importAndCompile, namespaceCompile } from './commands/compile';
7+
import { importAndCompile, namespaceCompile, importFolder as importFileOrFolder } from './commands/compile';
88
import { exportAll, exportExplorerItem } from './commands/export';
99
import { xml2doc } from './commands/xml2doc';
1010
import { subclass } from './commands/subclass';
@@ -140,6 +140,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
140140
vscode.commands.registerCommand('vscode-objectscript.compileWithFlags', () => importAndCompile(true)),
141141
vscode.commands.registerCommand('vscode-objectscript.compileAll', () => namespaceCompile(false)),
142142
vscode.commands.registerCommand('vscode-objectscript.compileAllWithFlags', () => namespaceCompile(true)),
143+
vscode.commands.registerCommand('vscode-objectscript.compileFolder', importFileOrFolder),
143144
vscode.commands.registerCommand('vscode-objectscript.export', exportAll),
144145
vscode.commands.registerCommand('vscode-objectscript.viewOthers', viewOthers),
145146
vscode.commands.registerCommand('vscode-objectscript.subclass', subclass),
@@ -209,4 +210,4 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
209210
);
210211
}
211212

212-
export async function deactivate() {}
213+
export async function deactivate() { }

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@
178178
"group": "objectscript.viewOthers",
179179
"when": "editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive"
180180
}
181+
],
182+
"explorer/context": [
183+
{
184+
"command": "vscode-objectscript.compileFolder",
185+
"when": "vscode-objectscript.connectActive"
186+
}
181187
]
182188
},
183189
"languages": [
@@ -347,6 +353,11 @@
347353
"category": "ObjectScript",
348354
"command": "vscode-objectscript.serverActions",
349355
"title": "Show server actions"
356+
},
357+
{
358+
"category": "ObjectScsript",
359+
"command": "vscode-objectscript.compileFolder",
360+
"title": "Import and compile"
350361
}
351362
],
352363
"keybindings": [
@@ -465,6 +476,7 @@
465476
"description": "Autocompile on save file",
466477
"type": "boolean",
467478
"default": false,
479+
"deprecationMessage": "Always enabled, now, when connection is active",
468480
"scope": "resource"
469481
},
470482
"objectscript.showExplorer": {

utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export interface CurrentFile {
1818
uri: vscode.Uri;
1919
}
2020

21-
export function currentFile(): CurrentFile {
22-
const document = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document : null;
21+
export function currentFile(document?: vscode.TextDocument): CurrentFile {
22+
document = document || (vscode.window.activeTextEditor.document ? vscode.window.activeTextEditor.document : null);
2323
if (!document || !document.fileName || !document.languageId || !document.languageId.startsWith('objectscript')) {
2424
return null;
2525
}

0 commit comments

Comments
 (0)