Skip to content

Commit

Permalink
Merge pull request #75 from GitHarpon/changement-terminal
Browse files Browse the repository at this point in the history
feat(terminal): ajout de la fonctionnalité
  • Loading branch information
Nemtecl authored Feb 26, 2019
2 parents bf2e825 + be80d7e commit cc47952
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { ElectronService } from './providers/electron.service';
import { GitService } from './providers/git.service';
import { LanguagePreferencesService } from './providers/language-preferences.service';
import { TerminalManagerService } from './providers/terminal-manager.service';
import { ThemePreferencesService } from './providers/theme-preferences.service';

import { WebviewDirective } from './directives/webview.directive';
Expand Down Expand Up @@ -114,7 +115,8 @@ export function HttpLoaderFactory(http: HttpClient) {
GitService,
EditorPreferencesService,
LanguagePreferencesService,
ThemePreferencesService
ThemePreferencesService,
TerminalManagerService
],
bootstrap: [AppComponent]
})
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/MockElectronService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class MockElectronService {
const CHEMIN = '/new';
return CHEMIN;
}

pathJoin(...paths: string[]) {
return paths.join('').toString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/MockRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export class MockRouter {
resolve(true);
});
}
}
}
38 changes: 38 additions & 0 deletions src/app/models/MockTerminalManagerService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Subject } from 'rxjs';
import { ServiceResult } from './ServiceResult';

export class MockTerminalManagerService {
languages: any[];
preferencesSubject = new Subject<string>();
terminalCmd: string;
terminalName: string;

constructor() {
this.terminalCmd = '';
this.terminalName = '';
}

emitPreferencesSubject() {
this.preferencesSubject.next(this.terminalCmd);
}

setCurrentTerminal(newCmd) {
this.terminalCmd = newCmd;
this.terminalName = 'terminator';
this.emitPreferencesSubject();
}

getTerminals() {
return [{key: 'toto', value: 'titi'}];
}

openTerminal() {
return new Promise((resolve, reject) => {
if (this.terminalName === 'terminator') {
resolve(new ServiceResult(true, '', ''));
} else {
reject(new ServiceResult(false, '', ''));
}
});
}
}
3 changes: 3 additions & 0 deletions src/app/providers/electron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ipcRenderer, webFrame, remote, shell } from 'electron';
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as process from 'process';
import * as url from 'url';

Expand All @@ -19,6 +20,7 @@ export class ElectronService {
process: typeof process;
fs: typeof fs;
path: typeof path;
os: typeof os;
shell: typeof shell;
url: typeof url;

Expand All @@ -35,6 +37,7 @@ export class ElectronService {
this.path = window.require('path');
this.process = window.require('process');
this.url = window.require('url');
this.os = window.require('os');
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/app/providers/terminal-manager.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { ElectronService } from './electron.service';
import { ServiceResult } from '../models/ServiceResult';
import { TranslateService } from '@ngx-translate/core';
import { LocalStorage } from 'ngx-store';

@Injectable()
export class TerminalManagerService {
@LocalStorage({key: 'terminalName'}) terminalName = '';
@LocalStorage({key: 'terminalCmd'}) terminalCmd = '';
currentOs: any;
preferencesSubject = new Subject<string>();

constructor(private electronService: ElectronService,
private translateService: TranslateService) {
this.preferencesSubject = new Subject<string>();
this.currentOs = this.electronService.os.type();
if (this.terminalName == '' || this.terminalCmd == '') {
this.terminalName = this.getTerminals()[0].value;
this.terminalCmd = this.getTerminals()[0].key;
}
}

openTerminal(): Promise<ServiceResult> {
return new Promise((resolve, reject) => {
this.electronService.childProcess.exec(this.terminalCmd, (err) => {
if (err) {
reject(new ServiceResult(false,
this.translateService.instant('TERMINAL.UNKNOWN'),
this.translateService.instant('TERMINAL.MESSAGE')));
} else {
resolve(new ServiceResult(true, '', ''));
}
});
});
}

getTerminals() {
switch (this.currentOs) {
case 'Linux':
return [
{ key: 'terminator', value: 'terminator' },
{ key: 'gnome-terminal', value: 'gnome-terminal' },
{ key: 'xterm', value: 'xterm' }
];
case 'Darwin':
return [
{ key: 'open -a Terminal', value: 'Terminal' },
{ key: 'open -a iTerm', value: 'iTerm' },
{ key: 'open -a terminator', value: 'terminator' }
];
case 'Windows_NT':
return [
{ key: 'start cmd.exe', value: 'cmd' },
{ key: 'start PowerShell.exe', value: 'PowerShell' },
{ key: 'start "" "%ProgramFiles%\\Git\\git-bash.exe"', value: 'git-bash' }
];
default:
return [];
}
}

emitPreferencesSubject() {
this.preferencesSubject.next(this.terminalCmd);
}

setCurrentTerminal(newCmd) {
this.terminalCmd = newCmd;
this.terminalName = this.getTerminals().filter(o => {
return o.key === this.terminalCmd;
})[0].value;
this.emitPreferencesSubject();
}
}
2 changes: 1 addition & 1 deletion src/app/screens/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</div>
<a class="cursor-pointer mr-3" (click)="displaySearchInputValue()">
<i [ngClass]="[(!repoName ? 'disabled-search' : '')]" class="fa fa-search"></i></a>
<a class="cursor-pointer mr-3" (click)="openTerminal()"><i class="fas fa-terminal"></i></a>
<a class="cursor-pointer mr-3" id="terminal-opener" (click)="openTerminal()"><i class="fas fa-terminal"></i></a>
<a class="cursor-pointer" (click)="openPreferences()"><i class="fas fa-cog"></i></a>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/app/screens/home/home.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import { MockThemePreferencesService } from '../../models/MockThemePreferencesSe
import { MockTranslateLoader } from '../../models/MockTranslateLoader';
import { InfoBarComponent } from '../../components/info-bar/info-bar.component';
import { MockRouter } from '../../models/MockRouter';
import { MockTerminalManagerService } from '../../models/MockTerminalManagerService';
import { TerminalManagerService } from '../../providers/terminal-manager.service';

describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
let terminalService: TerminalManagerService;
let originalTimeout;

beforeEach(async(() => {
Expand Down Expand Up @@ -79,6 +82,10 @@ describe('HomeComponent', () => {
provide: GitService,
useClass: MockGitService
},
{
provide: TerminalManagerService,
useClass: MockTerminalManagerService
},
ToastrService
]
})
Expand All @@ -88,6 +95,7 @@ describe('HomeComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
terminalService = TestBed.get(TerminalManagerService);
});

it('tests the component creation', () => {
Expand All @@ -106,6 +114,24 @@ describe('HomeComponent', () => {
expect(component.branchButtonClicked()).toBeTruthy();
});

it('tests the openTerminal function with success', (done) => {
const TerminalName = 'terminator';
terminalService.terminalName = TerminalName;
component.openTerminal().then((result) => {
expect(result).toBeTruthy();
done();
});
});

it('tests the openTerminal function with success', (done) => {
const TerminalName = 'not-a-terminal';
terminalService.terminalName = TerminalName;
component.openTerminal().then((result) => {
expect(result).toBeFalsy();
done();
});
});

it('tests the openPreferences function', (done) => {
component.openPreferences().then((result) => {
expect(result).toBeTruthy();
Expand Down
17 changes: 14 additions & 3 deletions src/app/screens/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Subscription } from 'rxjs';
import { ServiceResult } from '../../models/ServiceResult';
import { TranslateService } from '@ngx-translate/core';
import * as GitUrlParse from 'git-url-parse';
import { TerminalManagerService } from '../../providers/terminal-manager.service';
import { ThemePreferencesService } from '../../providers/theme-preferences.service';

@Component({
Expand Down Expand Up @@ -46,7 +47,8 @@ export class HomeComponent implements OnDestroy {

constructor(public router: Router, private toastr: ToastrService,
private electronService: ElectronService, private gitService: GitService,
private translateService: TranslateService, private themePrefService: ThemePreferencesService) {
private translateService: TranslateService, private terminalService: TerminalManagerService,
private themePrefService: ThemePreferencesService) {
this.pathSubscription = this.gitService.pathSubject.subscribe(
(path: any) => {
this.path = path;
Expand Down Expand Up @@ -87,8 +89,17 @@ export class HomeComponent implements OnDestroy {
return true;
}

openTerminal() {
console.log('on ouvre le terminal');
async openTerminal() {
return this.terminalService.openTerminal()
.then(() => {
return true;
})
.catch((data) => {
this.toastr.error(data.message, data.title, {
onActivateTick: true
});
return false;
});
}

async openPreferences() {
Expand Down
7 changes: 7 additions & 0 deletions src/app/screens/preferences/preferences.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
<app-dropdown [options]="dataDropdownLanguage" idKey="key" valueKey="value"
[(value)]="dropdownLanguageValue" [required]="true"></app-dropdown>
</div>
<div class="col-md-3 mt-2 text-right">
<label>{{ 'TERMINAL.NAME' | translate }}</label>
</div>
<div class="col-md-9 mt-2">
<app-dropdown [options]="dataDropdownTerminal" idKey="key" valueKey="value"
[(value)]="dropdownTerminalValue" [required]="true"></app-dropdown>
</div>
<div class="col-md-12 mt-2 text-right">
<app-button value="CHANGE_PREFERENCE" type="success" (buttonClicked)="saveChangedPreferences()"></app-button>
</div>
Expand Down
Loading

0 comments on commit cc47952

Please sign in to comment.