|
33 | 33 | import json |
34 | 34 | import os |
35 | 35 | import platform |
| 36 | +import re |
36 | 37 | import shlex |
37 | 38 | import shutil |
38 | 39 | import signal |
@@ -2028,34 +2029,61 @@ def handle_dotenv(dotenv_files): |
2028 | 2029 | print(f'Loaded: {dotenv_path}') |
2029 | 2030 |
|
2030 | 2031 |
|
2031 | | -def handle_env(env_variables, local_env, build_type=None): |
| 2032 | +def handle_env(env_variables, env=None, build_type=None): |
2032 | 2033 | if not env_variables: |
2033 | 2034 | return |
| 2035 | + |
| 2036 | + if env is None: |
| 2037 | + env = os.environ |
2034 | 2038 |
|
| 2039 | + # If k starts with +, append to existing variable |
2035 | 2040 | for k, v in env_variables.items(): |
2036 | | - if local_env: |
2037 | | - if 'PATH_PREPEND' in k: |
2038 | | - local_env['PATH'] = os.path.normpath(os.path.expandvars(v)) + os.pathsep + local_env['PATH'] |
2039 | | - continue |
2040 | | - if 'PATH_APPEND' in k: |
2041 | | - local_env['PATH'] = local_env['PATH'] + os.pathsep + os.path.normpath(os.path.expandvars(v)) |
2042 | | - continue |
| 2041 | + print(f'Processing env var: {k} = {v}') |
| 2042 | + |
| 2043 | + # Append to existing variable if the key starts with + |
| 2044 | + # (then remove the + from the key) |
| 2045 | + append = False |
| 2046 | + if k.startswith('+'): |
| 2047 | + append = True |
| 2048 | + k = k[1:] |
| 2049 | + |
| 2050 | + # TODO: obsolete? remove in the future |
| 2051 | + if 'PATH_PREPEND' in k: |
| 2052 | + env['PATH'] = os.path.normpath(os.path.expandvars(v)) + os.pathsep + env['PATH'] |
| 2053 | + continue |
| 2054 | + if 'PATH_APPEND' in k: |
| 2055 | + env['PATH'] = env['PATH'] + os.pathsep + os.path.normpath(os.path.expandvars(v)) |
| 2056 | + continue |
2043 | 2057 |
|
2044 | | - handle_build_type(local_env, build_type) |
| 2058 | + handle_build_type(env, build_type) |
2045 | 2059 |
|
2046 | | - local_env[k] = os.path.normpath(os.path.expandvars(v)) |
2047 | | - else: |
2048 | | - if 'PATH_PREPEND' in k: |
2049 | | - os.environ['PATH'] = os.path.normpath(os.path.expandvars(v)) + os.pathsep + os.environ['PATH'] |
2050 | | - continue |
2051 | | - if 'PATH_APPEND' in k: |
2052 | | - os.environ['PATH'] = os.environ['PATH'] + os.pathsep + os.path.normpath(os.path.expandvars(v)) |
| 2060 | + v = os.path.expandvars(v) |
| 2061 | + if append: |
| 2062 | + # If append empty string, skip |
| 2063 | + if k == '': |
2053 | 2064 | continue |
2054 | 2065 |
|
2055 | | - handle_build_type(os.environ, build_type) |
| 2066 | + # if a separator is specified in the key like `(;)SOMETHING_SOMETHING`, extract it |
| 2067 | + |
| 2068 | + # Separator extraction logic: |
| 2069 | + # sep = between '(' and ')' |
| 2070 | + # default separator is " " |
| 2071 | + sep = " " |
| 2072 | + if '(' in k and ')' in k: |
| 2073 | + sep = k.split('(')[1].split(')')[0] |
| 2074 | + k = k.split(')')[1] |
| 2075 | + |
| 2076 | + # Append to existing value |
| 2077 | + old_value = env.get(k, '') |
| 2078 | + if old_value != '': |
| 2079 | + v = old_value + sep + v |
| 2080 | + # skip append if there's no old value |
| 2081 | + |
| 2082 | + env[k] = v |
| 2083 | + if not env is os.environ: |
| 2084 | + os.environ[k] = v |
2056 | 2085 |
|
2057 | | - os.environ[k] = os.path.normpath(os.path.expandvars(v)) |
2058 | | - # print(f'global: {k} = {os.environ[k]}') |
| 2086 | + print(f'Final env var: {k} = {os.environ[k]}') |
2059 | 2087 |
|
2060 | 2088 |
|
2061 | 2089 | def handle_build_type(env, build_type=None): |
|
0 commit comments