Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / build-settings.service.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Injectable } from '@angular/core';
20 import { CookieService } from 'ngx-cookie';
21 import { Observable } from 'rxjs/Observable';
22 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
23
24 export interface IBuildSettings {
25   subpath: string;
26   cmdClean: string;
27   cmdPrebuild: string;
28   cmdBuild: string;
29   cmdPopulate: string;
30   cmdArgs: string[];
31   envVars: string[];
32 }
33
34 @Injectable()
35 export class BuildSettingsService {
36   public settings$: Observable<IBuildSettings>;
37
38   private settingsSubject: BehaviorSubject<IBuildSettings>;
39   private settingsStore: IBuildSettings;
40
41   constructor(
42     private cookie: CookieService,
43   ) {
44     this._load();
45   }
46
47   // Load build settings from cookie
48   private _load() {
49     // Try to retrieve previous config from cookie
50     const cookConf = this.cookie.getObject('xds-build-settings');
51     if (cookConf != null) {
52       this.settingsStore = <IBuildSettings>cookConf;
53     } else {
54       // Set default config
55       this.settingsStore = {
56         subpath: '',
57         cmdClean: 'rm -rf build && echo Done',
58         cmdPrebuild: 'mkdir -p build && cd build && cmake ..',
59         cmdBuild: 'cd build && make',
60         cmdPopulate: 'cd build && make remote-target-populate',
61         cmdArgs: [],
62         envVars: [],
63       };
64     }
65   }
66
67   // Save config into cookie
68   private _save() {
69     // Notify subscribers
70     this.settingsSubject.next(Object.assign({}, this.settingsStore));
71
72     const cfg = Object.assign({}, this.settingsStore);
73     this.cookie.putObject('xds-build-settings', cfg);
74   }
75
76   // Get whole config values
77   get(): IBuildSettings {
78     return this.settingsStore;
79   }
80
81   // Get whole config values
82   set(bs: IBuildSettings) {
83     this.settingsStore = bs;
84     this._save();
85   }
86
87   get subpath(): string {
88     return this.settingsStore.subpath;
89   }
90
91   set subpath(p: string) {
92     this.settingsStore.subpath = p;
93     this._save();
94   }
95
96 }