Skip to content

Commit

Permalink
codegen-windows command run with --check command always fails (#11187)
Browse files Browse the repository at this point in the history
* codegen-windows --check always fails

* Change files

* And ensure we properly delete files that should not exist anymore

* remove debugging logging

* Add verification that codegen output is up to date in vnext

* Change files

* codegen

* Provide feedback when codegen changes a file
  • Loading branch information
acoates-ms committed Feb 9, 2023
1 parent 08f6ac5 commit 6a6a622
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "codegen-windows --check always fails",
"packageName": "@react-native-windows/codegen",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Update generated code",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
9 changes: 8 additions & 1 deletion packages/@react-native-windows/codegen/src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
process.exit(1);
}

runCodeGen(argv);
const changesNecessary = runCodeGen(argv);

if (argv.test && changesNecessary) {
console.error(
'There is a change in the output of codegen. Rerun "react-native codegen-windows" to regenerate.',
);
process.exit(2);
}
27 changes: 21 additions & 6 deletions packages/@react-native-windows/codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ function checkFilesForChanges(
): boolean {
let hasChanges = false;

outputDir = path.resolve(outputDir);
const globbyDir = outputDir.replace(/\\/g, '/');
const allExistingFiles = globby
.sync(`${outputDir}/**`)
.map(_ => path.normalize(_))
.sort();
.sync([`${globbyDir}/**`, `${globbyDir}/**/.*`], {absolute: true})
.map(_ => path.normalize(_));
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();

if (
allExistingFiles.length !== allGeneratedFiles.length ||
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index])
!allGeneratedFiles.every(filepath =>
allExistingFiles.includes(
path.normalize(path.resolve(process.cwd(), filepath)),
),
)
)
return true;

Expand All @@ -97,10 +102,19 @@ function checkFilesForChanges(
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
let success = true;

outputDir = path.resolve(outputDir);
const globbyDir = outputDir.replace(/\\/g, '/');

// This ensures that we delete any generated files from modules that have been deleted
const allExistingFiles = globby.sync(`${outputDir}/**`);
const allExistingFiles = globby.sync(
[`${globbyDir}/**`, `${globbyDir}/**/.*`],
{absolute: true},
);

const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
allExistingFiles.forEach(existingFile => {
if (!map.has(path.normalize(existingFile))) {
if (!allGeneratedFiles.includes(path.normalize(existingFile))) {
console.log('Deleting ', existingFile);
fs.unlinkSync(existingFile);
}
});
Expand All @@ -117,6 +131,7 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
}
}

console.log('Writing ', fileName);
fs.writeFileSync(fileName, contents);
} catch (error) {
success = false;
Expand Down
8 changes: 7 additions & 1 deletion packages/sample-apps/just-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @ts-check
*/

const {task, series} = require('just-scripts');
const {task, series, parallel} = require('just-scripts');
const fs = require('fs');
const {execSync} = require('child_process');

Expand All @@ -16,6 +16,12 @@ task('codegen', () => {
execSync('npx react-native codegen-windows --logging', {env: process.env});
});

task('codegen:check', () => {
execSync('npx react-native codegen-windows --logging --check', {
env: process.env,
});
});

task('prepareBundle', () => {
fs.mkdirSync('windows/SampleAppCS/Bundle', {recursive: true});
fs.mkdirSync('windows/SampleAppCPP/Bundle', {recursive: true});
Expand Down
17 changes: 12 additions & 5 deletions vnext/just-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ const fs = require('fs');
option('production');
option('clean');

task('codegen', () => {
function codegen(test) {
execSync(
'react-native-windows-codegen --files Libraries/**/*Native*.js --namespace Microsoft::ReactNativeSpecs --libraryName rnwcore --modulesWindows --modulesCxx',
`react-native-windows-codegen --files Libraries/**/*Native*.js --namespace Microsoft::ReactNativeSpecs --libraryName rnwcore --modulesWindows --modulesCxx${
test ? ' --test' : ''
}`,
{env: process.env},
);
execSync(
'react-native-windows-codegen --files Microsoft.ReactNative.IntegrationTests/**/*Native*.js --namespace Microsoft::ReactNativeIntegrationTestSpecs --libraryName msrnIntegrationTests --modulesCxx --outputDirectory Microsoft.ReactNative.IntegrationTests/codegen',
`react-native-windows-codegen --files Microsoft.ReactNative.IntegrationTests/**/*Native*.js --namespace Microsoft::ReactNativeIntegrationTestSpecs --libraryName msrnIntegrationTests --modulesCxx --outputDirectory Microsoft.ReactNative.IntegrationTests/codegen${
test ? ' --test' : ''
}`,
{env: process.env},
);
});
}

task('codegen', () => codegen(false));
task('codegen:check', () => codegen(true));

task('copyReadmeAndLicenseFromRoot', () => {
fs.copyFileSync(
Expand Down Expand Up @@ -62,4 +69,4 @@ task(

task('clean', series('cleanRNLibraries'));

task('lint', series('eslint', 'flow-check'));
task('lint', series('eslint', 'codegen:check', 'flow-check'));

0 comments on commit 6a6a622

Please sign in to comment.