-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncenv.sh
executable file
·67 lines (58 loc) · 1.73 KB
/
syncenv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# -*- mode: sh; coding: utf-8; fill-column: 80; -*-
#
# syncenv.sh
# Created by Balakrishnan Chandrasekaran on 2017-12-05 00:33 +0100.
# Copyright (c) 2017 Balakrishnan Chandrasekaran <[email protected]>.
#
# Utility to parse Zsh configuration files and expose the exported environment
# variables to all applications using `launchctl`.
#
# (Utility is based on https://github.com/ersiner/osx-env-sync)
#
# Evaluate string containing shell variables.
function eval_str() {
echo $(echo 'echo '$1 | bash)
}
# Retrieve local configuration files.
function get_conf_files() {
local src="$1"
local files=()
local pos=0
for file in $(grep 'source' $src | grep '/.zsh/' | awk '{print $2}'); do
files[$pos]=$(eval_str $file)
let pos=pos+1
done
echo ${files[*]}
}
# Retrieve a list of files to search for environment variable exports.
function get_search_list() {
# List of files.
local files
local pos=0
readonly ZSH_CONF="$HOME/.zshenv"
# Start with Zsh's configuration file.
files[pos]="$ZSH_CONF"
let pos=pos+1
# Include system-specific configuration.
readonly SYS=$(get_conf_files ${files[0]})
files[pos]="$SYS"
let pos=pos+1
for file in $(get_conf_files ${files[1]}); do
files[pos]=$file
let pos=pos+1
done
echo ${files[*]}
}
# Export exported variables to GUI applications using `launchctl`.
function expose_exports() {
local files="$*"
for file in $files; do
grep 'export' $file | \
grep -vE '^\s*#' | \
while IFS=' =' read ignoreexport envvar ignorevalue; do
launchctl setenv ${envvar} "${!envvar}"
done
done
}
expose_exports $(get_search_list)