Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
intitni committed Feb 1, 2023
2 parents e2827fa + 8f3cec0 commit 0c742fc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
14 changes: 14 additions & 0 deletions EditorExtensionXPCService/main.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AppKit
import Foundation

let listener = NSXPCListener(
Expand All @@ -7,4 +8,17 @@ let listener = NSXPCListener(
let delegate = ServiceDelegate()
listener.delegate = delegate
listener.resume()

Task {
for await notification in NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.didTerminateApplicationNotification)
{
guard let app = notification
.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
app.bundleIdentifier == "com.apple.dt.Xcode"
else { continue }
exit(0)
}
}

RunLoop.main.run()
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Xccurate Formatter is an Xcode Source Editor Extension that reads the **project-specific configurations**. It provides a universal format file action for all file types it recognizes.

It supports the following formatters:
The following formatters are supported:

- [SwiftFormat](https://github.com/nicklockwood/SwiftFormat)
- [swift-format](https://github.com/apple/swift-format)
Expand All @@ -15,34 +15,34 @@ Check the file `UTIToExtensionName.swift` for supported file types. Some of them

## Note

The app was initially built with an Apple Script solution with many limitations. Luckily some troubles caused by it led me to a better solution. 塞翁失马焉知非福.
The application was originally built using an Apple Script solution with many limitations. Fortunately, some problems caused by it led me to a better solution. 塞翁失马焉知非福.

But the solution shift was such a rush that some code may not make too much sense to the new solution.
But the solution shift was such a rush that some code may not make too much sense for the new solution.

## Usage

### Enable Extension
### Enable the Extension

Go to Settings.app search for Xcode extension, and enable Xccurate Formatter.
Go to System Settings.app search for Xcode extension, and enable Xccurate Formatter.

If the extension complains that it has no permission to use Accessibility API, go turn it on in Settings.app for `XccurateFormatter`.
If the extension complains that it does not have permission to use the Accessibility API, go to System Settings.app and enable `XccurateFormatter`.

If it keeps complaining (especially when after you updated the app or run it from Xcode), try:
If it keeps complaining (especially when after you have updated the app or run it from Xcode), try to:

- Remove the app from Accessibility settings. Add it back manually, or use the extension once then check the list again.
- Remove the app from the Accessibility settings. Add it back manually, or use the extension once and check the list again.
- Remove Launch Agent and setup again.

It can be tricky.
This can be tricky.

### Setup Launch Agent

You have to click "Setup Launch Agent for XPC Service" to make the extension work. The app will put a plist file under `~/Library/LaunchAgents` and load it with `launchctl`.

If it fails, or if the extension complains that it can't connect to the helper, please try:
If it fails, or if the extension complains that it can't connect to the helper, please try to:

- restart Xcode.
- if still not working, run `launchctl list | grep com.intii` to see if `com.intii.XccurateFormatter.EditorExtensionXPCService` is loaded.
- if not, see if `com.intii.XccurateFormatter.EditorExtensionXPCService.plist` is created under `~/Library/LaunchAgents`.
- if not, check if `com.intii.XccurateFormatter.EditorExtensionXPCService.plist` is created under `~/Library/LaunchAgents`.
- if not, create it yourself. Fill it with content that you can find in the file `LaunchAgentManager.swift`. Then run `launchctl load the/path/to/the/plist`.

### Install Formatters
Expand All @@ -52,8 +52,8 @@ Xccurate Formatter doesn't support code formatting on its own. You will need to
For example, to enable SwiftFormat, you can:

1. install it via homebrew `brew install swiftformat`.
2. get the path of it `which swiftformat`.
3. paste the path in the app `/opt/homebrew/bin/swiftformat`.
2. get the path to it `which swiftformat`.
3. paste the path into the app `/opt/homebrew/bin/swiftformat`.

Alternatively, you can add a file `.xccurateformatter` to the project root to override the settings:

Expand All @@ -70,19 +70,19 @@ Alternatively, you can add a file `.xccurateformatter` to the project root to ov

All fields are optional.

### Update
### Configurations

After updating the app, please restart the XPC service manually.
Place the formatter configuration files at the project root or its parent directories. Xccurate Formatter will use the closet configuration it finds to determine which formatter to use.

### Configurations
If no configuration is found, it will use the first formatter in the supported formatters list that supports the language and has its executable path set.

Place the formatter configuration files at the project root or its parent directories. Xccurate Formatter will use the nearest configuration it finds to determine which formatter to use.
### Key Bindings

If no configuration is found, it will use the first formatter in the supported formatter list that supports the language and has its executable path set.
You can set key bindings for the command in Xcode settings.

## How It Works

The source extension itself must be sandboxed, but it can still talk to XPC services that are not. That way, we can let the XPC service do the dirty work and return the formatted code to the extension.
The source extension itself must be sandboxed, but it can still talk to XPC services that are not. This way, we can let the XPC service do the dirty work and return the formatted code to the extension.

The dirty work will be:

Expand All @@ -97,12 +97,12 @@ The dirty work will be:
|- code.swift
```
Swift Format will be used.
3. Create a temp file at the project root or in the same folder as the original file (so the formatters can read other configuration files like .swift-version), paste the code to the file, and run the formatter on the file.
4. Return the formatted code to the extension, and delete the temp file.
3. Create a temporary file at the project root or in the same folder as the original file (so the formatters can read other configuration files like .swift-version), paste the code into the file, and run the formatter on the file.
4. Return the formatted code to the extension, and delete the temporary file.

Though it's possible to embed a non-sandboxed XPC Service inside the sandboxed extension, notarization will fail.
Although it's possible to embed a non-sandboxed XPC Service inside the sandboxed extension, notarization will fail (not sure, notarization can fail for no reason). And managing permissions will be hard.

The workaround I am using is to build the XPC Service into a command line tool, and copy it into the main application's executables directory. Check this link for detail: [Creating a Launch Agent that provides an XPC service on macOS using Swift](https://rderik.com/blog/creating-a-launch-agent-that-provides-an-xpc-service-on-macos/).
The workaround I am using is to build the XPC Service into a command line tool, and copy it into the executable directory of the main application. Check this link for detail: [Creating a Launch Agent that provides an XPC service on macOS using Swift](https://rderik.com/blog/creating-a-launch-agent-that-provides-an-xpc-service-on-macos/).

This method also makes the Accessibility API usable from the XPC Service.

Expand Down
8 changes: 4 additions & 4 deletions XccurateFormatter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 36;
DEVELOPMENT_ASSET_PATHS = "\"XccurateFormatter/Preview Content\"";
DEVELOPMENT_TEAM = 5YKZ4Y3DAW;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -778,7 +778,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 0.4.0;
MARKETING_VERSION = 0.5.0;
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -796,7 +796,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 36;
DEVELOPMENT_ASSET_PATHS = "\"XccurateFormatter/Preview Content\"";
DEVELOPMENT_TEAM = 5YKZ4Y3DAW;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -810,7 +810,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 0.4.0;
MARKETING_VERSION = 0.5.0;
PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
2 changes: 0 additions & 2 deletions XccurateFormatter/LaunchAgentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ struct LaunchAgentManager {
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>RunAtLoad</key>
<true/>
<key>Label</key>
<string>\(serviceIdentifier)</string>
<key>Program</key>
Expand Down

0 comments on commit 0c742fc

Please sign in to comment.