Skip to content

Commit a2fccaa

Browse files
committed
feat: add build info [latest]
1 parent d1621c3 commit a2fccaa

8 files changed

Lines changed: 123 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ For detailed API documentation, see [API Documentation](./docs/api.md).
204204

205205
## 📚 Documentation
206206

207-
- [API Reference](./docs/API.md) - Complete REST API documentation
207+
- [API Reference](./docs/api.md) - Complete REST API documentation
208208
- [Architecture Diagrams](docs/) - System architecture and workflows
209209

210210
## 🤝 Contributing

build.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::env;
2+
use std::process::Command;
3+
4+
fn main() {
5+
println!(
6+
"cargo:rustc-env=CARGO_PKG_VERSION={}",
7+
env!("CARGO_PKG_VERSION")
8+
);
9+
10+
let build_time = std::time::SystemTime::now()
11+
.duration_since(std::time::UNIX_EPOCH)
12+
.unwrap()
13+
.as_secs();
14+
let build_time_str = format!("{}", build_time);
15+
println!("cargo:rustc-env=BUILD_TIME={}", build_time_str);
16+
17+
let git_commit = get_git_commit_hash();
18+
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit);
19+
20+
let git_branch = get_git_branch();
21+
println!("cargo:rustc-env=GIT_BRANCH={}", git_branch);
22+
23+
let git_dirty = get_git_dirty();
24+
println!("cargo:rustc-env=GIT_DIRTY={}", git_dirty);
25+
26+
println!("cargo:rerun-if-changed=Cargo.toml");
27+
println!("cargo:rerun-if-changed=.git/HEAD");
28+
println!("cargo:rerun-if-changed=.git/index");
29+
}
30+
31+
fn get_git_commit_hash() -> String {
32+
Command::new("git")
33+
.args(["rev-parse", "--short", "HEAD"])
34+
.output()
35+
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string())
36+
.unwrap_or_else(|_| "unknown".to_string())
37+
}
38+
39+
fn get_git_branch() -> String {
40+
Command::new("git")
41+
.args(["rev-parse", "--abbrev-ref", "HEAD"])
42+
.output()
43+
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string())
44+
.unwrap_or_else(|_| "unknown".to_string())
45+
}
46+
47+
fn get_git_dirty() -> String {
48+
Command::new("git")
49+
.args(["diff", "--quiet", "--ignore-submodules"])
50+
.output()
51+
.map(|output| {
52+
if output.status.success() {
53+
"clean"
54+
} else {
55+
"dirty"
56+
}
57+
})
58+
.unwrap_or_else(|_| "unknown")
59+
.to_string()
60+
}

docs/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ sequenceDiagram
139139
RustPBX->>Client: Send Answer Event with SDP
140140
Client->>RustPBX: Set Remote Description
141141
142-
Note over Client,RustPBX: SIP/RTP Media Flow
143-
Client->>RustPBX: RTP Audio Packets (PCMA/PCMU/G722/Opus)
144-
RustPBX->>Client: RTP Audio Response
142+
Note over SIP UA,SIP Server: SIP/RTP Media Flow
143+
SIP UA->>SIP Server: RTP Audio Packets (PCMA/PCMU/G722/Opus)
144+
SIP Server->>SIP UA: RTP Audio Response
145145
146146
Client->>RustPBX: Send TTS/Play Commands
147147
RustPBX->>Client: Send Audio Events

src/bin/rustpbx.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
22
use clap::Parser;
33
use dotenv::dotenv;
4-
use rustpbx::{app::AppStateBuilder, config::Config};
4+
use rustpbx::{app::AppStateBuilder, config::Config, version};
55
use std::fs::File;
66
use tokio::select;
77
use tracing::{info, level_filters::LevelFilter};
@@ -10,8 +10,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilte
1010
#[derive(Parser, Debug)]
1111
#[command(
1212
author,
13-
version,
14-
about = "A versatile SIP PBX server implemented in Rust"
13+
version = version::get_short_version(),
14+
about = "A versatile SIP PBX server implemented in Rust",
15+
long_about = version::get_version_info()
1516
)]
1617
struct Cli {
1718
/// Path to the configuration file
@@ -34,6 +35,7 @@ async fn main() -> Result<()> {
3435
.map(|conf| Config::load(&conf).expect("Failed to load config"))
3536
.unwrap_or_default();
3637

38+
println!("{}", version::get_version_info());
3739
let mut env_filter = EnvFilter::from_default_env();
3840
if let Some(Ok(level)) = config
3941
.log_level

src/bin/text2wav.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use dotenv::dotenv;
44
use futures::StreamExt;
55
use hound::{SampleFormat, WavSpec, WavWriter};
66
use regex::Regex;
7-
use rustpbx::PcmBuf;
7+
use rustpbx::{version, PcmBuf};
88
use std::path::PathBuf;
99
use tracing::{debug, error, info};
1010

@@ -21,7 +21,12 @@ struct TextSegment {
2121

2222
/// Convert text to WAV audio files using text-to-speech
2323
#[derive(Parser, Debug)]
24-
#[command(author, version, about, long_about = None)]
24+
#[command(
25+
author,
26+
version = version::get_short_version(),
27+
about = "Convert text to WAV audio files using text-to-speech",
28+
long_about = version::get_version_info()
29+
)]
2530
struct Args {
2631
/// Input text to convert to speech
2732
///

src/bin/wav2text.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ use rustpbx::{
44
event::SessionEvent,
55
media::track::file::read_wav_file,
66
transcription::{TencentCloudAsrClientBuilder, TranscriptionClient, TranscriptionOption},
7+
version,
78
};
89
use std::{path::PathBuf, time::Duration};
910
use tracing::{debug, info};
1011

1112
/// Convert WAV audio files to text using speech recognition
1213
#[derive(Parser, Debug)]
13-
#[command(author, version, about, long_about = None)]
14+
#[command(
15+
author,
16+
version = version::get_short_version(),
17+
about = "Convert WAV audio files to text using speech recognition",
18+
long_about = version::get_version_info()
19+
)]
1420
struct Args {
1521
/// Path to input WAV file
1622
#[arg(value_name = "INPUT")]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod proxy;
1313
pub mod synthesis;
1414
pub mod transcription;
1515
pub mod useragent;
16+
pub mod version;
1617

1718
pub type TrackId = String;
1819
pub type Sample = i16;

src/version.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use chrono::{DateTime, Local};
2+
3+
pub fn get_version_info() -> &'static str {
4+
let version = env!("CARGO_PKG_VERSION");
5+
let build_time = env!("BUILD_TIME");
6+
let git_commit = env!("GIT_COMMIT_HASH");
7+
let git_branch = env!("GIT_BRANCH");
8+
let git_dirty = env!("GIT_DIRTY");
9+
10+
let build_timestamp: i64 = build_time.parse().unwrap_or(0);
11+
let build_datetime: DateTime<Local> = DateTime::from_timestamp(build_timestamp, 0)
12+
.map(|utc| utc.with_timezone(&Local))
13+
.unwrap_or_else(|| Local::now());
14+
let build_time_str = build_datetime.format("%Y-%m-%d %H:%M:%S %Z").to_string();
15+
16+
Box::leak(
17+
format!(
18+
"rustpbx {}\n\
19+
Build Time: {}\n\
20+
Git Commit: {}\n\
21+
Git Branch: {}\n\
22+
Git Status: {}",
23+
version, build_time_str, git_commit, git_branch, git_dirty
24+
)
25+
.into_boxed_str(),
26+
)
27+
}
28+
29+
pub fn get_short_version() -> &'static str {
30+
let version = env!("CARGO_PKG_VERSION");
31+
let git_commit = env!("GIT_COMMIT_HASH");
32+
let git_dirty = env!("GIT_DIRTY");
33+
34+
if git_dirty == "dirty" {
35+
Box::leak(format!("{}-{}-dirty", version, git_commit).into_boxed_str())
36+
} else {
37+
Box::leak(format!("{}-{}", version, git_commit).into_boxed_str())
38+
}
39+
}

0 commit comments

Comments
 (0)