mirror of
https://github.com/imjasonh/krust
synced 2026-07-08 06:45:32 +00:00
Merge pull request #54 from imjasonh/ex
support building --example and --bin
This commit is contained in:
commit
bba26792f8
3 changed files with 66 additions and 4 deletions
12
README.md
12
README.md
|
|
@ -111,9 +111,21 @@ krust build example/hello-krust --no-push
|
|||
### Build with custom cargo arguments
|
||||
|
||||
```bash
|
||||
# Build with features
|
||||
krust build -- --features=prod
|
||||
|
||||
# Build an example binary
|
||||
krust build -- --example http_server
|
||||
|
||||
# Build a specific binary target
|
||||
krust build -- --bin my-app
|
||||
|
||||
# Combine multiple flags
|
||||
krust build -- --example server --features=tls
|
||||
```
|
||||
|
||||
When building examples or specific binaries, krust automatically detects the correct binary name and path from the cargo arguments.
|
||||
|
||||
## Supported Platforms
|
||||
|
||||
- `linux/amd64` (x86_64-unknown-linux-musl)
|
||||
|
|
|
|||
|
|
@ -142,10 +142,12 @@ impl RustBuilder {
|
|||
}
|
||||
|
||||
let binary_name = self.get_binary_name()?;
|
||||
let binary_path = target_dir
|
||||
.join(&self.target)
|
||||
.join("release")
|
||||
.join(&binary_name);
|
||||
let binary_subdir = self.get_binary_subdir();
|
||||
let mut binary_path = target_dir.join(&self.target).join("release");
|
||||
if let Some(subdir) = binary_subdir {
|
||||
binary_path = binary_path.join(subdir);
|
||||
}
|
||||
binary_path = binary_path.join(&binary_name);
|
||||
|
||||
// Sometimes cargo build completes but the binary isn't immediately visible
|
||||
// due to filesystem sync issues. Give it a moment.
|
||||
|
|
@ -169,6 +171,18 @@ impl RustBuilder {
|
|||
}
|
||||
|
||||
fn get_binary_name(&self) -> Result<String> {
|
||||
// Check if --example or --bin was specified
|
||||
let mut i = 0;
|
||||
while i < self.cargo_args.len() {
|
||||
if (self.cargo_args[i] == "--example" || self.cargo_args[i] == "--bin")
|
||||
&& i + 1 < self.cargo_args.len()
|
||||
{
|
||||
return Ok(self.cargo_args[i + 1].clone());
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Fall back to package name
|
||||
let cargo_toml_path = self.project_path.join("Cargo.toml");
|
||||
let content =
|
||||
std::fs::read_to_string(&cargo_toml_path).context("Failed to read Cargo.toml")?;
|
||||
|
|
@ -184,6 +198,16 @@ impl RustBuilder {
|
|||
|
||||
Ok(name.to_string())
|
||||
}
|
||||
|
||||
fn get_binary_subdir(&self) -> Option<&str> {
|
||||
// Check if --example was specified (examples go in "examples/" subdir)
|
||||
for (i, arg) in self.cargo_args.iter().enumerate() {
|
||||
if arg == "--example" && i + 1 < self.cargo_args.len() {
|
||||
return Some("examples");
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rust_target_triple(platform: &str) -> Result<String> {
|
||||
|
|
|
|||
26
tests/testdata/build_example.txt
vendored
Normal file
26
tests/testdata/build_example.txt
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# Test building examples with --example flag
|
||||
|
||||
-- Cargo.toml --
|
||||
[package]
|
||||
name = "test-app"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
-- src/main.rs --
|
||||
fn main() {
|
||||
println!("Hello from main!");
|
||||
}
|
||||
|
||||
-- examples/example_server.rs --
|
||||
fn main() {
|
||||
println!("Hello from example!");
|
||||
}
|
||||
|
||||
# Build the example binary
|
||||
[linux] env KRUST_REPO=ttl.sh/test exec ./krust build --no-push --platform linux/amd64 . -- --example example_server
|
||||
[darwin] env KRUST_REPO=ttl.sh/test exec ./krust build --no-push --platform linux/amd64 . -- --example example_server
|
||||
stderr 'Building Rust project'
|
||||
stderr 'Successfully built binary'
|
||||
stderr 'examples/example_server'
|
||||
stderr 'Successfully built image'
|
||||
Loading…
Add table
Add a link
Reference in a new issue