Skip to content

Commit b169ad3

Browse files
authored
Merge pull request #32 from nberth/fix-windows-ci
Fix Windows support
2 parents a118d7a + 47fc363 commit b169ad3

File tree

2 files changed

+44
-58
lines changed

2 files changed

+44
-58
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
os:
1616
- ubuntu-latest
1717
- macos-latest
18-
# - windows-latest
18+
- windows-latest
1919
node:
2020
- 20
2121
include:

src/parser.c.ts

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
import readline from "n-readlines";
2-
import * as nativePathFromPath from "path";
2+
import * as path from "path";
33
import * as fs from "fs";
44
import {DebuggerVariable, Attribute, VariableType} from "./debugger";
55

6-
const nativePath = {
7-
resolve: function (...args: string[]): string {
8-
const nat = nativePathFromPath.resolve(...args);
9-
return nat;
10-
},
11-
basename: function (path: string): string {
12-
return nativePathFromPath.basename(path);
13-
},
14-
isAbsolute: function (path: string): boolean {
15-
return nativePathFromPath.isAbsolute(path);
16-
},
17-
join: function (...args: string[]) {
18-
return nativePathFromPath.join(...args);
6+
function normalizeExistingFileName(f: string): string {
7+
if (f != undefined && process.platform === "win32") {
8+
const lc = f.toLowerCase();
9+
const uc = f.toUpperCase();
10+
return (!fs.existsSync(lc) || !fs.existsSync(uc)) ? f : lc;
1911
}
20-
};
12+
return f;
13+
}
2114

2215
function cFile (filename: string) : string {
23-
return nativePath.basename(filename.split('.').slice(0, -1).join('.') + '.c');
16+
return path.basename(filename.split('.').slice(0, -1).join('.') + '.c');
2417
}
2518

2619
const procedureRegex = /\/\*\sLine:\s([0-9]+)(\s+:\sEntry\s)?/i;
@@ -79,14 +72,14 @@ export class SourceMap {
7972
private isVersion2_2_or_3_1_1: boolean = false;
8073

8174
constructor(cwd: string, filesCobol: string[], sourceDirs: string[], private log: Function = (_ => {})) {
82-
this.cwd = fs.realpathSync(nativePathFromPath.resolve(cwd));
75+
this.cwd = fs.realpathSync(path.resolve(cwd));
8376
this.log(`Source dirs: ${sourceDirs}`);
8477
for (const cSourceDir of sourceDirs) {
8578
this.log(`Trying to resolve ${cSourceDir}`);
86-
let resolved_path = nativePathFromPath.resolve(this.cwd, cSourceDir);
79+
let resolved_path = path.resolve(this.cwd, cSourceDir);
8780
this.log(`Checking for ${resolved_path}`);
8881
if (fs.existsSync(resolved_path)) {
89-
this.sourceDirs.push(fs.realpathSync(resolved_path));
82+
this.sourceDirs.push(normalizeExistingFileName(fs.realpathSync(resolved_path)));
9083
}
9184
}
9285

@@ -104,9 +97,9 @@ export class SourceMap {
10497

10598
private lookupSourceFile (file: string) : string | undefined {
10699
for (const dir of this.sourceDirs) {
107-
const filePath = nativePath.join (dir, file);
100+
const filePath = path.join (dir, file);
108101
if (fs.existsSync (filePath)) {
109-
return filePath;
102+
return normalizeExistingFileName (filePath);
110103
}
111104
}
112105
return;
@@ -143,7 +136,7 @@ export class SourceMap {
143136
}
144137

145138
private unregister (givenFileC: string) : void {
146-
const [natFileC, fileC, cleanedFile] = this.ensureAbsolute (givenFileC);
139+
const [fileC, cleanedFile] = this.ensureAbsolute (givenFileC);
147140
this.lines = this.lines.filter (line => line.rootFileC != fileC) ?? [];
148141
for (const [k, v] of this.variablesByC) {
149142
if (v.rootFileC == fileC) this.variablesByC.delete(k);
@@ -153,17 +146,16 @@ export class SourceMap {
153146
}
154147
}
155148

156-
private ensureAbsolute (fileC: string) : [string, string, string] {
149+
private ensureAbsolute (fileC: string) : [string, string] {
157150
let nat = fileC;
158-
if (!nativePath.isAbsolute(fileC)) {
159-
nat = nativePathFromPath.resolve(this.cwd, fileC);
160-
fileC = nativePath.resolve(this.cwd, fileC);
151+
if (!path.isAbsolute(fileC)) {
152+
fileC = path.resolve(this.cwd, fileC);
161153
}
162154

163-
const basename = nativePath.basename(fileC);
155+
const basename = path.basename(fileC);
164156
const cleanedFile = basename.substring(0, basename.lastIndexOf(".c"));
165157

166-
return [process.platform === "win32" ? nat : fileC, fileC, cleanedFile];
158+
return [normalizeExistingFileName(fileC), cleanedFile];
167159
}
168160

169161
private register (givenFileC: string) : void {
@@ -176,24 +168,24 @@ export class SourceMap {
176168
rootFileC: string | undefined = undefined,
177169
functionName: string | undefined = undefined) : string {
178170

179-
const [natFileC, fileC, cleanedFile] = this.ensureAbsolute (givenFileC);
171+
const [fileC, cleanedFile] = this.ensureAbsolute (givenFileC);
180172
rootFileC = rootFileC ?? fileC;
181173

182174
let lineNumber = 0;
183175
let row: false | Buffer;
184176
let fileCobol: string;
185177

186-
const reader = new readline(natFileC);
178+
const reader = new readline(fileC);
187179
while (row = reader.next()) {
188180
const line = row.toString();
189181
let match = fileCobolRegex.exec(line);
190182
if (match) {
191183
const filename = match[1];
192-
const filebase = nativePath.basename (filename);
193-
if (!nativePath.isAbsolute(filename)) {
194-
fileCobol = nativePath.resolve(nativePathFromPath.dirname (fileC), filename);
184+
const filebase = path.basename (filename);
185+
if (!path.isAbsolute(filename)) {
186+
fileCobol = path.resolve(path.dirname (fileC), filename);
195187
if (!fs.existsSync(fileCobol)) {
196-
fileCobol = nativePath.resolve(nativePathFromPath.dirname (fileC), filebase);
188+
fileCobol = path.resolve(path.dirname (fileC), filebase);
197189
}
198190
if (!fs.existsSync(fileCobol)) {
199191
fileCobol = this.lookupSourceFile (filename);
@@ -202,16 +194,17 @@ export class SourceMap {
202194
fileCobol = filename;
203195
}
204196
if (!fs.existsSync(fileCobol)) {
205-
fileCobol = this.lookupSourceFile (filebase)
197+
fileCobol = this.lookupSourceFile (filebase);
206198
}
199+
fileCobol = normalizeExistingFileName (fileCobol);
207200
}
208201
match = functionRegex.exec(line);
209202
if (match) {
210203
functionName = cobEncodeInvalidChars(match[1]).toLowerCase() + "_";
211204
}
212205
match = procedureRegex.exec(line);
213206
if (match && !match[2]) {
214-
if (this.lines.length > 0 && fileNameCompare(this.lines[this.lines.length - 1].fileCobol, fileCobol) && this.lines[this.lines.length - 1].lineCobol === parseInt(match[1])) {
207+
if (this.lines.length > 0 && this.lines[this.lines.length - 1].fileCobol === fileCobol && this.lines[this.lines.length - 1].lineCobol === parseInt(match[1])) {
215208
this.lines.pop();
216209
}
217210
if(subroutineRegex.exec(line))
@@ -224,7 +217,7 @@ export class SourceMap {
224217
match = procedureFixRegex.exec(line);
225218
if (match && this.lines.length > 0 && this.lines[this.lines.length - 1].functionName == functionName) {
226219
let isOldFormat = fixOlderFormat.exec(prevLine);
227-
if(fileNameCompare(this.lines[this.lines.length - 1].fileCobol, fileCobol) && (this.isVersion2_2_or_3_1_1 || !isOldFormat)){ // Is it in the old format?
220+
if(this.lines[this.lines.length - 1].fileCobol === fileCobol && (this.isVersion2_2_or_3_1_1 || !isOldFormat)){ // Is it in the old format?
228221
let line = this.lines.pop();
229222
// this.log (`Fixing line: ${line.toString ()}`);
230223
line.lineC = parseInt(match[1]);
@@ -268,7 +261,7 @@ export class SourceMap {
268261
match = fileIncludeRegex.exec(line);
269262
if (match) {
270263
// Note: we assume the included file is in the same dir as the current file.
271-
const filename = nativePath.resolve (nativePathFromPath.dirname (rootFileC), match[1]);
264+
const filename = path.resolve (path.dirname (rootFileC), match[1]);
272265
functionName = this.parse(filename, prevLine, rootFileC, functionName);
273266
}
274267
match = versionRegex.exec(line);
@@ -329,42 +322,42 @@ export class SourceMap {
329322

330323
public hasLineCobol(fileC: string, lineC: number): boolean {
331324
if(!fileC || !lineC) return false;
332-
if (!nativePath.isAbsolute(fileC)) {
333-
fileC = nativePath.join(this.cwd, fileC);
325+
if (!path.isAbsolute(fileC)) {
326+
fileC = path.join(this.cwd, fileC);
334327
}
335328
return this.lines.some(e => e.fileC === fileC && e.lineC === lineC);
336329
}
337330

338331
// 002 - stepOver in routines with "perform"
339332
public hasLineSubroutine(fileC: string, lineC: number): number {
340333
if(!fileC || !lineC) return -1;
341-
if (!nativePath.isAbsolute(fileC)) {
342-
fileC = nativePath.join(this.cwd, fileC);
334+
if (!path.isAbsolute(fileC)) {
335+
fileC = path.join(this.cwd, fileC);
343336
}
344337
return this.lines.find(e => e.fileC === fileC && e.lineC === lineC)?.endPerformLine ?? -1;
345338
}
346339
// 002
347340

348341
public hasLineC(fileCobol: string, lineCobol: number): boolean {
349-
if (!nativePath.isAbsolute(fileCobol)) {
350-
fileCobol = nativePath.join(this.cwd, fileCobol);
342+
if (!path.isAbsolute(fileCobol)) {
343+
fileCobol = path.join(this.cwd, fileCobol);
351344
}
352-
return this.lines.some(e => fileNameCompare(e.fileCobol, fileCobol) && e.lineCobol === lineCobol);
345+
return this.lines.some(e => e.fileCobol === fileCobol && e.lineCobol === lineCobol);
353346
}
354347

355348
public getLineC(fileCobol: string, lineCobol: number): Line {
356-
if (!nativePath.isAbsolute(fileCobol)) {
357-
fileCobol = nativePath.join(this.cwd, fileCobol);
349+
if (!path.isAbsolute(fileCobol)) {
350+
fileCobol = path.join(this.cwd, fileCobol);
358351
}
359-
return this.lines.find(e => fileNameCompare(e.fileCobol, fileCobol) && e.lineCobol === lineCobol) ?? dummyLine;
352+
return this.lines.find(e => e.fileCobol === fileCobol && e.lineCobol === lineCobol) ?? dummyLine;
360353
}
361354

362355
public getLineCobol(fileC: string, lineC: number): Line {
363356
if (!fileC) {
364357
return dummyLine;
365358
}
366-
if (!nativePath.isAbsolute(fileC)) {
367-
fileC = nativePath.join(this.cwd, fileC);
359+
if (!path.isAbsolute(fileC)) {
360+
fileC = path.join(this.cwd, fileC);
368361
}
369362
return this.lines.find(e => e.fileC === fileC && e.lineC === lineC) ?? dummyLine;
370363
}
@@ -392,10 +385,3 @@ export class SourceMap {
392385
}
393386

394387
}
395-
396-
function fileNameCompare(fileNameOne: string, fileNameTwo: string): boolean {
397-
if(process.platform === "win32")
398-
return fileNameOne.toUpperCase() === fileNameTwo.toUpperCase();
399-
else
400-
return fileNameOne === fileNameTwo;
401-
}

0 commit comments

Comments
 (0)