11import {
22 CreatePlan ,
33 DestroyPlan ,
4+ getPty ,
45 ModifyPlan ,
56 ParameterChange ,
6- RefreshContext ,
77 Resource ,
8- ResourceSettings ,
9- getPty
8+ ResourceSettings
109} from 'codify-plugin-lib' ;
1110import { ResourceConfig } from 'codify-schemas' ;
1211
@@ -32,24 +31,20 @@ export class PipResource extends Resource<PipResourceConfig> {
3231 type : 'array' ,
3332 itemType : 'object' ,
3433 canModify : true ,
35- isElementEqual ( desired : PipListResult | string , current : PipListResult | string ) {
36- if ( typeof desired === 'string' && typeof current === 'string' ) {
37- return desired === current ;
38- }
39-
40- // We can do this check because of the pre-filtering we are doing in refresh. It converts the current to match the desired if it is defined.
41- return ( desired as PipListResult ) . name === ( current as PipListResult ) . name ;
42- }
34+ isElementEqual : this . isEqual ,
35+ filterInStatelessMode : ( desired , current ) =>
36+ current . filter ( ( c ) => desired . find ( ( d ) => this . isSame ( c , d ) ) )
4337 } ,
4438 virtualEnv : { type : 'directory' }
4539 } ,
4640 allowMultiple : {
4741 identifyingParameters : [ 'virtualEnv' ]
48- }
42+ } ,
43+ dependencies : [ 'pyenv' , 'git-repository' ]
4944 }
5045 }
5146
52- async refresh ( parameters : Partial < PipResourceConfig > , context : RefreshContext < PipResourceConfig > ) : Promise < Partial < PipResourceConfig > | Partial < PipResourceConfig > [ ] | null > {
47+ async refresh ( parameters : Partial < PipResourceConfig > ) : Promise < Partial < PipResourceConfig > | Partial < PipResourceConfig > [ ] | null > {
5348 const pty = getPty ( )
5449
5550 const { status : pipStatus } = await pty . spawnSafe ( 'which pip' ) ;
@@ -93,14 +88,13 @@ export class PipResource extends Resource<PipResourceConfig> {
9388 return { name, version } ;
9489 } ) ;
9590
96- console . log ( parsedInstalledPackages ) ;
97-
9891 return {
9992 ...parameters ,
10093 install : parsedInstalledPackages ,
10194 }
10295 }
10396
97+ // Pip cannot be individually installed. It's installed via installing python. This only installs packages when python is first created.
10498 async create ( plan : CreatePlan < PipResourceConfig > ) : Promise < void > {
10599 const { install, virtualEnv } = plan . desiredConfig ;
106100
@@ -123,11 +117,8 @@ export class PipResource extends Resource<PipResourceConfig> {
123117 }
124118 }
125119
126- async destroy ( plan : DestroyPlan < PipResourceConfig > ) : Promise < void > {
127- const { install, virtualEnv } = plan . currentConfig ;
128-
129- await this . pipUninstall ( install , virtualEnv ) ;
130- }
120+ // Pip cannot be individually destroyed.
121+ async destroy ( plan : DestroyPlan < PipResourceConfig > ) : Promise < void > { }
131122
132123 private async pipInstall ( packages : Array < PipListResult | string > , virtualEnv ?: string ) : Promise < void > {
133124 const packagesToInstall = packages . map ( ( p ) => {
@@ -159,29 +150,43 @@ export class PipResource extends Resource<PipResourceConfig> {
159150
160151 await codifySpawn (
161152 ( virtualEnv ? `source ${ virtualEnv } /bin/activate; ` : '' )
162- + `pip install ${ packagesToUninstall . join ( ' ' ) } `
153+ + `pip uninstall -y ${ packagesToUninstall . join ( ' ' ) } `
163154 )
164155 }
165156
166- findMatchingForModify ( d : PipListResult | string , cList : Array < PipListResult | string > ) : PipListResult | string | undefined {
167- return cList . find ( ( c ) => {
168- if ( typeof d === 'string' && typeof c === 'string' ) {
169- return d === c ;
170- }
157+ findMatchingForModify ( a : PipListResult | string , bList : Array < PipListResult | string > ) : PipListResult | string | undefined {
158+ return bList . find ( ( b ) => this . isEqual ( a , b ) )
159+ }
171160
172- if ( ! ( typeof d === 'object' && typeof c === 'object' ) ) {
173- return false ;
174- }
161+ isEqual ( a : PipListResult | string , b : PipListResult | string ) : boolean {
162+ if ( typeof a === 'string' && typeof b === 'string' ) {
163+ return a === b ;
164+ }
175165
176- if ( d . name !== c . name ) {
177- return false ;
178- }
166+ if ( ! ( typeof a === 'object' && typeof b === 'object' ) ) {
167+ return false ;
168+ }
179169
180- if ( d . version && d . version !== c . version ) {
181- return false
182- }
170+ if ( a . name !== b . name ) {
171+ return false ;
172+ }
173+
174+ if ( a . version && a . version !== b . version ) {
175+ return false
176+ }
177+
178+ return true ;
179+ }
180+
181+ isSame ( a : PipListResult | string , b : PipListResult | string ) : boolean {
182+ if ( typeof a === 'string' && typeof b === 'string' ) {
183+ return a === b ;
184+ }
185+
186+ if ( ! ( typeof a === 'object' && typeof b === 'object' ) ) {
187+ return false ;
188+ }
183189
184- return true ;
185- } )
190+ return a . name === b . name ;
186191 }
187192}
0 commit comments