Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 80 additions & 9 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cli/src/languages/rpgle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function setupParser(targets: Targets): Parser {
// Keep making the path less specific until we find a possible include
let parts = includeFile.split(`/`);
while (!file && parts.length > 0) {
file = targets.resolveLocalFile(includeFile);
file = await targets.resolveLocalFile(includeFile);

if (!file) {
parts.shift();
Expand All @@ -39,9 +39,9 @@ export function setupParser(targets: Targets): Parser {
includeFile = `${parent}/${includeFile}`;


file = targets.resolveLocalFile(includeFile);
file = await targets.resolveLocalFile(includeFile);
} else {
file = targets.resolveLocalFile(includeFile);
file = await targets.resolveLocalFile(includeFile);
}

if (file) {
Expand Down
40 changes: 21 additions & 19 deletions cli/src/targets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import glob from 'glob';
import path from 'path';
import Cache from "vscode-rpgle/language/models/cache";
import { IncludeStatement } from "vscode-rpgle/language/parserTypes";
Expand All @@ -11,7 +10,7 @@ import { rpgExtensions, clExtensions, ddsExtension, sqlExtensions, srvPgmExtensi
import Parser from "vscode-rpgle/language/parser";
import { setupParser } from './languages/rpgle';
import { Logger } from './logger';
import { asPosix, getReferenceObjectsFrom, getSystemNameFromPath, toLocalPath } from './utils';
import { asPosix, getReferenceObjectsFrom, getSystemNameFromPath, globalEntryIsValid, toLocalPath } from './utils';
import { extCanBeProgram, getObjectType } from './builders/environment';
import { isSqlFunction } from './languages/sql';
import { ReadFileSystem } from './readFileSystem';
Expand Down Expand Up @@ -294,40 +293,43 @@ export class Targets {
return this.getResolvedObjects().find(o => (o.systemName === lookFor.name || o.longName?.toUpperCase() === lookFor.name) && (lookFor.types === undefined || lookFor.types.includes(o.type)));
}

public resolveLocalFile(name: string, baseFile?: string): string | undefined {
public async resolveLocalFile(name: string, baseFile?: string): Promise<string> {
name = name.toUpperCase();

if (this.resolvedSearches[name]) return this.resolvedSearches[name];

if (!this.pathCache) {
// We don't really want to spam the FS
// So we can a list of files which can then
// use in glob again later.
this.pathCache = {};

glob.sync(`**/*`, {
(await this.fs.getFiles(this.getCwd(), `**/*`, {
cwd: this.cwd,
absolute: true,
nocase: true,
}).forEach(localPath => {
})).forEach(localPath => {
this.pathCache[localPath] = true;
});
}

let globString = `**/${name}*`;
const searchCache = (): string|undefined => {
for (let entry in this.pathCache) {
if (Array.isArray(this.pathCache[entry])) {
const subEntry = this.pathCache[entry].find(e => globalEntryIsValid(e, name));
if (subEntry) {
return subEntry;
}
} else {
if (globalEntryIsValid(entry, name)) {
return entry;
}
}
}
}

// TODO: replace with rfs.getFiles
const results = glob.sync(globString, {
cwd: this.cwd,
absolute: true,
nocase: true,
ignore: baseFile ? `**/${baseFile}` : undefined,
cache: this.pathCache
});
const result = searchCache();

if (results[0]) {
if (result) {
// To local path is required because glob returns posix paths
const localPath = toLocalPath(results[0])
const localPath = toLocalPath(result)
this.resolvedSearches[name] = localPath;
return localPath;
}
Expand Down
56 changes: 54 additions & 2 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export function getReferenceObjectsFrom(content: string) {
return pseudoObjects;
}

export function fromCl(cl: string): {command: string, parameters: CommandParameters} {
export function fromCl(cl: string): { command: string, parameters: CommandParameters } {
let gotCommandnName = false;
let parmDepth = 0;

Expand Down Expand Up @@ -253,7 +253,59 @@ export function toCl(command: string, parameters?: CommandParameters) {
}

export function checkFileExists(file) {
return fs.promises.access(file, fs.constants.F_OK)
return fs.promises.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
}

export function globalEntryIsValid(fullPath: string, search: string, ignoreBase?: string) {
search = search.toUpperCase();

if (ignoreBase) {
if (fullPath.toUpperCase().endsWith(ignoreBase.toUpperCase())) {
return false;
}
}

const baseParts = fullPath.toUpperCase().split(path.sep);
const nameParts = search.split(path.sep);

// Check the preceding parts of the path match
if (nameParts.length > 1) {
for (let i = 1; i < nameParts.length - 1; i++) {
const bPart = baseParts[baseParts.length - i];
const nPart = nameParts[nameParts.length - i - 1];

if (bPart && nPart) {
if (bPart !== nPart) return false;
}
}
}

const baseName = nameParts[nameParts.length - 1];

// Check different variations of the name.

if (baseName.endsWith('*')) {
// If the name ends with *, we do a startsWith check
const tempName = search.substring(0, search.length - 1);
const lastChar = tempName.charAt(tempName.length - 1);
const messedUpFullName = fullPath.lastIndexOf(lastChar) >= 0 ? fullPath.substring(0, fullPath.lastIndexOf(lastChar) + 1) : fullPath;
if (messedUpFullName.toUpperCase().endsWith(tempName)) return true;
} else if (!baseName.includes('.')) {
// If the name does not include a dot, we check if the current part (without extension) matches
let currentPart = baseParts[baseParts.length - 1];
if (currentPart.includes('.')) {
currentPart = currentPart.substring(0, currentPart.indexOf('.'));
}

if (currentPart === baseName) {
return true;
}

} else if (baseParts[baseParts.length - 1] === baseName) {
return true;
}

return false;
}
2 changes: 2 additions & 0 deletions cli/test/autofix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
const programs = targets.getTargetsOfType(`PGM`);
expect(programs.length).toBe(1);

expect(programs[0].headers.length).toBe(1);

Check failure on line 171 in cli/test/autofix.test.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

test/autofix.test.ts > Fix includes in same directory

TypeError: Cannot read properties of undefined (reading 'length') ❯ test/autofix.test.ts:171:30

let allLogs = targets.logger.getAllLogs();
expect(Object.keys(allLogs).length).toBe(1)

Expand Down
Loading