-
Notifications
You must be signed in to change notification settings - Fork 6
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
Automate darwin install #43
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
34226e3
add macos install
mzgoddard 166e36b
Merge branch 'main' into automate-darwin-ci-install
jugglinmike 5ca0350
Add note on interdependent values
jugglinmike 4873cbe
Document the intent of utility function
jugglinmike 967c5d7
Merge branch 'main' into automate-darwin-ci-install
jugglinmike e05b38e
Surface requirement for key press automation
jugglinmike 76639d7
Correct error in command
jugglinmike 452b09c
Add debugging information
jugglinmike 15539bf
Include files built for macOS in release
jugglinmike 92e6fe1
Improve detection algorithm
jugglinmike 99387ba
Support environment with no prior setting
jugglinmike bbd1383
Add support for "enter" key
jugglinmike ea80e6a
Retrieve VoiceOver version (via macOS version)
jugglinmike 48bc032
Reformat
jugglinmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,214 @@ | ||
'use strict'; | ||
|
||
exports.install = async function () { | ||
throw new Error('macos install not implemented'); | ||
const { exec: _exec } = require('child_process'); | ||
const { resolve } = require('path'); | ||
const { promisify } = require('util'); | ||
|
||
const debug = require('debug')('install'); | ||
|
||
const { 'interaction.pressKeys': pressKeys } = require('../modules/macos/interaction'); | ||
|
||
const LSREGISTER_EXECUTABLE_PATH = | ||
'/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister'; | ||
const APPLICATION_NAME = 'ATDriverGenericMacOS.app'; | ||
const EXTENSION_IDENTIFIER = 'com.bocoup.ATDriverGenericMacOS.ATDriverGenericMacOSExtension'; | ||
const VOICE_IDENTIFIER = | ||
'com.bocoup.ATDriverGenericMacOS.ATDriverGenericMacOSExtension.ATDriverGenericMacOSExtension'; | ||
const SYSTEM_VOICE_IDENTIFIER = 'com.apple.Fred'; | ||
/** | ||
* This string comprises three tokens (the "type", "subtype", and | ||
* "manufacturer" of the Audio Unit) which must be kept in-sync with other | ||
* references in this project: | ||
* | ||
* - src/macos/ATDriverGenericMacOS/ATDriverGenericMacOS/Model/AudioUnitHostModel.swift | ||
* - src/macos/ATDriverGenericMacOS/ATDriverGenericMacOSExtension/Info.plist | ||
*/ | ||
const PLUGIN_TRIPLET_IDENTIFIER = 'ausp atdg BOCU'; | ||
|
||
const exec = promisify(_exec); | ||
const enableKeyAutomationPrompt = `This tool can only be installed on systems which allow automated key pressing. | ||
Please allow the Terminal application to control your computer (the setting is | ||
controlled in System Settings > Privacy & Security > Accessibility).`; | ||
|
||
/** @typedef {import('child_process').ExecOptions} ExecOptions */ | ||
|
||
/** | ||
* Prompt the user to press any key. Resolves when the user presses a key. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
const promptForManualKeyPress = async () => { | ||
process.stdout.write('Press any key to continue... '); | ||
const wasRaw = process.stdin.isRaw; | ||
process.stdin.setRawMode(true); | ||
process.stdin.resume(); | ||
const byteArray = await new Promise(resolve => { | ||
process.stdin.once('data', data => resolve(Array.from(data))); | ||
}); | ||
|
||
process.stdin.pause(); | ||
process.stdin.setRawMode(wasRaw); | ||
process.stdout.write('\n'); | ||
|
||
// Honor "Control + C" motion by exiting. | ||
if (byteArray[0] === 3) { | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
/** | ||
* @param {object} options | ||
* @param {boolean} options.unattended - Whether installation should fail if | ||
* human intervention is required | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
exports.install = async function ({ unattended }) { | ||
const options = await getExecOptions(); | ||
|
||
if (!(await canPressKeys())) { | ||
if (unattended) { | ||
throw new Error('The system cannot automate key pressing.'); | ||
} else { | ||
console.error(enableKeyAutomationPrompt); | ||
|
||
await promptForManualKeyPress(); | ||
|
||
if (!(await canPressKeys())) { | ||
throw new Error('The system cannot automate key pressing.'); | ||
} | ||
} | ||
} | ||
|
||
if (await isInstalled()) { | ||
throw new Error('Already installed'); | ||
} | ||
|
||
await removeQuarantine(options); | ||
await registerExtensions(options); | ||
await enableExtension(); | ||
await setSystemVoice(VOICE_IDENTIFIER); | ||
}; | ||
|
||
/** | ||
* @returns {Promise<void>} | ||
*/ | ||
exports.uninstall = async function () { | ||
throw new Error('macos uninstall not implemented'); | ||
const options = await getExecOptions(); | ||
|
||
if (!(await isInstalled())) { | ||
throw new Error('Not installed'); | ||
} | ||
|
||
await setSystemVoice(SYSTEM_VOICE_IDENTIFIER); | ||
await unregisterExtensions(options); | ||
}; | ||
|
||
/** | ||
* Experimentally determine whether the current system supports automated key | ||
* pressing by attempting to press an arbitrary key. | ||
* | ||
* @returns {Promise<boolean>} | ||
*/ | ||
const canPressKeys = async () => { | ||
try { | ||
await pressKeys(null, { keys: ['shift'] }); | ||
} catch ({}) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const isInstalled = async function () { | ||
let stdout; | ||
|
||
try { | ||
({ stdout } = await exec(`auval -v ${PLUGIN_TRIPLET_IDENTIFIER}`)); | ||
} catch (error) { | ||
if (error.stdout && error.stdout.includes("didn't find the component")) { | ||
return false; | ||
} | ||
throw error; | ||
} | ||
|
||
return /ATDriverGenericMacOSExtension/.test(stdout); | ||
}; | ||
|
||
/** | ||
* @returns {Promise<ExecOptions>} | ||
*/ | ||
const getExecOptions = async function () { | ||
return { | ||
cwd: resolve(__dirname, '../../Release/macos'), | ||
}; | ||
}; | ||
|
||
/** | ||
* Remove the "quarantine" attribute which macOS uses to prevent accidental | ||
* execution of code from unverified sources. | ||
* | ||
* https://support.apple.com/en-us/101987 | ||
* | ||
* @param {ExecOptions} options | ||
* @returns {Promise<boolean>} Whether a change took place | ||
*/ | ||
async function removeQuarantine(options) { | ||
debug('Removing macOS quarantine'); | ||
await exec(`xattr -r -d com.apple.quarantine ${APPLICATION_NAME}`, options); | ||
return true; | ||
} | ||
|
||
/** | ||
* @param {ExecOptions} options | ||
* @returns {Promise<void>} | ||
*/ | ||
async function registerExtensions(options) { | ||
debug('Registering trusted macOS extension'); | ||
await exec(`${LSREGISTER_EXECUTABLE_PATH} -f -R -trusted ${APPLICATION_NAME}`, options); | ||
} | ||
|
||
/** | ||
* @param {ExecOptions} options | ||
* @returns {Promise<void>} | ||
*/ | ||
async function unregisterExtensions(options) { | ||
debug('Unregistering trusted macOS extension'); | ||
await exec(`${LSREGISTER_EXECUTABLE_PATH} -f -R -trusted -u ${APPLICATION_NAME}`, options); | ||
} | ||
|
||
async function enableExtension() { | ||
debug('Enabling macOS extension'); | ||
await exec(`pluginkit -e use -i ${EXTENSION_IDENTIFIER}`); | ||
} | ||
|
||
/** | ||
* @param {string} newValue the identifier for the voice to set | ||
* @returns {Promise<void>} | ||
*/ | ||
async function setSystemVoice(newValue) { | ||
debug(`Setting macOS system voice to "${newValue}"`); | ||
let stdout; | ||
|
||
try { | ||
({ stdout } = await exec( | ||
'defaults read com.apple.Accessibility SpeechVoiceIdentifierForLanguage', | ||
)); | ||
} catch (error) { | ||
if (!error || !error.stderr.includes('does not exist')) { | ||
throw error; | ||
} | ||
} | ||
|
||
const currentValue = stdout ? stdout.replace(/[\s]/g, '').match(/2={en="([^"]+)";};/) : null; | ||
|
||
debug(`Current value: ${currentValue ? JSON.stringify(currentValue[1]) : '(unset)'}`); | ||
|
||
if (currentValue && currentValue[1] === newValue) { | ||
debug('Already set.'); | ||
return; | ||
} | ||
|
||
await exec( | ||
`defaults write com.apple.Accessibility SpeechVoiceIdentifierForLanguage '{2 = {en = "${newValue}";};}'`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very useful!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad you think so; I had trouble explaining it in a way that even kind of makes sense