Skip to content

Commit 6c75491

Browse files
committed
add expo config plugin
1 parent 313af01 commit 6c75491

File tree

5 files changed

+365
-5
lines changed

5 files changed

+365
-5
lines changed

app.plugin.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./plugin/build');

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"devDependencies": {
5959
"@commitlint/config-conventional": "^17.0.2",
6060
"@evilmartians/lefthook": "^1.5.0",
61+
"@expo/config-plugins": "^7.9.1",
6162
"@react-native/eslint-config": "^0.72.2",
6263
"@release-it/conventional-changelog": "^5.0.0",
6364
"@types/jest": "^28.1.2",

plugin/src/index.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {
2+
AndroidConfig,
3+
ConfigPlugin,
4+
withGradleProperties,
5+
withProjectBuildGradle,
6+
withSettingsGradle,
7+
withStringsXml,
8+
} from 'expo/config-plugins';
9+
10+
const withUnity: ConfigPlugin<{ name?: string }> = (
11+
config,
12+
{ name = 'react-native-unity' } = {}
13+
) => {
14+
config.name = name;
15+
config = withProjectBuildGradleMod(config);
16+
config = withSettingsGradleMod(config);
17+
config = withGradlePropertiesMod(config);
18+
config = withStringsXMLMod(config);
19+
return config;
20+
};
21+
22+
const REPOSITORIES_END_LINE = `maven { url 'https://www.jitpack.io' }`;
23+
24+
const withProjectBuildGradleMod: ConfigPlugin = (config) =>
25+
withProjectBuildGradle(config, (modConfig) => {
26+
if (modConfig.modResults.contents.includes(REPOSITORIES_END_LINE)) {
27+
// use the last known line in expo's build.gradle file to append the newline after
28+
modConfig.modResults.contents = modConfig.modResults.contents.replace(
29+
REPOSITORIES_END_LINE,
30+
REPOSITORIES_END_LINE +
31+
'\nflatDir { dirs "${project(\':unityLibrary\').projectDir}/libs" }\n'
32+
);
33+
} else {
34+
throw new Error(
35+
'Failed to find the end of repositories in the android/build.gradle file`'
36+
);
37+
}
38+
return modConfig;
39+
});
40+
41+
const withSettingsGradleMod: ConfigPlugin = (config) =>
42+
withSettingsGradle(config, (modConfig) => {
43+
modConfig.modResults.contents += `
44+
include ':unityLibrary'
45+
project(':unityLibrary').projectDir=new File('../unity/builds/android/unityLibrary')
46+
`;
47+
return modConfig;
48+
});
49+
50+
const withGradlePropertiesMod: ConfigPlugin = (config) =>
51+
withGradleProperties(config, (modConfig) => {
52+
modConfig.modResults.push({
53+
type: 'property',
54+
key: 'unityStreamingAssets',
55+
value: '.unity3d',
56+
});
57+
return modConfig;
58+
});
59+
60+
// add string
61+
const withStringsXMLMod: ConfigPlugin = (config) =>
62+
withStringsXml(config, (config) => {
63+
config.modResults = AndroidConfig.Strings.setStringItem(
64+
[
65+
{
66+
_: 'Game View',
67+
$: {
68+
name: 'game_view_content_description',
69+
},
70+
},
71+
],
72+
config.modResults
73+
);
74+
return config;
75+
});
76+
77+
export default withUnity;

plugin/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "expo-module-scripts/tsconfig.plugin",
3+
"compilerOptions": {
4+
"outDir": "build",
5+
"rootDir": "src"
6+
},
7+
"include": ["./src"],
8+
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
9+
}

0 commit comments

Comments
 (0)