123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- use std::env;
- use std::fs::File;
- use std::io::{self, Write};
- use std::path::PathBuf;
- use datetime::{LocalDateTime, ISO};
- fn main() -> io::Result<()> {
- #![allow(clippy::write_with_newline)]
- let usage = include_str!("src/usage.txt");
- let tagline = "dog \\1;32m●\\0m command-line DNS client";
- let url = "https://dns.lookup.dog/";
- let ver =
- if is_debug_build() {
- format!("{}\nv{} \\1;31m(pre-release debug build!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), url)
- }
- else if is_development_version() {
- format!("{}\nv{} [{}] built on {} \\1;31m(pre-release!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), git_hash(), build_date(), url)
- }
- else {
- format!("{}\nv{}\n\\1;4;34m{}\\0m", tagline, version_string(), url)
- };
-
- let out = PathBuf::from(env::var("OUT_DIR").unwrap());
-
- let mut f = File::create(&out.join("version.pretty.txt"))?;
- writeln!(f, "{}", convert_codes(&ver))?;
-
- let mut f = File::create(&out.join("version.bland.txt"))?;
- writeln!(f, "{}", strip_codes(&ver))?;
-
- let mut f = File::create(&out.join("usage.pretty.txt"))?;
- writeln!(f, "{}", convert_codes(&tagline))?;
- writeln!(f)?;
- write!(f, "{}", convert_codes(&usage))?;
-
- let mut f = File::create(&out.join("usage.bland.txt"))?;
- writeln!(f, "{}", strip_codes(&tagline))?;
- writeln!(f)?;
- write!(f, "{}", strip_codes(&usage))?;
- Ok(())
- }
- fn convert_codes(input: &str) -> String {
- input.replace("\\", "\x1B[")
- }
- fn strip_codes(input: &str) -> String {
- input.replace("\\0m", "")
- .replace("\\1m", "")
- .replace("\\4m", "")
- .replace("\\32m", "")
- .replace("\\33m", "")
- .replace("\\1;31m", "")
- .replace("\\1;32m", "")
- .replace("\\1;33m", "")
- .replace("\\1;4;34", "")
- }
- fn git_hash() -> String {
- use std::process::Command;
- String::from_utf8_lossy(
- &Command::new("git")
- .args(&["rev-parse", "--short", "HEAD"])
- .output().unwrap()
- .stdout).trim().to_string()
- }
- fn is_development_version() -> bool {
- cargo_version().ends_with("-pre") || env::var("PROFILE").unwrap() == "debug"
- }
- fn is_debug_build() -> bool {
- env::var("PROFILE").unwrap() == "debug"
- }
- fn cargo_version() -> String {
- env::var("CARGO_PKG_VERSION").unwrap()
- }
- fn version_string() -> String {
- let mut ver = cargo_version();
- let feats = nonstandard_features_string();
- if ! feats.is_empty() {
- ver.push_str(&format!(" [{}]", &feats));
- }
- ver
- }
- fn feature_enabled(name: &str) -> bool {
- env::var(&format!("CARGO_FEATURE_{}", name))
- .map(|e| ! e.is_empty())
- .unwrap_or(false)
- }
- fn nonstandard_features_string() -> String {
- let mut s = Vec::new();
- if ! feature_enabled("WITH_IDNA") {
- s.push("-idna");
- }
- if ! feature_enabled("WITH_TLS") {
- s.push("-tls");
- }
- if ! feature_enabled("WITH_HTTPS") {
- s.push("-https");
- }
- s.join(", ")
- }
- fn build_date() -> String {
- let now = LocalDateTime::now();
- format!("{}", now.date().iso())
- }
|