Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow turbo-modules with names with @my-org/project-name #127

Merged
merged 4 commits into from
Oct 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/ubrn_cli/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ mod files {
templated_file!(PodspecTemplate, "module-template.podspec");
impl RenderedFile for PodspecTemplate {
fn path(&self, project_root: &Utf8Path) -> Utf8PathBuf {
let name = self.config.project.raw_name();
let name = self.config.project.podspec_filename();
let filename = format!("{name}.podspec");
project_root.join(filename)
}
Expand Down
5 changes: 2 additions & 3 deletions crates/ubrn_cli/src/codegen/templates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ include_directories(
${UNIFFI_BINDGEN_PATH}/cpp/includes
)

add_library(
${CMAKE_PROJECT_NAME} SHARED
add_library({{ self.config.project.cpp_filename() }} SHARED
{{ tm_dir }}/{{ self.config.project.cpp_filename() }}.cpp
{%- for m in self.config.modules %}
{{ bindings_dir }}/{{ m.cpp_filename() }}
Expand Down Expand Up @@ -63,7 +62,7 @@ find_package(fbjni REQUIRED CONFIG)
find_library(LOGCAT log)

target_link_libraries(
${CMAKE_PROJECT_NAME}
{{ self.config.project.cpp_filename() }}
fbjni::fbjni
ReactAndroid::jsi
ReactAndroid::turbomodulejsijni
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'

Pod::Spec.new do |s|
s.name = "{{ self.config.project.raw_name() }}"
s.name = "{{ self.config.project.cpp_filename() }}"
s.version = package["version"]
s.summary = package["description"]
s.homepage = package["homepage"]
Expand Down
35 changes: 30 additions & 5 deletions crates/ubrn_cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,31 @@ pub(crate) fn trim_react_native(name: &str) -> String {
.to_string()
}

pub(crate) fn org_and_name(name: &str) -> (Option<&str>, &str) {
if let Some((left, right)) = name.split_once('/') {
let org = left.strip_prefix('@').unwrap_or(left);
(Some(org), right)
} else {
(None, name)
}
}

pub(crate) fn lower(s: &str) -> String {
s.to_upper_camel_case().to_lowercase()
}

impl ProjectConfig {
pub(crate) fn project_root(&self) -> &Utf8Path {
&self.crate_.project_root
}

pub(crate) fn module_cpp(&self) -> String {
trim_react_native(&self.name).to_upper_camel_case()
let (org, name) = org_and_name(&self.name);
if org.is_some() {
name.to_upper_camel_case()
} else {
trim_react_native(name).to_upper_camel_case()
}
}
}

Expand All @@ -96,22 +114,29 @@ impl ProjectConfig {
}

pub(crate) fn cpp_namespace(&self) -> String {
trim_react_native(&self.name)
.to_upper_camel_case()
.to_lowercase()
let (org, name) = org_and_name(&self.name);
if let Some(org) = org {
format!("{}_{}", lower(org), lower(name))
} else {
lower(&trim_react_native(name))
}
}

pub(crate) fn cpp_filename(&self) -> String {
use heck::ToKebabCase;
self.raw_name().to_kebab_case()
}

pub(crate) fn podspec_filename(&self) -> String {
self.cpp_filename()
}

pub(crate) fn codegen_filename(&self) -> String {
format!("Native{}", self.spec_name())
}

pub(crate) fn spec_name(&self) -> String {
trim_react_native(&self.name).to_upper_camel_case()
self.module_cpp()
}

pub(crate) fn exclude_files(&self) -> &GlobSet {
Expand Down
21 changes: 12 additions & 9 deletions crates/ubrn_cli/src/config/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/

use heck::ToUpperCamelCase;
use serde::Deserialize;

use crate::config::{lower, org_and_name};

use super::{trim, trim_react_native};

#[derive(Deserialize)]
Expand Down Expand Up @@ -34,14 +35,16 @@ impl PackageJson {
.android
.java_package_name
.clone()
.unwrap_or_else(|| {
format!(
"com.{}",
trim_react_native(&self.name)
.to_upper_camel_case()
.to_lowercase()
)
})
.unwrap_or_else(|| self.default_android_package_name())
}

fn default_android_package_name(&self) -> String {
let (org, name) = org_and_name(&self.name);
if let Some(org) = org {
format!("com.{}.{}", lower(org), lower(name))
} else {
format!("com.{}", lower(&trim_react_native(name)))
}
}

pub(crate) fn repo(&self) -> &PackageJsonRepo {
Expand Down
15 changes: 10 additions & 5 deletions crates/ubrn_cli/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use std::{collections::HashMap, fmt::Display, process::Command, str::FromStr};
use anyhow::{Context, Error, Result};
use camino::{Utf8Path, Utf8PathBuf};
use clap::Args;
use heck::ToUpperCamelCase;
use serde::{Deserialize, Serialize};
use ubrn_common::{mk_dir, rm_dir, run_cmd, CrateMetadata};

use crate::{
building::{CommonBuildArgs, ExtraArgs},
config::{trim_react_native, ProjectConfig},
config::{org_and_name, trim_react_native, ProjectConfig},
rust::CrateConfig,
workspace,
};
Expand Down Expand Up @@ -44,10 +45,14 @@ impl IOsConfig {
}

fn default_framework_name() -> String {
format!(
"{}Framework",
trim_react_native(&workspace::package_json().name())
)
let name = workspace::package_json().name();
let (org, name) = org_and_name(&name);
let prefix = if let Some(org) = org {
format!("{}_{}", org, name).to_upper_camel_case()
} else {
trim_react_native(name).to_upper_camel_case()
};
format!("{prefix}Framework")
}

fn default_cargo_extras() -> ExtraArgs {
Expand Down
56 changes: 56 additions & 0 deletions integration/fixtures/turbo-module-testing/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { StyleSheet, View, Text } from "react-native";
import { multiply } from "react-native-by-hand";
import {
Calculator,
type BinaryOperator,
SafeAddition,
ComputationResult,
} from "../../src";

// A Rust object
const calculator = new Calculator();
// A Rust object implementing the Rust trait BinaryOperator
const addOp = new SafeAddition();

// A Typescript class, implementing BinaryOperator
class SafeMultiply implements BinaryOperator {
perform(lhs: bigint, rhs: bigint): bigint {
return lhs * rhs;
}
}
const multOp = new SafeMultiply();

// bigints
const three = 3n;
const seven = 7n;

// Perform the calculation, and to get an object
// representing the computation result.
const computation: ComputationResult = calculator
.calculate(addOp, three, three)
.calculateMore(multOp, seven)
.lastResult()!;

// Unpack the bigint value into a string.
const result = computation.value.toString();

export default function App() {
return (
<View style={styles.container}>
<Text>Result: {result}</Text>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
});
Loading