Summary:
TurboModules depend on a getConstants method. Existing ObjectiveC modules do not have this method. Therefore, I moved the contents of `constantsToExport` to `getConstants` and then had `constantsToExports` call `getConstants`.

facebook
Since all NativeModules will eventually need to be migrated to the TurboModule system, I didn't restrict this to just the NativeModules in Marketplace.

```
const fs = require('fs');

if (process.argv.length < 3) {
    throw new Error('Expected a file containing a list of native modules as the third param');
}

function read(filename) {
    return fs.readFileSync(filename, 'utf8');
}

const nativeModuleFilenames = read(process.argv[2]).split('\n').filter(Boolean);

nativeModuleFilenames.forEach((fileName) => {
    if (fileName.endsWith('.h')) {
        return;
    }

    const absPath = `${process.env.HOME}/${fileName}`;
    const fileSource = read(absPath);

    if (/(\n|^)-\s*\((.+)\)getConstants/.test(fileSource)) {
        return;
    }

    const constantsToExportRegex = /(\n|^)-\s*\((.+)\)constantsToExport/;
    const result = constantsToExportRegex.exec(fileSource);

    if (result == null) {
        throw new Error(`Didn't find a constantsToExport function inside NativeModule ${fileName}`);
    }

    const returnType = result[2];

    const newFileSource = fileSource.replace(
        constantsToExportRegex,
        '$1- ($2)constantsToExport\n' +
        '{\n' +
        `  return ${returnType.includes('ModuleConstants') ? '($2)' : ''}[self getConstants];\n` +
        '}\n' +
        '\n' +
        '- ($2)getConstants'
    );

    fs.writeFileSync(absPath, newFileSource);
});
```

```
> xbgs -l ')constantsToExport'
```

Reviewed By: fkgozali

Differential Revision: D13951197

fbshipit-source-id: 394a319d42aff466c56a3d748e17c335307a8f47
This commit is contained in:
Ramanpreet Nara 2019-02-04 17:41:36 -08:00 коммит произвёл Facebook Github Bot
Родитель ffc9908bef
Коммит f37093319b
10 изменённых файлов: 51 добавлений и 1 удалений

Просмотреть файл

@ -48,6 +48,11 @@ RCT_EXPORT_MODULE(BlobModule)
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"BLOB_URI_SCHEME": kBlobURIScheme,

Просмотреть файл

@ -52,6 +52,11 @@ RCT_EXPORT_MODULE()
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
return @{@"settings": RCTJSONClean([_defaults dictionaryRepresentation])};
}

Просмотреть файл

@ -90,6 +90,11 @@ RCT_EXPORT_MODULE()
RCT_EXPORT_MODULE()
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
_exportedConstants = YES;
_exportedConstantsOnMainQueue = RCTIsMainQueue();

Просмотреть файл

@ -37,6 +37,11 @@ RCT_EXPORT_MODULE(PlatformConstants)
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
UIDevice *device = [UIDevice currentDevice];
return @{

Просмотреть файл

@ -64,7 +64,12 @@ using namespace facebook::react;
return moduleMethods;
}
- (NSDictionary<NSString *, id> *)constantsToExport;
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants;
{
[self lazyInit];
if (!_module) {

Просмотреть файл

@ -48,6 +48,11 @@ RCT_EXPORT_MODULE()
}
- (NSDictionary *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary *)getConstants
{
return @{@"initialAppState": RCTCurrentAppBackgroundState()};
}

Просмотреть файл

@ -104,6 +104,11 @@ static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"Dimensions": RCTExportedDimensions(_bridge),

Просмотреть файл

@ -33,6 +33,11 @@ RCT_EXPORT_METHOD(swapLeftAndRightInRTL:(BOOL)value)
}
- (NSDictionary *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary *)getConstants
{
return @{
@"isRTL": @([[RCTI18nUtil sharedInstance] isRTL]),

Просмотреть файл

@ -21,6 +21,11 @@ RCT_EXPORT_MODULE()
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"scriptURL": self.bridge.bundleURL.absoluteString ?: @"",

Просмотреть файл

@ -1494,6 +1494,11 @@ static NSMutableDictionary<NSString *, id> *moduleConstantsForComponent(
}
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary<NSString *, id> *)getConstants
{
NSMutableDictionary<NSString *, NSDictionary *> *constants = [NSMutableDictionary new];
NSMutableDictionary<NSString *, NSDictionary *> *directEvents = [NSMutableDictionary new];