Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Create EnvironmentPath convenience wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Jun 2, 2016
1 parent d9342fb commit 601fb67
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
42 changes: 40 additions & 2 deletions src/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {delimiter} from "path";
import {executeCommandWithShellConfig} from "./PTY";
import {clone} from "lodash";
import {homeDirectory} from "./utils/Common";
import * as Path from "path";

const env: Dictionary<string> = {};
export async function loadEnvironment(): Promise<void> {
Expand Down Expand Up @@ -48,8 +49,8 @@ export class Environment {
return key in this.storage;
}

get path(): string {
return this.get("PATH");
get path(): EnvironmentPath {
return new EnvironmentPath(this);
}

cdpath(pwd: string): string[] {
Expand All @@ -68,3 +69,40 @@ export class Environment {
this.set("PWD", value);
}
}

export class EnvironmentPath {
constructor(private environment: Environment) {
}

append(path: string) {
if (!this.has(path)) {
this.environment.set("PATH", this.raw + Path.delimiter + path);
}
}

prepend(path: string) {
if (!this.has(path)) {
this.environment.set("PATH", path + Path.delimiter + this.raw);
}
}

get split() {
return this.raw.split(Path.delimiter);
}

remove(toRemove: string) {
this.removeWhere(existing => existing !== toRemove);
}

removeWhere(remover: (existing: string) => boolean) {
this.environment.set("PATH", this.split.filter(remover).join(Path.delimiter));
}

private has(path: string) {
return this.split.includes(path);
}

private get raw() {
return this.environment.get("PATH");
}
}
6 changes: 2 additions & 4 deletions src/main/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ export const menu = {
},
];

let menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
} else {
const template = [
{
Expand Down Expand Up @@ -141,8 +140,7 @@ export const menu = {
},
];

let menu = Menu.buildFromTemplate(template);
browserWindow.setMenu(menu);
browserWindow.setMenu(Menu.buildFromTemplate(template));
}
},
};
7 changes: 2 additions & 5 deletions src/plugins/NVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ PluginManager.registerEnvironmentObserver({

if (await exists(rcPath)) {
const version = (await readFile(rcPath)).trim();
const newPath = Path.join(homeDirectory, ".nvm", "versions", "node", version, "bin") + Path.delimiter + session.environment.path;

session.environment.set("PATH", newPath);
session.environment.path.prepend(Path.join(homeDirectory, ".nvm", "versions", "node", version, "bin"));
} else {
const path = session.environment.path.split(Path.delimiter).filter(path => !path.includes(".nvm")).join(Path.delimiter);
session.environment.setMany({PATH: path});
session.environment.path.removeWhere(path => !path.includes(".nvm"));
}
},
});
7 changes: 3 additions & 4 deletions src/plugins/RVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ PluginManager.registerEnvironmentObserver({
const rubyVersion = await getRubyVersion(session.directory);
const gemSetName = await getGemSetName(session.directory);
const gemPaths = getGemSetPaths(rubyVersion, gemSetName);
const path = binPaths(rubyVersion, gemSetName) + Path.delimiter + session.environment.path;
session.environment.path.prepend(binPaths(rubyVersion, gemSetName));

session.environment.setMany({
PATH: path,
GEM_PATH: gemPaths.join(Path.delimiter),
GEM_HOME: gemPaths[0],
});
} else {
const path = session.environment.path.split(Path.delimiter).filter(path => !path.includes(".rvm")).join(Path.delimiter);
session.environment.setMany({PATH: path, GEM_HOME: "", GEM_PATH: ""});
session.environment.path.removeWhere(path => !path.includes(".rvm"));
session.environment.setMany({GEM_HOME: "", GEM_PATH: ""});
}
},
});
5 changes: 3 additions & 2 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as e from "./../Enums";
import * as _ from "lodash";
import * as fs from "fs";
import {KeyCode} from "./../Enums";
import {EnvironmentPath} from "../Environment";

interface FSExtraWalkObject {
path: string;
Expand Down Expand Up @@ -160,12 +161,12 @@ export function baseName(path: string): string {
export const {executablesInPaths} = new class {
private executables: Array<string> = [];

executablesInPaths = async (paths: string): Promise<string[]> => {
executablesInPaths = async (path: EnvironmentPath): Promise<string[]> => {
if (this.executables.length) {
return this.executables;
}

const validPaths = await filterAsync(paths.split(Path.delimiter), isDirectory);
const validPaths = await filterAsync(path.split, isDirectory);
const allFiles: string[][] = await Promise.all(validPaths.map(filesIn));

return _.uniq(_.flatten(allFiles));
Expand Down
2 changes: 1 addition & 1 deletion src/views/3_JobComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class JobComponent extends React.Component<Props, State> implements KeyDo
componentDidMount() {
this.props.job
.on("data", () => this.forceUpdate())
.on("status", () => this.forceUpdate())
.on("status", () => this.forceUpdate());
}

componentDidUpdate() {
Expand Down

0 comments on commit 601fb67

Please sign in to comment.