@@ -30,48 +30,94 @@ export const getDotnetRootPath = () => {
3030 return path . join ( Bun . env . HOME , '.dotnet' )
3131}
3232
33- export type DotnetSdk = {
33+ export type DotnetSdkVersion = {
3434 major : number
3535 minor : number
3636 patch : number
3737}
3838
39- export type DotnetRuntime = {
39+ export type DotnetRuntimeVersion = {
4040 major : number
4141 minor : number
4242 patch : number
43+ type : 'net' | 'aspnetcore' | 'netcoreapp'
4344}
4445
45- export const getInstalledSdks = async ( ) : Promise < DotnetSdk [ ] > => {
46+ export const getInstalledSdkVersions = async ( ) : Promise < DotnetSdkVersion [ ] > => {
4647 const sdkPath = path . join ( getDotnetRootPath ( ) , 'sdk' )
4748
4849 const dirs = await getDirectories ( sdkPath )
4950
5051 return dirs . map ( dir => {
51- const split = dir
52+ const split = dir . name
5253 . split ( '.' )
5354 . map ( n => parseInt ( n ) )
5455
5556 return {
5657 major : split [ 0 ] ,
5758 minor : split [ 1 ] ,
58- patch : split [ 2 ]
59- } as DotnetSdk
59+ patch : split [ 2 ] ,
60+ path : dir . path
61+ } as DotnetSdkVersion
6062 } )
6163}
6264
63- const getDirectories = async ( source : string ) =>
65+ export const getInstalledRuntimeVersions = async ( ) : Promise < DotnetSdkVersion [ ] > => {
66+ const netcoreRuntimePath = path . join ( getDotnetRootPath ( ) , 'shared' , 'Microsoft.NETCore.App' )
67+ const aspnetCoreAllPath = path . join ( getDotnetRootPath ( ) , 'shared' , 'Microsoft.AspNetCore.All' ) // old ASPNET Core 2.1 stuff
68+ const aspnetCoreAppPath = path . join ( getDotnetRootPath ( ) , 'shared' , 'Microsoft.AspNetCore.App' )
69+
70+ const dirs = ( await getDirectories ( netcoreRuntimePath ) )
71+ . concat ( ( await getDirectories ( aspnetCoreAllPath ) ) )
72+ . concat ( ( await getDirectories ( aspnetCoreAppPath ) ) )
73+
74+ return dirs . map ( dir => {
75+ const versionSplit = dir . name
76+ . split ( '.' )
77+ . map ( n => parseInt ( n ) )
78+
79+ return {
80+ major : versionSplit [ 0 ] ,
81+ minor : versionSplit [ 1 ] ,
82+ patch : versionSplit [ 2 ] ,
83+ type : dir . path . startsWith ( netcoreRuntimePath ) ? 'net'
84+ : dir . path . startsWith ( aspnetCoreAppPath ) ? 'aspnetcore'
85+ : dir . path . startsWith ( aspnetCoreAllPath ) ? 'netcoreapp' : null ,
86+ path : dir . path
87+ } as DotnetRuntimeVersion
88+ } )
89+ }
90+
91+ const getDirectories = async ( source : string ) : Promise < Directory [ ] > =>
6492 ( await readdir ( source , { withFileTypes : true } ) )
6593 . filter ( dirent => dirent . isDirectory ( ) )
66- . map ( dirent => dirent . name )
94+ . map ( dirent => ( {
95+ name : dirent . name ,
96+ path : path . resolve ( source , dirent . name )
97+ } ) as Directory )
98+
99+ type Directory = {
100+ name : string
101+ path : string
102+ }
67103
104+ export const syncSdkVersion = async ( sdk : DotnetSdkVersion ) => {
105+ await syncSdkChannel ( `${ sdk . major } .${ sdk . minor } ` )
106+ }
107+
108+ export const getChannelFromVersion = ( version : DotnetSdkVersion | DotnetRuntimeVersion ) =>
109+ `${ version . major } .${ version . minor } `
110+
111+ export const syncSdkChannel = async ( channel : string ) => {
112+ if ( ! ( await hasInstallScript ( ) ) ) {
113+ await fetchInstallScript ( )
114+ }
68115
69- export const syncSdk = async ( sdk : DotnetSdk ) => {
70116 const proc = Bun . spawn ( [
71117 'bash' ,
72118 getInstallScriptPath ( ) ,
73119 '--channel' ,
74- ` ${ sdk . major } . ${ sdk . minor } `
120+ channel
75121 ] , {
76122 stdout : 'inherit' ,
77123 stderr : 'inherit' ,
@@ -88,12 +134,19 @@ export const syncSdk = async (sdk: DotnetSdk) => {
88134 }
89135}
90136
91- export const syncRuntime = async ( runtime : DotnetRuntime ) => {
137+ export const syncRuntime = async ( runtime : DotnetRuntimeVersion ) =>
138+ await syncRuntimeChannel ( `${ runtime . major } /${ runtime . minor } ` )
139+
140+ export const syncRuntimeChannel = async ( channel : string ) => {
141+ if ( ! ( await hasInstallScript ( ) ) ) {
142+ await fetchInstallScript ( )
143+ }
144+
92145 const proc = Bun . spawn ( [
93146 'bash' ,
94147 getInstallScriptPath ( ) ,
95148 '--channel' ,
96- ` ${ runtime . major } . ${ runtime . minor } ` ,
149+ channel ,
97150 '--runtime'
98151 ] , {
99152 stdout : 'inherit' ,
0 commit comments