cargo_zng/res/
built_in.rs1use std::{
4 env, fs,
5 io::{self, BufRead as _},
6 path::{Path, PathBuf},
7};
8
9use crate::util;
10
11pub const ZR_WORKSPACE_DIR: &str = "ZR_WORKSPACE_DIR";
15pub const ZR_SOURCE_DIR: &str = "ZR_SOURCE_DIR";
17pub const ZR_TARGET_DIR: &str = "ZR_TARGET_DIR";
22pub const ZR_CACHE_DIR: &str = "ZR_CACHE_DIR";
26
27pub const ZR_REQUEST: &str = "ZR_REQUEST";
29pub const ZR_REQUEST_DD: &str = "ZR_REQUEST_DD";
31pub const ZR_TARGET: &str = "ZR_TARGET";
35pub const ZR_TARGET_DD: &str = "ZR_TARGET_DD";
37
38pub const ZR_HELP: &str = "ZR_HELP";
40
41pub const ZR_VERBOSE: &str = "ZR_VERBOSE";
46
47pub const ZR_APP_ID: &str = "ZR_APP_ID";
49pub const ZR_APP: &str = "ZR_APP";
51pub const ZR_ORG: &str = "ZR_ORG";
53pub const ZR_VERSION: &str = "ZR_VERSION";
55pub const ZR_DESCRIPTION: &str = "ZR_DESCRIPTION";
57pub const ZR_HOMEPAGE: &str = "ZR_HOMEPAGE";
59pub const ZR_LICENSE: &str = "ZR_LICENSE";
61pub const ZR_PKG_NAME: &str = "ZR_PKG_NAME";
63pub const ZR_PKG_AUTHORS: &str = "ZR_PKG_AUTHORS";
65pub const ZR_CRATE_NAME: &str = "ZR_CRATE_NAME";
67pub const ZR_QUALIFIER: &str = "ZR_QUALIFIER";
69
70pub fn help(help: &str) {
72 if env::var(ZR_HELP).is_ok() {
73 println!("{help}");
74 std::process::exit(0);
75 };
76}
77
78pub fn path(var: &str) -> PathBuf {
80 env::var(var).unwrap_or_else(|_| panic!("missing {var}")).into()
81}
82
83pub fn display_path(p: &Path) -> String {
85 let base = path(ZR_WORKSPACE_DIR);
86 let r = if let Ok(local) = p.strip_prefix(base) {
87 local.display().to_string()
88 } else {
89 p.display().to_string()
90 };
91
92 #[cfg(windows)]
93 return r.replace('\\', "/");
94
95 #[cfg(not(windows))]
96 r
97}
98
99fn read_line(path: &Path, expected: &str) -> io::Result<String> {
100 match read_lines(path).next() {
101 Some(r) => r.map(|(_, l)| l),
102 None => Err(io::Error::new(
103 io::ErrorKind::InvalidInput,
104 format!("expected {expected} in tool file content"),
105 )),
106 }
107}
108
109fn read_lines(path: &Path) -> impl Iterator<Item = io::Result<(usize, String)>> {
110 enum State {
111 Open(io::Result<fs::File>),
112 Lines(usize, io::Lines<io::BufReader<fs::File>>),
113 End,
114 }
115 let mut state = State::Open(fs::File::open(path));
117 std::iter::from_fn(move || {
118 loop {
119 match std::mem::replace(&mut state, State::End) {
120 State::Lines(count, mut lines) => {
121 if let Some(l) = lines.next() {
122 match l {
123 Ok(l) => {
125 state = State::Lines(count + 1, lines);
126 let test = l.trim();
127 if !test.is_empty() && !test.starts_with('#') {
128 return Some(Ok((count, l)));
129 }
130 }
131 Err(e) => {
133 return Some(Err(e));
134 }
135 }
136 }
137 }
138 State::Open(r) => match r {
139 Ok(f) => state = State::Lines(1, io::BufReader::new(f).lines()),
141 Err(e) => return Some(Err(e)),
143 },
144 State::End => return None,
146 }
147 }
148 })
149}
150
151fn read_path(request_file: &Path) -> io::Result<PathBuf> {
152 read_line(request_file, "path").map(PathBuf::from)
153}
154
155pub(crate) fn symlink_warn(path: &Path) {
156 warn!("symlink ignored in `{}`, use cargo res tools to 'link'", path.display());
157}
158
159pub const ENV_TOOL: &str = "ZNG_RES_TOOL";
160
161macro_rules! built_in {
162 ($($tool:tt),+ $(,)?) => {
163 $(
164 mod $tool;
165 use $tool::$tool;
166 )+
167
168 pub static BUILT_INS: &[&str] = &[
169 $(stringify!($tool),)+
170 ];
171 static BUILT_IN_FNS: &[fn()] = &[
172 $($tool,)+
173 ];
174 };
175}
176built_in! { apk, copy, fail, glob, l10n, rp, sfx, sh, tar, warn, z }
181
182pub(crate) use l10n::release_langs;
183pub(crate) use sh::sh_run;
184
185pub fn run() {
186 if let Ok(tool) = env::var(ENV_TOOL) {
187 unsafe {
191 env::remove_var(ENV_TOOL);
192 }
193
194 if let Some(i) = BUILT_INS.iter().position(|n| *n == tool.as_str()) {
195 (BUILT_IN_FNS[i])();
196 std::process::exit(0);
197 } else {
198 fatal!("`tool` is not a built-in tool");
199 }
200 }
201}