diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-22 23:40:47 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-22 23:40:47 +0900 |
| commit | 99ef82a9807578c1e5749156a027949efaba75c4 (patch) | |
| tree | e5f6bfb4c74c1ced20a9bc1c884f811f582e38eb /crates/shirabe/src/json | |
| parent | ffd3e239a56172d2a336fb4d6253c01f6b1a0100 (diff) | |
| download | php-shirabe-99ef82a9807578c1e5749156a027949efaba75c4.tar.gz php-shirabe-99ef82a9807578c1e5749156a027949efaba75c4.tar.zst php-shirabe-99ef82a9807578c1e5749156a027949efaba75c4.zip | |
feat(json): resolve bundled Composer schema files via build.rs
JsonFile's schema paths previously relied on php_dir() (__DIR__), which has
no runtime equivalent. Add a build.rs that copies composer-schema.json and
composer-lock-schema.json next to the built executable, and resolve them with
std::env::current_exe(). FilesystemRepository now embeds InstalledVersions.php
directly via include_str! instead of reading it through php_dir().
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json')
| -rw-r--r-- | crates/shirabe/src/json/json_file.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index 4145385..0aada36 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -11,8 +11,8 @@ use shirabe_external_packages::seld::json_lint::ParsingException; use shirabe_php_shim::{ InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents, - file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, php_dir, realpath, - str_contains, str_ends_with, str_repeat, strlen, strpos, usleep, + file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, realpath, str_contains, + str_ends_with, str_repeat, strlen, strpos, usleep, }; use crate::downloader::TransportException; @@ -87,14 +87,24 @@ impl JsonFile { pub const INDENT_DEFAULT: &'static str = " "; - /// PHP: __DIR__ . '/../../../res/composer-schema.json' - pub fn composer_schema_path() -> String { - format!("{}/../../../res/composer-schema.json", php_dir()) + /// build.rs copies the Composer schema files into a res/ directory next to the + /// executable; this resolves that path via the running executable's location. + /// + /// TODO(phase-f): this on-disk layout is hard to distribute. Embed the schema with + /// include_str! and extract it to a temporary file at runtime instead. + pub fn composer_schema_path() -> std::path::PathBuf { + Self::schema_res_path("composer-schema.json") + } + + /// See composer_schema_path. + pub fn lock_schema_path() -> std::path::PathBuf { + Self::schema_res_path("composer-lock-schema.json") } - /// PHP: __DIR__ . '/../../../res/composer-lock-schema.json' - pub fn lock_schema_path() -> String { - format!("{}/../../../res/composer-lock-schema.json", php_dir()) + fn schema_res_path(filename: &str) -> std::path::PathBuf { + let exe = std::env::current_exe().expect("failed to resolve current executable path"); + let dir = exe.parent().expect("executable has no parent directory"); + dir.join("res").join(filename) } /// Initializes json file reader/parser. @@ -329,8 +339,8 @@ impl JsonFile { schema_file: Option<&str>, ) -> Result<bool> { let mut is_composer_schema_file = false; - let mut schema_file: String = match schema_file { - Some(f) => f.to_string(), + let mut schema_file = match schema_file { + Some(f) => f.into(), None => { if schema == Self::LOCK_SCHEMA { Self::lock_schema_path() @@ -340,6 +350,7 @@ impl JsonFile { } } }; + let mut schema_file = schema_file.to_string_lossy().into_owned(); // Prepend with file:// only when not using a special schema already (e.g. in the phar) if strpos(&schema_file, "://").is_none() { |
