Skip to content

Commit 0e0dabb

Browse files
authored
Merge pull request #1888 from Karthiccc23/fix-plugins-androidx
Update and fix plugin conversion to support Androidx
2 parents 4e524a2 + 63add16 commit 0e0dabb

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

ern-container-gen-android/src/AndroidGenerator.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,21 @@ export default class AndroidGenerator implements ContainerGenerator {
218218
config.outDir,
219219
'lib/src/main',
220220
);
221+
221222
if (semver.gte(reactNativePlugin.version, '0.60.0')) {
222-
const filesWithSupportLib: string[] = [];
223-
shell.pushd(relPathToPluginSource);
224-
shell.ls('**/*.java').forEach((file) => {
225-
if (shell.grep('android.support', file).trim().length !== 0) {
226-
filesWithSupportLib.push(file);
227-
}
228-
});
229-
if (filesWithSupportLib.length !== 0) {
223+
const convertedFiles = this.convertToAndroidX(
224+
relPathToPluginSource,
225+
);
226+
if (convertedFiles > 0) {
230227
log.info(
231228
`${plugin.name} contains source files with references to the Android Support Library (android.support.*)`,
232229
);
233-
filesWithSupportLib.forEach((file) => {
234-
shell.sed('-i', 'android.support', 'androidx', file);
235-
});
236230
log.info(
237-
`${filesWithSupportLib.length} files successfully converted to use AndroidX (androidx.*)`,
231+
`${convertedFiles} files successfully converted to use AndroidX (androidx.*)`,
238232
);
239233
}
240-
shell.popd();
241234
}
235+
242236
shell.cp('-R', relPathToPluginSource, absPathToCopyPluginSourceTo);
243237
}
244238

@@ -707,10 +701,51 @@ You should replace "${annotationProcessorPrefix}:${dependency}" with "annotation
707701
'lib/src/main/java/com/walmartlabs/ern/container/plugins',
708702
);
709703
shell.cp(pathToPluginConfigHook, pathToCopyPluginConfigHookTo);
704+
705+
if (semver.gte(rnVersion, '0.60.0')) {
706+
const filesConverted = this.convertToAndroidX(
707+
pathToCopyPluginConfigHookTo,
708+
);
709+
if (filesConverted > 0) {
710+
log.info(
711+
`${plugin.name} contains source files with references to the Android Support Library (android.support.*)`,
712+
);
713+
log.info(
714+
`${filesConverted} files successfully converted to use AndroidX (androidx.*)`,
715+
);
716+
}
717+
}
710718
}
711719
}
712720
}
713721

722+
/**
723+
* Convert files in a directory from support library to AndroidX
724+
* eg: import android.support.annotation.NonNull => import androidx.annotation.NonNull
725+
*/
726+
public convertToAndroidX(dir: string): number {
727+
const filesWithSupportLib: string[] = [];
728+
shell.pushd(dir);
729+
shell
730+
.ls('-R', '.')
731+
.filter((file) => file.match(/\.(java|kt)$/))
732+
.forEach((file) => {
733+
if (shell.grep('android.support', file).trim().length !== 0) {
734+
filesWithSupportLib.push(file);
735+
}
736+
});
737+
738+
if (filesWithSupportLib.length !== 0) {
739+
filesWithSupportLib.forEach((file) => {
740+
shell.sed('-i', 'android.support', 'androidx', file);
741+
});
742+
}
743+
744+
shell.popd();
745+
746+
return filesWithSupportLib.length;
747+
}
748+
714749
public async buildAndroidPluginsViews(
715750
plugins: PackagePath[],
716751
mustacheView: any,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect } from 'chai';
2+
import { AndroidGenerator } from 'ern-container-gen-android';
3+
4+
describe('Test Convert to AndroidX Imports Logic', () => {
5+
it('Expect files in directory to be converted to AndroidX', () => {
6+
expect(new AndroidGenerator().convertToAndroidX('fixtures')).to.eq(2);
7+
});
8+
9+
it('Return 0 files converting the existing AndroidX plugins above', () => {
10+
expect(new AndroidGenerator().convertToAndroidX('fixtures')).to.eq(0);
11+
});
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.walmartlabs.ern.container.plugins;
2+
3+
import android.app.Application;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
7+
import com.facebook.react.ReactPackage;
8+
import com.httpapi.RNHttpapiPackage;
9+
10+
public class RNHttpapiPackagePlugin implements ReactPlugin {
11+
public ReactPackage hook(@NonNull Application application, @Nullable ReactPluginConfig config) {
12+
return new RNHttpapiPackage();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.walmartlabs.ern.container.plugins;
2+
3+
import android.app.Application;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
7+
import com.facebook.react.ReactPackage;
8+
import com.walmart.wmbarcode.react.WMBarcodePackage;
9+
10+
public class WMBarcodePackagePlugin implements ReactPlugin {
11+
public ReactPackage hook(@NonNull Application application, @Nullable ReactPluginConfig config) {
12+
return new WMBarcodePackage();
13+
}
14+
}

0 commit comments

Comments
 (0)