Skip to content

Commit

Permalink
Support for IFS linter, fix for picking up new changes
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Barry Allan <[email protected]>
  • Loading branch information
worksofliam committed Nov 8, 2022
1 parent 1b0f034 commit 49c67fe
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 61 deletions.
8 changes: 8 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export function activate(context: ExtensionContext) {
return;
});

client.onRequest("getWorkingDirectory", async (): Promise<string|undefined> => {
const instance = getBase();
if (instance && instance.getConnection()) {
const config = instance.getConfig();
return config.homeDirectory;
}
})

client.onRequest("getFile", async (stringUri: string): Promise<string | undefined> => {
// Always assumes URI is valid. Use getUri first
const uri = Uri.parse(stringUri);
Expand Down
133 changes: 74 additions & 59 deletions client/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function initialise(context: ExtensionContext) {
console.log(`Creating Uri path: ${JSON.stringify(uri)}`);

await workspace.fs.writeFile(
uri,
uri,
Buffer.from(JSON.stringify(defaultConfig, null, 2), `utf8`)
);
}
Expand All @@ -46,7 +46,7 @@ export function initialise(context: ExtensionContext) {
} else if (instance && instance.getConnection()) {
/** @type {"member"|"streamfile"} */
let type = `member`;
let configPath: string|undefined;
let configPath: string | undefined;

if (filter && filter.description) {
// Bad way to get the library for the filter ..
Expand All @@ -56,25 +56,32 @@ export function initialise(context: ExtensionContext) {
} else if (editor) {
//@ts-ignore
type = editor.document.uri.scheme;

console.log(`Uri remote path: ${JSON.stringify(editor.document.uri)}`);

const [_, baseLibrary, baseSourceFile, basename] = editor.document.uri.path.split(`/`);
const cleanString = [
baseLibrary,
`VSCODE`,
`RPGLINT.JSON`
].join(`/`);

const memberUri = Uri.from({
scheme: `member`,
path: cleanString
});
console.log(`Uri remote path: ${JSON.stringify(editor.document.uri)}`);

if (memberUri) {
configPath = memberUri.path;
} else {
window.showErrorMessage(`No lint config path for this file. File must either be a member or a streamfile on the host IBM i.`);
switch (type) {
case `member`:
const [_, baseLibrary, baseSourceFile, basename] = editor.document.uri.path.split(`/`);
const cleanString = [
baseLibrary,
`VSCODE`,
`RPGLINT.JSON`
].join(`/`);

const memberUri = Uri.from({
scheme: `member`,
path: cleanString
});

configPath = memberUri.path;
break;

case `streamfile`:
const config = instance.getConfig();
if (config.homeDirectory) {
configPath = path.posix.join(config.homeDirectory, `.vscode`, `rpglint.json`)
}
break;
}
} else {
window.showErrorMessage(`No active editor found.`);
Expand All @@ -89,52 +96,60 @@ export function initialise(context: ExtensionContext) {
const content = instance.getContent();

window.showErrorMessage(`RPGLE linter config doesn't exist for this file. Would you like to create a default at ${configPath}?`, `Yes`, `No`).then
(async (value) => {
if (value === `Yes`) {
const jsonString = JSON.stringify(defaultConfig, null, 2);

switch (type) {
case `member`:
if (configPath) {
const memberPath = configPath.split(`/`);

// Will not crash, even if it fails
await commands.executeCommand(
`code-for-ibmi.runCommand`,
{
'command': `CRTSRCPF FILE(${memberPath[0]}/VSCODE) RCDLEN(112)`
}
);

// Will not crash, even if it fails
await commands.executeCommand(
`code-for-ibmi.runCommand`,
{
command: `ADDPFM FILE(${memberPath[0]}/VSCODE) MBR(RPGLINT) SRCTYPE(JSON)`
(async (value) => {
if (value === `Yes`) {
const jsonString = JSON.stringify(defaultConfig, null, 2);

switch (type) {
case `member`:
if (configPath) {
const memberPath = configPath.split(`/`);

// Will not crash, even if it fails
await commands.executeCommand(
`code-for-ibmi.runCommand`,
{
'command': `CRTSRCPF FILE(${memberPath[0]}/VSCODE) RCDLEN(112)`
}
);

// Will not crash, even if it fails
await commands.executeCommand(
`code-for-ibmi.runCommand`,
{
command: `ADDPFM FILE(${memberPath[0]}/VSCODE) MBR(RPGLINT) SRCTYPE(JSON)`
}
);

try {
console.log(`Member path: ${[memberPath[0], `VSCODE`, `RPGLINT`].join(`/`)}`);

await content.uploadMemberContent(null, memberPath[0], `VSCODE`, `RPGLINT`, jsonString);
await commands.executeCommand(`code-for-ibmi.openEditable`, configPath);
} catch (e) {
console.log(e);
window.showErrorMessage(`Failed to create and open new lint configuration file: ${configPath}`);
}
}
);
break;

try {
console.log(`Member path: ${[memberPath[0], `VSCODE`, `RPGLINT`].join(`/`)}`);
case `streamfile`:
console.log(`IFS path: ${configPath}`);

await content.uploadMemberContent(null, memberPath[0], `VSCODE`, `RPGLINT`, jsonString);
await commands.executeCommand(`code-for-ibmi.openEditable`, configPath);
} catch (e) {
console.log(e);
}
try {
await content.writeStreamfile(configPath, jsonString);
await commands.executeCommand(`code-for-ibmi.openEditable`, configPath);
} catch (e) {
console.log(e);
window.showErrorMessage(`Failed to create and open new lint configuration file: ${configPath}`);
}
break;
}
break;

case `streamfile`:
console.log(`IFS path: ${configPath}`);

await content.writeStreamfile(configPath, jsonString);
await commands.executeCommand(`code-for-ibmi.openEditable`, configPath);
break;
}
}
});
});
}
} else {
window.showErrorMessage(`No lint config path for this file. File must either be a member or a streamfile on the host IBM i.`);
}
} else {
window.showErrorMessage(`Not connected to a system.`);
Expand Down
4 changes: 4 additions & 0 deletions server/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export async function getFileRequest(uri: string) {
return;
}

export function getWorkingDirectory(): Promise<string|undefined> {
return connection.sendRequest("getWorkingDirectory");
}

export function getObject(objectPath: string): Promise<object[]> {
return connection.sendRequest("getObject", objectPath);
}
Expand Down
16 changes: 14 additions & 2 deletions server/src/providers/linter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import codeActionsProvider from './codeActions';
import documentFormattingProvider from './documentFormatting';

import * as Project from "../project";
import { connection, getFileRequest, validateUri, watchedFilesChangeEvent } from '../../connection';
import { connection, getFileRequest, getWorkingDirectory, validateUri, watchedFilesChangeEvent } from '../../connection';

export let jsonCache: { [uri: string]: string } = {};

Expand Down Expand Up @@ -56,7 +56,7 @@ export function initialise(connection: _Connection) {
}
});

documents.onDidClose(async (e: TextDocumentChangeEvent<TextDocument>) => {
documents.onDidOpen(async e => {
const uri = e.document.uri;

const possibleUri = await getLintConfigUri(uri);
Expand Down Expand Up @@ -104,6 +104,18 @@ export async function getLintConfigUri(workingUri: string) {
if (jsonCache[cleanString]) return cleanString;
cleanString = await validateUri(cleanString);
break;

case `streamfile`:
const workingDir = await getWorkingDirectory();
if (workingDir) {
cleanString = URI.from({
scheme: `streamfile`,
path: path.posix.join(workingDir, `.vscode`, `rpglint.json`)
}).toString();

cleanString = await validateUri(cleanString, uri.scheme);
}
break;

case `file`:
cleanString = await validateUri(`rpglint.json`, uri.scheme);
Expand Down

0 comments on commit 49c67fe

Please sign in to comment.