Skip to content

Commit 7f5fbc7

Browse files
authored
fix: bob codegen deletes view specs when codegen type is all (#736)
### Summary Fixes #728 The codegen patcher script was deleting the whole `facebook` namespace when the codegen type was all or module. This was not a problem when the type was module but when the type was all, view manager specs were also getting removed.s ### Test plan 1. Create a new turbo module 2. Set codegen type to all 3. Add turbo module specs 4. Run `yarn bob build --target codegen` 5. Make sure `android/generated/java/com/facebook/react` exists and has view manager specs
1 parent 5a68aa7 commit 7f5fbc7

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

packages/react-native-builder-bob/src/targets/codegen/patches/patchCodegenAndroidPackage.test.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const mockReport: Report = {
2323
success: console.log,
2424
};
2525

26-
const mockJavaSpec = `
26+
const mockJavaModuleSpec = `
2727
/**
2828
* Some comment
2929
*/
3030
31-
package com.bobtest;
31+
package com.facebook.fbreact.specs;
3232
3333
import com.example.exampleimport;
3434
@@ -38,20 +38,36 @@ class SomeClass {
3838
}
3939
}`;
4040

41+
const mockJavaViewSpec = `
42+
/**
43+
* Some comment
44+
*/
45+
46+
package com.facebook.react.viewmanagers;
47+
48+
public interface SomeInterface<T extends View> {
49+
void setColor(T view, @Nullable String value);
50+
}
51+
`;
52+
4153
const mockProjectPath = path.resolve(__dirname, 'mockProject');
42-
const mockCodegenSpecsPath = path.resolve(
54+
const mockCodegenModuleSpecsPath = path.resolve(
4355
mockProjectPath,
4456
'android/generated/java/com/facebook/fbreact/specs'
4557
);
58+
const mockCodegenViewSpecsPath = path.resolve(
59+
mockProjectPath,
60+
'android/generated/java/com/facebook/react/viewmanagers'
61+
);
4662

4763
describe('patchCodegenAndroidPackage', () => {
4864
beforeEach(() => {
4965
mockfs({
5066
[mockProjectPath]: {
5167
'package.json': JSON.stringify(mockPackageJson),
5268
},
53-
[mockCodegenSpecsPath]: {
54-
'NativeBobtestSpec.java': mockJavaSpec,
69+
[mockCodegenModuleSpecsPath]: {
70+
'NativeBobtestSpec.java': mockJavaModuleSpec,
5571
},
5672
});
5773
});
@@ -101,6 +117,40 @@ describe('patchCodegenAndroidPackage', () => {
101117
mockReport
102118
);
103119

104-
expect(await fs.pathExists(mockCodegenSpecsPath)).toBe(false);
120+
expect(await fs.pathExists(mockCodegenModuleSpecsPath)).toBe(false);
121+
});
122+
123+
it("doesn't delete the view manager specs", async () => {
124+
const mockPackageJsonWithTypeAll = {
125+
...mockPackageJson,
126+
codegenConfig: {
127+
...mockPackageJson.codegenConfig,
128+
type: 'all',
129+
},
130+
};
131+
132+
mockfs({
133+
[mockProjectPath]: {
134+
'package.json': JSON.stringify(mockPackageJsonWithTypeAll),
135+
},
136+
[mockCodegenModuleSpecsPath]: {
137+
'NativeBobtestSpec.java': mockJavaModuleSpec,
138+
},
139+
[mockCodegenViewSpecsPath]: {
140+
'BobtestViewManagerInterface.java': mockJavaViewSpec,
141+
},
142+
});
143+
144+
await patchCodegenAndroidPackage(
145+
mockProjectPath,
146+
mockPackageJsonWithTypeAll,
147+
mockReport
148+
);
149+
150+
expect(
151+
await fs.pathExists(
152+
path.join(mockCodegenViewSpecsPath, 'BobtestViewManagerInterface.java')
153+
)
154+
).toBeTruthy();
105155
});
106156
});

packages/react-native-builder-bob/src/targets/codegen/patches/patchCodegenAndroidPackage.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function patchCodegenAndroidPackage(
3030

3131
if (!(await fs.pathExists(codegenAndroidPath))) {
3232
throw new Error(
33-
`The codegen android path defined in your package.json: ${codegenAndroidPath} doesnt' exist.`
33+
`The codegen android path defined in your package.json: ${codegenAndroidPath} doesn't exist.`
3434
);
3535
}
3636

@@ -89,7 +89,19 @@ export async function patchCodegenAndroidPackage(
8989
})
9090
);
9191

92-
await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook'), {
93-
recursive: true,
94-
});
92+
if (
93+
await fs.pathExists(
94+
path.resolve(codegenAndroidPath, 'java/com/facebook/react/viewmanagers')
95+
)
96+
) {
97+
// Keep the view managers
98+
await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook/fbreact'), {
99+
recursive: true,
100+
});
101+
} else {
102+
// Delete the entire facebook namespace
103+
await fs.rm(path.resolve(codegenAndroidPath, 'java/com/facebook'), {
104+
recursive: true,
105+
});
106+
}
95107
}

0 commit comments

Comments
 (0)