diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-08 03:52:33 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-08 10:38:35 +0900 |
| commit | 0209f63210e5b547b5c6b73367bb80ea86c255ec (patch) | |
| tree | 6ce0f50f343fc51da0db413278093d63d0a9fe70 /crates/shirabe/build.rs | |
| parent | b4f16a379e919eefc2cb37bcddee589c0f26eaad (diff) | |
| download | php-shirabe-0209f63210e5b547b5c6b73367bb80ea86c255ec.tar.gz php-shirabe-0209f63210e5b547b5c6b73367bb80ea86c255ec.tar.zst php-shirabe-0209f63210e5b547b5c6b73367bb80ea86c255ec.zip | |
feat(composer): bake the dev build warning deadline in at build time
Composer defines COMPOSER_DEV_WARNING_TIME from its phar stub when the
compiled version is a commit hash rather than a tag, so the value exists
only in the build artifact. It was a todo!() in the PHP shim, leaving the
warning branch in Application::do_run unreachable.
A build script now derives it the way Compiler does, from git describe
and the HEAD commit date, and composer::COMPOSER_DEV_WARNING_TIME holds
the result as a Rust constant instead of a runtime-defined one.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/build.rs')
| -rw-r--r-- | crates/shirabe/build.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/crates/shirabe/build.rs b/crates/shirabe/build.rs new file mode 100644 index 00000000..22c8be58 --- /dev/null +++ b/crates/shirabe/build.rs @@ -0,0 +1,55 @@ +//! ref: composer/src/Composer/Compiler.php +//! +//! Generates a constant value of `composer::COMPOSER_DEV_WARNING_TIME`. + +fn git(repo_root: &std::path::Path, args: &[&str]) -> Option<String> { + let output = std::process::Command::new("git") + .args(args) + .current_dir(repo_root) + .output() + .ok()?; + if !output.status.success() { + return None; + } + Some(String::from_utf8(output.stdout).ok()?.trim().to_string()) +} + +fn main() { + println!("cargo::rerun-if-changed=build.rs"); + + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let repo_root = std::path::Path::new(&manifest_dir) + .parent() + .unwrap() + .parent() + .unwrap(); + + if let Some(git_dir) = git(repo_root, &["rev-parse", "--git-dir"]) { + let git_dir = repo_root.join(git_dir); + for path in ["HEAD", "packed-refs", "refs/tags"] { + let path = git_dir.join(path); + if path.exists() { + println!("cargo::rerun-if-changed={}", path.display()); + } + } + } + + let dev_warning_time = + if git(repo_root, &["describe", "--tags", "--exact-match", "HEAD"]).is_some() { + None + } else { + git(repo_root, &["log", "-n1", "--pretty=%ct", "HEAD"]) + .and_then(|date| date.parse::<i64>().ok()) + .map(|date| date + 60 * 86400) + }; + + let out_dir = std::env::var("OUT_DIR").unwrap(); + std::fs::write( + std::path::Path::new(&out_dir).join("dev_warning_time.rs"), + match dev_warning_time { + Some(time) => format!("Some({time})"), + None => "None".to_string(), + }, + ) + .unwrap(); +} |
