Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ivy): ngcc - extract file writing out into a class (#29092)
This is in preparation of having different file writing strategies. PR Close #29092
- Loading branch information
1 parent
a827bc2
commit 849b327
Showing
6 changed files
with
159 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {EntryPoint} from '../packages/entry_point'; | ||
import {EntryPointBundle} from '../packages/entry_point_bundle'; | ||
import {FileInfo} from '../rendering/renderer'; | ||
|
||
|
||
/** | ||
* Responsible for writing out the transformed files to disk. | ||
*/ | ||
export interface FileWriter { | ||
writeBundle(entryPoint: EntryPoint, bundle: EntryPointBundle, transformedFiles: FileInfo[]): void; | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/compiler-cli/ngcc/src/writing/in_place_file_writer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {dirname} from 'canonical-path'; | ||
import {existsSync, writeFileSync} from 'fs'; | ||
import {mkdir, mv} from 'shelljs'; | ||
|
||
import {EntryPoint} from '../packages/entry_point'; | ||
import {EntryPointBundle} from '../packages/entry_point_bundle'; | ||
import {FileInfo} from '../rendering/renderer'; | ||
|
||
import {FileWriter} from './file_writer'; | ||
|
||
/** | ||
* This FileWriter overwrites the transformed file, in-place, while creating | ||
* a back-up of the original file with an extra `.bak` extension. | ||
*/ | ||
export class InPlaceFileWriter implements FileWriter { | ||
writeBundle(_entryPoint: EntryPoint, _bundle: EntryPointBundle, transformedFiles: FileInfo[]) { | ||
transformedFiles.forEach(file => this.writeFileAndBackup(file)); | ||
} | ||
protected writeFileAndBackup(file: FileInfo): void { | ||
mkdir('-p', dirname(file.path)); | ||
const backPath = file.path + '.__ivy_ngcc_bak'; | ||
if (existsSync(backPath)) { | ||
throw new Error( | ||
`Tried to overwrite ${backPath} with an ngcc back up file, which is disallowed.`); | ||
} | ||
if (existsSync(file.path)) { | ||
mv(file.path, backPath); | ||
} | ||
writeFileSync(file.path, file.contents, 'utf8'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/compiler-cli/ngcc/test/writing/in_place_file_writer_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {existsSync, readFileSync} from 'fs'; | ||
import * as mockFs from 'mock-fs'; | ||
|
||
import {EntryPoint} from '../../src/packages/entry_point'; | ||
import {EntryPointBundle} from '../../src/packages/entry_point_bundle'; | ||
import {InPlaceFileWriter} from '../../src/writing/in_place_file_writer'; | ||
|
||
function createMockFileSystem() { | ||
mockFs({ | ||
'/package/path': { | ||
'top-level.js': 'ORIGINAL TOP LEVEL', | ||
'folder-1': { | ||
'file-1.js': 'ORIGINAL FILE 1', | ||
'file-2.js': 'ORIGINAL FILE 2', | ||
}, | ||
'folder-2': { | ||
'file-3.js': 'ORIGINAL FILE 3', | ||
'file-4.js': 'ORIGINAL FILE 4', | ||
}, | ||
'already-backed-up.js.__ivy_ngcc_bak': 'BACKED UP', | ||
} | ||
}); | ||
} | ||
|
||
function restoreRealFileSystem() { | ||
mockFs.restore(); | ||
} | ||
|
||
describe('InPlaceFileWriter', () => { | ||
beforeEach(createMockFileSystem); | ||
afterEach(restoreRealFileSystem); | ||
|
||
it('should write all the FileInfo to the disk', () => { | ||
const fileWriter = new InPlaceFileWriter(); | ||
fileWriter.writeBundle({} as EntryPoint, {} as EntryPointBundle, [ | ||
{path: '/package/path/top-level.js', contents: 'MODIFIED TOP LEVEL'}, | ||
{path: '/package/path/folder-1/file-1.js', contents: 'MODIFIED FILE 1'}, | ||
{path: '/package/path/folder-2/file-4.js', contents: 'MODIFIED FILE 4'}, | ||
{path: '/package/path/folder-3/file-5.js', contents: 'NEW FILE 5'}, | ||
]); | ||
expect(readFileSync('/package/path/top-level.js', 'utf8')).toEqual('MODIFIED TOP LEVEL'); | ||
expect(readFileSync('/package/path/folder-1/file-1.js', 'utf8')).toEqual('MODIFIED FILE 1'); | ||
expect(readFileSync('/package/path/folder-1/file-2.js', 'utf8')).toEqual('ORIGINAL FILE 2'); | ||
expect(readFileSync('/package/path/folder-2/file-3.js', 'utf8')).toEqual('ORIGINAL FILE 3'); | ||
expect(readFileSync('/package/path/folder-2/file-4.js', 'utf8')).toEqual('MODIFIED FILE 4'); | ||
expect(readFileSync('/package/path/folder-3/file-5.js', 'utf8')).toEqual('NEW FILE 5'); | ||
}); | ||
|
||
it('should create backups of all files that previously existed', () => { | ||
const fileWriter = new InPlaceFileWriter(); | ||
fileWriter.writeBundle({} as EntryPoint, {} as EntryPointBundle, [ | ||
{path: '/package/path/top-level.js', contents: 'MODIFIED TOP LEVEL'}, | ||
{path: '/package/path/folder-1/file-1.js', contents: 'MODIFIED FILE 1'}, | ||
{path: '/package/path/folder-2/file-4.js', contents: 'MODIFIED FILE 4'}, | ||
{path: '/package/path/folder-3/file-5.js', contents: 'NEW FILE 5'}, | ||
]); | ||
expect(readFileSync('/package/path/top-level.js.__ivy_ngcc_bak', 'utf8')) | ||
.toEqual('ORIGINAL TOP LEVEL'); | ||
expect(readFileSync('/package/path/folder-1/file-1.js.__ivy_ngcc_bak', 'utf8')) | ||
.toEqual('ORIGINAL FILE 1'); | ||
expect(existsSync('/package/path/folder-1/file-2.js.__ivy_ngcc_bak')).toBe(false); | ||
expect(existsSync('/package/path/folder-2/file-3.js.__ivy_ngcc_bak')).toBe(false); | ||
expect(readFileSync('/package/path/folder-2/file-4.js.__ivy_ngcc_bak', 'utf8')) | ||
.toEqual('ORIGINAL FILE 4'); | ||
expect(existsSync('/package/path/folder-3/file-5.js.__ivy_ngcc_bak')).toBe(false); | ||
}); | ||
|
||
it('should error if the backup file already exists', () => { | ||
const fileWriter = new InPlaceFileWriter(); | ||
expect( | ||
() => fileWriter.writeBundle( | ||
{} as EntryPoint, {} as EntryPointBundle, | ||
[ | ||
{path: '/package/path/already-backed-up.js', contents: 'MODIFIED BACKED UP'}, | ||
])) | ||
.toThrowError( | ||
'Tried to overwrite /package/path/already-backed-up.js.__ivy_ngcc_bak with an ngcc back up file, which is disallowed.'); | ||
}); | ||
}); |