app.plugin.js 2.12 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const configPlugin = require('@expo/config-plugins');
const generateCode = require('@expo/config-plugins/build/utils/generateCode');
const pkg = require('onnxruntime-react-native/package.json');
const path = require('path');
const fs = require('fs');

const withOrt = (config) => {
  // Add build dependency to gradle file
  config = configPlugin.withAppBuildGradle(config, (config) => {
    if (config.modResults.language === 'groovy') {
      config.modResults.contents = generateCode
                                       .mergeContents({
                                         src: config.modResults.contents,
                                         newSrc: '    implementation project(\':onnxruntime-react-native\')',
                                         tag: 'onnxruntime-react-native',
                                         anchor: /^dependencies[ \t]*\{$/,
                                         offset: 1,
                                         comment: '    // onnxruntime-react-native'
                                       })
                                       .contents;
    } else {
      throw new Error('Cannot add ONNX Runtime maven gradle because the build.gradle is not groovy');
    }

    return config;
  });

  // Add build dependency to pod file
  config = configPlugin.withDangerousMod(config, [
    'ios',
    (config) => {
      const podFilePath = path.join(config.modRequest.platformProjectRoot, 'PodFile');
      const contents = fs.readFileSync(podFilePath, {encoding: 'utf-8'});
      const updatedContents =
          generateCode
              .mergeContents({
                src: contents,
                newSrc: '  pod \'onnxruntime-react-native\', :path => \'../node_modules/onnxruntime-react-native\'',
                tag: 'onnxruntime-react-native',
                anchor: /^target.+do$/,
                offset: 1,
                comment: '  # onnxruntime-react-native'
              })
              .contents;
      fs.writeFileSync(podFilePath, updatedContents);
      return config;
    }
  ]);

  return config;
};

exports.default = configPlugin.createRunOncePlugin(withOrt, pkg.name, pkg.version)