From ed4072772293af2e02be07dbdba687e7f66bff01 Mon Sep 17 00:00:00 2001 From: ddwwwww <18829031738@163.com> Date: Sat, 27 Jul 2024 10:15:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E4=BE=9Bcpp=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- harmony/fs/src/main/cpp/CMakeLists.txt | 12 ++ harmony/fs/src/main/cpp/FsPackage.h | 50 ++++++ harmony/fs/src/main/cpp/FsTurboModule.cpp | 152 ++++++++++++++++++ harmony/fs/src/main/cpp/FsTurboModule.h | 35 ++++ harmony/fs/src/main/ets/FsTurboModule.ts | 2 +- harmony/fs/src/main/ets/namespace/index.ets | 7 + harmony/fs/src/main/ets/namespace/ts.ts | 7 + .../namespace/turboModules/ReactNativeFs.ts | 57 +++++++ .../src/main/ets/namespace/turboModules/ts.ts | 7 + package.json | 9 +- 10 files changed, 329 insertions(+), 9 deletions(-) create mode 100644 harmony/fs/src/main/cpp/CMakeLists.txt create mode 100644 harmony/fs/src/main/cpp/FsPackage.h create mode 100644 harmony/fs/src/main/cpp/FsTurboModule.cpp create mode 100644 harmony/fs/src/main/cpp/FsTurboModule.h create mode 100644 harmony/fs/src/main/ets/namespace/index.ets create mode 100644 harmony/fs/src/main/ets/namespace/ts.ts create mode 100644 harmony/fs/src/main/ets/namespace/turboModules/ReactNativeFs.ts create mode 100644 harmony/fs/src/main/ets/namespace/turboModules/ts.ts diff --git a/harmony/fs/src/main/cpp/CMakeLists.txt b/harmony/fs/src/main/cpp/CMakeLists.txt new file mode 100644 index 00000000..1e5f76c5 --- /dev/null +++ b/harmony/fs/src/main/cpp/CMakeLists.txt @@ -0,0 +1,12 @@ +# 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. + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_VERBOSE_MAKEFILE on) + +file(GLOB rnoh_fs_SRC CONFIGURE_DEPENDS *.cpp) +add_library(rnoh_fs SHARED ${rnoh_fs_SRC}) +target_include_directories(rnoh_fs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(rnoh_fs PUBLIC rnoh) diff --git a/harmony/fs/src/main/cpp/FsPackage.h b/harmony/fs/src/main/cpp/FsPackage.h new file mode 100644 index 00000000..a41529f8 --- /dev/null +++ b/harmony/fs/src/main/cpp/FsPackage.h @@ -0,0 +1,50 @@ +/** + * MIT License + * + * Copyright (C) 2024 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "RNOH/Package.h" +#include "FsTurboModule.h" + +using namespace rnoh; +using namespace facebook; + +class NativeRTNFsFactoryDelegate : public TurboModuleFactoryDelegate { + public: + SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override { + if (name == "ReactNativeFs") { + return std::make_shared(ctx, name); + } + return nullptr; + } +}; + +namespace rnoh { + +class FsPackage : public Package { + public: + FsPackage(Package::Context ctx) : Package(ctx) {} + std::unique_ptr createTurboModuleFactoryDelegate() override { + return std::make_unique(); + } +}; +} // namespace rnoh diff --git a/harmony/fs/src/main/cpp/FsTurboModule.cpp b/harmony/fs/src/main/cpp/FsTurboModule.cpp new file mode 100644 index 00000000..18ac3421 --- /dev/null +++ b/harmony/fs/src/main/cpp/FsTurboModule.cpp @@ -0,0 +1,152 @@ +/** + * MIT License + * + * Copyright (C) 2024 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "FsTurboModule.h" +#include "RNOH/ArkTSTurboModule.h" + +using namespace rnoh; +using namespace facebook; + +static jsi::Value __hostFunction_FsTurboModule_getConstants(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + { + return jsi::Value(static_cast(turboModule).call(rt, "getConstants", args, count)); + } +} + +static jsi::Value __hostFunction_FsTurboModule_appendFile(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt,"appendFile",args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_copyFile(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "copyFile",args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_exists(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "exists",args,count); +} + +static jsi::Value __hostFunction_FsTurboModule_mkdir(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "mkdir", args,count); +} + + +static jsi::Value __hostFunction_FsTurboModule_readFile(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "readFile",args, count); +} + + +static jsi::Value __hostFunction_FsTurboModule_writeFile(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "writeFile",args, count); +} + + +static jsi::Value __hostFunction_FsTurboModule_readFileAssets(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, + size_t count) { + return static_cast(turboModule).callAsync(rt, "readFileAssets", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_unlink(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "unlink", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_hash(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "hash", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_moveFile(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "moveFile", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_read(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "read", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_write(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "write", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_touch(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "touch", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_stat(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "stat", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_downloadFile(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "downloadFile", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_readDir(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "readDir", args, count); +} + +static jsi::Value __hostFunction_FsTurboModule_existsAssets(jsi::Runtime &rt, react::TurboModule &turboModule, + const jsi::Value *args, size_t count) { + return static_cast(turboModule).callAsync(rt, "existsAssets", args, count); +} + +FsTurboModule::FsTurboModule(const ArkTSTurboModule::Context ctx, const std::string name) + : ArkTSTurboModule(ctx, name) { + methodMap_["getConstants"] = MethodMetadata{0, __hostFunction_FsTurboModule_getConstants}; + methodMap_["readFile"] = MethodMetadata{1, __hostFunction_FsTurboModule_readFile}; + methodMap_["exists"] = MethodMetadata{1, __hostFunction_FsTurboModule_exists}; + methodMap_["mkdir"] = MethodMetadata{2, __hostFunction_FsTurboModule_mkdir}; + methodMap_["appendFile"] = MethodMetadata{3, __hostFunction_FsTurboModule_appendFile}; + methodMap_["writeFile"] = MethodMetadata{3, __hostFunction_FsTurboModule_writeFile}; + methodMap_["readFileAssets"] = MethodMetadata{1, __hostFunction_FsTurboModule_readFileAssets}; + methodMap_["copyFile"] = MethodMetadata{2, __hostFunction_FsTurboModule_copyFile}; + methodMap_["unlink"] = MethodMetadata{1, __hostFunction_FsTurboModule_unlink}; + methodMap_["hash"] = MethodMetadata{2, __hostFunction_FsTurboModule_hash}; + methodMap_["moveFile"] = MethodMetadata{2, __hostFunction_FsTurboModule_moveFile}; + methodMap_["read"] = MethodMetadata{3, __hostFunction_FsTurboModule_read}; + methodMap_["write"] = MethodMetadata{3, __hostFunction_FsTurboModule_write}; + methodMap_["touch"] = MethodMetadata{3, __hostFunction_FsTurboModule_touch}; + methodMap_["stat"] = MethodMetadata{1, __hostFunction_FsTurboModule_stat}; + methodMap_["downloadFile"] = MethodMetadata{1, __hostFunction_FsTurboModule_downloadFile}; + methodMap_["readDir"] = MethodMetadata{1, __hostFunction_FsTurboModule_readDir}; + methodMap_["existsAssets"] = MethodMetadata{1, __hostFunction_FsTurboModule_existsAssets}; +} diff --git a/harmony/fs/src/main/cpp/FsTurboModule.h b/harmony/fs/src/main/cpp/FsTurboModule.h new file mode 100644 index 00000000..9ceeb466 --- /dev/null +++ b/harmony/fs/src/main/cpp/FsTurboModule.h @@ -0,0 +1,35 @@ +/** + * MIT License + * + * Copyright (C) 2024 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +// NOTE: This entire file should be codegen'ed. +#pragma once +#include "RNOH/ArkTSTurboModule.h" + +namespace rnoh { + +class JSI_EXPORT FsTurboModule : public ArkTSTurboModule { +public: + FsTurboModule(const ArkTSTurboModule::Context ctx, const std::string name); +}; + +} // namespace rnoh diff --git a/harmony/fs/src/main/ets/FsTurboModule.ts b/harmony/fs/src/main/ets/FsTurboModule.ts index 907e98b8..d4bb68ba 100644 --- a/harmony/fs/src/main/ets/FsTurboModule.ts +++ b/harmony/fs/src/main/ets/FsTurboModule.ts @@ -23,7 +23,7 @@ */ import { TurboModule, RNOHError, TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; -import { TM } from "@rnoh/react-native-openharmony/generated/ts" +import { TM } from "./namespace/ts"; import fs, { ListFileOptions, ReadOptions, ReadTextOptions, WriteOptions } from '@ohos.file.fs'; import hash from '@ohos.file.hash'; import { BusinessError } from '@ohos.base'; diff --git a/harmony/fs/src/main/ets/namespace/index.ets b/harmony/fs/src/main/ets/namespace/index.ets new file mode 100644 index 00000000..513e07ed --- /dev/null +++ b/harmony/fs/src/main/ets/namespace/index.ets @@ -0,0 +1,7 @@ +/** + * This code was generated by "react-native codegen-harmony" + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost once the code is regenerated. + */ +export * from "./ts" diff --git a/harmony/fs/src/main/ets/namespace/ts.ts b/harmony/fs/src/main/ets/namespace/ts.ts new file mode 100644 index 00000000..d80a95b6 --- /dev/null +++ b/harmony/fs/src/main/ets/namespace/ts.ts @@ -0,0 +1,7 @@ +/** + * This code was generated by "react-native codegen-harmony" + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost once the code is regenerated. + */ +export * as TM from "./turboModules/ts" diff --git a/harmony/fs/src/main/ets/namespace/turboModules/ReactNativeFs.ts b/harmony/fs/src/main/ets/namespace/turboModules/ReactNativeFs.ts new file mode 100644 index 00000000..51a5ed98 --- /dev/null +++ b/harmony/fs/src/main/ets/namespace/turboModules/ReactNativeFs.ts @@ -0,0 +1,57 @@ +/** + * This code was generated by "react-native codegen-harmony" + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost once the code is regenerated. + * + * @generatorVersion: 1 + */ + +export namespace ReactNativeFs { + export const NAME = 'ReactNativeFs' as const + + export type MkdirOptionsT = {NSURLIsExcludedFromBackupKey?: boolean} + + export type StatResult = {ctime: number, mtime: number, size: number, mode: number, originalFilepath: string, type: number} + + export type DownloadResult = {jobId: number, statusCode: number, bytesWritten: number} + + export interface Spec { + getConstants(): Object; + + readFile(path: string): Promise; + + exists(path: string): Promise; + + mkdir(path: string, options: MkdirOptionsT): Promise; + + appendFile(path: string, contents: string, encoding: string): Promise; + + writeFile(path: string, contents: string, encoding: string): Promise; + + readFileAssets(path: string): Promise; + + copyFile(from: string, into: string): Promise; + + unlink(filepath: string): Promise; + + hash(filepath: string, algorithm: string): Promise; + + moveFile(filepath: string, destPath: string): Promise; + + read(path: string, length: number, position: number): Promise; + + write(filepath: string, contents: string, position: number): Promise; + + stat(filepath: string): Promise; + + touch(filepath: string, mtime: number, ctime: number): Promise; + + downloadFile(bridgeOptions: Object): Promise; + + readDir(dirpath: string): Promise; + + existsAssets(filepath: string): Promise; + + } +} diff --git a/harmony/fs/src/main/ets/namespace/turboModules/ts.ts b/harmony/fs/src/main/ets/namespace/turboModules/ts.ts new file mode 100644 index 00000000..c4df0af9 --- /dev/null +++ b/harmony/fs/src/main/ets/namespace/turboModules/ts.ts @@ -0,0 +1,7 @@ +/** + * This code was generated by "react-native codegen-harmony" + * + * Do not edit this file as changes may cause incorrect behavior and will be + * lost once the code is regenerated. + */ +export * from "./ReactNativeFs" diff --git a/package.json b/package.json index 3343dbe1..a48e34d2 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,7 @@ "flow": "flow; test $? -eq 0 -o $? -eq 2" }, "harmony": { - "alias": "react-native-fs", - "codegenConfig": { - "specPaths": [ - "./src" - ] - } + "alias": "react-native-fs" }, "repository": { "type": "git", @@ -23,8 +18,6 @@ "keywords": [ "react-component", "react-native", - "ios", - "android", "fs", "filesystem", "download",