@@ -29,6 +29,7 @@ import {
2929} from '../common-ext/createComponentHelpers' ;
3030import { loadWebviewHtml , validateGitURL } from '../common-ext/utils' ;
3131import { Devfile , DevfileRegistry , TemplateProjectIdentifier } from '../common/devfile' ;
32+ import { DevfileV1 } from '../../util/devfileV1Type' ;
3233
3334interface CloneProcess {
3435 status : boolean ;
@@ -281,11 +282,19 @@ export default class CreateComponentLoader {
281282 action : 'cloneFailed' ,
282283 } ) ;
283284 } else {
285+ const isGirDevfileExists = await isDevfileExists ( tmpFolder ) ;
284286 void CreateComponentLoader . panel . webview . postMessage ( {
285287 action : 'devfileExists' ,
286- data : await isDevfileExists ( tmpFolder ) ,
288+ data : isGirDevfileExists ,
287289 } ) ;
288- void CreateComponentLoader . getRecommendedDevfile ( tmpFolder ) ;
290+
291+ if ( isGirDevfileExists ) {
292+ // Use the Devfile existing in Gir-repo
293+ void CreateComponentLoader . getExistingDevfile ( tmpFolder ) ;
294+ } else {
295+ // Use recommended Devfile
296+ void CreateComponentLoader . getRecommendedDevfile ( tmpFolder ) ;
297+ }
289298 }
290299 break ;
291300 }
@@ -295,7 +304,7 @@ export default class CreateComponentLoader {
295304 case 'createComponent' : {
296305 const componentName : string = message . data . componentName ;
297306 const portNumber : number = message . data . portNumber ;
298- let componentFolder : string ;
307+ let componentFolder : string = '' ;
299308 try {
300309 if ( message . data . isFromTemplateProject ) {
301310 // from template project
@@ -337,14 +346,19 @@ export default class CreateComponentLoader {
337346 await fse . copy ( tmpFolder . fsPath , componentFolder ) ;
338347 }
339348 const devfileType = getDevfileType ( message . data . devfileDisplayName ) ;
340- if ( ! await isDevfileExists ( Uri . file ( componentFolder ) ) ) {
349+ const componentFolderUri = Uri . file ( componentFolder ) ;
350+ if ( ! await isDevfileExists ( componentFolderUri ) ) {
341351 await Odo . Instance . createComponentFromLocation (
342352 devfileType ,
343353 componentName ,
344354 portNumber ,
345355 Uri . file ( componentFolder ) ,
346356 ) ;
357+ } else {
358+ // Update component devfile with component'a selected name
359+ await CreateComponentLoader . updateDevfileWithComponentName ( componentFolderUri , componentName ) ;
347360 }
361+
348362 await sendTelemetry ( 'newComponentCreated' , {
349363 strategy,
350364 // eslint-disable-next-line camelcase
@@ -424,6 +438,67 @@ export default class CreateComponentLoader {
424438 }
425439 }
426440
441+ static async updateDevfileWithComponentName ( ucomponentFolderUri : vscode . Uri , componentName : string ) : Promise < void > {
442+ const devFilePath = path . join ( ucomponentFolderUri . fsPath , 'devfile.yaml' ) ;
443+ const file = await fs . readFile ( devFilePath , 'utf8' ) ;
444+ const devfile = JSYAML . load ( file . toString ( ) ) as any ;
445+ if ( devfile ?. metadata ?. name !== componentName ) {
446+ devfile . metadata . name = componentName ;
447+ await fs . unlink ( devFilePath ) ;
448+ const yaml = JSYAML . dump ( devfile , { sortKeys : true } ) ;
449+ await fs . writeFile ( devFilePath , yaml . toString ( ) , 'utf-8' ) ;
450+ }
451+ }
452+
453+ static async getExistingDevfile ( uri : Uri ) : Promise < void > {
454+ const devFileYamlPath = path . join ( tmpFolder . fsPath , 'devfile.yaml' ) ;
455+ let rawDevfile : any ;
456+ let supportsDebug = false ; // Initial value
457+ let supportsDeploy = false ; // Initial value
458+ try {
459+ void CreateComponentLoader . panel . webview . postMessage ( {
460+ action : 'getRecommendedDevfileStart'
461+ } ) ;
462+ const componentDescription = await Odo . Instance . describeComponent ( uri . fsPath ) ;
463+ if ( componentDescription ) {
464+ rawDevfile = componentDescription . devfileData . devfile ;
465+ supportsDebug = componentDescription . devfileData . supportedOdoFeatures . debug ;
466+ supportsDeploy = componentDescription . devfileData . supportedOdoFeatures . deploy ;
467+ }
468+ } catch ( Error ) {
469+ // Will try reading the raw devfile
470+ } finally {
471+ if ( ! rawDevfile ) {
472+ //Try reading the raw devfile
473+ const file = await fs . readFile ( devFileYamlPath , 'utf8' ) ;
474+ rawDevfile = JSYAML . load ( file . toString ( ) ) ;
475+ }
476+
477+ void CreateComponentLoader . panel . webview . postMessage ( {
478+ action : 'getRecommendedDevfile'
479+ } ) ;
480+
481+ const devfile : Devfile = {
482+ description : rawDevfile . metadata . description ,
483+ registryName : devFileYamlPath ,
484+ name : rawDevfile . metadata . displayName ? rawDevfile . metadata . displayName : rawDevfile . metadata . name ,
485+ id : rawDevfile . metadata . name ,
486+ starterProjects : rawDevfile . starterProjects ,
487+ tags : [ ] ,
488+ yaml : JSYAML . dump ( rawDevfile ) ,
489+ supportsDebug,
490+ supportsDeploy,
491+ } as Devfile ;
492+
493+ void CreateComponentLoader . panel . webview . postMessage ( {
494+ action : 'recommendedDevfile' ,
495+ data : {
496+ devfile,
497+ } ,
498+ } ) ;
499+ }
500+ }
501+
427502 static async getRecommendedDevfile ( uri : Uri ) : Promise < void > {
428503 let analyzeRes : AnalyzeResponse [ ] = [ ] ;
429504 let compDescriptions : ComponentTypeDescription [ ] = [ ] ;
@@ -444,7 +519,7 @@ export default class CreateComponentLoader {
444519 try {
445520 const devFileV1Path = path . join ( uri . fsPath , 'devfile.yaml' ) ;
446521 const file = await fs . readFile ( devFileV1Path , 'utf8' ) ;
447- const devfileV1 = JSYAML . load ( file . toString ( ) ) ;
522+ const devfileV1 = JSYAML . load ( file . toString ( ) ) as DevfileV1 ;
448523 await fs . unlink ( devFileV1Path ) ;
449524 analyzeRes = await Odo . Instance . analyze ( uri . fsPath ) ;
450525 compDescriptions = getCompDescription ( analyzeRes ) ;
@@ -475,7 +550,7 @@ export default class CreateComponentLoader {
475550 } ) ;
476551 const devfileRegistry : DevfileRegistry [ ] = getDevfileRegistries ( ) ;
477552 const allDevfiles : Devfile [ ] = devfileRegistry . flatMap ( ( registry ) => registry . devfiles ) ;
478- const devfile : Devfile =
553+ const devfile : Devfile | undefined =
479554 compDescriptions . length !== 0
480555 ? allDevfiles . find (
481556 ( devfile ) => devfile . name === compDescriptions [ 0 ] . displayName ,
@@ -515,7 +590,7 @@ function getDevfileType(devfileDisplayName: string): string {
515590 const devfileDescription : ComponentTypeDescription = Array . from ( compDescriptions ) . find (
516591 ( description ) => description . displayName === devfileDisplayName ,
517592 ) ;
518- return devfileDescription . name ;
593+ return devfileDescription ? devfileDescription . name : devfileDisplayName ;
519594}
520595
521596function getEndPoints ( compDescription : ComponentTypeDescription ) : Endpoint [ ] {
0 commit comments