1- import { CreatePlan , DestroyPlan , Resource , ResourceSettings , untildify , } from 'codify-plugin-lib' ;
1+ import { CreatePlan , DestroyPlan , getPty , Resource , ResourceSettings , untildify , } from 'codify-plugin-lib' ;
22import { ResourceConfig } from 'codify-schemas' ;
33import * as fs from 'node:fs/promises' ;
44import path from 'node:path' ;
@@ -26,12 +26,12 @@ export class AsdfInstallResource extends Resource<AsdfInstallConfig> {
2626 schema : AsdfInstallSchema ,
2727 parameterSettings : {
2828 directory : { type : 'directory' , inputTransformation : ( input ) => untildify ( input ) } ,
29- versions : { type : 'stateful' , definition : new AsdfPluginVersionsParameter ( ) }
29+ versions : { type : 'array' }
3030 } ,
3131 import : {
3232 requiredParameters : [ 'directory' ] ,
3333 refreshKeys : [ 'directory' ]
34- }
34+ } ,
3535 }
3636 }
3737
@@ -50,15 +50,17 @@ export class AsdfInstallResource extends Resource<AsdfInstallConfig> {
5050 }
5151
5252 async refresh ( parameters : Partial < AsdfInstallConfig > ) : Promise < Partial < AsdfInstallConfig > | Partial < AsdfInstallConfig > [ ] | null > {
53- if ( ( await codifySpawn ( 'which asdf' , { throws : false } ) ) . status === SpawnStatus . ERROR ) {
53+ const $ = getPty ( ) ;
54+
55+ if ( ( await $ . spawnSafe ( 'which asdf' ) ) . status === SpawnStatus . ERROR ) {
5456 return null ;
5557 }
5658
5759 if ( parameters . directory ) {
5860 const desiredTools = await this . getToolVersions ( parameters . directory ) ;
5961
6062 for ( const { plugin, version } of desiredTools ) {
61- const { status, data } = await codifySpawn ( `asdf current ${ plugin } ` , { throws : false , cwd : parameters . directory } ) ;
63+ const { status, data } = await $ . spawnSafe ( `asdf current ${ plugin } ` , { cwd : parameters . directory } ) ;
6264 if ( status === SpawnStatus . ERROR || data . trim ( ) === '' ) {
6365 return null ;
6466 }
@@ -74,12 +76,25 @@ export class AsdfInstallResource extends Resource<AsdfInstallConfig> {
7476 } ;
7577 }
7678
77- if ( ( await codifySpawn ( 'which asdf' , { throws : false } ) ) . status === SpawnStatus . ERROR ) {
79+ // Directly check plugin version
80+ const versionsQuery = await $ . spawnSafe ( `asdf list ${ parameters . plugin } ` ) ;
81+ if ( versionsQuery . status === SpawnStatus . ERROR || versionsQuery . data . trim ( ) === 'No versions installed' ) {
7882 return null ;
7983 }
8084
85+ const latest = parameters . versions ?. includes ( 'latest' )
86+ ? ( await codifySpawn ( `asdf latest ${ parameters . plugin } ` ) ) . data . trim ( )
87+ : null ;
88+
89+ const versions = versionsQuery . data . split ( / \n / )
90+ . map ( ( l ) => l . trim ( ) )
91+ . map ( ( l ) => l . replaceAll ( '*' , '' ) )
92+ . map ( ( l ) => l . trim ( ) === latest ? 'latest' : l )
93+ . filter ( Boolean ) ;
94+
8195 return {
8296 plugin : parameters . plugin ,
97+ versions,
8398 }
8499 }
85100
@@ -95,7 +110,10 @@ export class AsdfInstallResource extends Resource<AsdfInstallConfig> {
95110 }
96111
97112 await codifySpawn ( 'asdf install' , { cwd : plan . desiredConfig . directory } ) ;
113+ return ;
98114 }
115+
116+ await codifySpawn ( `asdf install ${ plan . desiredConfig ?. plugin } ${ plan . desiredConfig . versions ?. join ( ' ' ) } ` ) ;
99117 }
100118
101119 async destroy ( plan : DestroyPlan < AsdfInstallConfig > ) : Promise < void > {
@@ -106,7 +124,12 @@ export class AsdfInstallResource extends Resource<AsdfInstallConfig> {
106124 for ( const { plugin, version } of desiredTools ) {
107125 await codifySpawn ( `asdf uninstall ${ plugin } ${ version } ` ) ;
108126 }
127+
128+ return ;
109129 }
130+
131+ // Other path is uninstalled through the stateful parameter
132+ await codifySpawn ( `asdf uninstall ${ plan . currentConfig ?. plugin } ${ plan . currentConfig . versions ?. join ( ' ' ) } ` ) ;
110133 }
111134
112135 private async getToolVersions ( directory : string ) : Promise < Array < { plugin : string ; version : string } > > {
0 commit comments