дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
feat(run): read xcode schemes from disk (#6790)
Adds ide_run::xcode::schemes(root), which reads the schemes of the .xcodeproj/.xcworkspace bundles at a project root from xcshareddata/xcschemes, falling back to the current user's xcuserdata (a shared scheme shadows a personal one of the same name). Each scheme carries its container, whether it is shared, the launch and test build configurations, enabled launch arguments and environment variables, and the buildable references. Closes #6785 Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
1 джерело
511d3f21bb
коміт
5b904f7896
7 files changed
+260
No files matched your search
@@ -21,6 +21,7 @@ pub mod scope;
|
||||
pub mod tomcat;
|
||||
mod utf8;
|
||||
pub mod wrap;
|
||||
pub mod xcode;
|
||||
|
||||
pub use detect::cmake::{build_configs as cmake_build_configs, prepare as prepare_cmake};
|
||||
pub use process::{DetachedProcessKills, ProcessManager, StartOptions};
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
//! Xcode schemes read straight from the `.xcscheme` files of the `.xcodeproj` / `.xcworkspace`
|
||||
//! bundles at a project root. `xcodebuild -list` would also know the schemes Xcode autocreates for
|
||||
//! projects without any, but it takes a second or two, too slow for detection.
|
||||
|
||||
use quick_xml::{Reader, XmlVersion, events::Event};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct XcodeScheme {
|
||||
pub name: String,
|
||||
/// The `.xcodeproj` or `.xcworkspace` the scheme belongs to.
|
||||
pub container: PathBuf,
|
||||
/// False for a scheme only found under `xcuserdata/` (not shared with the team).
|
||||
pub shared: bool,
|
||||
pub launch_configuration: Option<String>,
|
||||
pub test_configuration: Option<String>,
|
||||
/// The `LaunchAction`'s enabled command-line arguments, each as Xcode stores it (one entry may
|
||||
/// hold several shell words).
|
||||
pub arguments: Vec<String>,
|
||||
/// The `LaunchAction`'s enabled environment variables.
|
||||
pub environment: Vec<(String, String)>,
|
||||
/// The `.xctestplan` files the `TestAction` references.
|
||||
pub test_plans: Vec<PathBuf>,
|
||||
pub default_test_plan: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// The schemes of every `.xcodeproj` / `.xcworkspace` directly in `root`, sorted by container
|
||||
/// and then by name. Unreadable or malformed scheme files are skipped.
|
||||
pub fn schemes(root: &Path) -> Vec<XcodeScheme> {
|
||||
let Ok(entries) = fs::read_dir(root) else { return Vec::new() };
|
||||
let mut containers: Vec<PathBuf> = entries
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_dir() && p.extension().is_some_and(|ext| ext == "xcodeproj" || ext == "xcworkspace"))
|
||||
.collect();
|
||||
containers.sort();
|
||||
containers.iter().flat_map(|c| container_schemes(c)).collect()
|
||||
}
|
||||
|
||||
fn container_schemes(container: &Path) -> Vec<XcodeScheme> {
|
||||
let mut out: Vec<XcodeScheme> = Vec::new();
|
||||
let mut add = |dir: PathBuf, shared: bool| {
|
||||
let Ok(entries) = fs::read_dir(dir) else { return };
|
||||
let mut files: Vec<PathBuf> =
|
||||
entries.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|e| e == "xcscheme")).collect();
|
||||
files.sort();
|
||||
for file in files {
|
||||
let Some(name) = file.file_stem().and_then(|s| s.to_str()) else { continue };
|
||||
// A shared scheme wins over a personal copy of the same name.
|
||||
if out.iter().any(|s| s.name == name) {
|
||||
continue;
|
||||
}
|
||||
let Some(xml) = fs::read_to_string(&file).ok() else { continue };
|
||||
if let Some(mut scheme) = parse_scheme(&xml, container) {
|
||||
scheme.name = name.to_owned();
|
||||
scheme.shared = shared;
|
||||
out.push(scheme);
|
||||
}
|
||||
}
|
||||
};
|
||||
add(container.join("xcshareddata/xcschemes"), true);
|
||||
if let Ok(users) = fs::read_dir(container.join("xcuserdata")) {
|
||||
let mut users: Vec<PathBuf> = users.flatten().map(|e| e.path()).collect();
|
||||
users.sort();
|
||||
for user in users {
|
||||
add(user.join("xcschemes"), false);
|
||||
}
|
||||
}
|
||||
out.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
out
|
||||
}
|
||||
|
||||
/// Parses one `.xcscheme` file. `name` and `shared` are left for the caller, which knows the file.
|
||||
pub fn parse_scheme(xml: &str, container: &Path) -> Option<XcodeScheme> {
|
||||
let mut scheme = XcodeScheme { container: container.to_owned(), ..Default::default() };
|
||||
let mut reader = Reader::from_str(xml);
|
||||
let mut stack: Vec<String> = Vec::new();
|
||||
let mut seen_root = false;
|
||||
loop {
|
||||
let event = reader.read_event().ok()?;
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
match event {
|
||||
Event::Start(tag) | Event::Empty(tag) => {
|
||||
let name = tag.local_name().as_ref().to_owned();
|
||||
if stack.is_empty() {
|
||||
if seen_root || name != "Scheme" {
|
||||
return None;
|
||||
}
|
||||
seen_root = true;
|
||||
}
|
||||
let attr = |key: &str| -> Option<String> {
|
||||
tag.attributes()
|
||||
.flatten()
|
||||
.find(|a| a.key.as_ref() == key)
|
||||
.and_then(|a| a.normalized_value(XmlVersion::Implicit1_0).ok().map(|v| v.into_owned()))
|
||||
};
|
||||
let enabled = || attr("isEnabled").as_deref() == Some("YES");
|
||||
let action = stack.get(1).map(String::as_str);
|
||||
match (action, name.as_str()) {
|
||||
(None, "LaunchAction") => scheme.launch_configuration = attr("buildConfiguration"),
|
||||
(None, "TestAction") => scheme.test_configuration = attr("buildConfiguration"),
|
||||
(Some("LaunchAction"), "CommandLineArgument") if enabled() => {
|
||||
scheme.arguments.extend(attr("argument"));
|
||||
}
|
||||
(Some("LaunchAction"), "EnvironmentVariable") if enabled() => {
|
||||
if let Some(key) = attr("key") {
|
||||
scheme.environment.push((key, attr("value").unwrap_or_default()));
|
||||
}
|
||||
}
|
||||
(Some("TestAction"), "TestPlanReference") => {
|
||||
if let Some(plan) = attr("reference").map(|r| resolve(&r, container)) {
|
||||
if attr("default").as_deref() == Some("YES") {
|
||||
scheme.default_test_plan = Some(plan.clone());
|
||||
}
|
||||
scheme.test_plans.push(plan);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if !empty {
|
||||
stack.push(name);
|
||||
}
|
||||
}
|
||||
Event::End(_) => {
|
||||
stack.pop()?;
|
||||
}
|
||||
Event::Eof => return (seen_root && stack.is_empty()).then_some(scheme),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves a scheme file reference such as `container:Tests/All.xctestplan`, which is relative
|
||||
/// to the folder holding the `.xcodeproj` / `.xcworkspace`.
|
||||
fn resolve(reference: &str, container: &Path) -> PathBuf {
|
||||
match reference.split_once(':') {
|
||||
Some(("absolute", path)) => PathBuf::from(path),
|
||||
Some((_, path)) => container.parent().unwrap_or(container).join(path),
|
||||
None => container.parent().unwrap_or(container).join(reference),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn fixtures() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/xcode")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_shared_and_personal_schemes_of_each_container() {
|
||||
let root = fixtures();
|
||||
let found = schemes(&root);
|
||||
let names: Vec<_> =
|
||||
found.iter().map(|s| (s.container.file_name().unwrap().to_str().unwrap(), &*s.name, s.shared)).collect();
|
||||
// `Broken.xcscheme` is skipped; the personal `App` copy loses to the shared one.
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
("App.xcodeproj", "App", true),
|
||||
("App.xcodeproj", "Scratch", false),
|
||||
("App.xcworkspace", "Everything", true)
|
||||
]
|
||||
);
|
||||
|
||||
let app = &found[0];
|
||||
assert_eq!(app.container, root.join("App.xcodeproj"));
|
||||
assert_eq!(app.launch_configuration.as_deref(), Some("Staging"));
|
||||
assert_eq!(app.test_configuration.as_deref(), Some("Debug"));
|
||||
// Disabled entries and the TestAction's own arguments are left out; entities are decoded.
|
||||
assert_eq!(app.arguments, ["-com.apple.CoreData.SQLDebug 1", "--name=\"A & B\""]);
|
||||
assert_eq!(app.environment, [("API_HOST".to_owned(), "localhost:8080".to_owned())]);
|
||||
assert_eq!(app.test_plans, [root.join("Tests/Unit.xctestplan"), root.join("Tests/All.xctestplan")]);
|
||||
assert_eq!(app.default_test_plan, Some(root.join("Tests/All.xctestplan")));
|
||||
|
||||
let scratch = &found[1];
|
||||
assert_eq!(scratch.launch_configuration.as_deref(), Some("Debug"));
|
||||
assert_eq!(scratch.test_configuration, None);
|
||||
assert!(scratch.arguments.is_empty() && scratch.test_plans.is_empty());
|
||||
|
||||
let everything = &found[2];
|
||||
assert_eq!(everything.default_test_plan, Some(PathBuf::from("/tmp/Shared.xctestplan")));
|
||||
assert_eq!(everything.launch_configuration, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_files_that_are_not_schemes() {
|
||||
let c = Path::new("A.xcodeproj");
|
||||
assert_eq!(parse_scheme("not xml <", c), None);
|
||||
assert_eq!(parse_scheme("<Workspace/>", c), None);
|
||||
assert_eq!(parse_scheme("<Scheme><LaunchAction>", c), None);
|
||||
assert!(parse_scheme("<Scheme/>", c).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_root_without_xcode_projects_has_no_schemes() {
|
||||
assert!(schemes(&fixtures().join("App.xcodeproj")).is_empty());
|
||||
assert!(schemes(Path::new("/nonexistent")).is_empty());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme LastUpgradeVersion = "1600" version = "1.7">
|
||||
<BuildAction parallelizeBuildables = "YES" buildImplicitDependencies = "YES">
|
||||
</BuildAction>
|
||||
<TestAction buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB">
|
||||
<TestPlans>
|
||||
<TestPlanReference reference = "container:Tests/Unit.xctestplan">
|
||||
</TestPlanReference>
|
||||
<TestPlanReference reference = "container:Tests/All.xctestplan" default = "YES">
|
||||
</TestPlanReference>
|
||||
</TestPlans>
|
||||
<CommandLineArguments>
|
||||
<CommandLineArgument argument = "-test-only" isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
</CommandLineArguments>
|
||||
</TestAction>
|
||||
<LaunchAction buildConfiguration = "Staging" launchStyle = "0">
|
||||
<BuildableProductRunnable runnableDebuggingMode = "0">
|
||||
<BuildableReference BuildableIdentifier = "primary" BuildableName = "App.app" BlueprintName = "App" ReferencedContainer = "container:App.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
<CommandLineArguments>
|
||||
<CommandLineArgument argument = "-com.apple.CoreData.SQLDebug 1" isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
<CommandLineArgument argument = "-disabled" isEnabled = "NO">
|
||||
</CommandLineArgument>
|
||||
<CommandLineArgument argument = "--name="A & B"" isEnabled = "YES">
|
||||
</CommandLineArgument>
|
||||
</CommandLineArguments>
|
||||
<EnvironmentVariables>
|
||||
<EnvironmentVariable key = "API_HOST" value = "localhost:8080" isEnabled = "YES">
|
||||
</EnvironmentVariable>
|
||||
<EnvironmentVariable key = "OFF" value = "1" isEnabled = "NO">
|
||||
</EnvironmentVariable>
|
||||
</EnvironmentVariables>
|
||||
</LaunchAction>
|
||||
<ArchiveAction buildConfiguration = "Release" revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
||||
Vendored
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme LastUpgradeVersion = "1600" version = "1.7">
|
||||
<LaunchAction buildConfiguration = "Debug"/>
|
||||
</Scheme>
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme LastUpgradeVersion = "1600" version = "1.7">
|
||||
<LaunchAction buildConfiguration = "Debug"/>
|
||||
</Scheme>
|
||||
@@ -0,0 +1 @@
|
||||
not xml <
|
||||
Vendored
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme LastUpgradeVersion = "1600" version = "1.7">
|
||||
<TestAction buildConfiguration = "Debug">
|
||||
<TestPlans>
|
||||
<TestPlanReference reference = "absolute:/tmp/Shared.xctestplan" default = "YES">
|
||||
</TestPlanReference>
|
||||
</TestPlans>
|
||||
</TestAction>
|
||||
</Scheme>
|
||||
Reference in new issue
Заблокувати користувача