Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@

'use strict';

import type {StructProperty} from './StructCollector';
import type {Nullable} from '../../../CodegenSchema';
import type {StructProperty, StructTypeAnnotation} from './StructCollector';

const {unwrapNullable} = require('../../../parsers/parsers-commons');
const {wrapOptional: wrapCxxOptional} = require('../../TypeUtils/Cxx');
const {
wrapOptional: wrapObjCOptional,
} = require('../../TypeUtils/Objective-C');
const {capitalize} = require('../../Utils');

function getSafePropertyName(property: StructProperty): string {
if (property.name === 'id') {
Expand All @@ -26,7 +34,93 @@ function getNamespacedStructName(
return `JS::${hasteModuleName}::${structName}`;
}

type ArrayTypeWrapperType = 'std::vector' | 'facebook::react::LazyVector';

function toObjCType(
hasteModuleName: string,
nullableTypeAnnotation: Nullable<StructTypeAnnotation>,
isOptional: boolean = false,
arrayTypeWrapper: ArrayTypeWrapperType = 'std::vector',
): string {
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
const isRequired = !nullable && !isOptional;

switch (typeAnnotation.type) {
case 'ReservedTypeAnnotation':
switch (typeAnnotation.name) {
case 'RootTag':
return wrapCxxOptional('double', isRequired);
default:
(typeAnnotation.name: empty);
throw new Error(`Unknown prop type, found: ${typeAnnotation.name}"`);
}
case 'StringTypeAnnotation':
return 'NSString *';
case 'StringLiteralTypeAnnotation':
return 'NSString *';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'Int32TypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'DoubleTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'StringTypeAnnotation':
return 'NSString *';
default:
throw new Error(
`Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return wrapObjCOptional('id<NSObject>', isRequired);
case 'ArrayTypeAnnotation':
if (typeAnnotation.elementType.type === 'AnyTypeAnnotation') {
return wrapObjCOptional('id<NSObject>', isRequired);
}

return wrapCxxOptional(
`${arrayTypeWrapper}<${toObjCType(
hasteModuleName,
typeAnnotation.elementType,
false,
arrayTypeWrapper,
)}>`,
isRequired,
);
case 'TypeAliasTypeAnnotation':
const structName = capitalize(typeAnnotation.name);
const namespacedStructName = getNamespacedStructName(
hasteModuleName,
structName,
);
return wrapCxxOptional(
arrayTypeWrapper === 'std::vector'
? `${namespacedStructName}::Builder`
: namespacedStructName,
isRequired,
);
default:
(typeAnnotation.type: empty);
throw new Error(
`Couldn't convert into ObjC type: ${typeAnnotation.type}"`,
);
}
}

module.exports = {
getSafePropertyName,
getNamespacedStructName,
toObjCType,
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import type {ConstantsStruct, StructTypeAnnotation} from '../StructCollector';
import type {StructSerilizationOutput} from './serializeStruct';

const {unwrapNullable} = require('../../../../parsers/parsers-commons');
const {wrapOptional: wrapCxxOptional} = require('../../../TypeUtils/Cxx');
const {
wrapOptional: wrapObjCOptional,
} = require('../../../TypeUtils/Objective-C');
const {capitalize} = require('../../../Utils');
const {getNamespacedStructName, getSafePropertyName} = require('../Utils');
const {getSafePropertyName, toObjCType: sharedToObjCType} = require('../Utils');

const StructTemplate = ({
hasteModuleName,
Expand Down Expand Up @@ -80,74 +75,12 @@ function toObjCType(
nullableTypeAnnotation: Nullable<StructTypeAnnotation>,
isOptional: boolean = false,
): string {
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
const isRequired = !nullable && !isOptional;

switch (typeAnnotation.type) {
case 'ReservedTypeAnnotation':
switch (typeAnnotation.name) {
case 'RootTag':
return wrapCxxOptional('double', isRequired);
default:
(typeAnnotation.name: empty);
throw new Error(`Unknown prop type, found: ${typeAnnotation.name}"`);
}
case 'StringTypeAnnotation':
return 'NSString *';
case 'StringLiteralTypeAnnotation':
return 'NSString *';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'Int32TypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'DoubleTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'StringTypeAnnotation':
return 'NSString *';
default:
throw new Error(
`Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return wrapObjCOptional('id<NSObject>', isRequired);
case 'ArrayTypeAnnotation':
if (typeAnnotation.elementType.type === 'AnyTypeAnnotation') {
return wrapObjCOptional('id<NSObject>', isRequired);
}

return wrapCxxOptional(
`std::vector<${toObjCType(
hasteModuleName,
typeAnnotation.elementType,
)}>`,
isRequired,
);
case 'TypeAliasTypeAnnotation':
const structName = capitalize(typeAnnotation.name);
const namespacedStructName = getNamespacedStructName(
hasteModuleName,
structName,
);
return wrapCxxOptional(`${namespacedStructName}::Builder`, isRequired);
default:
(typeAnnotation.type: empty);
throw new Error(
`Couldn't convert into ObjC type: ${typeAnnotation.type}"`,
);
}
return sharedToObjCType(
hasteModuleName,
nullableTypeAnnotation,
isOptional,
'std::vector',
);
}

function toObjCValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import type {RegularStruct, StructTypeAnnotation} from '../StructCollector';
import type {StructSerilizationOutput} from './serializeStruct';

const {unwrapNullable} = require('../../../../parsers/parsers-commons');
const {wrapOptional: wrapCxxOptional} = require('../../../TypeUtils/Cxx');
const {
wrapOptional: wrapObjCOptional,
} = require('../../../TypeUtils/Objective-C');
const {capitalize} = require('../../../Utils');
const {getNamespacedStructName, getSafePropertyName} = require('../Utils');
const {
getNamespacedStructName,
getSafePropertyName,
toObjCType: sharedToObjCType,
} = require('../Utils');

const StructTemplate = ({
hasteModuleName,
Expand Down Expand Up @@ -71,73 +71,12 @@ function toObjCType(
nullableTypeAnnotation: Nullable<StructTypeAnnotation>,
isOptional: boolean = false,
): string {
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
const isRequired = !nullable && !isOptional;

switch (typeAnnotation.type) {
case 'ReservedTypeAnnotation':
switch (typeAnnotation.name) {
case 'RootTag':
return wrapCxxOptional('double', isRequired);
default:
(typeAnnotation.name: empty);
throw new Error(`Unknown prop type, found: ${typeAnnotation.name}"`);
}
case 'StringTypeAnnotation':
return 'NSString *';
case 'StringLiteralTypeAnnotation':
return 'NSString *';
case 'StringLiteralUnionTypeAnnotation':
return 'NSString *';
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'NumberLiteralTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'FloatTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'Int32TypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'DoubleTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapCxxOptional('double', isRequired);
case 'StringTypeAnnotation':
return 'NSString *';
default:
throw new Error(
`Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return wrapObjCOptional('id<NSObject>', isRequired);
case 'ArrayTypeAnnotation':
if (typeAnnotation.elementType.type === 'AnyTypeAnnotation') {
return wrapObjCOptional('id<NSObject>', isRequired);
}
return wrapCxxOptional(
`facebook::react::LazyVector<${toObjCType(
hasteModuleName,
typeAnnotation.elementType,
)}>`,
isRequired,
);
case 'TypeAliasTypeAnnotation':
const structName = capitalize(typeAnnotation.name);
const namespacedStructName = getNamespacedStructName(
hasteModuleName,
structName,
);
return wrapCxxOptional(namespacedStructName, isRequired);
default:
(typeAnnotation.type: empty);
throw new Error(
`Couldn't convert into ObjC type: ${typeAnnotation.type}"`,
);
}
return sharedToObjCType(
hasteModuleName,
nullableTypeAnnotation,
isOptional,
'facebook::react::LazyVector',
);
}

function toObjCValue(
Expand Down
90 changes: 90 additions & 0 deletions packages/react-native/scripts/replace-build-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

const {spawnSync} = require('child_process');
const fs = require('fs');

function validateBuildConfiguration(configuration /*: string */) {
if (!['Debug', 'Release'].includes(configuration)) {
throw new Error(`Invalid configuration ${configuration}`);
}
}

function validateVersion(version /*: ?string */) {
if (version == null || version === '') {
throw new Error('Version cannot be empty');
}
}

function shouldReplaceConfiguration(
lastBuildFilename /*: string */,
configuration /*: string */,
componentName /*: string */,
) /*: boolean */ {
const fileExists = fs.existsSync(lastBuildFilename);

if (fileExists) {
console.log(`Found ${lastBuildFilename} file`);
const oldConfiguration = fs.readFileSync(lastBuildFilename).toString();
if (oldConfiguration === configuration) {
console.log(
`Same config of the previous build. No need to replace ${componentName}`,
);
return false;
}
}

// Assumption: if there is no stored last build, we assume that it was build for debug.
if (!fileExists && configuration === 'Debug') {
console.log(
`No previous build detected, but Debug Configuration. No need to replace ${componentName}`,
);
return false;
}

return true;
}

function replaceConfiguration(
tarballPath /*: string */,
finalLocation /*: string */,
postExtractCallback /*: ?() => void */,
) {
console.log('Preparing the final location', finalLocation);
fs.rmSync(finalLocation, {force: true, recursive: true});
fs.mkdirSync(finalLocation, {recursive: true});

console.log('Extracting the tarball', tarballPath);
spawnSync('tar', ['-xf', tarballPath, '-C', finalLocation], {
stdio: 'inherit',
});

if (postExtractCallback != null) {
postExtractCallback();
}
}

function updateLastBuildConfiguration(
lastBuildFilename /*: string */,
configuration /*: string */,
) {
console.log(`Updating ${lastBuildFilename} with ${configuration}`);
fs.writeFileSync(lastBuildFilename, configuration);
}

module.exports = {
validateBuildConfiguration,
validateVersion,
shouldReplaceConfiguration,
replaceConfiguration,
updateLastBuildConfiguration,
};
Loading