дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
File > New Project could only create SwiftPM packages. A new Xcode Project entry (Swift group; also found by iOS, macOS, App, SwiftUI or Xcode) opens a chooser modelled on Xcode's template sheet: platform tabs (Multiplatform, iOS, macOS), a filter and template tiles under Application and Framework & Library, then an options page (Product Name, Team, Organization Identifier, read-only Bundle Identifier preview, Interface, Language, Testing System). The App and Framework templates are generator definitions that write a minimal Xcode 16 project with synchronized folders and a shared scheme, so no XcodeGen or Tuist is needed and the existing scheme, simulator, run and test support takes over when it opens. The entry shows the installed Xcode's own icon, rendered at runtime, or a bundled glyph without Xcode.
Scaffold gains {{#if field=value}} blocks, c99 / bundle filters, generator platforms / section / name-pattern and readonly / remember fields. Template tree tests skip the generator's tool requirements so they run without Xcode.
Closes #7393
Co-authored-by: wildcatDownstairs <[email protected]>
124 рядки
4.7 KiB
Rust
124 рядки
4.7 KiB
Rust
//! The installed Xcode's app icon, for the New Project wizard's Xcode Project entry. It is read
|
|
//! from the user's own Xcode at runtime (`NSWorkspace.iconForFile`), so no Apple artwork ships;
|
|
//! without Xcode, or off macOS, the wizard draws the bundled Simple Icons glyph instead.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::OnceLock;
|
|
|
|
/// Pixel size of the rendered icon: 32 px drawn at 16 pt in the list, sharp on Retina screens.
|
|
#[cfg(target_os = "macos")]
|
|
const PIXELS: isize = 64;
|
|
|
|
/// The `.app` bundle a developer directory belongs to (`/Applications/Xcode.app` for
|
|
/// `/Applications/Xcode.app/Contents/Developer`); `None` for the Command Line Tools.
|
|
pub fn app_of(developer_dir: &Path) -> Option<PathBuf> {
|
|
developer_dir.ancestors().find(|p| p.extension().is_some_and(|e| e == "app")).map(Path::to_path_buf)
|
|
}
|
|
|
|
/// The active Xcode: the one `DEVELOPER_DIR` or `xcode-select -p` names, else
|
|
/// `/Applications/Xcode.app` when it exists.
|
|
fn active_xcode() -> Option<PathBuf> {
|
|
let developer_dir = ide_core::process::host_var_os("DEVELOPER_DIR").map(PathBuf::from).or_else(|| {
|
|
let out = std::process::Command::new("xcode-select").arg("-p").output().ok()?;
|
|
out.status.success().then(|| PathBuf::from(String::from_utf8_lossy(&out.stdout).trim()))
|
|
});
|
|
developer_dir
|
|
.as_deref()
|
|
.and_then(app_of)
|
|
.or_else(|| Some(PathBuf::from("/Applications/Xcode.app")))
|
|
.filter(|app| app.is_dir())
|
|
}
|
|
|
|
/// The active Xcode's icon as a PNG `data:` URL, read once per run; `None` without Xcode, off
|
|
/// macOS, or with `OPENIDEA_NO_XCODE_ICON` set (to see the fallback).
|
|
pub fn data_url() -> Option<String> {
|
|
static ICON: OnceLock<Option<String>> = OnceLock::new();
|
|
ICON.get_or_init(|| {
|
|
if std::env::var_os("OPENIDEA_NO_XCODE_ICON").is_some() {
|
|
return None;
|
|
}
|
|
let png = render(&active_xcode()?)?;
|
|
use base64::Engine as _;
|
|
Some(format!("data:image/png;base64,{}", base64::engine::general_purpose::STANDARD.encode(png)))
|
|
})
|
|
.clone()
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn render(app: &Path) -> Option<Vec<u8>> {
|
|
use objc2::AnyThread;
|
|
use objc2_app_kit::{
|
|
NSBitmapImageFileType, NSBitmapImageRep, NSCompositingOperation, NSDeviceRGBColorSpace, NSGraphicsContext,
|
|
NSWorkspace,
|
|
};
|
|
use objc2_foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSString};
|
|
|
|
let icon = NSWorkspace::sharedWorkspace().iconForFile(&NSString::from_str(&app.to_string_lossy()));
|
|
let rep = unsafe {
|
|
NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(
|
|
NSBitmapImageRep::alloc(),
|
|
std::ptr::null_mut(),
|
|
PIXELS,
|
|
PIXELS,
|
|
8,
|
|
4,
|
|
true,
|
|
false,
|
|
NSDeviceRGBColorSpace,
|
|
0,
|
|
0,
|
|
)
|
|
}?;
|
|
let context = NSGraphicsContext::graphicsContextWithBitmapImageRep(&rep)?;
|
|
NSGraphicsContext::saveGraphicsState_class();
|
|
NSGraphicsContext::setCurrentContext(Some(&context));
|
|
let size = PIXELS as f64;
|
|
icon.drawInRect_fromRect_operation_fraction(
|
|
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(size, size)),
|
|
NSRect::ZERO,
|
|
NSCompositingOperation::Copy,
|
|
1.0,
|
|
);
|
|
context.flushGraphics();
|
|
NSGraphicsContext::restoreGraphicsState_class();
|
|
let png = unsafe { rep.representationUsingType_properties(NSBitmapImageFileType::PNG, &NSDictionary::new()) }?;
|
|
Some(png.to_vec())
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
fn render(_app: &Path) -> Option<Vec<u8>> {
|
|
None
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn finds_the_app_of_a_developer_dir() {
|
|
assert_eq!(
|
|
app_of(Path::new("/Applications/Xcode-27.1.app/Contents/Developer")),
|
|
Some(PathBuf::from("/Applications/Xcode-27.1.app"))
|
|
);
|
|
// The Command Line Tools have no app: the wizard falls back to /Applications/Xcode.app, then the glyph.
|
|
assert_eq!(app_of(Path::new("/Library/Developer/CommandLineTools")), None);
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
#[test]
|
|
fn renders_an_app_icon_as_png() {
|
|
// Any app's icon goes through the same path as Xcode's.
|
|
let png = render(Path::new("/System/Applications/Calculator.app")).unwrap();
|
|
assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"));
|
|
// IHDR: width and height.
|
|
assert_eq!(&png[16..24], &[0, 0, 0, 64, 0, 0, 0, 64]);
|
|
}
|
|
|
|
#[test]
|
|
fn no_icon_when_switched_off() {
|
|
// SAFETY: no other test of this binary reads the variable.
|
|
unsafe { std::env::set_var("OPENIDEA_NO_XCODE_ICON", "1") };
|
|
assert_eq!(data_url(), None);
|
|
}
|
|
}
|