1- import { CreatePlan , DestroyPlan , Resource , ResourceSettings , SpawnStatus , getPty } from 'codify-plugin-lib' ;
1+ import { CreatePlan , DestroyPlan , Resource , ResourceSettings , SpawnStatus , getPty , FileUtils } from 'codify-plugin-lib' ;
22import { OS , ResourceConfig } from 'codify-schemas' ;
33import fs from 'node:fs/promises' ;
44import os , { homedir } from 'node:os' ;
55import path from 'node:path' ;
66
7- import { FileUtils } from '../../utils/file-utils.js' ;
87import { Utils } from '../../utils/index.js' ;
98import AsdfSchema from './asdf-schema.json' ;
109import { AsdfPluginsParameter } from './plugins-parameter.js' ;
@@ -17,7 +16,7 @@ export class AsdfResource extends Resource<AsdfConfig> {
1716 getSettings ( ) : ResourceSettings < AsdfConfig > {
1817 return {
1918 id : 'asdf' ,
20- operatingSystems : [ OS . Darwin ] ,
19+ operatingSystems : [ OS . Darwin , OS . Linux ] ,
2120 schema : AsdfSchema ,
2221 parameterSettings : {
2322 plugins : { type : 'stateful' , definition : new AsdfPluginsParameter ( ) } ,
@@ -35,25 +34,67 @@ export class AsdfResource extends Resource<AsdfConfig> {
3534 async create ( plan : CreatePlan < AsdfConfig > ) : Promise < void > {
3635 const $ = getPty ( ) ;
3736
38- if ( ! ( await Utils . isHomebrewInstalled ( ) ) ) {
39- throw new Error ( 'Homebrew is not installed. Please install Homebrew before installing asdf.' ) ;
37+ if ( Utils . isMacOS ( ) ) {
38+ if ( ! ( await Utils . isHomebrewInstalled ( ) ) ) {
39+ throw new Error ( 'Homebrew is not installed. Please install Homebrew before installing asdf.' ) ;
40+ }
41+
42+ await $ . spawn ( 'brew install asdf' , { interactive : true , env : { HOMEBREW_NO_AUTO_UPDATE : 1 } } ) ;
43+
4044 }
4145
42- await $ . spawn ( 'brew install asdf' , { interactive : true , env : { HOMEBREW_NO_AUTO_UPDATE : 1 } } ) ;
46+ if ( Utils . isLinux ( ) ) {
47+ const { data : latestVersion } = await $ . spawn ( 'curl -s https://api.github.com/repos/asdf-vm/asdf/releases/latest | grep \'"tag_name":\' | sed -E \'s/.*"([^"]+)".*/\\1/\'' ) ;
4348
44- await FileUtils . addAllToStartupFile ( [
45- 'export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"' ,
46- ] ) ;
47- }
49+ // Create .asdf directory if it doesn't exist
50+ const asdfDir = path . join ( os . homedir ( ) , '.local' , 'bin' ) ;
51+ await fs . mkdir ( asdfDir , { recursive : true } ) ;
52+ const tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , 'codify-asdf' ) ) ;
53+ const arch = Utils . isLinux ( ) ? 'amd64' : 'arm64' ;
4854
49- async destroy ( plan : DestroyPlan < AsdfConfig > ) : Promise < void > {
50- if ( ! ( await Utils . isHomebrewInstalled ( ) ) ) {
51- return ;
55+ // Download and extract asdf
56+ await $ . spawn ( `curl -Lo ${ tmpDir } /asdf.tar.gz "https://github.com/asdf-vm/asdf/releases/download/${ latestVersion } /asdf-${ latestVersion } -linux-${ arch } .tar.gz"` , { cwd : tmpDir } ) ;
57+ console . log ( await $ . spawn ( 'ls -la' , { cwd : tmpDir } ) ) ;
58+ await $ . spawn ( `tar -xzf ${ tmpDir } /asdf.tar.gz -C ${ asdfDir } ` , { cwd : tmpDir } ) ;
59+ await fs . chmod ( path . join ( asdfDir , 'asdf' ) , 0o755 ) ;
60+
61+ await fs . rm ( tmpDir , { recursive : true , force : true } ) ;
5262 }
5363
64+ await FileUtils . addPathToShellRc ( path . join ( os . homedir ( ) , '.local' , 'bin' ) , true ) ;
65+ // eslint-disable-next-line no-template-curly-in-string
66+ await FileUtils . addToShellRc ( 'export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"' )
67+
68+ // TODO: Add filtering to resources for operating systems
69+ // os parameter
70+ // TODO: Move all utils to codify-plugin-lib
71+ // TODO: Move OsUtils to a separate name space? All things that have to do with the os.
72+ // TODO: Add a way to run multiple commands in sequence
73+ // TODO: Add a easier way to make sure path is on the path
74+ // TODO: Change all plugins to install to ~/.local/bin
75+
76+ await $ . spawnSafe ( 'which asdf' , { interactive : true } ) ;
77+ await $ . spawnSafe ( 'ls ~/.local/bin' ) ;
78+ console . log ( ( await $ . spawnSafe ( 'echo $PATH' , { interactive : true } ) ) . data ) ;
79+
80+ }
81+
82+ async destroy ( ) : Promise < void > {
5483 const $ = getPty ( ) ;
55- await $ . spawn ( 'brew uninstall asdf' , { interactive : true , env : { HOMEBREW_NO_AUTO_UPDATE : 1 } } ) ;
56- await FileUtils . removeLineFromStartupFile ( 'export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"' )
84+
85+ const asdfDir = ( await $ . spawn ( 'which asdf' , { interactive : true } ) ) . data ;
86+ if ( Utils . isMacOS ( ) && asdfDir . includes ( 'homebrew' ) ) {
87+ if ( ! ( await Utils . isHomebrewInstalled ( ) ) ) {
88+ return ;
89+ }
90+
91+ await $ . spawn ( 'brew uninstall asdf' , { interactive : true , env : { HOMEBREW_NO_AUTO_UPDATE : 1 } } ) ;
92+ } else {
93+ await fs . rm ( asdfDir , { recursive : true , force : true } ) ;
94+ }
95+
96+ // eslint-disable-next-line no-template-curly-in-string
97+ await FileUtils . removeLineFromShellRc ( 'export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"' )
5798 await fs . rm ( path . join ( os . homedir ( ) , '.asdf' ) , { recursive : true , force : true } ) ;
5899 }
59100
0 commit comments