nitro-codegen
Version:
The code-generator for react-native-nitro-modules.
85 lines (70 loc) • 2.98 kB
text/typescript
import { NitroConfig } from '../../config/NitroConfig.js'
import { getForwardDeclaration } from '../../syntax/c++/getForwardDeclaration.js'
import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
import { getAllKnownTypes } from '../../syntax/createType.js'
import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
import {
createFileMetadataString,
isNotDuplicate,
} from '../../syntax/helpers.js'
import type { SourceFile } from '../../syntax/SourceFile.js'
import { getTypeAs } from '../../syntax/types/getTypeAs.js'
import { HybridObjectType } from '../../syntax/types/HybridObjectType.js'
export function getUmbrellaHeaderName(): string {
const moduleName = NitroConfig.getIosModuleName()
return `${moduleName}-Swift-Cxx-Umbrella.hpp`
}
export function createSwiftUmbrellaHeader(): SourceFile {
const moduleName = NitroConfig.getIosModuleName()
const filename = getUmbrellaHeaderName()
const types = getAllKnownTypes('swift')
const swiftForwardDeclares = types
.filter((t) => t.kind === 'hybrid-object')
.map((t) => {
const hybridObjectType = getTypeAs(t, HybridObjectType)
const name = getHybridObjectName(hybridObjectType.hybridObjectName)
return getForwardDeclaration('class', name.HybridTSpecCxx, moduleName)
})
.filter(isNotDuplicate)
const imports = types.flatMap((t) => t.getRequiredImports())
const forwardDeclarations = imports
.map((i) => i.forwardDeclaration)
.filter((f) => f != null)
.filter(isNotDuplicate)
const includes = imports.map((i) => includeHeader(i)).filter(isNotDuplicate)
const code = `
${createFileMetadataString(filename, '///')}
// Forward declarations of C++ defined types
${forwardDeclarations.sort().join('\n')}
// Include C++ defined types
${includes.sort().join('\n')}
// C++ helpers for Swift
// Common C++ types used in Swift
// Forward declarations of Swift defined types
${swiftForwardDeclares.sort().join('\n')}
// Include Swift defined types
// This header is generated by Xcode/Swift on every app build.
// If it cannot be found, make sure the Swift module's name (= podspec name) is actually "${moduleName}".
// Same as above, but used when building with frameworks (\`use_frameworks\`)
`
return {
content: code,
language: 'c++',
name: filename,
platform: 'ios',
subdirectory: [],
}
}