Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pkg:/ paths for setFile #701

Merged
merged 1 commit into from
Sep 29, 2022
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
43 changes: 35 additions & 8 deletions src/Program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,35 +560,62 @@ describe('Program', () => {
expect(
getPaths('source/main.brs', rootDir)
).to.eql({
src: s`${rootDir}/source/main.brs`,
dest: s`source/main.brs`
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('works for absolute src', () => {
expect(
getPaths(`${rootDir}/source\\main.brs`, rootDir)
).to.eql({
src: s`${rootDir}/source/main.brs`,
dest: s`source/main.brs`
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('works for missing src', () => {
expect(
getPaths({ dest: 'source/main.brs' }, rootDir)
).to.eql({
src: s`${rootDir}/source/main.brs`,
dest: s`source/main.brs`
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('works for missing dest', () => {
expect(
getPaths({ src: `${rootDir}/source/main.brs` }, rootDir)
).to.eql({
src: s`${rootDir}/source/main.brs`,
dest: s`source/main.brs`
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('works for pkg string', () => {
expect(
getPaths('pkg:/source/main.brs', rootDir)
).to.eql({
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('favors pkgPath over destPath', () => {
expect(
getPaths({ srcPath: `${rootDir}/source/main.brs`, destPath: 'source/DontUse.brs', pkgPath: `pkg:/source/main.brs` })
).to.eql({
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});

it('works when given a file', () => {
expect(
getPaths({ srcPath: `${rootDir}/source/main.brs`, pkgPath: `source/main.brs` })
).to.eql({
srcPath: s`${rootDir}/source/main.brs`,
pkgPath: s`source/main.brs`
});
});
});
Expand Down
57 changes: 37 additions & 20 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export class Program {
public setFile<T extends BscFile>(fileEntry: FileObj, fileContents: string): T;
public setFile<T extends BscFile>(fileParam: FileObj | string, fileContents: string): T {
//normalize the file paths
const { src: srcPath, dest: pkgPath } = this.getPaths(fileParam, this.options.rootDir);
const { srcPath, pkgPath } = this.getPaths(fileParam, this.options.rootDir);

let file = this.logger.time(LogLevel.debug, ['Program.setFile()', chalk.green(srcPath)], () => {
//if the file is already loaded, remove it
Expand Down Expand Up @@ -492,48 +492,65 @@ export class Program {
* Given a srcPath, a pkgPath, or both, resolve whichever is missing, relative to rootDir.
* @param rootDir must be a pre-normalized path
*/
private getPaths(fileParam: string | FileObj, rootDir: string) {
let src: string;
let dest: string;
private getPaths(fileParam: string | FileObj | { srcPath?: string; pkgPath?: string }, rootDir: string) {
let srcPath: string;
let pkgPath: string;

assert.ok(fileParam, 'fileParam is required');

//lift the srcPath and pkgPath vars from the incoming param
if (typeof fileParam === 'string') {
src = s`${path.resolve(rootDir, fileParam)}`;
dest = s`${util.replaceCaseInsensitive(src, rootDir, '')}`;
fileParam = this.removePkgPrefix(fileParam);
srcPath = s`${path.resolve(rootDir, fileParam)}`;
pkgPath = s`${util.replaceCaseInsensitive(srcPath, rootDir, '')}`;
} else {
if (fileParam.src) {
src = s`${fileParam.src}`;
let param: any = fileParam;

if (param.src) {
srcPath = s`${param.src}`;
}
if (param.srcPath) {
srcPath = s`${param.srcPath}`;
}
if (param.dest) {
pkgPath = s`${this.removePkgPrefix(param.dest)}`;
}
if (fileParam.dest) {
dest = s`${fileParam.dest}`;
if (param.pkgPath) {
pkgPath = s`${this.removePkgPrefix(param.pkgPath)}`;
}
}

//if there's no srcPath, use the pkgPath to build an absolute srcPath
if (!src) {
src = s`${rootDir}/${dest}`;
if (!srcPath) {
srcPath = s`${rootDir}/${pkgPath}`;
}
//coerce srcPath to an absolute path
if (!path.isAbsolute(src)) {
src = util.standardizePath(src);
if (!path.isAbsolute(srcPath)) {
srcPath = util.standardizePath(srcPath);
}

//if there's no pkgPath, compute relative path from rootDir
if (!dest) {
dest = s`${util.replaceCaseInsensitive(src, rootDir, '')}`;
if (!pkgPath) {
pkgPath = s`${util.replaceCaseInsensitive(srcPath, rootDir, '')}`;
}

assert.ok(src, 'fileEntry.src is required');
assert.ok(dest, 'fileEntry.dest is required');
assert.ok(srcPath, 'fileEntry.src is required');
assert.ok(pkgPath, 'fileEntry.dest is required');

return {
src: src,
srcPath: srcPath,
//remove leading slash from pkgPath
dest: dest.replace(/^[\/\\]+/, '')
pkgPath: pkgPath.replace(/^[\/\\]+/, '')
};
}

/**
* Remove any leading `pkg:/` found in the path
*/
private removePkgPrefix(path: string) {
return path.replace(/^pkg:\//i, '');
}

/**
* Ensure source scope is created.
* Note: automatically called internally, and no-op if it exists already.
Expand Down