Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider implementing xcode-locator in osascript #14174

Closed
steeve opened this issue Oct 26, 2021 · 2 comments
Closed

Consider implementing xcode-locator in osascript #14174

steeve opened this issue Oct 26, 2021 · 2 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) platform: apple stale Issues or PRs that are stale (no activity for 30 days) team-Rules-CPP Issues for C++ rules type: feature request

Comments

@steeve
Copy link
Contributor

steeve commented Oct 26, 2021

Description of the problem / feature request:

xcode-locator locates the xcode versions installed on the system . It is written in ObjC because it needs LSCopyApplicationURLsForBundleIdentifier. Thus, it needs to be compiled inside a repository rule, which is somewhat of an anti pattern.

It is possible to replace it with JXA, here is a rapid prototype:

#!/usr/bin/osascript -l JavaScript
ObjC.import("CoreServices");

ObjC.bindFunction("CFMakeCollectable", [ "id", [ "void *" ] ]);
Ref.prototype.toNS = function () { return $.CFMakeCollectable(this); };
String.prototype.toCF = function() { return $.CFStringCreateWithCString(0, this, 0).toNS(); };

function applicationURLsForBundleIdentifier(bundleId) {
	const bundles = $.LSCopyApplicationURLsForBundleIdentifier(bundleId.toCF(), $());
	return ObjC.deepUnwrap(bundles.toNS());
}

function bundleVersion(bundle) {
	return ObjC.unwrap(bundle.infoDictionary.objectForKey("CFBundleShortVersionString"));
}

function xcodeBundleProductVersion(bundle) {
	const versionPlistUrl = bundle.bundleURL.URLByAppendingPathComponent("Contents/version.plist");
	const info = $.NSDictionary.alloc.initWithContentsOfURL(versionPlistUrl);
	return ObjC.unwrap(info.objectForKey("ProductBuildVersion"));
}

function expandVersion(version) {
	const parts = version.split(".");
	switch (parts.length) {
		case 1:
			return version + ".0.0";
		case 2:
			return version + ".0";
	}
	return version;
}

function xcodeBundles() {
	const xcodeBundleId = "com.apple.dt.Xcode";
	return applicationURLsForBundleIdentifier(xcodeBundleId).map((url) => {
		return $.NSBundle.bundleWithURL(url);
	}).filter((bundle) => {
		return ObjC.unwrap(bundle.bundleIdentifier) == xcodeBundleId
	}).reduce((xcodes, bundle) => {
		var version = expandVersion(bundleVersion(bundle));
		const productVersion = xcodeBundleProductVersion(bundle);
		if (productVersion) {
			version += "." + productVersion;
		}
		xcodes[version] = ObjC.unwrap(bundle.bundlePath) + "/Contents/Developer";
		return xcodes;
	}, {});
}

function main(argv) {
	const xcodes = xcodeBundles();
	console.log(JSON.stringify(xcodes));
}

main($.NSProcessInfo.processInfo.arguments.js.filter((_, idx) => idx >= 3).map((arg) => arg.js));

When run:

$ ./xcodes.js
{"12.5.0.12E262":"/Applications/Xcode-12.5.0.app/Contents/Developer"}
$

What operating system are you running Bazel on?

macOS

Have you found anything relevant by searching the web?

@steeve steeve changed the title Consider replacing xcode-locator with osascript Consider implementing xcode-locator in osascript Oct 26, 2021
@oquenchil oquenchil added platform: apple team-Rules-CPP Issues for C++ rules type: feature request P3 We're not considering working on this, but happy to review a PR. (No assignee) labels Nov 2, 2021
@github-actions
Copy link

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label May 25, 2023
@github-actions
Copy link

github-actions bot commented Jun 8, 2023

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please reach out to the triage team (@bazelbuild/triage). Thanks!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) platform: apple stale Issues or PRs that are stale (no activity for 30 days) team-Rules-CPP Issues for C++ rules type: feature request
Projects
None yet
Development

No branches or pull requests

2 participants