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

allow optionally specifying bslib destination directory #871

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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,16 @@ Type: `boolean`

Allow BrighterScript features (classes, interfaces, etc...) to be included in BrightScript (`.brs`) files, and force those files to be transpiled.

#### `bslibDestinationDir`

Type: `string`

Override the destination directory for the bslib.brs file. Use this if you want
to customize where the bslib.brs file is located in the staging directory. Note
that using a location outside of `source` will break scripts inside `source`
that depend on bslib.brs. Defaults to `source`.


## Ignore errors and warnings on a per-line basis
In addition to disabling an entire class of errors in `bsconfig.json` by using `ignoreErrorCodes`, you may also disable errors for a subset of the complier rules within a file with the following comment flags:
- `bs:disable-next-line`
Expand Down
8 changes: 8 additions & 0 deletions src/BsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,12 @@ export interface BsConfig {
* @default false
*/
allowBrighterScriptInBrightScript?: boolean;

/**
* Override the destination directory for the bslib.brs file. Use this if
* you want to customize where the bslib.brs file is located in the staging
* directory. Note that using a location outside of `source` will break
* scripts inside `source` that depend on bslib.brs. Defaults to `source`.
*/
bslibDestinationDir?: string;
}
28 changes: 28 additions & 0 deletions src/Program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,16 @@ describe('Program', () => {
expect(fsExtra.pathExistsSync(s`${stagingDir}/source/bslib.brs`)).is.true;
});

it('copies the bslib.brs file to optionally specified directory', async () => {
fsExtra.ensureDirSync(program.options.stagingDir);
program.options.bslibDestinationDir = 'source/opt';
program.validate();

await program.transpile([], program.options.stagingDir);

expect(fsExtra.pathExistsSync(s`${stagingDir}/source/opt/bslib.brs`)).is.true;
});

describe('getTranspiledFileContents', () => {
it('fires plugin events', async () => {
const file = program.setFile('source/main.brs', trim`
Expand Down Expand Up @@ -2079,6 +2089,24 @@ describe('Program', () => {
`);
});

it('uses custom bslib path when specified in .xml file', async () => {
program.options.bslibDestinationDir = 'source/opt';
program.setFile('components/Component1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="Component1" extends="Scene">
</component>
`);
await program.transpile([], program.options.stagingDir);
expect(trimMap(
fsExtra.readFileSync(s`${stagingDir}/components/Component1.xml`).toString()
)).to.eql(trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="Component1" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/opt/bslib.brs" />
</component>
`);
});

it('uses sourceRoot when provided for brs files', async () => {
let sourceRoot = s`${tempDir}/sourceRootFolder`;
program = new Program({
Expand Down
4 changes: 2 additions & 2 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class Program {

//default to the embedded version
} else {
return `source${path.sep}bslib.brs`;
return `${this.options.bslibDestinationDir}${path.sep}bslib.brs`;
}
}

Expand Down Expand Up @@ -1244,7 +1244,7 @@ export class Program {

//if there's no bslib file already loaded into the program, copy it to the staging directory
if (!this.getFile(bslibAliasedRokuModulesPkgPath) && !this.getFile(s`source/bslib.brs`)) {
promises.push(util.copyBslibToStaging(stagingDir));
promises.push(util.copyBslibToStaging(stagingDir, this.options.bslibDestinationDir));
}
await Promise.all(promises);

Expand Down
20 changes: 20 additions & 0 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ describe('util', () => {
let config = util.normalizeAndResolveConfig({ watch: true });
expect(config.watch).to.be.true;
});

it('sets default value for bslibDestinationDir', () => {
expect(util.normalizeConfig(<any>{ }).bslibDestinationDir).to.equal('source');
});

it('strips leading and/or trailing slashes from bslibDestinationDir', () => {
['source/opt', '/source/opt', 'source/opt/', '/source/opt/'].forEach(input => {
expect(util.normalizeConfig(<any>{ bslibDestinationDir: input }).bslibDestinationDir).to.equal('source/opt');
});
});
});

describe('areArraysEqual', () => {
Expand Down Expand Up @@ -556,6 +566,16 @@ describe('util', () => {
)
).not.to.be.null;
});

it('copies from local bslib dependency to optionally specified destination directory', async () => {
await util.copyBslibToStaging(tempDir, 'source/opt');
expect(fsExtra.pathExistsSync(`${tempDir}/source/opt/bslib.brs`)).to.be.true;
expect(
/^function bslib_toString\(/mg.exec(
fsExtra.readFileSync(`${tempDir}/source/opt/bslib.brs`).toString()
)
).not.to.be.null;
});
});

describe('rangesIntersect', () => {
Expand Down
11 changes: 8 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export class Util {
config.logLevel = LogLevel[(config.logLevel as string).toLowerCase()];
}
config.logLevel = config.logLevel ?? LogLevel.log;
config.bslibDestinationDir = config.bslibDestinationDir ?? 'source';
if (config.bslibDestinationDir !== 'source') {
// strip leading and trailing slashes
config.bslibDestinationDir = config.bslibDestinationDir.replace(/^(\/*)(.*?)(\/*)$/, '$2');
}
return config;
}

Expand Down Expand Up @@ -1223,9 +1228,9 @@ export class Util {
/**
* Copy the version of bslib from local node_modules to the staging folder
*/
public async copyBslibToStaging(stagingDir: string) {
public async copyBslibToStaging(stagingDir: string, bslibDestinationDir = 'source') {
//copy bslib to the output directory
await fsExtra.ensureDir(standardizePath(`${stagingDir}/source`));
await fsExtra.ensureDir(standardizePath(`${stagingDir}/${bslibDestinationDir}`));
// eslint-disable-next-line
const bslib = require('@rokucommunity/bslib');
let source = bslib.source as string;
Expand All @@ -1243,7 +1248,7 @@ export class Util {
const position = positions[i];
source = source.slice(0, position) + 'bslib_' + source.slice(position);
}
await fsExtra.writeFile(`${stagingDir}/source/bslib.brs`, source);
await fsExtra.writeFile(`${stagingDir}/${bslibDestinationDir}/bslib.brs`, source);
}

/**
Expand Down