Skip to content

Commit

Permalink
chore: wip testing windows compatibility (#2)
Browse files Browse the repository at this point in the history
* chore: wip testing windows compatibility

* chore: testing
  • Loading branch information
aorumbayev authored Dec 7, 2023
1 parent 5e6a4e2 commit 7c5a1ed
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"git.branchProtectionPrompt": "alwaysCommitToNewBranch",

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
"editor.formatOnSave": true,

"mochaExplorer.files": "tests/**/*.ts",
"mochaExplorer.require": "ts-node/register"
}
13 changes: 13 additions & 0 deletions extension/src/fileAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ export const workspaceFileAccessor: FileAccessor = {
}
return uri.path.substring(lastSlash + 1);
},
filePathRelativeTo: function (base: string, filePath: string): string {
// Create a Uri object with the base path
const baseUri = vscode.Uri.file(base);

// Resolve the file path against the base Uri
const fullUri = vscode.Uri.joinPath(baseUri, filePath);

// Extract the path from the Uri
const resolvedPath = fullUri.path;

// Normalize the resolved path
return resolvedPath.replace(/\\/g, '/');
},
};

function pathToUri(path: string) {
Expand Down
1 change: 1 addition & 0 deletions src/common/fileAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export interface FileAccessor {
isWindows: boolean;
readFile(path: string): Promise<Uint8Array>;
writeFile(path: string, contents: Uint8Array): Promise<void>;
filePathRelativeTo(base: string, filePath: string): string;
basename(path: string): string;
}
2 changes: 1 addition & 1 deletion src/common/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class AvmRuntime extends EventEmitter {

private normalizePathAndCasing(filePath: string) {
if (this.fileAccessor.isWindows) {
return filePath.replace(/\//g, '\\').toLowerCase();
return filePath.replace(/\//g, '\\');
} else {
return filePath.replace(/\\/g, '/');
}
Expand Down
7 changes: 6 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,23 @@ interface ProgramSourceEntry {
}

export class ProgramSourceDescriptor {
public readonly fileAccessor: FileAccessor;
public readonly sourcemapFileLocation: string;
public readonly sourcemap: algosdk.ProgramSourceMap;
public readonly hash: Uint8Array;

constructor({
fileAccessor,
sourcemapFileLocation,
sourcemap,
hash,
}: {
fileAccessor: FileAccessor;
sourcemapFileLocation: string;
sourcemap: algosdk.ProgramSourceMap;
hash: Uint8Array;
}) {
this.fileAccessor = fileAccessor;
this.sourcemapFileLocation = sourcemapFileLocation;
this.sourcemap = sourcemap;
this.hash = hash;
Expand All @@ -172,7 +176,7 @@ export class ProgramSourceDescriptor {
}

public getFullSourcePath(index: number): string {
return filePathRelativeTo(
return this.fileAccessor.filePathRelativeTo(
this.sourcemapFileLocation,
this.sourcemap.sources[index],
);
Expand All @@ -196,6 +200,7 @@ export class ProgramSourceDescriptor {
);

return new ProgramSourceDescriptor({
fileAccessor,
sourcemapFileLocation,
sourcemap,
hash: algosdk.base64ToBytes(data.hash),
Expand Down
19 changes: 19 additions & 0 deletions src/node/fileAccessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFile, writeFile } from 'fs/promises';
import { basename } from 'path';
import { FileAccessor } from '../common';
import * as path from 'path';

export const nodeFileAccessor: FileAccessor = {
isWindows: process.platform === 'win32',
Expand All @@ -13,4 +14,22 @@ export const nodeFileAccessor: FileAccessor = {
basename(path: string): string {
return basename(path);
},
filePathRelativeTo: function (base: string, filePath: string): string {
// Create a URL object with the file protocol and the base path
const baseURL = new URL(base, 'file:///');

// Resolve the file path against the base URL
const fullURL = new URL(filePath, baseURL);

// Convert the URL back to a local file path
// On Windows, this will correctly handle the drive letter and convert to backslashes
const resolvedPath = path.resolve(fullURL.pathname);

// Normalize the resolved path to ensure it's in the correct format for the current OS
if (this.isWindows) {
return resolvedPath.replace(/\//g, '\\');
} else {
return resolvedPath.replace(/\\/g, '/');
}
},
};

0 comments on commit 7c5a1ed

Please sign in to comment.