1import escapeRegExp from 'lodash/escapeRegExp';
2
3import { transformString } from '../../../Transforms';
4import { FileTransform, FileTransforms, StringTransform } from '../../../Transforms.types';
5import { baseCmakeTransforms } from '../cmakeTransforms';
6import { JniLibNames } from '../libraries';
7import { packagesToKeep, packagesToRename } from '../packagesConfig';
8import { deleteLinesBetweenTags } from '../utils';
9
10export function expoviewTransforms(abiVersion: string): FileTransforms {
11  const sdkVersion = abiVersion.replace(/abi(\d+)_0_0/, 'sdk$1');
12  return {
13    path: [
14      {
15        find: 'src/main/java/versioned',
16        replaceWith: `src/main/java/${abiVersion}`,
17      },
18      {
19        find: 'src/main/java/com',
20        replaceWith: `src/main/java/${abiVersion}/com`,
21      },
22    ],
23    content: [
24      {
25        paths: './build.gradle',
26        find: /\/\/ WHEN_VERSIONING_REPLACE_WITH_DEPENDENCIES/g,
27        replaceWith: 'implementation project(":expoview")',
28      },
29      {
30        paths: ['./build.gradle', './src/main/AndroidManifest.xml'],
31        transform: (text: string) =>
32          deleteLinesBetweenTags(
33            /WHEN_VERSIONING_REMOVE_FROM_HERE/,
34            /WHEN_VERSIONING_REMOVE_TO_HERE/,
35            text
36          ),
37      },
38      {
39        paths: './build.gradle',
40        find: /.*WHEN_VERSIONING_UNCOMMENT_(TO_HERE|FROM_HERE).*\n/g,
41        replaceWith: '',
42      },
43      {
44        paths: './build.gradle',
45        find: `useVendoredModulesForExpoView('unversioned')`,
46        replaceWith: `useVendoredModulesForExpoView('${sdkVersion}')`,
47      },
48      {
49        paths: './src/main/AndroidManifest.xml',
50        find: /host\.exp\.expoview/g,
51        replaceWith: `${abiVersion}.host.exp.expoview`,
52      },
53      {
54        paths: './src/main/AndroidManifest.xml',
55        find: /versioned\.host\.exp\.exponent/g,
56        replaceWith: `${abiVersion}.host.exp.exponent`,
57      },
58      ...packagesToKeep.map((pkg: string) => ({
59        paths: './src/main/java/**/*.{java,kt}',
60        find: new RegExp(`([, ^(<])${escapeRegExp(pkg)}`, 'g'),
61        replaceWith: `$1temporarydonotversion.${pkg}`,
62      })),
63      {
64        paths: './src/main/java/**/*.{java,kt}',
65        find: /import (static |)expo\./g,
66        replaceWith: `import $1${abiVersion}.expo.`,
67      },
68      {
69        paths: './src/main/java/**/*.{java,kt}',
70        find: /versioned\.host\.exp\.exponent/g,
71        replaceWith: `${abiVersion}.host.exp.exponent`,
72      },
73      ...packagesToRename.map((pkg: string) => ({
74        paths: './src/main/java/**/*.{java,kt}',
75        find: new RegExp(`([, ^(<])${escapeRegExp(pkg)}`, 'g'),
76        replaceWith: `$1${abiVersion}.${pkg}`,
77      })),
78      {
79        paths: `./src/main/java/**/*.{java,kt}`,
80        find: /temporarydonotversion\./g,
81        replaceWith: '',
82      },
83      // currently it's matching only reanimated
84      ...[...JniLibNames, 'fb', 'fbjni'].map((libName) => ({
85        paths: '*.java',
86        find: new RegExp(`(SoLoader|System).loadLibrary\\\("${escapeRegExp(libName)}"\\\)`),
87        replaceWith: `$1.loadLibrary("${libName}_${abiVersion}")`,
88      })),
89      {
90        paths: '*.{java,h,cpp}',
91        find: /versioned\/host\/exp\/exponent\/modules\/api\/reanimated/g,
92        replaceWith: `${abiVersion}/host/exp/exponent/modules/api/reanimated`,
93      },
94      ...reanimatedCmakeTransforms(abiVersion),
95    ],
96  };
97}
98
99function reanimatedCmakeTransforms(abiVersion: string): FileTransform[] {
100  const libNames = JniLibNames.map((lib: string): string =>
101    lib.startsWith('lib') ? lib.slice(3) : lib
102  ).filter((lib: string) => !['fbjni'].includes(lib));
103  const renameSecondArg = (text: string) =>
104    transformString(
105      text,
106      libNames.map((lib) => ({
107        find: new RegExp(`^(\\\s*\\\S+\\\s+)${escapeRegExp(lib)}($|\\\s)`),
108        replaceWith: `$1${lib}_${abiVersion}$2`,
109      }))
110    );
111
112  return [
113    ...baseCmakeTransforms(abiVersion, libNames).map((transform: StringTransform) => ({
114      paths: 'CMakeLists.txt',
115      ...transform,
116    })),
117    {
118      paths: 'CMakeLists.txt',
119      find: /(find_library\()([\s\S]*?)(\))/g,
120      replaceWith: (_, p1, p2, p3) => [p1, renameSecondArg(p2), p3].join(''),
121    },
122    {
123      paths: 'CMakeLists.txt',
124      find: 'set (PACKAGE_NAME "reanimated")',
125      replaceWith: `set (PACKAGE_NAME "reanimated_${abiVersion}")`,
126    },
127  ];
128}
129