@@ -11,7 +11,7 @@ import { isURL } from 'validator';
1111import { EventEmitter } from 'events' ;
1212import * as YAML from 'yaml'
1313import OpenShiftItem , { selectTargetApplication , selectTargetComponent } from './openshiftItem' ;
14- import { OpenShiftObject , ContextType , OpenShiftObjectImpl , OpenShiftComponent } from '../odo' ;
14+ import { OpenShiftObject , ContextType , OpenShiftObjectImpl , OpenShiftComponent , OpenShiftApplication } from '../odo' ;
1515import { Command } from '../odo/command' ;
1616import { Progress } from '../util/progress' ;
1717import { CliExitData } from '../cli' ;
@@ -24,14 +24,15 @@ import LogViewLoader from '../view/log/LogViewLoader';
2424import DescribeViewLoader from '../view/describe/describeViewLoader' ;
2525import { vsCommand , VsCommandError } from '../vscommand' ;
2626import { SourceType } from '../odo/config' ;
27- import { ComponentKind , ComponentTypeAdapter } from '../odo/componentType' ;
27+ import { ComponentKind , ComponentTypeAdapter , DevfileComponentType , ImageStreamTag , isDevfileComponent , isImageStreamTag } from '../odo/componentType' ;
2828import { Url } from '../odo/url' ;
2929import { ComponentDescription , StarterProjectDescription } from '../odo/catalog' ;
3030import path = require( 'path' ) ;
3131
3232import globby = require( 'globby' ) ;
3333import treeKill = require( 'tree-kill' ) ;
3434import fs = require( 'fs-extra' ) ;
35+ import { isStarterProject , StarterProject } from '../odo/componentTypeDescription' ;
3536
3637const waitPort = require ( 'wait-port' ) ;
3738
@@ -106,7 +107,7 @@ export class Component extends OpenShiftItem {
106107 @selectTargetApplication (
107108 'In which Application you want to create a Component'
108109 )
109- static async create ( application : OpenShiftObject ) : Promise < string > {
110+ static async create ( application : OpenShiftApplication ) : Promise < string > {
110111 if ( ! application ) return null ;
111112
112113 const componentSource = await window . showQuickPick ( SourceTypeChoice . asArray ( ) , {
@@ -541,16 +542,37 @@ export class Component extends OpenShiftItem {
541542 return JSON . parse ( UrlDetails . stdout ) . items ;
542543 }
543544
545+ @vsCommand ( 'openshift.componentType.newComponent' )
546+ public static async createComponentFromCatalogEntry ( context : DevfileComponentType | StarterProject | ImageStreamTag ) : Promise < string > {
547+ const application = await Component . getOpenShiftCmdData ( undefined ,
548+ 'Select an Application where you want to create a Component'
549+ ) ;
550+ let componentTypeName : string ,
551+ version : string ,
552+ starterProjectName :string ;
553+ if ( isDevfileComponent ( context ) ) {
554+ componentTypeName = context . Name ;
555+ } else if ( isImageStreamTag ( context ) ) {
556+ componentTypeName = context . typeName ;
557+ version = context . name ;
558+ } else if ( isStarterProject ( context ) ) {
559+ componentTypeName = context . typeName ;
560+ starterProjectName = context . name ;
561+ }
562+
563+ return Component . createFromLocal ( application , [ ] , componentTypeName , version , starterProjectName ) ;
564+ }
565+
544566 @vsCommand ( 'openshift.component.createFromLocal' )
545567 @selectTargetApplication (
546568 'Select an Application where you want to create a Component'
547569 )
548- static async createFromLocal ( application : OpenShiftObject ) : Promise < string | null > {
570+ static async createFromLocal ( application : OpenShiftApplication , selection ?: OpenShiftObject [ ] , componentTypeName ?: string , version ?: string , starterProjectName ?: string ) : Promise < string | null > {
549571 if ( ! application ) return null ;
550572 const workspacePath = await selectWorkspaceFolder ( ) ;
551573 if ( ! workspacePath ) return null ;
552574
553- return Component . createFromRootWorkspaceFolder ( workspacePath , [ ] , application ) ;
575+ return Component . createFromRootWorkspaceFolder ( workspacePath , [ ] , application , componentTypeName , version ? ComponentKind . S2I : ComponentKind . DEVFILE , version , starterProjectName ) ;
554576 }
555577
556578 /**
@@ -566,15 +588,19 @@ export class Component extends OpenShiftItem {
566588 */
567589
568590 @vsCommand ( 'openshift.component.createFromRootWorkspaceFolder' )
569- static async createFromRootWorkspaceFolder ( folder : Uri , selection : Uri [ ] , context : OpenShiftObject , componentTypeName ?: string , componentKind = ComponentKind . DEVFILE ) : Promise < string | null > {
591+ static async createFromRootWorkspaceFolder ( folder : Uri , selection : Uri [ ] , context : OpenShiftApplication , componentTypeName ?: string , componentKind = ComponentKind . DEVFILE , version ?: string , starterProjectName ?: string ) : Promise < string | null > {
570592
571593 const application = await Component . getOpenShiftCmdData ( context ,
572594 'Select an Application where you want to create a Component'
573595 ) ;
574596
575597 if ( ! application ) return null ;
598+
599+ let useExistingDevfile = false ;
576600 const devFileLocation = path . join ( folder . fsPath , 'devfile.yaml' ) ;
577- const useExistingDevfile = fs . existsSync ( devFileLocation ) ;
601+ if ( componentKind === ComponentKind . DEVFILE ) {
602+ useExistingDevfile = fs . existsSync ( devFileLocation ) ;
603+ }
578604
579605 let initialNameValue : string ;
580606 if ( useExistingDevfile ) {
@@ -597,9 +623,9 @@ export class Component extends OpenShiftItem {
597623 let createStarter : string ;
598624 let componentType : ComponentTypeAdapter ;
599625 if ( ! useExistingDevfile ) {
600- const componentTypes = Component . odo . getComponentTypes ( ) ;
626+ const componentTypes = await Component . odo . getComponentTypes ( ) ;
601627 if ( componentTypeName ) {
602- componentType = ( await componentTypes ) . find ( type => type . name === componentTypeName && type . kind === componentKind ) ;
628+ componentType = componentTypes . find ( type => type . name === componentTypeName && type . kind === componentKind && ( ! version || type . version === version ) ) ;
603629 }
604630 if ( ! componentType ) {
605631 componentType = await window . showQuickPick ( componentTypes , { placeHolder : 'Component type' , ignoreFocusOut : true } ) ;
@@ -611,23 +637,27 @@ export class Component extends OpenShiftItem {
611637 const globbyPath = `${ folder . fsPath . replace ( '\\' , '/' ) } /` ;
612638 const paths = globby . sync ( `${ globbyPath } *` , { dot : true , onlyFiles : false } ) ;
613639 if ( paths . length === 0 ) {
614- const descr = await Component . odo . execute ( Command . describeCatalogComponent ( componentType . name ) ) ;
615- const starterProjects : StarterProjectDescription [ ] = Component . odo . loadItems < StarterProjectDescription > ( descr , ( data :{ Data :ComponentDescription } ) => data . Data . starterProjects ) ;
616- if ( starterProjects ?. length && starterProjects ?. length > 0 ) {
617- const create = await window . showQuickPick ( [ 'Yes' , 'No' ] , { placeHolder : `Initialize Component using ${ starterProjects . length === 1 ? '\'' . concat ( starterProjects [ 0 ] . name . concat ( '\' ' ) ) : '' } Starter Project?` } ) ;
618- if ( create === 'Yes' ) {
619- if ( starterProjects . length === 1 ) {
620- createStarter = starterProjects [ 0 ] . name ;
621- } else {
622- const selectedStarter = await window . showQuickPick (
623- starterProjects . map ( prj => ( { label : prj . name , description : prj . description } ) ) ,
624- { placeHolder : 'Select Starter Project to initialize Component' }
625- ) ;
626- if ( ! selectedStarter ) return null ;
627- createStarter = selectedStarter . label ;
640+ if ( starterProjectName ) {
641+ createStarter = starterProjectName ;
642+ } else {
643+ const descr = await Component . odo . execute ( Command . describeCatalogComponent ( componentType . name ) ) ;
644+ const starterProjects : StarterProjectDescription [ ] = Component . odo . loadItems < StarterProjectDescription > ( descr , ( data :{ Data :ComponentDescription } ) => data . Data . starterProjects ) ;
645+ if ( starterProjects ?. length && starterProjects ?. length > 0 ) {
646+ const create = await window . showQuickPick ( [ 'Yes' , 'No' ] , { placeHolder : `Initialize Component using ${ starterProjects . length === 1 ? '\'' . concat ( starterProjects [ 0 ] . name . concat ( '\' ' ) ) : '' } Starter Project?` } ) ;
647+ if ( create === 'Yes' ) {
648+ if ( starterProjects . length === 1 ) {
649+ createStarter = starterProjects [ 0 ] . name ;
650+ } else {
651+ const selectedStarter = await window . showQuickPick (
652+ starterProjects . map ( prj => ( { label : prj . name , description : prj . description } ) ) ,
653+ { placeHolder : 'Select Starter Project to initialize Component' }
654+ ) ;
655+ if ( ! selectedStarter ) return null ;
656+ createStarter = selectedStarter . label ;
657+ }
658+ } else if ( ! create ) {
659+ return null ;
628660 }
629- } else if ( ! create ) {
630- return null ;
631661 }
632662 }
633663 }
@@ -638,8 +668,8 @@ export class Component extends OpenShiftItem {
638668 `Creating new Component '${ componentName } '` ,
639669 ( ) => Component . odo . createComponentFromFolder (
640670 application ,
641- componentType ? componentType . name : undefined ,
642- componentType ? componentType . version : undefined ,
671+ componentType . name ,
672+ componentType . version ,
643673 componentName ,
644674 folder ,
645675 createStarter ,
0 commit comments