@@ -35,36 +35,33 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
3535
3636 process . env . DEBIAN_FRONTEND = "noninteractive"
3737
38- const allRepositories = [ ...new Set ( packages . flatMap ( ( pack ) => pack . repositories ?? [ ] ) ) ]
39-
40- if ( allRepositories . length !== 0 ) {
41- for ( const repo of allRepositories ) {
42- // eslint-disable-next-line no-await-in-loop
43- execRootSync ( "add-apt-repository" , [ "-y" , repo ] )
44- }
45-
46- updateRepos ( apt )
47- }
38+ // Add the repos if needed
39+ await addRepositories ( apt , packages )
4840
41+ // Qualify the packages into full package name/version
4942 let qualifiedPacks = await Promise . all ( packages . map ( ( pack ) => getAptArg ( pack . name , pack . version ) ) )
5043
5144 // find the packages that are not installed
5245 qualifiedPacks = await Promise . all ( qualifiedPacks . filter ( async ( pack ) => ! ( await isPackageInstalled ( pack ) ) ) )
5346
5447 if ( qualifiedPacks . length === 0 ) {
48+ info ( "All packages are already installed" )
5549 return { binDir : "/usr/bin/" }
5650 }
5751
52+ // Update the repos if needed
5853 if ( ! didUpdate || update ) {
5954 updateRepos ( apt )
6055 didUpdate = true
6156 }
6257
58+ // Initialize apt if needed
6359 if ( ! didInit ) {
6460 await initApt ( apt )
6561 didInit = true
6662 }
6763
64+ // Install
6865 try {
6966 execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , ...qualifiedPacks ] )
7067 } catch ( err ) {
@@ -89,6 +86,23 @@ export enum AptPackageType {
8986 None = 3 ,
9087}
9188
89+ async function addRepositories ( apt : string , packages : AptPackage [ ] ) {
90+ const allRepositories = [ ...new Set ( packages . flatMap ( ( pack ) => pack . repositories ?? [ ] ) ) ]
91+ if ( allRepositories . length !== 0 ) {
92+ if ( ! didInit ) {
93+ await initApt ( apt )
94+ didInit = true
95+ }
96+ await installAddAptRepo ( )
97+ for ( const repo of allRepositories ) {
98+ // eslint-disable-next-line no-await-in-loop
99+ execRootSync ( "add-apt-repository" , [ "-y" , repo ] )
100+ }
101+ updateRepos ( apt )
102+ didUpdate = true
103+ }
104+ }
105+
92106export async function aptPackageType ( name : string , version : string | undefined ) : Promise < AptPackageType > {
93107 if ( version !== undefined && version !== "" ) {
94108 const { stdout } = await execa ( "apt-cache" , [
@@ -121,6 +135,13 @@ export async function aptPackageType(name: string, version: string | undefined):
121135 // ignore
122136 }
123137
138+ // If apt-cache fails, update the repos and try again
139+ if ( ! didUpdate ) {
140+ updateRepos ( getApt ( ) )
141+ didUpdate = true
142+ return aptPackageType ( name , version )
143+ }
144+
124145 return AptPackageType . None
125146}
126147
@@ -156,17 +177,27 @@ function updateRepos(apt: string) {
156177 execRootSync ( apt , apt !== "nala" ? [ "update" , "-y" ] : [ "update" ] )
157178}
158179
159- /** Install apt utils and certificates (usually missing from docker containers) */
180+ async function installAddAptRepo ( ) {
181+ if ( await isPackageInstalled ( "software-properties-common" ) ) {
182+ return
183+ }
184+ execRootSync ( "apt-get" , [ "install" , "-y" , "software-properties-common" ] )
185+ }
186+
187+ /** Install gnupg and certificates (usually missing from docker containers) */
160188async function initApt ( apt : string ) {
161- execRootSync ( apt , [
162- "install" ,
163- "--fix-broken" ,
164- "-y" ,
165- "software-properties-common" ,
166- "apt-utils" ,
167- "ca-certificates" ,
168- "gnupg" ,
169- ] )
189+ // Update the repos if needed
190+ if ( ! didUpdate ) {
191+ updateRepos ( apt )
192+ didUpdate = true
193+ }
194+
195+ if ( ! ( await isPackageInstalled ( "ca-certificates" ) ) ) {
196+ execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , "ca-certificates" ] )
197+ }
198+ if ( ! ( await isPackageInstalled ( "gnupg" ) ) ) {
199+ execRootSync ( apt , [ "install" , "--fix-broken" , "-y" , "gnupg" ] )
200+ }
170201 const promises : Promise < string | void > [ ] = [
171202 addAptKeyViaServer ( [ "3B4FE6ACC0B21F32" , "40976EAF437D05B5" ] , "setup-cpp-ubuntu-archive.gpg" ) ,
172203 addAptKeyViaServer ( [ "1E9377A2BA9EF27F" ] , "launchpad-toolchain.gpg" ) ,
0 commit comments