Skip to content

Commit

Permalink
fixup! Add devfile support in UD
Browse files Browse the repository at this point in the history
  • Loading branch information
ashumilova committed May 20, 2019
1 parent 22baf6e commit e0c361d
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export class CreateWorkspaceSvc {

return this.checkEditingProgress().then(() => {
workspaceConfig.projects = projectTemplates;
this.addProjectCommands(workspaceConfig, projectTemplates);
//TODO
//this.addProjectCommands(workspaceConfig, projectTemplates);
return this.cheWorkspace.createWorkspaceFromConfig(namespaceId, workspaceConfig, attributes).then((workspace: che.IWorkspace) => {
return this.cheWorkspace.fetchWorkspaces().then(() => this.cheWorkspace.getWorkspaceById(workspace.id));
})
Expand Down Expand Up @@ -235,17 +236,15 @@ export class CreateWorkspaceSvc {
/**
* Adds commands from the bunch of project templates to provided workspace config.
*
* @param {che.IWorkspaceConfig} workspaceConfig workspace config
* @param {che.IWorkspace} workspace
* @param {Array<che.IProjectTemplate>} projectTemplates the list of project templates
*/
addProjectCommands(workspaceConfig: che.IWorkspaceConfig, projectTemplates: Array<che.IProjectTemplate>): void {
workspaceConfig.commands = workspaceConfig.commands || [];

addProjectCommands(workspace: che.IWorkspace, projectTemplates: Array<che.IProjectTemplate>): void {
projectTemplates.forEach((template: che.IProjectTemplate) => {
let projectName = template.name;
template.commands.forEach((command: any) => {
command.name = projectName + ':' + command.name;
workspaceConfig.commands.push(command);
this.cheWorkspace.getWorkspaceDataManager().addCommand(workspace, command);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ export class ListWorkspacesCtrl {
return this.$q.when();
}
let message = error.data && error.data.message ? ' Reason: ' + error.data.message : '';
this.cheNotification.showError('Failed to retrieve workspace ' + workspace.config.name + ' data.' + message) ;
let workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
this.cheNotification.showError('Failed to retrieve workspace ' + workspaceName + ' data.' + message) ;
return this.$q.reject(error);
})
.then(() => {
Expand Down Expand Up @@ -249,7 +250,7 @@ export class ListWorkspacesCtrl {
if (!workspace) {
return;
}
workspaceName = workspace.config.name;
workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
let stoppedStatusPromise = this.cheWorkspace.fetchStatusChange(workspaceId, 'STOPPED');

// stop workspace if it's status is RUNNING
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2015-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
/**
* @ngdoc controller
* @name workspaces.devfile-editor.controller:WorkspaceDevfileEditorController
* @description This class is handling the controller for the workspace devfile editor widget
* @author Anna Shumilova
*/
export class WorkspaceDevfileEditorController {

static $inject = ['$log', '$scope', '$timeout'];

$log: ng.ILogService;
$scope: ng.IScope;
$timeout: ng.ITimeoutService;

editorOptions: {
lineWrapping: boolean,
lineNumbers: boolean,
matchBrackets: boolean,
mode: string,
onLoad: Function
};
devfileValidationMessages: string[] = [];
isActive: boolean;
workspaceDevfile: any;
devfileYaml: string;
newWorkspaceDevfile: any;
workspaceDevfileOnChange: Function;
private saveTimeoutPromise: ng.IPromise<any>;
private isSaving: boolean;


/**
* Default constructor that is using resource
*/
constructor($log: ng.ILogService, $scope: ng.IScope, $timeout: ng.ITimeoutService) {
this.$log = $log;
this.$scope = $scope;
this.$timeout = $timeout;
this.isSaving = false;
this.devfileYaml = jsyaml.dump(this.workspaceDevfile);

$scope.$watch(() => {
return this.workspaceDevfile;
}, () => {
let editedWorkspaceDevfile;
try {
editedWorkspaceDevfile = jsyaml.load(this.devfileYaml);
angular.extend(editedWorkspaceDevfile, this.workspaceDevfile);
} catch (e) {
editedWorkspaceDevfile = this.workspaceDevfile;
}
this.devfileYaml = jsyaml.dump(this.workspaceDevfile);
const validateOnly = true;
this.onChange(validateOnly);
}, true);

}

/**
* Callback when editor content is changed.
*/
onChange(validateOnly?: boolean): void {
if (!this.devfileYaml) {
this.devfileValidationMessages = ['The config is required.'];
return;
}

try {
let devfile = jsyaml.load(this.devfileYaml);

if (validateOnly || !this.isActive) {
return;
}
this.isSaving = (this.devfileValidationMessages.length === 0) && !angular.equals(devfile, this.workspaceDevfile);

if (this.saveTimeoutPromise) {
this.$timeout.cancel(this.saveTimeoutPromise);
}

this.saveTimeoutPromise = this.$timeout(() => {
// immediately apply config on IU
this.newWorkspaceDevfile = angular.copy(devfile);
this.isSaving = false;
this.applyChanges();
}, 2000);


} catch (e) {
if (this.devfileValidationMessages.length === 0) {
this.devfileValidationMessages = ['Devfile is invalid.'];
}
this.$log.error(e);
}
}

/**
* Callback when user applies new config.
*/
applyChanges(): void {
this.workspaceDevfileOnChange({devfile: this.newWorkspaceDevfile});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2015-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';

/**
* Defines a directive for displaying devfile editor widget.
* @author Anna Shumilova
*/
export class WorkspaceDevfileEditor {
restrict: string = 'E';
templateUrl: string = 'app/workspaces/workspace-details/devfile/workspace-devfile-editor.html';
replace: boolean = false;

controller: string = 'WorkspaceDevfileEditorController';
controllerAs: string = 'workspaceDevfileEditorController';

bindToController: boolean = true;

scope: {
[paramName: string]: string;
};

/**
* Default constructor that is using resource
*/
constructor() {
// scope values
this.scope = {
isActive: '=',
workspaceDevfile: '=',
workspaceDevfileOnChange: '&'
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<div layout="column" class="config-import">
<div class="saving-message">
<span ng-if="workspaceDevfileEditorController.isSaving">Saving workspace devfile...</span>
</div>
<div class="config-editor">
<che-editor editor-content="workspaceDevfileEditorController.devfileYaml"
editor-state="editorState"
on-content-change="workspaceDevfileEditorController.onChange()"
editor-mode="text/x-yaml"
required>
<div ng-message="required">A reference is required.</div>
</che-editor>
</div>
<div layout="column">
<!-- Other errors -->
<div ng-if="workspaceConfigImportController.devfileValidationMessages.length===0">
<!-- Settings tab errors -->
<div
ng-if="workspaceConfigImportController.otherValidationMessages[workspaceConfigImportController.errorsScopeSettings].length>0">
<div>Settings tab contains error(s):</div>
<div class="error-message"
ng-repeat="settingsValidationMessage in workspaceConfigImportController.otherValidationMessages[workspaceConfigImportController.errorsScopeSettings]">
{{settingsValidationMessage}}
</div>
</div>
<!-- Runtime tab errors -->
<div
ng-if="workspaceConfigImportController.otherValidationMessages[workspaceConfigImportController.errorsScopeEnvironment].length>0">
<div>Runtime tab contains error(s):</div>
<div class="error-message"
ng-repeat="envValidationMessage in workspaceConfigImportController.otherValidationMessages[workspaceConfigImportController.errorsScopeEnvironment]">
{{envValidationMessage}}
</div>
</div>
</div>
</div>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.config-import
margin-left 10px

.config-editor
min-width 500px
max-width 1000px
margin-top 20px
margin-right 8px

.config-editor .CodeMirror
border 1px solid $list-separator-color
height 300px
min-height 300px
font-size 12px

.config-docs-link
margin-top 5px

.error-message
color $error-color

.saving-message
color $label-info-color
height 18px
top 12px
position absolute
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {WorkspaceRecipeAuthoringController} from './select-stack/recipe-authorin
import {WorkspaceRecipeAuthoring} from './select-stack/recipe-authoring/workspace-recipe-authoring.directive';
import {WorkspaceConfigImportController} from './config-import/workspace-config-import.controller';
import {WorkspaceConfigImport} from './config-import/workspace-config-import.directive';
import {WorkspaceDevfileEditorController} from './devfile/workspace-devfile-editor.controller';
import {WorkspaceDevfileEditor} from './devfile/workspace-devfile-editor.directive';
import {ReadyToGoStacksController} from './select-stack/ready-to-go-stacks/ready-to-go-stacks.controller';
import {ReadyToGoStacks} from './select-stack/ready-to-go-stacks/ready-to-go-stacks.directive';
import {CreateProjectStackLibraryController} from './select-stack/stack-library/create-project-stack-library.controller';
Expand Down Expand Up @@ -115,6 +117,8 @@ export class WorkspaceDetailsConfig {
register.directive('cheWorkspaceRecipeAuthoring', WorkspaceRecipeAuthoring);
register.controller('WorkspaceConfigImportController', WorkspaceConfigImportController);
register.directive('cheWorkspaceConfigImport', WorkspaceConfigImport);
register.controller('WorkspaceDevfileEditorController', WorkspaceDevfileEditorController);
register.directive('workspaceDevfileEditor', WorkspaceDevfileEditor);
register.controller('ReadyToGoStacksController', ReadyToGoStacksController);
register.directive('readyToGoStacks', ReadyToGoStacks);
register.controller('CreateProjectStackLibraryController', CreateProjectStackLibraryController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export interface IInitData {
workspaceDetails: che.IWorkspace;
}

const TAB: Array<string> = ['Overview', 'Projects', 'Containers', 'Servers', 'Env_Variables', 'Volumes', 'Config', 'SSH', 'Plugins', 'Editors'];

/**
* @ngdoc controller
* @name workspaces.workspace.details.controller:WorkspaceDetailsController
Expand Down Expand Up @@ -66,6 +64,8 @@ export class WorkspaceDetailsController {
private errorMessage: string = '';
private tabsValidationTimeout: ng.IPromise<any>;
private pluginRegistry: string;
private TAB: Array<string>;

/**
* There are unsaved changes to apply (with restart) when is't <code>true</code>.
*/
Expand Down Expand Up @@ -123,7 +123,7 @@ export class WorkspaceDetailsController {
this.originWorkspaceDetails = angular.copy(initData.workspaceDetails);
this.workspaceDetails = angular.copy(initData.workspaceDetails);
this.checkEditMode();

this.TAB = this.workspaceDetails.config ? ['Overview', 'Projects', 'Containers', 'Servers', 'Env_Variables', 'Volumes', 'Config', 'SSH', 'Plugins', 'Editors'] : ['Overview', 'Projects', 'Plugins', 'Editors', 'Devfile'];
this.updateTabs();

this.updateSelectedTab(this.$location.search().tab);
Expand Down Expand Up @@ -182,7 +182,7 @@ export class WorkspaceDetailsController {
*/
updateTabs(): void {
this.tab = {};
TAB.forEach((tab: string, $index: number) => {
this.TAB.forEach((tab: string, $index: number) => {
const index = $index.toString();
this.tab[tab] = index;
this.tab[index] = tab;
Expand Down Expand Up @@ -271,7 +271,6 @@ export class WorkspaceDetailsController {
if (angular.equals(this.workspaceDetails.config, config)) {
return;
}

if (this.newName !== config.name) {
this.newName = config.name;
}
Expand Down Expand Up @@ -300,6 +299,36 @@ export class WorkspaceDetailsController {
}
}

/**
* Callback when workspace devfile has been changed in editor.
*
* @param devfile workspace devfile
*/
updateWorkspaceDevfile(devfile: any): void {
if (!devfile) {
return;
}
if (angular.equals(this.workspaceDetails.devfile, devfile)) {
return;
}

if (this.newName !== devfile.name) {
this.newName = devfile.name;
}

this.workspaceDetails.devfile = devfile;


if (!this.originWorkspaceDetails || !this.workspaceDetails) {
return;
}

this.workspaceDetailsService.publishWorkspaceChange(this.workspaceDetails);

let runningWorkspace = this.getWorkspaceStatus() === WorkspaceStatus[WorkspaceStatus.STARTING] || this.getWorkspaceStatus() === WorkspaceStatus[WorkspaceStatus.RUNNING];
this.saveConfigChanges(false, runningWorkspace);
}

/**
* This method checks form validity on each tab and returns <code>true</code> if
* all forms are valid.
Expand Down Expand Up @@ -391,7 +420,7 @@ export class WorkspaceDetailsController {
}

return this.tabsValidationTimeout = this.$timeout(() => {
const configIsDiffer = !angular.equals(this.originWorkspaceDetails.config, this.workspaceDetails.config);
const configIsDiffer = this.originWorkspaceDetails.config ? !angular.equals(this.originWorkspaceDetails.config, this.workspaceDetails.config) : !angular.equals(this.originWorkspaceDetails.devfile, this.workspaceDetails.devfile);

// the workspace should be restarted only if its status is STARTING or RUNNING
if (this.getWorkspaceStatus() === WorkspaceStatus[WorkspaceStatus.STARTING] || this.getWorkspaceStatus() === WorkspaceStatus[WorkspaceStatus.RUNNING]) {
Expand Down Expand Up @@ -467,7 +496,8 @@ export class WorkspaceDetailsController {

if (refreshPage) {
return this.cheWorkspace.fetchWorkspaceDetails(this.originWorkspaceDetails.id).then(() => {
this.$location.path('/workspace/' + this.namespaceId + '/' + this.workspaceDetails.config.name).search({tab: this.tab[this.selectedTabIndex]});
let name = this.cheWorkspace.getWorkspaceDataManager().getName(this.workspaceDetails);
this.$location.path('/workspace/' + this.namespaceId + '/' + name).search({tab: this.tab[this.selectedTabIndex]});
});
}
})
Expand Down
Loading

0 comments on commit e0c361d

Please sign in to comment.