diff --git a/src/debugSession/BrightScriptDebugSession.spec.ts b/src/debugSession/BrightScriptDebugSession.spec.ts index 949d62ca..36033738 100644 --- a/src/debugSession/BrightScriptDebugSession.spec.ts +++ b/src/debugSession/BrightScriptDebugSession.spec.ts @@ -1608,8 +1608,13 @@ describe('BrightScriptDebugSession', () => { sinon.stub(ComponentLibraryProject.prototype, 'stage').resolves(); sinon.stub(ComponentLibraryProject.prototype, 'postfixFiles').resolves(); sinon.stub(ComponentLibraryProject.prototype, 'zipPackage').resolves(); + sinon.stub(ComponentLibraryProject.prototype, 'fixMainProjectLibraryDependency').resolves(); session['launchConfiguration'].host = '192.168.1.100'; session['launchConfiguration'].password = 'test123'; + session.projectManager.mainProject = { + stagingDir: s`${tempDir}/main-staging`, + zipPackage: sinon.stub().resolves() + }; } it('installs libraries sequentially when marked install=true', async () => { @@ -1683,8 +1688,13 @@ describe('BrightScriptDebugSession', () => { sinon.stub(rokuDeploy, 'deleteAllComponentLibraries').resolves(); sinon.stub(session['componentLibraryServer'], 'startStaticFileHosting').resolves(); sinon.stub(ComponentLibraryProject.prototype, 'postfixFiles').resolves(); + sinon.stub(ComponentLibraryProject.prototype, 'fixMainProjectLibraryDependency').resolves(); session['launchConfiguration'].host = '192.168.1.100'; session['launchConfiguration'].password = 'test123'; + session.projectManager.mainProject = { + stagingDir: s`${tempDir}/main-staging`, + zipPackage: sinon.stub().resolves() + }; const events = []; sinon.stub(ComponentLibraryProject.prototype, 'stage').callsFake(async function(this: ComponentLibraryProject) { diff --git a/src/debugSession/BrightScriptDebugSession.ts b/src/debugSession/BrightScriptDebugSession.ts index 6fb89a58..caf23228 100644 --- a/src/debugSession/BrightScriptDebugSession.ts +++ b/src/debugSession/BrightScriptDebugSession.ts @@ -1202,6 +1202,9 @@ export class BrightScriptDebugSession extends BaseDebugSession { //wait for this complib to finish being staged and zipped await compLibPromises[i]; + await compLibProject.fixMainProjectLibraryDependency(this.projectManager.mainProject.stagingDir); + await this.projectManager.mainProject.zipPackage({ retainStagingFolder: true }); + if (componentLibraries[i].packageTask) { await this.sendCustomRequest('executeTask', { task: componentLibraries[i].packageTask }); } diff --git a/src/managers/ProjectManager.spec.ts b/src/managers/ProjectManager.spec.ts index 01f4024a..49a73f77 100644 --- a/src/managers/ProjectManager.spec.ts +++ b/src/managers/ProjectManager.spec.ts @@ -873,4 +873,39 @@ describe('ComponentLibraryProject', () => { expect(project.name).to.equal('SGLibrary'); }); }); + + describe('fixMainProjectLibraryDependency', () => { + const mainStagingDir = s`${tempPath}/mainStagingDir`; + + it('replaces Library statement with postfixed filename when file is in fileMappings', async () => { + const project = new ComponentLibraryProject(params); + project.fileMappings = [{ + src: s`${rootDir}/source/mylib.brs`, + dest: s`${params.stagingDir}/source/mylib__lib0.brs` + }]; + fsExtra.outputFileSync(s`${mainStagingDir}/source/main.brs`, `Library "mylib.brs"`); + + await project.fixMainProjectLibraryDependency(mainStagingDir); + + expect( + fsExtra.readFileSync(s`${mainStagingDir}/source/main.brs`).toString() + ).to.equal(`Library "mylib__lib0.brs"`); + }); + + it('leaves Library statement unchanged when file is not in fileMappings', async () => { + const project = new ComponentLibraryProject(params); + project.fileMappings = [{ + src: s`${rootDir}/source/otherlib.brs`, + dest: s`${params.stagingDir}/source/otherlib__lib0.brs` + }]; + const original = `Library "mylib.brs"`; + fsExtra.outputFileSync(s`${mainStagingDir}/source/main.brs`, original); + + await project.fixMainProjectLibraryDependency(mainStagingDir); + + expect( + fsExtra.readFileSync(s`${mainStagingDir}/source/main.brs`).toString() + ).to.equal(original); + }); + }); }); diff --git a/src/managers/ProjectManager.ts b/src/managers/ProjectManager.ts index 1c088c6f..911c9c02 100644 --- a/src/managers/ProjectManager.ts +++ b/src/managers/ProjectManager.ts @@ -769,4 +769,29 @@ export class ComponentLibraryProject extends Project { } }); } + + public async fixMainProjectLibraryDependency(mainProjectStagingDir: string) { + await replaceInFile({ + files: [ + path.join(mainProjectStagingDir, '**/*.brs') + ], + from: /Library\s+"(.+)\.brs"/gi, + to: (match: string) => { + const pathMatch = /"([^"]+)"/.exec(match); + if (!pathMatch) { + return match; + } + + const internalPath = pathMatch[1]; + const fileNameWithoutExtension = internalPath.replace(/\.brs$/i, ''); + for (let fileMapping of this.fileMappings) { + if (fileMapping.dest.includes(fileNameWithoutExtension)) { + const fileWithPostfix = `${fileNameWithoutExtension}${this.postfix}`; + return match.replace(fileNameWithoutExtension, fileWithPostfix); + } + } + return match; + } + }); + } }