Skip to content

Commit

Permalink
OSX: Add to PATH Command. (onivim#1121)
Browse files Browse the repository at this point in the history
* Start an add to path script.

* Remove debug calls.

* Update to use run script on macOS.

* Temporarily jam into KeyDisplayer.

This is very temporary, and just so I can test it out.

* Fix path to resource.

* Move action around.

* Run format.

* Tidy up effect.

* Add remove action.

* Fix mac link location.

Should be more general now.

* Don't run if the link location doesn't exist.

* Format

Co-authored-by: Bryan Phelps <[email protected]>
  • Loading branch information
CrossR and bryphe committed Jan 3, 2020
1 parent eaa5a13 commit fd0b4e4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
76 changes: 76 additions & 0 deletions node/add-to-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// add-to-path.js
// This is a Node-side script to add Oni2 to their path.
// This is done in node, to get sudo-prompt.
const fs = require("fs");
const os = require("os");
const path = require("path");
const sudo = require("sudo-prompt");

const isWindows = () => os.platform() === "win32"
const isMac = () => os.platform() === "darwin"
const isLinux = () => os.platform() === "linux"

const getLinkPath = () => (isMac() || isLinux() ? "/usr/local/bin/oni2" : "")

const isAddedToPath = () => {
try {
fs.lstatSync(getLinkPath());
} catch (_) {
return false
}
return true
}
const removeFromPath = () => {
if (isAddedToPath() && !isWindows()) {
fs.unlinkSync(getLinkPath())
}
}

const addToPath = async () => {
if (!isAddedToPath() && !isWindows()) {
const appDirectory = path.join(path.dirname(process.mainModule.filename), "..");
let imgPath = path.join(appDirectory, "Onivim2.icns");

// TODO: Check this is valid for all use cases.
if (!fs.existsSync(imgPath)) {
imgPath = path.join(appDirectory, "assets", "images", "Onivim2.icns");
}

const options = { name: "Oni2", icns: imgPath };
let linkDest = "";

if (isMac()) {
linkDest = path.join(appDirectory, "run.sh");
} else {
linkDest = ""; // TODO.
}

if (!fs.existsSync(linkDest)) {
return
}

await runSudoCommand(`ln -fs ${linkDest} ${getLinkPath()}`, options);
}
}

const runSudoCommand = async (command, options) => {
return new Promise(resolve => {
sudo.exec(command, options, (error, stdout, stderr) => {
resolve({ error, stdout, stderr });
});
});
}

const toggleAddToPath = async () => {
if (isAddedToPath()) {
removeFromPath()
} else {
await addToPath()
}
}

(async () => {
try {
await Promise.resolve(toggleAddToPath());
} catch (_) {}
})();
2 changes: 1 addition & 1 deletion scripts/osx/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

open "$DIR/Onivim2.app" --args --working-directory "$CWD" "$@"
"$DIR"/../MacOS/Oni2 --working-directory "$CWD" "$@"
2 changes: 2 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ if (process.platform == "linux") {

fs.copySync(eulaFile, path.join(resourcesDirectory, "EULA.md"));
fs.copySync(thirdPartyFile, path.join(resourcesDirectory, "ThirdPartyLicenses.txt"));
fs.copySync("scripts/osx/run.sh", path.join(resourcesDirectory, "run.sh"));
fs.copySync("assets/images/Onivim2.icns", path.join(resourcesDirectory, "Onivim2.icns"));

// Copy icon
copy(iconSourcePath, path.join(resourcesDirectory, "Onivim2.icns"));
Expand Down
37 changes: 37 additions & 0 deletions src/Store/CommandStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ open Oni_Core;
open Oni_Model;
open Oni_Model.Actions;

let pathSymlinkEnabled = (~addingLink) =>
(
Revery.Environment.os == Revery.Environment.Mac
&& !Sys.file_exists("/usr/local/bin/oni2")
)
== addingLink;

let createDefaultCommands = getState => {
State.(
Actions.[
Expand Down Expand Up @@ -106,6 +113,20 @@ let createDefaultCommands = getState => {
~action=CopyActiveFilepathToClipboard,
(),
),
Command.create(
~category=Some("System"),
~name="Add Oni2 to System PATH",
~enabled=() => pathSymlinkEnabled(~addingLink=true),
~action=Command("system.addToPath"),
(),
),
Command.create(
~category=Some("System"),
~name="Remove Oni2 from System PATH",
~enabled=() => pathSymlinkEnabled(~addingLink=false),
~action=Command("system.removeFromPath"),
(),
),
Command.create(
~category=None,
~name="Goto symbol in file...",
Expand Down Expand Up @@ -182,9 +203,25 @@ let start = (getState, contributedCommands) => {
});
};

let togglePathEffect = name =>
Isolinear.Effect.create(
~name,
() => {
let _ =
Oni_Extensions.NodeTask.run(
~scheduler=Scheduler.immediate,
~setup=Oni_Core.Setup.init(),
"add-to-path.js",
);
();
},
);

let commands = [
("keyDisplayer.enable", _ => singleActionEffect(EnableKeyDisplayer)),
("keyDisplayer.disable", _ => singleActionEffect(DisableKeyDisplayer)),
("system.addToPath", _ => togglePathEffect),
("system.removeFromPath", _ => togglePathEffect),
(
"references-view.find",
_ => singleActionEffect(References(References.Requested)),
Expand Down

0 comments on commit fd0b4e4

Please sign in to comment.