@@ -5,16 +5,16 @@ 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 , 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"
12
12
13
13
type AddEnvOptions = {
14
14
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
15
- shouldEscapeSpace ? : boolean
15
+ shouldEscapeSpace : boolean
16
16
/** If true, the variable will be only added if it is not defined */
17
- shouldAddOnlyIfNotDefined ? : boolean
17
+ shouldAddOnlyIfNotDefined : boolean
18
18
}
19
19
20
20
const defaultAddEnvOptions : AddEnvOptions = {
@@ -30,8 +30,10 @@ const defaultAddEnvOptions: AddEnvOptions = {
30
30
export async function addEnv (
31
31
name : string ,
32
32
valGiven : string | undefined ,
33
- options : AddEnvOptions = defaultAddEnvOptions ,
33
+ givenOptions : Partial < AddEnvOptions > = defaultAddEnvOptions ,
34
34
) {
35
+ const options = { ...defaultAddEnvOptions , ...givenOptions }
36
+
35
37
const val = escapeString ( valGiven ?? "" , options . shouldEscapeSpace )
36
38
try {
37
39
if ( GITHUB_ACTIONS ) {
@@ -119,12 +121,12 @@ async function addEnvSystem(name: string, valGiven: string | undefined, options:
119
121
}
120
122
case "linux" :
121
123
case "darwin" : {
122
- await setupCppInProfile ( )
124
+ await sourceCpprc ( )
123
125
if ( options . shouldAddOnlyIfNotDefined ) {
124
- 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` )
125
127
info ( `if not defined ${ name } then ${ name } ="${ val } " was added to "${ cpprc_path } ` )
126
128
} else {
127
- appendFileSync ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
129
+ await appendFile ( cpprc_path , `\nexport ${ name } ="${ val } "\n` )
128
130
info ( `${ name } ="${ val } " was added to "${ cpprc_path } ` )
129
131
}
130
132
return
@@ -148,8 +150,8 @@ async function addPathSystem(path: string) {
148
150
}
149
151
case "linux" :
150
152
case "darwin" : {
151
- await setupCppInProfile ( )
152
- appendFileSync ( cpprc_path , `\nexport PATH="${ path } :$PATH"\n` )
153
+ await sourceCpprc ( )
154
+ await appendFile ( cpprc_path , `\nexport PATH="${ path } :$PATH"\n` )
153
155
info ( `"${ path } " was added to "${ cpprc_path } "` )
154
156
return
155
157
}
@@ -163,54 +165,70 @@ async function addPathSystem(path: string) {
163
165
let setupCppInProfile_called = false
164
166
165
167
/// handles adding conditions to source .cpprc file from .bashrc and .profile
166
- export async function setupCppInProfile ( ) {
168
+ export async function sourceCpprc ( ) {
167
169
if ( setupCppInProfile_called ) {
168
170
return
169
171
}
170
172
171
- // a variable that prevents source_cpprc from being called from .bashrc and .profile
172
- const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
173
-
174
- if ( await pathExists ( cpprc_path ) ) {
175
- const cpprc_content = readFileSync ( cpprc_path , "utf8" )
176
- if ( cpprc_content . includes ( source_cpprc_str ) ) {
177
- // already executed setupCppInProfile
178
- return
179
- }
180
- }
181
-
182
- appendFileSync ( cpprc_path , `\n${ source_cpprc_str } \n` )
183
- info ( `Added ${ source_cpprc_str } to ${ cpprc_path } ` )
184
-
185
- // source cpprc in bashrc/profile
186
-
187
173
const source_cpprc_string =
188
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`
189
175
190
176
try {
191
- // source cpprc in .profile
192
- const profile_path = untildifyUser ( "~/.profile" )
193
- appendFileSync ( profile_path , source_cpprc_string )
194
- info ( `${ source_cpprc_string } was added to ${ profile_path } ` )
195
-
196
- // source cpprc in .bashrc too
197
- const bashrc_path = untildifyUser ( "~/.bashrc" )
198
- appendFileSync ( bashrc_path , source_cpprc_string )
199
- info ( `${ source_cpprc_string } was added to ${ bashrc_path } ` )
177
+ await Promise . all ( [
178
+ addCpprcHeader ( ) ,
179
+ sourceCpprcInProfile ( source_cpprc_string ) ,
180
+ sourceCpprcInBashrc ( source_cpprc_string ) ,
181
+ ] )
200
182
} catch ( err ) {
201
183
warning ( `Failed to add ${ source_cpprc_string } to .profile or .bashrc. You should add it manually: ${ err } ` )
202
184
}
203
185
204
186
setupCppInProfile_called = true
205
187
}
206
188
189
+ async function addCpprcHeader ( ) {
190
+ // a variable that prevents source_cpprc from being called from .bashrc and .profile
191
+ const cpprc_header = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
192
+
193
+ if ( await pathExists ( cpprc_path ) ) {
194
+ const cpprc_content = await readFile ( cpprc_path , "utf8" )
195
+ if ( ! cpprc_content . includes ( cpprc_header ) ) {
196
+ // already executed setupCppInProfile
197
+ await appendFile ( cpprc_path , `\n${ cpprc_header } \n` )
198
+ info ( `Added ${ cpprc_header } to ${ cpprc_path } ` )
199
+ }
200
+ }
201
+ }
202
+
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 } ` )
210
+ }
211
+ }
212
+ }
213
+
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 } ` )
221
+ }
222
+ }
223
+ }
224
+
207
225
export async function finalizeCpprc ( ) {
208
226
if ( await pathExists ( cpprc_path ) ) {
209
- const entries = readFileSync ( cpprc_path , "utf-8" ) . split ( "\n" )
227
+ const entries = ( await readFile ( cpprc_path , "utf-8" ) ) . split ( "\n" )
210
228
211
229
const unique_entries = [ ...new Set ( entries . reverse ( ) ) ] . reverse ( ) // remove duplicates, keeping the latest entry
212
230
213
- writeFileSync ( cpprc_path , unique_entries . join ( "\n" ) )
231
+ await writeFile ( cpprc_path , unique_entries . join ( "\n" ) )
214
232
215
233
await grantUserWriteAccess ( cpprc_path )
216
234
}
0 commit comments