Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace version functions by constants #595

Merged
merged 5 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn initial_state() -> State {
fn initial_device_state(config: ConnectConfig) -> DeviceState {
{
let mut msg = DeviceState::new();
msg.set_sw_version(version::version_string());
msg.set_sw_version(version::VERSION_STRING.to_string());
msg.set_is_active(false);
msg.set_can_play(true);
msg.set_volume(0);
Expand Down
3 changes: 2 additions & 1 deletion core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ fn main() {
.map(|()| rng.sample(Alphanumeric))
.take(8)
.collect();
println!("cargo:rustc-env=VERGEN_BUILD_ID={}", build_id);

println!("cargo:rustc-env=LIBRESPOT_BUILD_ID={}", build_id);
}
2 changes: 1 addition & 1 deletion core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Default for SessionConfig {
fn default() -> SessionConfig {
let device_id = Uuid::new_v4().to_hyphenated().to_string();
SessionConfig {
user_agent: version::version_string(),
user_agent: version::VERSION_STRING.to_string(),
device_id: device_id,
proxy: None,
ap_port: None,
Expand Down
6 changes: 3 additions & 3 deletions core/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ pub fn authenticate(
.mut_system_info()
.set_system_information_string(format!(
"librespot_{}_{}",
version::short_sha(),
version::build_id()
version::SHA_SHORT,
version::BUILD_ID
));
packet.mut_system_info().set_device_id(device_id);
packet.set_version_string(version::version_string());
packet.set_version_string(version::VERSION_STRING.to_string());

let cmd = 0xab;
let data = packet.write_to_bytes().unwrap();
Expand Down
51 changes: 12 additions & 39 deletions core/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
pub fn version_string() -> String {
format!("librespot-{}", short_sha())
}
/// Version string of the form "librespot-<sha>"
pub const VERSION_STRING: &str = concat!("librespot-", env!("VERGEN_SHA_SHORT"));

// Generate a timestamp representing now (UTC) in RFC3339 format.
pub fn now() -> &'static str {
env!("VERGEN_BUILD_TIMESTAMP")
}
/// Generate a timestamp string representing the build date (UTC).
pub const BUILD_DATE: &str = env!("VERGEN_BUILD_DATE");

// Generate a timstamp string representing now (UTC).
pub fn short_now() -> &'static str {
env!("VERGEN_BUILD_DATE")
}
/// Short sha of the latest git commit.
pub const SHA_SHORT: &str = env!("VERGEN_SHA_SHORT");

// Generate a SHA string
pub fn sha() -> &'static str {
env!("VERGEN_SHA")
}
/// Date of the latest git commit.
pub const COMMIT_DATE: &str = env!("VERGEN_COMMIT_DATE");

// Generate a short SHA string
pub fn short_sha() -> &'static str {
env!("VERGEN_SHA_SHORT")
}
/// Librespot crate version.
pub const SEMVER: &str = env!("CARGO_PKG_VERSION");

// Generate the commit date string
pub fn commit_date() -> &'static str {
env!("VERGEN_COMMIT_DATE")
}

// Generate the target triple string
pub fn target() -> &'static str {
env!("VERGEN_TARGET_TRIPLE")
}

// Generate a semver string
pub fn semver() -> &'static str {
// env!("VERGEN_SEMVER")
env!("CARGO_PKG_VERSION")
}

// Generate a random build id.
pub fn build_id() -> &'static str {
env!("VERGEN_BUILD_ID")
}
/// A random build id.
pub const BUILD_ID: &str = env!("LIBRESPOT_BUILD_ID");
32 changes: 24 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ fn list_backends() {
}
}

fn print_version() {
println!(
"librespot {semver} {sha} (Built on {build_date}, Build ID: {build_id})",
semver = version::SEMVER,
sha = version::SHA_SHORT,
build_date = version::BUILD_DATE,
build_id = version::BUILD_ID
);
}

#[derive(Clone)]
struct Setup {
backend: fn(Option<String>) -> Box<dyn Sink>,
Expand Down Expand Up @@ -103,7 +113,7 @@ fn setup(args: &[String]) -> Setup {
"Path to a directory where system files (credentials, volume) will be cached. Can be different from cache option value",
"SYTEMCACHE",
).optflag("", "disable-audio-cache", "Disable caching of the audio data.")
.reqopt("n", "name", "Device name", "NAME")
.optopt("n", "name", "Device name", "NAME")
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
.optopt(
"b",
Expand All @@ -119,6 +129,7 @@ fn setup(args: &[String]) -> Setup {
)
.optflag("", "emit-sink-events", "Run program set by --onevent before sink is opened and after it is closed.")
.optflag("v", "verbose", "Enable verbose output")
.optflag("V", "version", "Display librespot version string")
.optopt("u", "username", "Username to sign in with", "USERNAME")
.optopt("p", "password", "Password", "PASSWORD")
.optopt("", "proxy", "HTTP proxy to use when connecting", "PROXY")
Expand Down Expand Up @@ -220,15 +231,20 @@ fn setup(args: &[String]) -> Setup {
}
};

if matches.opt_present("version") {
print_version();
exit(0);
}

let verbose = matches.opt_present("verbose");
setup_logging(verbose);

info!(
"librespot {} ({}). Built on {}. Build ID: {}",
version::short_sha(),
version::commit_date(),
version::short_now(),
version::build_id()
"librespot {semver} {sha} (Built on {build_date}, Build ID: {build_id})",
semver = version::SEMVER,
sha = version::SHA_SHORT,
build_date = version::BUILD_DATE,
build_id = version::BUILD_ID
);

let backend_name = matches.opt_str("backend");
Expand Down Expand Up @@ -306,7 +322,7 @@ fn setup(args: &[String]) -> Setup {
.map(|port| port.parse::<u16>().unwrap())
.unwrap_or(0);

let name = matches.opt_str("name").unwrap();
let name = matches.opt_str("name").unwrap_or("Librespot".to_string());

let credentials = {
let cached_credentials = cache.as_ref().and_then(Cache::credentials);
Expand All @@ -329,7 +345,7 @@ fn setup(args: &[String]) -> Setup {
let device_id = device_id(&name);

SessionConfig {
user_agent: version::version_string(),
user_agent: version::VERSION_STRING.to_string(),
device_id: device_id,
proxy: matches.opt_str("proxy").or(std::env::var("http_proxy").ok()).map(
|s| {
Expand Down