initial
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
node_modules/
|
||||||
|
dist_ts/
|
||||||
|
dist_rust/
|
||||||
|
.nogit/
|
||||||
|
coverage/
|
||||||
|
.DS_Store
|
||||||
4
cli.child.ts
Normal file
4
cli.child.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
process.env.CLI_CALL = 'true';
|
||||||
|
import * as cliTool from './ts/index.js';
|
||||||
|
cliTool.runCli();
|
||||||
4
cli.js
Normal file
4
cli.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
process.env.CLI_CALL = 'true';
|
||||||
|
const cliTool = await import('./dist_ts/index.js');
|
||||||
|
cliTool.runCli();
|
||||||
4
cli.ts.js
Normal file
4
cli.ts.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
process.env.CLI_CALL = 'true';
|
||||||
|
import * as tsrun from '@git.zone/tsrun';
|
||||||
|
tsrun.runPath('./cli.child.js');
|
||||||
21
license.md
Normal file
21
license.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 Task Venture Capital GmbH
|
||||||
|
|
||||||
|
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.
|
||||||
29
npmextra.json
Normal file
29
npmextra.json
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"@git.zone/cli": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "code.foss.global",
|
||||||
|
"gitscope": "git.zone",
|
||||||
|
"gitrepo": "tsrust",
|
||||||
|
"description": "A tool for compiling Rust projects, detecting Cargo workspaces, building with cargo, and placing binaries in a conventional dist_rust directory.",
|
||||||
|
"npmPackagename": "@git.zone/tsrust",
|
||||||
|
"license": "MIT",
|
||||||
|
"keywords": [
|
||||||
|
"Rust",
|
||||||
|
"cargo",
|
||||||
|
"compilation",
|
||||||
|
"CLI tool",
|
||||||
|
"build tool",
|
||||||
|
"workspace",
|
||||||
|
"binary"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
package.json
Normal file
62
package.json
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
{
|
||||||
|
"name": "@git.zone/tsrust",
|
||||||
|
"version": "1.0.1",
|
||||||
|
"private": false,
|
||||||
|
"description": "A tool for compiling Rust projects, detecting Cargo workspaces, building with cargo, and placing binaries in a conventional dist_rust directory.",
|
||||||
|
"main": "dist_ts/index.js",
|
||||||
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
|
"bin": {
|
||||||
|
"tsrust": "./cli.js"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "tstest test/test.ts --verbose",
|
||||||
|
"build": "tsbuild --web --skiplibcheck"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.foss.global/git.zone/tsrust.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"Rust",
|
||||||
|
"cargo",
|
||||||
|
"compilation",
|
||||||
|
"CLI tool",
|
||||||
|
"build tool",
|
||||||
|
"workspace",
|
||||||
|
"binary"
|
||||||
|
],
|
||||||
|
"author": "Task Venture Capital GmbH",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://code.foss.global/git.zone/tsrust/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://code.foss.global/git.zone/tsrust#README",
|
||||||
|
"dependencies": {
|
||||||
|
"@push.rocks/early": "^4.0.4",
|
||||||
|
"@push.rocks/smartcli": "^4.0.19",
|
||||||
|
"@push.rocks/smartfile": "^13.1.2",
|
||||||
|
"@push.rocks/smartpath": "^6.0.0",
|
||||||
|
"@push.rocks/smartshell": "^3.0.6",
|
||||||
|
"smol-toml": "^1.3.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@git.zone/tsbuild": "^4.1.2",
|
||||||
|
"@git.zone/tsrun": "^2.0.1",
|
||||||
|
"@git.zone/tstest": "^3.1.4",
|
||||||
|
"@types/node": "^22.15.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
]
|
||||||
|
}
|
||||||
8327
pnpm-lock.yaml
generated
Normal file
8327
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
readme.hints.md
Normal file
12
readme.hints.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# tsrust hints
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
- Follows tsbuild patterns exactly (cli.js, cli.child.ts, cli.ts.js entry points)
|
||||||
|
- Uses smartcli for CLI, smartshell for cargo execution, smol-toml for TOML parsing
|
||||||
|
- Three modules: mod_cli, mod_cargo, mod_fs
|
||||||
|
|
||||||
|
## Key patterns
|
||||||
|
- smartshell `.exec()` streams output to terminal (non-silent)
|
||||||
|
- smartshell `.execSilent()` captures output without printing
|
||||||
|
- Cargo workspace detection: check for `[workspace]` section in Cargo.toml
|
||||||
|
- Binary targets: look for `[[bin]]` entries in member crate Cargo.toml files
|
||||||
179
readme.md
Normal file
179
readme.md
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
# @git.zone/tsrust
|
||||||
|
|
||||||
|
A CLI build tool for Rust projects that follows the same conventions as `@git.zone/tsbuild`. It detects your `rust/` source directory, parses `Cargo.toml` (including workspaces), runs `cargo build --release`, and copies the resulting binaries into a clean `dist_rust/` directory at the project root.
|
||||||
|
|
||||||
|
## Issue Reporting and Security
|
||||||
|
|
||||||
|
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Install globally via npm:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install -g @git.zone/tsrust
|
||||||
|
```
|
||||||
|
|
||||||
|
Or as a project-level dev dependency:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm install --save-dev @git.zone/tsrust
|
||||||
|
```
|
||||||
|
|
||||||
|
> ⚡ **Prerequisite:** You need a working Rust toolchain. Install via [rustup.rs](https://rustup.rs/) if you haven't already.
|
||||||
|
|
||||||
|
## The Convention
|
||||||
|
|
||||||
|
`tsrust` mirrors the directory convention established by `tsbuild`:
|
||||||
|
|
||||||
|
| Tool | Source Directory | Output Directory |
|
||||||
|
|------|-----------------|-----------------|
|
||||||
|
| `tsbuild` | `ts/` | `dist_ts/` |
|
||||||
|
| **`tsrust`** | **`rust/`** | **`dist_rust/`** |
|
||||||
|
|
||||||
|
Your Rust code lives in `rust/` (or `ts_rust/` as fallback), and compiled binaries land in `dist_rust/` — ready for packaging, deployment, or further tooling.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### 🔨 Build (Default Command)
|
||||||
|
|
||||||
|
Simply run `tsrust` from your project root:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tsrust
|
||||||
|
```
|
||||||
|
|
||||||
|
This will:
|
||||||
|
1. Verify that `cargo` is available
|
||||||
|
2. Locate your `rust/` directory (containing `Cargo.toml`)
|
||||||
|
3. Parse the workspace to discover all `[[bin]]` targets
|
||||||
|
4. Run `cargo build --release` with full streaming output
|
||||||
|
5. Copy each binary to `dist_rust/` with executable permissions (`chmod 755`)
|
||||||
|
6. Report file sizes and total build time
|
||||||
|
|
||||||
|
**Example output:**
|
||||||
|
|
||||||
|
```
|
||||||
|
Using cargo 1.90.0 (840b83a10 2025-07-30)
|
||||||
|
Found Rust project at: rust
|
||||||
|
Detected Cargo workspace
|
||||||
|
Binary targets: rustproxy
|
||||||
|
Running: cargo build --release
|
||||||
|
Compiling rustproxy v0.1.0
|
||||||
|
Finished `release` profile [optimized] target(s) in 29.01s
|
||||||
|
Copied rustproxy (13.4 MB) -> dist_rust/rustproxy
|
||||||
|
Done in 29.2s
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🐛 Debug Build
|
||||||
|
|
||||||
|
Build with the debug profile instead of release:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tsrust --debug
|
||||||
|
```
|
||||||
|
|
||||||
|
Binaries are taken from `rust/target/debug/` instead of `rust/target/release/`.
|
||||||
|
|
||||||
|
### 🧹 Clean Before Building
|
||||||
|
|
||||||
|
Run `cargo clean` before building to force a full rebuild:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tsrust --clean
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🗑️ Clean Only
|
||||||
|
|
||||||
|
Remove all build artifacts without rebuilding:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tsrust clean
|
||||||
|
```
|
||||||
|
|
||||||
|
This runs `cargo clean` in the Rust directory and deletes the `dist_rust/` output directory.
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
`tsrust` expects your project to follow this layout:
|
||||||
|
|
||||||
|
```
|
||||||
|
my-project/
|
||||||
|
├── rust/ # 🦀 Your Rust source code
|
||||||
|
│ ├── Cargo.toml # Root manifest (workspace or single crate)
|
||||||
|
│ ├── src/
|
||||||
|
│ │ └── main.rs # (for single-crate projects)
|
||||||
|
│ └── crates/ # (for workspace projects)
|
||||||
|
│ ├── my-binary/
|
||||||
|
│ │ ├── Cargo.toml # Contains [[bin]] targets
|
||||||
|
│ │ └── src/
|
||||||
|
│ └── my-lib/
|
||||||
|
│ ├── Cargo.toml
|
||||||
|
│ └── src/
|
||||||
|
├── dist_rust/ # 📦 Output: compiled binaries go here
|
||||||
|
│ └── my-binary
|
||||||
|
├── ts/ # (your TypeScript code, built by tsbuild)
|
||||||
|
├── dist_ts/ # (TypeScript output)
|
||||||
|
└── package.json
|
||||||
|
```
|
||||||
|
|
||||||
|
### Workspace Support
|
||||||
|
|
||||||
|
`tsrust` fully supports Cargo workspaces. It reads the `[workspace]` section from your root `Cargo.toml`, iterates through all `members`, and discovers binary targets from each member crate's `Cargo.toml`.
|
||||||
|
|
||||||
|
Binary target discovery follows Cargo's own rules:
|
||||||
|
- **Explicit `[[bin]]` entries** → uses the `name` field from each entry
|
||||||
|
- **Implicit binary** → if no `[[bin]]` is declared but `src/main.rs` exists, uses the `[package] name`
|
||||||
|
- **Library-only crates** → skipped (no binary output expected)
|
||||||
|
|
||||||
|
### Fallback Directory
|
||||||
|
|
||||||
|
If no `rust/` directory is found, `tsrust` checks for `ts_rust/` as a fallback. This supports projects that use the `ts_` prefix convention for all source directories.
|
||||||
|
|
||||||
|
## Programmatic API
|
||||||
|
|
||||||
|
`tsrust` exports its internals for use in other Node.js/TypeScript tools:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { CargoConfig, CargoRunner, FsHelpers, TsRustCli } from '@git.zone/tsrust';
|
||||||
|
|
||||||
|
// Parse a Cargo workspace
|
||||||
|
const config = new CargoConfig('/path/to/rust');
|
||||||
|
const info = await config.parse();
|
||||||
|
console.log(info.isWorkspace); // true
|
||||||
|
console.log(info.binTargets); // ['rustproxy']
|
||||||
|
|
||||||
|
// Run cargo build
|
||||||
|
const runner = new CargoRunner('/path/to/rust');
|
||||||
|
const result = await runner.build({ debug: false, clean: false });
|
||||||
|
console.log(result.success); // true
|
||||||
|
console.log(result.exitCode); // 0
|
||||||
|
|
||||||
|
// File helpers
|
||||||
|
await FsHelpers.ensureEmptyDir('/path/to/dist_rust');
|
||||||
|
await FsHelpers.copyFile(src, dest);
|
||||||
|
await FsHelpers.makeExecutable(dest);
|
||||||
|
const size = await FsHelpers.getFileSize(dest);
|
||||||
|
console.log(FsHelpers.formatFileSize(size)); // "13.4 MB"
|
||||||
|
```
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||||
|
|
||||||
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
### Trademarks
|
||||||
|
|
||||||
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||||
|
|
||||||
|
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||||
|
|
||||||
|
### Company Information
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||||
24
test/test.ts
Normal file
24
test/test.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||||
|
import * as tsrust from '../ts/index.js';
|
||||||
|
|
||||||
|
tap.test('should export CargoConfig', async () => {
|
||||||
|
expect(tsrust.CargoConfig).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should export CargoRunner', async () => {
|
||||||
|
expect(tsrust.CargoRunner).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should export FsHelpers', async () => {
|
||||||
|
expect(tsrust.FsHelpers).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should export TsRustCli', async () => {
|
||||||
|
expect(tsrust.TsRustCli).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should export runCli', async () => {
|
||||||
|
expect(tsrust.runCli).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tap.start();
|
||||||
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitance data
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@git.zone/tsrust',
|
||||||
|
version: '1.0.1',
|
||||||
|
description: 'A tool for compiling Rust projects, detecting Cargo workspaces, building with cargo, and placing binaries in a conventional dist_rust directory.',
|
||||||
|
};
|
||||||
8
ts/index.ts
Normal file
8
ts/index.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
plugins.early.start('@git.zone/tsrust');
|
||||||
|
|
||||||
|
export * from './mod_fs/index.js';
|
||||||
|
export * from './mod_cargo/index.js';
|
||||||
|
export * from './mod_cli/index.js';
|
||||||
|
|
||||||
|
plugins.early.stop();
|
||||||
89
ts/mod_cargo/classes.cargoconfig.ts
Normal file
89
ts/mod_cargo/classes.cargoconfig.ts
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import * as path from 'path';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as smolToml from 'smol-toml';
|
||||||
|
import { FsHelpers } from '../mod_fs/index.js';
|
||||||
|
|
||||||
|
export interface ICargoWorkspaceInfo {
|
||||||
|
isWorkspace: boolean;
|
||||||
|
rustDir: string;
|
||||||
|
binTargets: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CargoConfig {
|
||||||
|
private rustDir: string;
|
||||||
|
|
||||||
|
constructor(rustDir: string) {
|
||||||
|
this.rustDir = rustDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async parse(): Promise<ICargoWorkspaceInfo> {
|
||||||
|
const cargoTomlPath = path.join(this.rustDir, 'Cargo.toml');
|
||||||
|
const content = await fs.promises.readFile(cargoTomlPath, 'utf-8');
|
||||||
|
const parsed = smolToml.parse(content);
|
||||||
|
|
||||||
|
const isWorkspace = !!(parsed as any).workspace;
|
||||||
|
let binTargets: string[] = [];
|
||||||
|
|
||||||
|
if (isWorkspace) {
|
||||||
|
binTargets = await this.collectWorkspaceBinTargets(parsed);
|
||||||
|
} else {
|
||||||
|
binTargets = this.collectCrateBinTargets(parsed, this.rustDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
isWorkspace,
|
||||||
|
rustDir: this.rustDir,
|
||||||
|
binTargets,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async collectWorkspaceBinTargets(parsed: any): Promise<string[]> {
|
||||||
|
const members: string[] = parsed.workspace?.members || [];
|
||||||
|
const binTargets: string[] = [];
|
||||||
|
|
||||||
|
for (const member of members) {
|
||||||
|
const memberDir = path.join(this.rustDir, member);
|
||||||
|
const memberCargoToml = path.join(memberDir, 'Cargo.toml');
|
||||||
|
|
||||||
|
if (!(await FsHelpers.fileExists(memberCargoToml))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const memberContent = await fs.promises.readFile(memberCargoToml, 'utf-8');
|
||||||
|
const memberParsed = smolToml.parse(memberContent);
|
||||||
|
const memberBins = this.collectCrateBinTargets(memberParsed, memberDir);
|
||||||
|
binTargets.push(...memberBins);
|
||||||
|
}
|
||||||
|
|
||||||
|
return binTargets;
|
||||||
|
}
|
||||||
|
|
||||||
|
private collectCrateBinTargets(parsed: any, crateDir: string): string[] {
|
||||||
|
const binTargets: string[] = [];
|
||||||
|
|
||||||
|
// Check for explicit [[bin]] entries
|
||||||
|
if (Array.isArray(parsed.bin)) {
|
||||||
|
for (const bin of parsed.bin) {
|
||||||
|
if (bin.name) {
|
||||||
|
binTargets.push(bin.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no [[bin]] but package has a name and src/main.rs exists, use package name
|
||||||
|
if (binTargets.length === 0 && parsed.package?.name) {
|
||||||
|
const mainRsPath = path.join(crateDir, 'src', 'main.rs');
|
||||||
|
// Use sync check since this is called during parsing
|
||||||
|
try {
|
||||||
|
const stat = fs.statSync(mainRsPath);
|
||||||
|
if (stat.isFile()) {
|
||||||
|
binTargets.push(parsed.package.name);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// No main.rs, not a binary crate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return binTargets;
|
||||||
|
}
|
||||||
|
}
|
||||||
59
ts/mod_cargo/classes.cargorunner.ts
Normal file
59
ts/mod_cargo/classes.cargorunner.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import * as plugins from '../plugins.js';
|
||||||
|
|
||||||
|
export interface ICargoRunResult {
|
||||||
|
success: boolean;
|
||||||
|
exitCode: number;
|
||||||
|
stdout: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CargoRunner {
|
||||||
|
private shell: plugins.smartshell.Smartshell;
|
||||||
|
private rustDir: string;
|
||||||
|
|
||||||
|
constructor(rustDir: string) {
|
||||||
|
this.rustDir = rustDir;
|
||||||
|
this.shell = new plugins.smartshell.Smartshell({
|
||||||
|
executor: 'bash',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async checkCargoInstalled(): Promise<boolean> {
|
||||||
|
const result = await this.shell.execSilent('cargo --version');
|
||||||
|
return result.exitCode === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getCargoVersion(): Promise<string> {
|
||||||
|
const result = await this.shell.execSilent('cargo --version');
|
||||||
|
return result.stdout.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async build(options: { debug?: boolean; clean?: boolean } = {}): Promise<ICargoRunResult> {
|
||||||
|
if (options.clean) {
|
||||||
|
console.log('Cleaning previous build...');
|
||||||
|
await this.clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = options.debug ? '' : ' --release';
|
||||||
|
const command = `cd ${this.rustDir} && cargo build${profile}`;
|
||||||
|
|
||||||
|
console.log(`Running: cargo build${profile}`);
|
||||||
|
const result = await this.shell.exec(command);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: result.exitCode === 0,
|
||||||
|
exitCode: result.exitCode,
|
||||||
|
stdout: result.stdout,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async clean(): Promise<ICargoRunResult> {
|
||||||
|
const command = `cd ${this.rustDir} && cargo clean`;
|
||||||
|
const result = await this.shell.exec(command);
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: result.exitCode === 0,
|
||||||
|
exitCode: result.exitCode,
|
||||||
|
stdout: result.stdout,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
2
ts/mod_cargo/index.ts
Normal file
2
ts/mod_cargo/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export { CargoConfig } from './classes.cargoconfig.js';
|
||||||
|
export { CargoRunner } from './classes.cargorunner.js';
|
||||||
146
ts/mod_cli/classes.tsrustcli.ts
Normal file
146
ts/mod_cli/classes.tsrustcli.ts
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
import * as path from 'path';
|
||||||
|
import * as plugins from '../plugins.js';
|
||||||
|
import { CargoConfig } from '../mod_cargo/index.js';
|
||||||
|
import { CargoRunner } from '../mod_cargo/index.js';
|
||||||
|
import { FsHelpers } from '../mod_fs/index.js';
|
||||||
|
|
||||||
|
export class TsRustCli {
|
||||||
|
private cli: plugins.smartcli.Smartcli;
|
||||||
|
private cwd: string;
|
||||||
|
|
||||||
|
constructor(cwd: string = process.cwd()) {
|
||||||
|
this.cwd = cwd;
|
||||||
|
this.cli = new plugins.smartcli.Smartcli();
|
||||||
|
this.registerCommands();
|
||||||
|
}
|
||||||
|
|
||||||
|
private registerCommands(): void {
|
||||||
|
this.registerStandardCommand();
|
||||||
|
this.registerCleanCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
private registerStandardCommand(): void {
|
||||||
|
this.cli.standardCommand().subscribe(async (argvArg) => {
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
|
// Check cargo is installed
|
||||||
|
const runner = new CargoRunner(this.cwd); // temporary, just for version check
|
||||||
|
if (!(await runner.checkCargoInstalled())) {
|
||||||
|
console.error('Error: cargo is not installed or not in PATH.');
|
||||||
|
console.error('Install Rust via https://rustup.rs/');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cargoVersion = await runner.getCargoVersion();
|
||||||
|
console.log(`Using ${cargoVersion}`);
|
||||||
|
|
||||||
|
// Detect rust directory
|
||||||
|
const rustDir = await this.detectRustDir();
|
||||||
|
if (!rustDir) {
|
||||||
|
console.error('Error: No rust/ or ts_rust/ directory found with a Cargo.toml.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Found Rust project at: ${path.relative(this.cwd, rustDir) || '.'}`);
|
||||||
|
|
||||||
|
// Parse Cargo.toml
|
||||||
|
const cargoConfig = new CargoConfig(rustDir);
|
||||||
|
const workspaceInfo = await cargoConfig.parse();
|
||||||
|
|
||||||
|
if (workspaceInfo.isWorkspace) {
|
||||||
|
console.log('Detected Cargo workspace');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (workspaceInfo.binTargets.length === 0) {
|
||||||
|
console.error('Error: No binary targets found in Cargo.toml.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Binary targets: ${workspaceInfo.binTargets.join(', ')}`);
|
||||||
|
|
||||||
|
// Build
|
||||||
|
const isDebug = !!(argvArg as any).debug;
|
||||||
|
const shouldClean = !!(argvArg as any).clean;
|
||||||
|
const cargoRunner = new CargoRunner(rustDir);
|
||||||
|
const buildResult = await cargoRunner.build({ debug: isDebug, clean: shouldClean });
|
||||||
|
|
||||||
|
if (!buildResult.success) {
|
||||||
|
console.error(`Build failed with exit code ${buildResult.exitCode}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy binaries to dist_rust/
|
||||||
|
const profile = isDebug ? 'debug' : 'release';
|
||||||
|
const targetDir = path.join(rustDir, 'target', profile);
|
||||||
|
const distDir = path.join(this.cwd, 'dist_rust');
|
||||||
|
|
||||||
|
await FsHelpers.ensureEmptyDir(distDir);
|
||||||
|
|
||||||
|
for (const binName of workspaceInfo.binTargets) {
|
||||||
|
const srcBinary = path.join(targetDir, binName);
|
||||||
|
const destBinary = path.join(distDir, binName);
|
||||||
|
|
||||||
|
if (!(await FsHelpers.fileExists(srcBinary))) {
|
||||||
|
console.warn(`Warning: Expected binary not found: ${srcBinary}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await FsHelpers.copyFile(srcBinary, destBinary);
|
||||||
|
await FsHelpers.makeExecutable(destBinary);
|
||||||
|
|
||||||
|
const size = await FsHelpers.getFileSize(destBinary);
|
||||||
|
console.log(`Copied ${binName} (${FsHelpers.formatFileSize(size)}) -> dist_rust/${binName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
|
console.log(`Done in ${elapsed}s`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private registerCleanCommand(): void {
|
||||||
|
this.cli.addCommand('clean').subscribe(async (_argvArg) => {
|
||||||
|
// Clean cargo build
|
||||||
|
const rustDir = await this.detectRustDir();
|
||||||
|
if (rustDir) {
|
||||||
|
console.log('Running cargo clean...');
|
||||||
|
const runner = new CargoRunner(rustDir);
|
||||||
|
await runner.clean();
|
||||||
|
console.log('Cargo clean complete.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove dist_rust/
|
||||||
|
const distDir = path.join(this.cwd, 'dist_rust');
|
||||||
|
if (await FsHelpers.directoryExists(distDir)) {
|
||||||
|
await FsHelpers.removeDirectory(distDir);
|
||||||
|
console.log('Removed dist_rust/');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Clean complete.');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async detectRustDir(): Promise<string | null> {
|
||||||
|
// Check rust/ first
|
||||||
|
const rustDir = path.join(this.cwd, 'rust');
|
||||||
|
if (await FsHelpers.fileExists(path.join(rustDir, 'Cargo.toml'))) {
|
||||||
|
return rustDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to ts_rust/
|
||||||
|
const tsRustDir = path.join(this.cwd, 'ts_rust');
|
||||||
|
if (await FsHelpers.fileExists(path.join(tsRustDir, 'Cargo.toml'))) {
|
||||||
|
return tsRustDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public run(): void {
|
||||||
|
this.cli.startParse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const runCli = async (): Promise<void> => {
|
||||||
|
const cli = new TsRustCli();
|
||||||
|
cli.run();
|
||||||
|
};
|
||||||
1
ts/mod_cli/index.ts
Normal file
1
ts/mod_cli/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { TsRustCli, runCli } from './classes.tsrustcli.js';
|
||||||
56
ts/mod_fs/classes.fshelpers.ts
Normal file
56
ts/mod_fs/classes.fshelpers.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
export class FsHelpers {
|
||||||
|
public static async fileExists(filePath: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const stat = await fs.promises.stat(filePath);
|
||||||
|
return stat.isFile();
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async directoryExists(dirPath: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const stat = await fs.promises.stat(dirPath);
|
||||||
|
return stat.isDirectory();
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async ensureEmptyDir(dirPath: string): Promise<void> {
|
||||||
|
if (await FsHelpers.directoryExists(dirPath)) {
|
||||||
|
await fs.promises.rm(dirPath, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
await fs.promises.mkdir(dirPath, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async copyFile(src: string, dest: string): Promise<void> {
|
||||||
|
const destDir = path.dirname(dest);
|
||||||
|
await fs.promises.mkdir(destDir, { recursive: true });
|
||||||
|
await fs.promises.copyFile(src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async makeExecutable(filePath: string): Promise<void> {
|
||||||
|
await fs.promises.chmod(filePath, 0o755);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getFileSize(filePath: string): Promise<number> {
|
||||||
|
const stat = await fs.promises.stat(filePath);
|
||||||
|
return stat.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async removeDirectory(dirPath: string): Promise<void> {
|
||||||
|
if (await FsHelpers.directoryExists(dirPath)) {
|
||||||
|
await fs.promises.rm(dirPath, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static formatFileSize(bytes: number): string {
|
||||||
|
if (bytes < 1024) return `${bytes} B`;
|
||||||
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
||||||
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
ts/mod_fs/index.ts
Normal file
1
ts/mod_fs/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { FsHelpers } from './classes.fshelpers.js';
|
||||||
13
ts/plugins.ts
Normal file
13
ts/plugins.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import * as early from '@push.rocks/early';
|
||||||
|
import * as smartcli from '@push.rocks/smartcli';
|
||||||
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
|
import * as smartpath from '@push.rocks/smartpath';
|
||||||
|
import * as smartshell from '@push.rocks/smartshell';
|
||||||
|
|
||||||
|
export {
|
||||||
|
early,
|
||||||
|
smartcli,
|
||||||
|
smartfile,
|
||||||
|
smartpath,
|
||||||
|
smartshell,
|
||||||
|
};
|
||||||
9
tsconfig.json
Normal file
9
tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user