react-native-navigation的迁移库

applicationLinker.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @ts-check
  2. var path = require('./path');
  3. var fs = require("fs");
  4. var { warnn, logn, infon, debugn } = require("./log");
  5. class ApplicationLinker {
  6. constructor() {
  7. this.applicationPath = path.mainApplicationJava;
  8. }
  9. link() {
  10. if (this.applicationPath) {
  11. logn("Linking MainApplication...");
  12. var applicationContents = fs.readFileSync(this.applicationPath, "utf8");
  13. var linkers = [this._extendNavigationApplication, this._extendNavigationHost, this._removeSOLoaderInit];
  14. applicationContents = this._extendNavigationApplication(applicationContents);
  15. applicationContents = this._extendNavigationHost(applicationContents);
  16. applicationContents = this._removeSOLoaderInit(applicationContents);
  17. fs.writeFileSync(this.applicationPath, applicationContents);
  18. infon("MainApplication linked successfully!\n");
  19. }
  20. }
  21. _extendNavigationApplication(applicationContent) {
  22. if (this._doesExtendApplication(applicationContent)) {
  23. debugn(" Extending NavigationApplication");
  24. return applicationContent
  25. .replace(/extends\s+Application\s+implements\s+ReactApplication/gi, "extends NavigationApplication")
  26. .replace("import com.facebook.react.ReactApplication;", "import com.reactnativenavigation.NavigationApplication;");
  27. }
  28. warnn(" MainApplication already extends NavigationApplication");
  29. return applicationContent;
  30. }
  31. _doesExtendApplication(applicationContent) {
  32. return /\s+MainApplication\s+extends\s+Application\s+implements\s+ReactApplication\s+/.test(applicationContent);
  33. }
  34. _extendNavigationHost(applicationContent) {
  35. if (this._doesExtendReactNativeHost(applicationContent)) {
  36. debugn(" Changing host implementation to NavigationReactNativeHost");
  37. return applicationContent
  38. .replace("new ReactNativeHost(this)", "new NavigationReactNativeHost(this)")
  39. .replace("import com.facebook.react.ReactNativeHost;", "import com.facebook.react.ReactNativeHost;\nimport com.reactnativenavigation.react.NavigationReactNativeHost;")
  40. }
  41. warnn(" NavigationReactNativeHost is already used");
  42. return applicationContent;
  43. }
  44. _removeSOLoaderInit(applicationContent) {
  45. if (this._isSOLoaderInitCalled(applicationContent)) {
  46. debugn(" Removing call to SOLoader.init()");
  47. return applicationContent.replace(/SoLoader.init\(\s*this\s*,\s*[/* native exopackage */]*\s*false\s*\);/, "")
  48. }
  49. warnn(" SOLoader.init() is not called");
  50. return applicationContent;
  51. }
  52. _isSOLoaderInitCalled(applicationContent) {
  53. return /SoLoader.init\(this,\s*[/* native exopackage */]*\s*false\);/.test(applicationContent);
  54. }
  55. _doesExtendReactNativeHost(applicationContent) {
  56. return /\s*new ReactNativeHost\(this\)\s*/.test(applicationContent);
  57. }
  58. }
  59. module.exports = ApplicationLinker;