use std::{
collections::BTreeSet,
env,
ffi::OsString,
fmt::Write as _,
fs,
path::{Path, PathBuf},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Service {
Camera,
PhotoLibrary,
PhotoLibraryAdd,
Microphone,
Location,
Notifications,
Background,
Media,
Billing,
Overlay,
Haptics,
Update,
Network,
}
impl Service {
pub const fn name(self) -> &'static str {
match self {
Service::Camera => "camera",
Service::PhotoLibrary => "photo-library",
Service::PhotoLibraryAdd => "photo-library-add",
Service::Microphone => "microphone",
Service::Location => "location",
Service::Notifications => "notifications",
Service::Background => "background",
Service::Media => "media",
Service::Billing => "billing",
Service::Overlay => "overlay",
Service::Haptics => "haptics",
Service::Update => "update",
Service::Network => "network",
}
}
pub const fn android_permissions(self) -> &'static [&'static str] {
match self {
Service::Camera => &["android.permission.CAMERA"],
Service::PhotoLibrary => &[],
Service::PhotoLibraryAdd => &[],
Service::Microphone => &["android.permission.RECORD_AUDIO"],
Service::Location => &["android.permission.ACCESS_COARSE_LOCATION"],
Service::Notifications => &["android.permission.POST_NOTIFICATIONS"],
Service::Background => &[
"android.permission.FOREGROUND_SERVICE",
"android.permission.FOREGROUND_SERVICE_DATA_SYNC",
],
Service::Media => &[
"android.permission.FOREGROUND_SERVICE",
"android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK",
],
Service::Billing => &["com.android.vending.BILLING"],
Service::Overlay => &["android.permission.SYSTEM_ALERT_WINDOW"],
Service::Haptics => &["android.permission.VIBRATE"],
Service::Update => &["android.permission.REQUEST_INSTALL_PACKAGES"],
Service::Network => &[
"android.permission.INTERNET",
"android.permission.ACCESS_NETWORK_STATE",
],
}
}
pub const fn apple_key(self) -> Option<&'static str> {
match self {
Service::Camera => Some("NSCameraUsageDescription"),
Service::PhotoLibrary => Some("NSPhotoLibraryUsageDescription"),
Service::PhotoLibraryAdd => Some("NSPhotoLibraryAddUsageDescription"),
Service::Microphone => Some("NSMicrophoneUsageDescription"),
Service::Location => Some("NSLocationWhenInUseUsageDescription"),
_ => None,
}
}
pub const fn takes_reason(self) -> bool {
self.apple_key().is_some()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Use {
service: Service,
reason: Option<&'static str>,
}
impl Use {
pub const fn camera(reason: &'static str) -> Self {
Self {
service: Service::Camera,
reason: Some(reason),
}
}
pub const fn photo_library(reason: &'static str) -> Self {
Self {
service: Service::PhotoLibrary,
reason: Some(reason),
}
}
pub const fn photo_library_add(reason: &'static str) -> Self {
Self {
service: Service::PhotoLibraryAdd,
reason: Some(reason),
}
}
pub const fn microphone(reason: &'static str) -> Self {
Self {
service: Service::Microphone,
reason: Some(reason),
}
}
pub const fn location(reason: &'static str) -> Self {
Self {
service: Service::Location,
reason: Some(reason),
}
}
pub const fn notifications() -> Self {
Self {
service: Service::Notifications,
reason: None,
}
}
pub const fn background() -> Self {
Self {
service: Service::Background,
reason: None,
}
}
pub const fn media() -> Self {
Self {
service: Service::Media,
reason: None,
}
}
pub const fn billing() -> Self {
Self {
service: Service::Billing,
reason: None,
}
}
pub const fn overlay() -> Self {
Self {
service: Service::Overlay,
reason: None,
}
}
pub const fn haptics() -> Self {
Self {
service: Service::Haptics,
reason: None,
}
}
pub const fn update() -> Self {
Self {
service: Service::Update,
reason: None,
}
}
pub const fn network() -> Self {
Self {
service: Service::Network,
reason: None,
}
}
pub const fn service(self) -> Service {
self.service
}
pub const fn reason(self) -> Option<&'static str> {
self.reason
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Demand {
Camera,
Microphone,
Watch,
Telephony,
Bluetooth,
Nfc,
Wifi,
Location,
}
impl Demand {
pub const fn android_feature(self) -> &'static str {
match self {
Demand::Camera => "android.hardware.camera",
Demand::Microphone => "android.hardware.microphone",
Demand::Watch => "android.hardware.type.watch",
Demand::Telephony => "android.hardware.telephony",
Demand::Bluetooth => "android.hardware.bluetooth",
Demand::Nfc => "android.hardware.nfc",
Demand::Wifi => "android.hardware.wifi",
Demand::Location => "android.hardware.location",
}
}
pub const fn name(self) -> &'static str {
self.android_feature()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Capabilities<'a> {
pub uses: &'a [Use],
pub demands: &'a [Demand],
pub opens: &'a [&'a str],
}
impl Capabilities<'_> {
pub const NONE: Capabilities<'static> = Capabilities {
uses: &[],
demands: &[],
opens: &[],
};
pub fn has(&self, service: Service) -> bool {
self.uses.iter().any(|entry| entry.service == service)
}
pub fn reason_for(&self, service: Service) -> Option<&'static str> {
self.uses
.iter()
.find(|entry| entry.service == service)
.and_then(|entry| entry.reason())
}
}
#[must_use = "a declaration reaches the platform builds only through emit()"]
#[derive(Clone, Copy, Debug)]
pub struct Declaration<'a> {
capabilities: Capabilities<'a>,
}
pub const fn declare(uses: &[Use]) -> Declaration<'_> {
Declaration {
capabilities: Capabilities {
uses,
demands: &[],
opens: &[],
},
}
}
impl<'a> Declaration<'a> {
pub const fn demanding(self, demands: &'a [Demand]) -> Declaration<'a> {
Declaration {
capabilities: Capabilities {
demands,
..self.capabilities
},
}
}
pub const fn opening(self, opens: &'a [&'a str]) -> Declaration<'a> {
Declaration {
capabilities: Capabilities {
opens,
..self.capabilities
},
}
}
pub fn emit(self) {
let out = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set for a build script"));
let package = env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME is set for a build script");
write_file(
&out.join("cranpose_capabilities.rs"),
&rust_source(&self.capabilities),
);
let crate_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set for a build script"),
);
let shared = shared_dir(&crate_dir, env::var_os(CAPABILITIES_DIR));
fs::create_dir_all(&shared).expect("the shared capabilities directory");
let outputs = shared_outputs(&shared, &package);
let contents = [
json(&self.capabilities),
android_manifest(&self.capabilities),
apple_usage(&self.capabilities),
];
for (path, text) in outputs.iter().zip(contents.iter()) {
write_file(path, text);
}
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-env-changed={CAPABILITIES_DIR}");
for directive in rerun_directives(&outputs) {
println!("{directive}");
}
}
}
fn shared_outputs(shared: &Path, package: &str) -> [PathBuf; 3] {
[
shared.join(format!("{package}-capabilities.json")),
shared.join(format!("{package}-permissions.xml")),
shared.join(format!("{package}-usage.plist")),
]
}
fn rerun_directives(outputs: &[PathBuf]) -> Vec<String> {
outputs
.iter()
.map(|path| format!("cargo::rerun-if-changed={}", path.display()))
.collect()
}
fn write_file(path: &Path, text: &str) {
fs::write(path, text).unwrap_or_else(|error| panic!("writing {}: {error}", path.display()));
}
const CAPABILITIES_DIR: &str = "CRANPOSE_CAPABILITIES_DIR";
fn shared_dir(crate_dir: &Path, named: Option<OsString>) -> PathBuf {
match named.filter(|value| !value.is_empty()) {
Some(value) => PathBuf::from(value),
None => workspace_root(crate_dir).join("target").join("cranpose"),
}
}
fn workspace_root(crate_dir: &Path) -> PathBuf {
found_workspace(crate_dir, &|path| {
fs::read_to_string(path.join("Cargo.toml"))
.is_ok_and(|manifest| manifest.contains("[workspace]"))
})
}
fn found_workspace(crate_dir: &Path, holds_workspace: &dyn Fn(&Path) -> bool) -> PathBuf {
crate_dir
.ancestors()
.filter(|path| holds_workspace(path))
.last()
.map_or_else(|| crate_dir.to_path_buf(), Path::to_path_buf)
}
fn rust_source(capabilities: &Capabilities<'_>) -> String {
let mut text = String::from(
"pub const CAPABILITIES: cranpose_capabilities::Capabilities =\n \
cranpose_capabilities::Capabilities {\n uses: &[\n",
);
for entry in capabilities.uses {
let call = constructor(entry);
let _ = writeln!(text, " cranpose_capabilities::Use::{call},");
}
text.push_str(" ],\n demands: &[\n");
for demand in capabilities.demands {
let _ = writeln!(
text,
" cranpose_capabilities::Demand::{demand:?},"
);
}
text.push_str(" ],\n opens: &[\n");
for media_type in capabilities.opens {
let _ = writeln!(text, " \"{}\",", escape(media_type));
}
text.push_str(" ],\n };\n");
text
}
fn constructor(entry: &Use) -> String {
let name = entry.service.name().replace('-', "_");
match entry.reason {
Some(reason) => format!("{name}(\"{}\")", escape(reason)),
None => format!("{name}()"),
}
}
fn escape(text: &str) -> String {
text.replace('\\', "\\\\").replace('"', "\\\"")
}
pub fn json(capabilities: &Capabilities<'_>) -> String {
let mut text = String::from("{\n \"services\": [\n");
for (at, entry) in capabilities.uses.iter().enumerate() {
let comma = if at + 1 == capabilities.uses.len() {
""
} else {
","
};
let reason = match entry.reason {
Some(reason) => format!("\"{}\"", escape_json(reason)),
None => String::from("null"),
};
let _ = writeln!(
text,
" {{ \"name\": \"{}\", \"reason\": {reason} }}{comma}",
entry.service.name()
);
}
text.push_str(" ],\n \"permissions\": [\n");
let permissions = android_permissions(capabilities);
for (at, permission) in permissions.iter().enumerate() {
let comma = if at + 1 == permissions.len() { "" } else { "," };
let _ = writeln!(text, " \"{permission}\"{comma}");
}
text.push_str(" ],\n \"demands\": [\n");
for (at, demand) in capabilities.demands.iter().enumerate() {
let comma = if at + 1 == capabilities.demands.len() {
""
} else {
","
};
let _ = writeln!(text, " \"{}\"{comma}", demand.android_feature());
}
text.push_str(" ],\n \"opens\": [\n");
for (at, media_type) in capabilities.opens.iter().enumerate() {
let comma = if at + 1 == capabilities.opens.len() {
""
} else {
","
};
let _ = writeln!(text, " \"{}\"{comma}", escape_json(media_type));
}
text.push_str(" ]\n}\n");
text
}
fn escape_json(text: &str) -> String {
escape(text).replace('\n', "\\n")
}
pub fn android_permissions(capabilities: &Capabilities<'_>) -> Vec<&'static str> {
let mut named = BTreeSet::new();
for entry in capabilities.uses {
for permission in entry.service.android_permissions() {
named.insert(*permission);
}
}
named.into_iter().collect()
}
pub fn android_manifest(capabilities: &Capabilities<'_>) -> String {
let mut text = String::from(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\">\n",
);
for permission in android_permissions(capabilities) {
let _ = writeln!(
text,
" <uses-permission android:name=\"{permission}\" />"
);
}
for demand in capabilities.demands {
let _ = writeln!(
text,
" <uses-feature android:name=\"{}\" android:required=\"true\" />",
demand.android_feature()
);
}
if !capabilities.opens.is_empty() {
text.push_str(&android_opening_activity(capabilities.opens));
}
text.push_str("</manifest>\n");
text
}
fn android_opening_activity(opens: &[&str]) -> String {
let mut text = String::from(
" <application>\n \
<activity android:name=\"dev.cranpose.android.CranposeActivity\">\n",
);
for actions in [
&[
"android.intent.action.SEND",
"android.intent.action.SEND_MULTIPLE",
][..],
&["android.intent.action.VIEW"][..],
] {
text.push_str(" <intent-filter>\n");
for action in actions {
let _ = writeln!(text, " <action android:name=\"{action}\" />");
}
text.push_str(
" <category android:name=\"android.intent.category.DEFAULT\" />\n",
);
for media_type in opens {
let _ = writeln!(
text,
" <data android:mimeType=\"{}\" />",
escape_xml(media_type)
);
}
text.push_str(" </intent-filter>\n");
}
text.push_str(" </activity>\n </application>\n");
text
}
pub fn apple_usage(capabilities: &Capabilities<'_>) -> String {
let mut text = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \
\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n<dict>\n",
);
for entry in capabilities.uses {
let (Some(key), Some(reason)) = (entry.service.apple_key(), entry.reason) else {
continue;
};
let _ = writeln!(text, "\t<key>{key}</key>");
let _ = writeln!(text, "\t<string>{}</string>", escape_xml(reason));
}
text.push_str("</dict>\n</plist>\n");
text
}
fn escape_xml(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
#[cfg(test)]
#[path = "tests/capabilities_tests.rs"]
mod tests;