Skip to content

Commit

Permalink
refactor: create integration functions and update all packages
Browse files Browse the repository at this point in the history
  • Loading branch information
bugra9 committed Dec 9, 2024
1 parent b976f88 commit afcc1df
Show file tree
Hide file tree
Showing 121 changed files with 17,122 additions and 11,692 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
LICENSE
*.xcframework.zip
*.tgz
.cppjs
ios/build
android/build
2 changes: 1 addition & 1 deletion packages/cpp.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpp.js",
"version": "1.0.0-beta.20",
"version": "1.0.0-beta.26",
"license": "MIT",
"homepage": "https://cpp.js.org",
"repository": "https://github.com/bugra9/cpp.js.git",
Expand Down
1 change: 0 additions & 1 deletion packages/cpp.js/src/actions/buildJs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import fs from 'fs';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
Expand Down
2 changes: 1 addition & 1 deletion packages/cpp.js/src/bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import fs, { mkdirSync } from 'node:fs';
import fs from 'node:fs';
import { Command, Option } from 'commander';
import glob from 'glob';
import upath from 'upath';
Expand Down
2 changes: 2 additions & 0 deletions packages/cpp.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export { default as getCmakeParameters } from './actions/getCmakeParameters.js';
export { default as createXCFramework } from './actions/createXCFramework.js';
export { default as getAllBridges } from './actions/getAllBridges.js';
export { default as run } from './actions/run.js';
export { default as getCppJsScript } from './integration/getCppJsScript.js';
export { default as getDependFilePath } from './integration/getDependFilePath.js';
91 changes: 91 additions & 0 deletions packages/cpp.js/src/integration/getCppJsScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import getData from '../actions/getData.js';
import loadJson from '../utils/loadJson.js';
import state from '../state/index.js';

export default function getCppJsScript(platform, bridgePath = null) {
if (!platform || !state.platforms.All.includes(platform)) {
throw new Error('The platform is not available!');
}
const env = JSON.stringify(getData('env', platform));
const getPlatformScript = state.platforms.WebAssembly.includes(platform) ? getWebScript : getReactNativeScript;

const bridgeExportFile = `${bridgePath}.exports.json`;
let symbols = null;
if (bridgePath) {
symbols = loadJson(bridgeExportFile);
}

let symbolExportDefineString = '';
let symbolExportAssignString = '';
if (symbols && Array.isArray(symbols)) {
symbolExportDefineString = symbols.map((s) => `export let ${s} = null;`).join('\n');
symbolExportAssignString = symbols.map((s) => `${s} = m.${s};`).join('\n');
}

const scriptContent = `
AllSymbols = m;
${symbolExportAssignString}
`;

return `
export let AllSymbols = {};
${symbolExportDefineString}
${getPlatformScript(env, scriptContent)}
`;
}

function getReactNativeScript(env, modulePrefix) {
return `
import { NativeModules } from 'react-native';
import Module from '@cpp.js/core-embind-jsi';
const { RNJsiLib } = NativeModules;
function setEnv() {
const env = JSON.parse('${env}');
const CPPJS_DATA_PATH = Module.CppJS.getEnv('CPPJS_DATA_PATH');
Object.entries(env).forEach(([key, value]) => {
Module.CppJS.setEnv(key, value.replace('_CPPJS_DATA_PATH_', CPPJS_DATA_PATH), true);
});
}
export function initCppJs(config = {}) {
return new Promise((resolve, reject) => {
if (RNJsiLib && RNJsiLib.start) {
RNJsiLib.start();
setEnv();
const m = Module;
${modulePrefix}
resolve(Module);
} else {
reject('Module failed to initialise.');
}
});
}
`;
}

function getWebScript(env, modulePrefix) {
const params = `{
...config,
env: {...${env}, ...config.env},
paths: {
wasm: 'cpp.wasm',
data: 'cpp.data.txt'
}
}`;

return `
export function initCppJs(config = {}) {
return new Promise((resolve, reject) => {
import(/* webpackIgnore: true */ '/cpp.js').then(n => {
return window.CppJs.initCppJs(${params});
}).then(m => {
${modulePrefix}
resolve(m);
});
});
}
`;
}
22 changes: 22 additions & 0 deletions packages/cpp.js/src/integration/getDependFilePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import state from '../state/index.js';

export default function getDependFilePath(source, platform) {
const headerRegex = new RegExp(`\\.(${state.config.ext.header.join('|')})$`);
const moduleRegex = new RegExp(`\\.(${state.config.ext.module.join('|')})$`);

const dependPackage = state.config.allDependencies.find((d) => source.startsWith(d.package.name));
if (dependPackage) {
const filePath = source.substring(dependPackage.package.name.length + 1);

let path = `${dependPackage.paths.output}/prebuilt/${platform}/${filePath}`;
if (headerRegex.test(source)) {
path = `${dependPackage.paths.output}/prebuilt/${platform}/include/${filePath}`;
} else if (moduleRegex.test(source)) {
path = `${dependPackage.paths.output}/prebuilt/${platform}/swig/${filePath}`;
}

return path;
}

return null;
}
2 changes: 1 addition & 1 deletion packages/cpp.js/src/utils/getOsUserAndGroupId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os from 'os';
import os from 'node:os';

let osUserAndGroupId;
export default function getOsUserAndGroupId() {
Expand Down
4 changes: 2 additions & 2 deletions packages/cpp.js/src/utils/pullDockerImage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { execFileSync } from 'child_process';
import { execFileSync } from 'node:child_process';

let isDockerImageAvailable = false;

export function getDockerImage() {
return 'bugra9/cpp.js:0.2.6';
return 'bugra9/cpp.js:0.2.7';
}

export default function pullDockerImage() {
Expand Down
7 changes: 4 additions & 3 deletions packages/cppjs-core-create-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-cpp.js",
"version": "1.0.0-beta.9",
"version": "1.0.0-beta.14",
"description": "Create Cpp.js Applications",
"homepage": "https://github.com/bugra9/cpp.js/tree/main/packages/cppjs-core-create-app#readme",
"repository": "https://github.com/bugra9/cpp.js.git",
Expand All @@ -9,6 +9,7 @@
"main": "src/index.js",
"type": "module",
"dependencies": {
"cpp.js": "workspace:*",
"@cpp.js/sample-lib-cmake": "workspace:*",
"@cpp.js/sample-lib-prebuilt-matrix": "workspace:*",
"@cpp.js/sample-lib-source": "workspace:*",
Expand All @@ -20,8 +21,8 @@
"@cpp.js/sample-web-vue-vite": "workspace:*",
"@cpp.js/sample-backend-nodejs-wasm": "workspace:*",
"@cpp.js/sample-cloud-cloudflare-worker": "workspace:*",
"@cpp.js/sample-mobile-reactnative-nativecli": "1.0.0-beta.7",
"@cpp.js/sample-mobile-reactnative-expo": "1.0.0-beta.3",
"@cpp.js/sample-mobile-reactnative-nativecli": "1.0.0-beta.9",
"@cpp.js/sample-mobile-reactnative-expo": "1.0.0-beta.4",
"fs-extra": "^11.1.0",
"kleur": "^4.1.5",
"prompts": "^2.4.2"
Expand Down
4 changes: 2 additions & 2 deletions packages/cppjs-core-create-app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
/* eslint-disable no-plusplus */
/* eslint-disable no-console */
import fs from 'fs';
import fs from 'node:fs';
import fse from 'fs-extra';
import path from 'path';
import path from 'node:path';
import {
bold, cyan, gray, green,
} from 'kleur/colors';
Expand Down
28 changes: 15 additions & 13 deletions packages/cppjs-core-create-app/src/samples.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import getParentPath from 'cpp.js/src/utils/getParentPath.js';

import sampleLibCmake from '@cpp.js/sample-lib-cmake/cppjs.config.js';
import sampleLibMatrix from '@cpp.js/sample-lib-prebuilt-matrix/cppjs.config.js';
import sampleLibSource from '@cpp.js/sample-lib-source/cppjs.config.js';
Expand All @@ -22,27 +24,27 @@ export default {
'Select a bundler:',
],
Vanilla: {
path: sampleWebVanilla.paths.project,
path: getParentPath(sampleWebVanilla.paths.config),
},
React: {
'Create React App (CRA)': {
path: sampleWebReactCRA.paths.project,
path: getParentPath(sampleWebReactCRA.paths.config),
},
Rspack: {
path: sampleWebReactRspack.paths.project,
path: getParentPath(sampleWebReactRspack.paths.config),
},
Vite: {
path: sampleWebReactVite.paths.project,
path: getParentPath(sampleWebReactVite.paths.config),
},
},
Vue: {
Vite: {
path: sampleWebVueVite.paths.project,
path: getParentPath(sampleWebVueVite.paths.config),
},
},
Svelte: {
Vite: {
path: sampleWebSvelteVite.paths.project,
path: getParentPath(sampleWebSvelteVite.paths.config),
},
},
},
Expand All @@ -53,10 +55,10 @@ export default {
],
'React Native': {
'Native CLI': {
path: sampleMobileReactNativeNativeCLI.paths.project,
path: getParentPath(sampleMobileReactNativeNativeCLI.paths.config),
},
Expo: {
path: sampleMobileReactNativeExpo.paths.project,
path: getParentPath(sampleMobileReactNativeExpo.paths.config),
},
},
},
Expand All @@ -67,7 +69,7 @@ export default {
],
'Node.js': {
WebAssembly: {
path: sampleBackendNodeJsWasm.paths.project,
path: getParentPath(sampleBackendNodeJsWasm.paths.config),
},
},
},
Expand All @@ -76,21 +78,21 @@ export default {
'Select a service:',
],
'Cloudflare Worker': {
path: sampleCloudCloudflareWorker.paths.project,
path: getParentPath(sampleCloudCloudflareWorker.paths.config),
},
},
Library: {
Questions: [
'Select a library type:',
],
Prebuilt: {
path: sampleLibMatrix.paths.project,
path: getParentPath(sampleLibMatrix.paths.config),
},
Source: {
path: sampleLibSource.paths.project,
path: getParentPath(sampleLibSource.paths.config),
},
CMake: {
path: sampleLibCmake.paths.project,
path: getParentPath(sampleLibCmake.paths.config),
},
},
};
2 changes: 1 addition & 1 deletion packages/cppjs-core-create-app/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileURLToPath } from 'url';
import { fileURLToPath } from 'node:url';

const pkgFromUserAgent = (userAgent) => {
if (!userAgent) return {};
Expand Down
2 changes: 1 addition & 1 deletion packages/cppjs-core-docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0-beta.1",
"private": "true",
"scripts": {
"build": "docker build --no-cache -t bugra9/cpp.js:latest -t bugra9/cpp.js:0.2.6 .",
"build": "docker build --no-cache -t bugra9/cpp.js:latest -t bugra9/cpp.js:0.2.7 .",
"push": "docker push bugra9/cpp.js --all-tags"
}
}
5 changes: 1 addition & 4 deletions packages/cppjs-core-embind-jsi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cpp.js/core-embind-jsi",
"version": "1.0.0-beta.21",
"version": "1.0.0-beta.26",
"description": "The Embind JSI integration tool enables seamless C++ integration with React Native and Expo.",
"homepage": "https://github.com/bugra9/cpp.js/tree/main/packages/cppjs-core-embind-jsi#readme",
"repository": "https://github.com/bugra9/cpp.js.git",
Expand All @@ -10,9 +10,6 @@
"@babel/runtime": "*",
"react-native": "*"
},
"dependencies": {
"cpp.js": "workspace:*"
},
"keywords": [
"embind",
"jsi",
Expand Down
4 changes: 4 additions & 0 deletions packages/cppjs-package-expat/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
*.xcframework.zip
*.tgz
.cppjs
*.dylib
dist/prebuilt/Android-arm64-v8a/lib/*.a
dist/prebuilt/**/bin
47 changes: 47 additions & 0 deletions packages/cppjs-package-expat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# @cpp.js/package-expat
**Precompiled expat library built with cpp.js for seamless integration in JavaScript, WebAssembly and React Native projects.**

<a href="https://www.npmjs.com/package/@cpp.js/package-expat">
<img alt="NPM version" src="https://img.shields.io/npm/v/@cpp.js/package-expat?style=for-the-badge" />
</a>
<a href="https://github.com/libexpat/libexpat/blob/master/COPYING">
<img alt="License" src="https://img.shields.io/npm/l/%40cpp.js%2Fpackage-expat?style=for-the-badge" />
</a>

## Integration
Start by installing these package with the following command:

```sh
npm install @cpp.js/package-expat
```

To enable the library, modify the cppjs.config.js file as shown below.
```diff
+import expat from '@cpp.js/package-expat/cppjs.config.js';

export default {
dependencies: [
+ expat
]
paths: {
config: import.meta.url,
}
};
```

## Usage
Below are the steps to use the expat in your C++ or JavaScript code.

### Usage in C++ Code
```diff
+#include <expat.h>

std::string Native::sample() {
+ return std::string(XML_ExpatVersion());
}
```

## License
This project includes the precompiled expat library, which is distributed under the [MIT License](https://github.com/libexpat/libexpat/blob/master/COPYING).

Expat Homepage: <https://github.com/libexpat/libexpat>
6 changes: 3 additions & 3 deletions packages/cppjs-package-expat/cppjs-package-expat.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Pod::Spec.new do |s|
s.module_name = "expat"
s.name = package["name"]
s.version = package["nativeVersion"]
s.summary = package["description"]
s.homepage = package["homepage"]
s.author = package["author"]
s.summary = "Fast streaming XML parser"
s.homepage = "https://github.com/libexpat/libexpat"
s.author = "Thai Open Source Software Center Ltd, Clark Cooper, and Expat maintainers"
s.source = { :http => "https://cpp.js.org" }
s.vendored_frameworks = 'expat.xcframework'
end
Loading

0 comments on commit afcc1df

Please sign in to comment.