Skip to content

Commit

Permalink
Add excludedPlatform option to CodeSchema
Browse files Browse the repository at this point in the history
Summary:
Currently we generate Java ViewManager interfaces and C++ classes for iOS regardless whether the component is supported on platform or it isn't. This adds an option to exclude either iOS to Android in order to avoid this.

Changelog: In codegen it is now possible to exclude one or the other platform

Reviewed By: rickhanlonii

Differential Revision: D18217185

fbshipit-source-id: 1c569b92c92a5b991c96b0abdff6b8ed395e449f
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Nov 4, 2019
1 parent 03f5951 commit b6a23d8
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 56 deletions.
1 change: 1 addition & 0 deletions Libraries/Utilities/codegenNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Options = $ReadOnly<{|
interfaceOnly?: boolean,
paperComponentName?: string,
paperComponentNameDeprecated?: string,
excludedPlatform?: 'iOS' | 'android',
|}>;

export type NativeComponentType<T> = HostComponent<T>;
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ export type OptionsShape = $ReadOnly<{|
// Does not check for new name
paperComponentName?: string,

// Use for components that are not used on one or the other platform.
excludedPlatform?: 'iOS' | 'android',

// Use for components currently being renamed in paper
// Will use new name if it is available and fallback to this name
paperComponentNameDeprecated?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ module.exports = {
}

return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return component.excludedPlatform !== 'iOS';
})
.map(componentName => {
return [
generateProtocol(components[componentName], componentName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ module.exports = {
}

return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return component.excludedPlatform !== 'iOS';
})
.map(componentName => {
const component = components[componentName];
const newName = `${componentName}Props`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ module.exports = {
}
return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return component.excludedPlatform !== 'iOS';
})
.map(componentName => {
const component = components[componentName];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,38 +269,43 @@ module.exports = {
return;
}

return Object.keys(components).forEach(componentName => {
const component = components[componentName];
const className = getDelegateJavaClassName(componentName);
const interfaceClassName = getInterfaceJavaClassName(componentName);
const fileName = `${className}.java`;
return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return component.excludedPlatform !== 'android';
})
.forEach(componentName => {
const component = components[componentName];
const className = getDelegateJavaClassName(componentName);
const interfaceClassName = getInterfaceJavaClassName(componentName);
const fileName = `${className}.java`;

const imports = getDelegateImports(component);
const propsString = generatePropCasesString(component, componentName);
const commandsString = generateCommandCasesString(
component,
componentName,
);
const extendString = getClassExtendString(component);
const imports = getDelegateImports(component);
const propsString = generatePropCasesString(component, componentName);
const commandsString = generateCommandCasesString(
component,
componentName,
);
const extendString = getClassExtendString(component);

const replacedTemplate = template
.replace(
/::_IMPORTS_::/g,
Array.from(imports)
.sort()
.join('\n'),
)
.replace(/::_CLASSNAME_::/g, className)
.replace('::_EXTEND_CLASSES_::', extendString)
.replace('::_PROP_CASES_::', propsString)
.replace(
'::_METHODS_::',
generateMethods(propsString, commandsString),
)
.replace(/::_INTERFACE_CLASSNAME_::/g, interfaceClassName);
const replacedTemplate = template
.replace(
/::_IMPORTS_::/g,
Array.from(imports)
.sort()
.join('\n'),
)
.replace(/::_CLASSNAME_::/g, className)
.replace('::_EXTEND_CLASSES_::', extendString)
.replace('::_PROP_CASES_::', propsString)
.replace(
'::_METHODS_::',
generateMethods(propsString, commandsString),
)
.replace(/::_INTERFACE_CLASSNAME_::/g, interfaceClassName);

files.set(fileName, replacedTemplate);
});
files.set(fileName, replacedTemplate);
});
});

return files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,33 +208,41 @@ module.exports = {
return;
}

return Object.keys(components).forEach(componentName => {
const component = components[componentName];
const className = getInterfaceJavaClassName(componentName);
const fileName = `${className}.java`;

const imports = getImports(component);
const propsString = generatePropsString(component, imports);
const commandsString = generateCommandsString(component, componentName);
const extendString = getClassExtendString(component);

const replacedTemplate = template
.replace(
/::_IMPORTS_::/g,
Array.from(imports)
.sort()
.join('\n'),
)
.replace(/::_CLASSNAME_::/g, className)
.replace('::_EXTEND_CLASSES_::', extendString)
.replace(
'::_METHODS_::',
[propsString, commandsString].join('\n' + ' ').trimRight(),
)
.replace('::_COMMAND_HANDLERS_::', commandsString);

files.set(fileName, replacedTemplate);
});
return Object.keys(components)
.filter(componentName => {
const component = components[componentName];
return component.excludedPlatform !== 'android';
})
.forEach(componentName => {
const component = components[componentName];
const className = getInterfaceJavaClassName(componentName);
const fileName = `${className}.java`;

const imports = getImports(component);
const propsString = generatePropsString(component, imports);
const commandsString = generateCommandsString(
component,
componentName,
);
const extendString = getClassExtendString(component);

const replacedTemplate = template
.replace(
/::_IMPORTS_::/g,
Array.from(imports)
.sort()
.join('\n'),
)
.replace(/::_CLASSNAME_::/g, className)
.replace('::_EXTEND_CLASSES_::', extendString)
.replace(
'::_METHODS_::',
[propsString, commandsString].join('\n' + ' ').trimRight(),
)
.replace('::_COMMAND_HANDLERS_::', commandsString);

files.set(fileName, replacedTemplate);
});
});

return files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,27 @@ const COMMANDS_AND_PROPS: SchemaType = {
},
};

const EXCLUDE_ANDROID: SchemaType = {
modules: {
ExcludedAndroid: {
components: {
ExcludedAndroidComponent: {
excludedPlatform: 'android',
extendsProps: [
{
type: 'ReactNativeBuiltInType',
knownTypeName: 'ReactNativeCoreViewProps',
},
],
events: [],
props: [],
commands: [],
},
},
},
},
};

module.exports = {
NO_PROPS_NO_EVENTS,
INTERFACE_ONLY,
Expand All @@ -1528,4 +1549,5 @@ module.exports = {
TWO_COMPONENTS_DIFFERENT_FILES,
COMMANDS,
COMMANDS_AND_PROPS,
EXCLUDE_ANDROID,
};
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,32 @@ namespace react {
} // namespace react
} // namespace facebook
",
}
`;

exports[`GenerateComponentDescriptorH can generate fixture EXCLUDE_ANDROID 1`] = `
Map {
"ComponentDescriptors.h" => "
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/components/EXCLUDE_ANDROID/ShadowNodes.h>
#include <react/core/ConcreteComponentDescriptor.h>
namespace facebook {
namespace react {
using ExcludedAndroidComponentComponentDescriptor = ConcreteComponentDescriptor<ExcludedAndroidComponentShadowNode>;
} // namespace react
} // namespace facebook
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,29 @@ NS_ASSUME_NONNULL_END",
}
`;
exports[`GenerateComponentHObjCpp can generate fixture EXCLUDE_ANDROID 1`] = `
Map {
"RCTComponentViewHelpers.h" => "/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol RCTExcludedAndroidComponentViewProtocol <NSObject>
@end
NS_ASSUME_NONNULL_END",
}
`;
exports[`GenerateComponentHObjCpp can generate fixture FLOAT_PROPS 1`] = `
Map {
"RCTComponentViewHelpers.h" => "/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ void InterfaceOnlyComponentEventEmitter::onDire tChange(InterfaceOnlyComponentOn
});
}
} // namespace react
} // namespace facebook
",
}
`;

exports[`GenerateEventEmitterCpp can generate fixture EXCLUDE_ANDROID 1`] = `
Map {
"EventEmitters.cpp" => "
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <react/components/EXCLUDE_ANDROID/EventEmitters.h>
namespace facebook {
namespace react {
} // namespace react
} // namespace facebook
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,30 @@ class InterfaceOnlyComponentEventEmitter : public ViewEventEmitter {
void onDire tChange(InterfaceOnlyComponentOnDire tChangeStruct value) const;
};
} // namespace react
} // namespace facebook
",
}
`;

exports[`GenerateEventEmitterH can generate fixture EXCLUDE_ANDROID 1`] = `
Map {
"EventEmitters.h" => "
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/components/view/ViewEventEmitter.h>
namespace facebook {
namespace react {
} // namespace react
} // namespace facebook
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,35 @@ InterfaceOnlyComponentProps::InterfaceOnlyComponentProps(
}
`;

exports[`GeneratePropsCpp can generate fixture EXCLUDE_ANDROID 1`] = `
Map {
"Props.cpp" => "
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <react/components/EXCLUDE_ANDROID/Props.h>
#include <react/core/propsConversions.h>
namespace facebook {
namespace react {
ExcludedAndroidComponentProps::ExcludedAndroidComponentProps(
const ExcludedAndroidComponentProps &sourceProps,
const RawProps &rawProps): ViewProps(sourceProps, rawProps)
{}
} // namespace react
} // namespace facebook
",
}
`;

exports[`GeneratePropsCpp can generate fixture FLOAT_PROPS 1`] = `
Map {
"Props.cpp" => "
Expand Down
Loading

0 comments on commit b6a23d8

Please sign in to comment.