@@ -5,7 +5,7 @@ import { error, warning } from "ci-log"
5
5
import escapeSpace from "escape-path-with-spaces"
6
6
import escapeQuote from "escape-quotes"
7
7
import { execPowershell } from "exec-powershell"
8
- import { appendFileSync , readFile , readFileSync , writeFileSync } from "fs"
8
+ import { appendFile , readFile , writeFile } from "fs/promises "
9
9
import { delimiter } from "path"
10
10
import { pathExists } from "path-exists"
11
11
import { untildifyUser } from "untildify-user"
@@ -121,12 +121,12 @@ async function addEnvSystem(name: string, valGiven: string | undefined, options:
121
121
}
122
122
case "linux" :
123
123
case "darwin" : {
124
- await setupCppInProfile ( )
124
+ await sourceCpprc ( )
125
125
if ( options . shouldAddOnlyIfNotDefined ) {
126
- appendFileSync ( cpprc_path , `\nif [ -z "\${${ name } }" ]; then export ${ name } ="${ val } "; fi\n` )
126
+ await appendFile ( cpprc_path , `\nif [ -z "\${${ name } }" ]; then export ${ name } ="${ val } "; fi\n` )
127
127
info ( `if not defined ${ name } then ${ name } ="${ val } " was added to "${ cpprc_path } ` )
128
128
} else {
129
- appendFileSync ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
129
+ await appendFile ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
130
130
info ( `${ name } ="${ val } " was added to "${ cpprc_path } ` )
131
131
}
132
132
return
@@ -150,8 +150,8 @@ async function addPathSystem(path: string) {
150
150
}
151
151
case "linux" :
152
152
case "darwin" : {
153
- await setupCppInProfile ( )
154
- appendFileSync ( cpprc_path , `\nexport PATH="${ path } :$PATH"\n` )
153
+ await sourceCpprc ( )
154
+ await appendFile ( cpprc_path , `\nexport PATH="${ path } :$PATH"\n` )
155
155
info ( `"${ path } " was added to "${ cpprc_path } "` )
156
156
return
157
157
}
@@ -165,62 +165,70 @@ async function addPathSystem(path: string) {
165
165
let setupCppInProfile_called = false
166
166
167
167
/// handles adding conditions to source .cpprc file from .bashrc and .profile
168
- export async function setupCppInProfile ( ) {
168
+ export async function sourceCpprc ( ) {
169
169
if ( setupCppInProfile_called ) {
170
170
return
171
171
}
172
172
173
+ const source_cpprc_string =
174
+ `\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${ cpprc_path } " ]]; then source "${ cpprc_path } "; fi\n`
175
+
176
+ try {
177
+ await Promise . all ( [
178
+ addCpprcHeader ( ) ,
179
+ sourceCpprcInProfile ( source_cpprc_string ) ,
180
+ sourceCpprcInBashrc ( source_cpprc_string ) ,
181
+ ] )
182
+ } catch ( err ) {
183
+ warning ( `Failed to add ${ source_cpprc_string } to .profile or .bashrc. You should add it manually: ${ err } ` )
184
+ }
185
+
186
+ setupCppInProfile_called = true
187
+ }
188
+
189
+ async function addCpprcHeader ( ) {
173
190
// a variable that prevents source_cpprc from being called from .bashrc and .profile
174
- const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
191
+ const cpprc_header = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
175
192
176
193
if ( await pathExists ( cpprc_path ) ) {
177
- const cpprc_content = readFileSync ( cpprc_path , "utf8" )
178
- if ( ! cpprc_content . includes ( source_cpprc_str ) ) {
194
+ const cpprc_content = await readFile ( cpprc_path , "utf8" )
195
+ if ( ! cpprc_content . includes ( cpprc_header ) ) {
179
196
// already executed setupCppInProfile
180
- appendFileSync ( cpprc_path , `\n${ source_cpprc_str } \n` )
181
- info ( `Added ${ source_cpprc_str } to ${ cpprc_path } ` )
197
+ await appendFile ( cpprc_path , `\n${ cpprc_header } \n` )
198
+ info ( `Added ${ cpprc_header } to ${ cpprc_path } ` )
182
199
}
183
200
}
201
+ }
184
202
185
- // source cpprc in bashrc/profile
186
-
187
- const source_cpprc_string =
188
- `\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${ cpprc_path } " ]]; then source "${ cpprc_path } "; fi\n`
189
-
190
- try {
191
- // source cpprc in .profile
192
- const profile_path = untildifyUser ( "~/.profile" )
193
- if ( await pathExists ( profile_path ) ) {
194
- const profileContent = readFileSync ( profile_path , "utf-8" )
195
- if ( ! profileContent . includes ( source_cpprc_string ) ) {
196
- appendFileSync ( profile_path , source_cpprc_string )
197
- info ( `${ source_cpprc_string } was added to ${ profile_path } ` )
198
- }
203
+ async function sourceCpprcInBashrc ( source_cpprc_string : string ) {
204
+ const bashrc_path = untildifyUser ( "~/.bashrc" )
205
+ if ( await pathExists ( bashrc_path ) ) {
206
+ const bashrcContent = await readFile ( bashrc_path , "utf-8" )
207
+ if ( ! bashrcContent . includes ( source_cpprc_string ) ) {
208
+ await appendFile ( bashrc_path , source_cpprc_string )
209
+ info ( `${ source_cpprc_string } was added to ${ bashrc_path } ` )
199
210
}
211
+ }
212
+ }
200
213
201
- // source cpprc in .bashrc too
202
- const bashrc_path = untildifyUser ( "~/.bashrc" )
203
- if ( await pathExists ( bashrc_path ) ) {
204
- const bashrcContent = readFileSync ( bashrc_path , "utf-8" )
205
- if ( ! bashrcContent . includes ( source_cpprc_string ) ) {
206
- appendFileSync ( bashrc_path , source_cpprc_string )
207
- info ( `${ source_cpprc_string } was added to ${ bashrc_path } ` )
208
- }
214
+ async function sourceCpprcInProfile ( source_cpprc_string : string ) {
215
+ const profile_path = untildifyUser ( "~/.profile" )
216
+ if ( await pathExists ( profile_path ) ) {
217
+ const profileContent = await readFile ( profile_path , "utf-8" )
218
+ if ( ! profileContent . includes ( source_cpprc_string ) ) {
219
+ await appendFile ( profile_path , source_cpprc_string )
220
+ info ( `${ source_cpprc_string } was added to ${ profile_path } ` )
209
221
}
210
- } catch ( err ) {
211
- warning ( `Failed to add ${ source_cpprc_string } to .profile or .bashrc. You should add it manually: ${ err } ` )
212
222
}
213
-
214
- setupCppInProfile_called = true
215
223
}
216
224
217
225
export async function finalizeCpprc ( ) {
218
226
if ( await pathExists ( cpprc_path ) ) {
219
- const entries = readFileSync ( cpprc_path , "utf-8" ) . split ( "\n" )
227
+ const entries = ( await readFile ( cpprc_path , "utf-8" ) ) . split ( "\n" )
220
228
221
229
const unique_entries = [ ...new Set ( entries . reverse ( ) ) ] . reverse ( ) // remove duplicates, keeping the latest entry
222
230
223
- writeFileSync ( cpprc_path , unique_entries . join ( "\n" ) )
231
+ await writeFile ( cpprc_path , unique_entries . join ( "\n" ) )
224
232
225
233
await grantUserWriteAccess ( cpprc_path )
226
234
}
0 commit comments