craby_codegen 0.1.0-dev.20251027111854

Craby code generator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::path::PathBuf;

use craby_common::{
    constants::{android_path, dest_lib_name, java_base_path, jni_base_path},
    utils::string::{flat_case, kebab_case, pascal_case, SanitizedString},
};
use indoc::formatdoc;

use crate::{
    constants::cxx_mod_cls_name,
    types::{CodegenContext, Schema},
    utils::indent_str,
};

use super::types::{GenerateResult, Generator, GeneratorInvoker, Template};

pub struct AndroidTemplate;
pub struct AndroidGenerator;

pub enum AndroidFileType {
    JNIEntry,
    CmakeLists,
    RctPackage,
}

impl AndroidTemplate {
    fn file_path(&self, file_type: &AndroidFileType, project_name: &str) -> PathBuf {
        match file_type {
            AndroidFileType::JNIEntry => PathBuf::from("OnLoad.cpp"),
            AndroidFileType::CmakeLists => PathBuf::from("CMakeLists.txt"),
            AndroidFileType::RctPackage => {
                PathBuf::from(format!("{}Package.kt", pascal_case(project_name)))
            }
        }
    }

    /// Returns `JNI_OnLoad` function implementation
    ///
    /// # Generated Code
    ///
    /// ```cpp
    /// jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    ///   facebook::react::registerCxxModuleToGlobalModuleMap(
    ///     craby::mymodule::MyTestModule::kModuleName,
    ///     [](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
    ///       return std::make_shared<craby::mymodule::MyTestModule>(jsInvoker);
    ///     });
    ///   return JNI_VERSION_1_6;
    /// }
    ///
    /// extern "C"
    /// JNIEXPORT void JNICALL
    /// Java_com_mymodule_MyTestModulePackage_nativeSetDataPath(JNIEnv *env, jclass clazz, jstring jDataPath) {
    ///     auto dataPath = std::string(env->GetStringUTFChars(jDataPath, nullptr));
    ///     craby::mymodule::MyTestModule::dataPath = dataPath;
    /// }
    /// ```
    fn jni_entry(
        &self,
        schemas: &Vec<Schema>,
        project_name: &str,
    ) -> Result<String, anyhow::Error> {
        let mut cxx_includes = vec![];
        let mut cxx_prepares = Vec::with_capacity(schemas.len());
        let mut cxx_registers = Vec::with_capacity(schemas.len());
        let jni_fn_name = format!(
            "Java_com_{}_{}Package_nativeSetDataPath",
            flat_case(project_name),
            pascal_case(project_name)
        );

        for schema in schemas {
            let cxx_mod = cxx_mod_cls_name(&schema.module_name);
            let flat_name = flat_case(&schema.module_name);

            let cxx_namespace = format!("craby::{}::{}", flat_name, cxx_mod);
            let cxx_include = format!("#include <{cxx_mod}.hpp>");
            let cxx_prepare = format!("{cxx_namespace}::dataPath = dataPath;");
            let cxx_register = formatdoc! {
                r#"
                facebook::react::registerCxxModuleToGlobalModuleMap(
                  {cxx_namespace}::kModuleName,
                  [](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {{
                    return std::make_shared<{cxx_namespace}>(jsInvoker);
                  }});"#,
                cxx_namespace = cxx_namespace
            };

            cxx_includes.push(cxx_include);
            cxx_prepares.push(cxx_prepare);
            cxx_registers.push(cxx_register);
        }

        let content = formatdoc! {
            r#"
            {cxx_includes}
            #include <ReactCommon/CxxTurboModuleUtils.h>
            #include <jni.h>

            jint JNI_OnLoad(JavaVM *vm, void *reserved) {{
            {cxx_registers}
              return JNI_VERSION_1_6;
            }}
            
            extern "C"
            JNIEXPORT void JNICALL
            {jni_fn_name}(JNIEnv *env, jclass clazz, jstring jDataPath) {{
              auto dataPath = std::string(env->GetStringUTFChars(jDataPath, nullptr));
            {cxx_prepares}
            }}"#,
            cxx_includes = cxx_includes.join("\n"),
            cxx_prepares = indent_str(cxx_prepares.join("\n"), 2),
            cxx_registers = indent_str(cxx_registers.join("\n"), 2),
        };

        Ok(content)
    }

    /// Generates the CMakeLists.txt for Android native module build configuration.
    ///
    /// # Generated Code
    ///
    /// ```cmake
    /// cmake_minimum_required(VERSION 3.13)
    ///
    /// project(craby-my-app)
    ///
    /// set (CMAKE_VERBOSE_MAKEFILE ON)
    /// set (CMAKE_CXX_STANDARD 20)
    ///
    /// find_package(ReactAndroid REQUIRED CONFIG)
    ///
    /// # Import the pre-built Craby library
    /// add_library(my-app-lib STATIC IMPORTED)
    /// set_target_properties(my-app-lib PROPERTIES
    ///   IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/src/main/jni/libs/${ANDROID_ABI}/libcraby_my_app.a"
    /// )
    /// target_include_directories(my-app-lib INTERFACE
    ///   "${CMAKE_SOURCE_DIR}/src/main/jni/include"
    /// )
    ///
    /// # Generated C++ source files by Craby
    /// add_library(cxx-my-app SHARED
    ///   src/main/jni/OnLoad.cpp
    ///   src/main/jni/src/ffi.rs.cc
    ///   ../cpp/CxxMyTestModule.cpp
    /// )
    /// target_include_directories(cxx-my-app PRIVATE
    ///   ../cpp
    /// )
    ///
    /// target_link_libraries(cxx-my-app
    ///   # android
    ///   ReactAndroid::reactnative
    ///   ReactAndroid::jsi
    ///   # my-app-lib
    ///   my-app-lib
    /// )
    ///
    /// # From ReactAndroid/cmake-utils/folly-flags.cmake
    /// target_compile_definitions(cxx-craby-test PRIVATE
    ///   -DFOLLY_NO_CONFIG=1
    ///   -DFOLLY_HAVE_CLOCK_GETTIME=1
    ///   -DFOLLY_USE_LIBCPP=1
    ///   -DFOLLY_CFG_NO_COROUTINES=1
    ///   -DFOLLY_MOBILE=1
    ///   -DFOLLY_HAVE_RECVMMSG=1
    ///   -DFOLLY_HAVE_PTHREAD=1
    ///   # Once we target android-23 above, we can comment
    ///   # the following line. NDK uses GNU style stderror_r() after API 23.
    ///   -DFOLLY_HAVE_XSI_STRERROR_R=1
    /// )
    /// ```
    fn cmakelists(&self, project: &CodegenContext) -> String {
        let kebab_name = kebab_case(&project.name);
        let lib_name = dest_lib_name(&SanitizedString::from(&project.name));
        let cxx_mod_cpp_files = project
            .schemas
            .iter()
            .map(|schema| format!("../cpp/{}.cpp", cxx_mod_cls_name(&schema.module_name)))
            .collect::<Vec<_>>();

        formatdoc! {
            r#"
            cmake_minimum_required(VERSION 3.13)

            project(craby-{kebab_name})

            set (CMAKE_VERBOSE_MAKEFILE ON)
            set (CMAKE_CXX_STANDARD 20)

            find_package(ReactAndroid REQUIRED CONFIG)

            # Import the pre-built Craby library
            add_library({kebab_name}-lib STATIC IMPORTED)
            set_target_properties({kebab_name}-lib PROPERTIES
              IMPORTED_LOCATION "${{CMAKE_SOURCE_DIR}}/src/main/jni/libs/${{ANDROID_ABI}}/{lib_name}"
            )
            target_include_directories({kebab_name}-lib INTERFACE
              "${{CMAKE_SOURCE_DIR}}/src/main/jni/include"
            )

            # Generated C++ source files by Craby
            add_library(cxx-{kebab_name} SHARED
              src/main/jni/OnLoad.cpp
              src/main/jni/src/ffi.rs.cc
            {cxx_mod_cpp_files}
            )
            target_include_directories(cxx-{kebab_name} PRIVATE
              ../cpp
            )

            target_link_libraries(cxx-{kebab_name}
              # android
              ReactAndroid::reactnative
              ReactAndroid::jsi
              # {kebab_name}-lib
              {kebab_name}-lib
            )

            # From ReactAndroid/cmake-utils/folly-flags.cmake
            target_compile_definitions(cxx-craby-test PRIVATE
              -DFOLLY_NO_CONFIG=1
              -DFOLLY_HAVE_CLOCK_GETTIME=1
              -DFOLLY_USE_LIBCPP=1
              -DFOLLY_CFG_NO_COROUTINES=1
              -DFOLLY_MOBILE=1
              -DFOLLY_HAVE_RECVMMSG=1
              -DFOLLY_HAVE_PTHREAD=1
              # Once we target android-23 above, we can comment
              # the following line. NDK uses GNU style stderror_r() after API 23.
              -DFOLLY_HAVE_XSI_STRERROR_R=1
            )"#,
            kebab_name = kebab_name,
            lib_name = lib_name,
            cxx_mod_cpp_files = indent_str(cxx_mod_cpp_files.join("\n"), 2),
        }
    }

    fn rct_package(&self, schemas: &[Schema], project_name: &str) -> String {
        let lib_name = format!("cxx-{}", kebab_case(project_name));
        let flat_name = flat_case(project_name);
        let pascal_name = pascal_case(project_name);
        let jni_prepare_module_names = schemas
            .iter()
            .map(|schema| format!("\"__craby{}_JNI_prepare__\"", schema.module_name))
            .collect::<Vec<_>>();

        formatdoc! {
            r#"
            package com.{flat_name}

            import com.facebook.react.BaseReactPackage
            import com.facebook.react.bridge.NativeModule
            import com.facebook.react.bridge.ReactApplicationContext
            import com.facebook.react.bridge.ReactContextBaseJavaModule
            import com.facebook.react.module.model.ReactModuleInfo
            import com.facebook.react.module.model.ReactModuleInfoProvider
            import com.facebook.react.turbomodule.core.interfaces.TurboModule
            import com.facebook.soloader.SoLoader
            import javax.annotation.Nonnull

            class {pascal_name}Package : BaseReactPackage() {{
              companion object {{
                val JNI_PREPARE_MODULE_NAME = setOf(
            {jni_prepare_module_names}
                )
              }}

              init {{
                SoLoader.loadLibrary("{lib_name}")
              }}

              override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {{
                if (name in JNI_PREPARE_MODULE_NAME) {{
                  nativeSetDataPath(reactContext.filesDir.absolutePath)
                  return {pascal_name}Package.TurboModulePlaceholder(reactContext, name)
                }}
                return null
              }}

              override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {{
                return ReactModuleInfoProvider {{
                  val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
                  JNI_PREPARE_MODULE_NAME.forEach {{ name ->
                    moduleInfos[name] = ReactModuleInfo(
                      name,
                      name,
                      false,  // canOverrideExistingModule
                      false,  // needsEagerInit
                      false,  // isCxxModule
                      true,  // isTurboModule
                    )
                  }}
                  moduleInfos
                }}
              }}

              private external fun nativeSetDataPath(dataPath: String)

              class TurboModulePlaceholder(reactContext: ReactApplicationContext?, private val name: String) :
                ReactContextBaseJavaModule(reactContext),
                TurboModule {{
                @Nonnull
                override fun getName(): String {{
                  return name
                }}
              }}
            }}"#,
            lib_name = lib_name,
            flat_name = flat_name,
            pascal_name = pascal_name,
            jni_prepare_module_names = indent_str(jni_prepare_module_names.join(",\n"), 6),
        }
    }
}

impl Template for AndroidTemplate {
    type FileType = AndroidFileType;

    fn render(
        &self,
        project: &CodegenContext,
        file_type: &Self::FileType,
    ) -> Result<Vec<(PathBuf, String)>, anyhow::Error> {
        let path = self.file_path(file_type, &project.name);
        let content = match file_type {
            AndroidFileType::JNIEntry => self.jni_entry(&project.schemas, &project.name),
            AndroidFileType::CmakeLists => Ok(self.cmakelists(project)),
            AndroidFileType::RctPackage => Ok(self.rct_package(&project.schemas, &project.name)),
        }?;

        Ok(vec![(path, content)])
    }
}

impl Default for AndroidGenerator {
    fn default() -> Self {
        Self::new()
    }
}

impl AndroidGenerator {
    pub fn new() -> Self {
        Self
    }
}

impl Generator<AndroidTemplate> for AndroidGenerator {
    fn cleanup(_: &CodegenContext) -> Result<(), anyhow::Error> {
        Ok(())
    }

    fn generate(&self, project: &CodegenContext) -> Result<Vec<GenerateResult>, anyhow::Error> {
        let android_base_path = android_path(&project.root);
        let jni_base_path = jni_base_path(&project.root);
        let java_base_path = java_base_path(&project.root, &project.name);
        let template = self.template_ref();
        let mut files = vec![];

        let jni_res = template
            .render(project, &AndroidFileType::JNIEntry)?
            .into_iter()
            .map(|(path, content)| GenerateResult {
                path: jni_base_path.join(path),
                content,
                overwrite: true,
            })
            .collect::<Vec<_>>();

        let cmake_res = template
            .render(project, &AndroidFileType::CmakeLists)?
            .into_iter()
            .map(|(path, content)| GenerateResult {
                path: android_base_path.join(path),
                content,
                overwrite: true,
            })
            .collect::<Vec<_>>();

        let rct_package_res = template
            .render(project, &AndroidFileType::RctPackage)?
            .into_iter()
            .map(|(path, content)| GenerateResult {
                path: java_base_path.join(path),
                content,
                overwrite: true,
            })
            .collect::<Vec<_>>();

        files.extend(jni_res);
        files.extend(cmake_res);
        files.extend(rct_package_res);

        Ok(files)
    }

    fn template_ref(&self) -> &AndroidTemplate {
        &AndroidTemplate
    }
}

impl GeneratorInvoker for AndroidGenerator {
    fn invoke_generate(
        &self,
        project: &CodegenContext,
    ) -> Result<Vec<GenerateResult>, anyhow::Error> {
        self.generate(project)
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;

    use crate::tests::get_codegen_context;

    use super::*;

    #[test]
    fn test_android_generator() {
        let ctx = get_codegen_context();
        let generator = AndroidGenerator::new();
        let results = generator.generate(&ctx).unwrap();
        let result = results
            .iter()
            .map(|res| format!("{}\n{}", res.path.display(), res.content))
            .collect::<Vec<_>>()
            .join("\n\n");

        assert_snapshot!(result);
    }
}