Skip to content

Instantly share code, notes, and snippets.

@CvX
Created November 25, 2014 10:41
Show Gist options
  • Save CvX/a4b2656ac045cb265d73 to your computer and use it in GitHub Desktop.
Save CvX/a4b2656ac045cb265d73 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def compile_script(file)
compiled = File.readlines(file).map do |line|
case line
when /^#!/
"#!/usr/bin/env fish"
when /^\s*(?:export )?(.*?)=(.*)/
name, content = $1, $2
if !content.match(/:\/\//) # not protocol strings (eg. "http://")
content.gsub!(/:\$/, ' $') # yay fuzzy heuristics
end
content.gsub!(/\$\{(.+?)\}/, '$\1') # ${VARIABLE} -> $VARIABLE
content.gsub!(/`(.*)`/) {
contents = $1
contents.sub!(/^([^\s]+?)=([^\s]+)/, 'env \1=\2')
"(#{contents})"
}
content.gsub!(/\$\{(.*)\}/, '(\1)')
"set -x #{name} #{content}\n"
when /^\s*set [\-\+][eu]/
"# #{line.chomp}\n"
when /^\s*eval "\$\((.+?) init -\)"/
name = $1
[
"set -x PATH $BOXEN_HOME/#{name}/shims $PATH",
"#{name} rehash 2>/dev/null"
].join("\n")
when /^\s*#.*/
line
when /^\s*$/
line
else
"###### PARSE ERR: #{line.chomp}\n"
end
end
compiled.join
end
Dir.chdir("/opt/boxen") do
boxen_fish_path = File.expand_path("~/.config/fish/boxen.fish")
bash_scripts = ["env.sh", *Dir.glob("env.d/*.sh")]
File.open(boxen_fish_path, "w") do |f|
bash_scripts.each do |file|
f.puts compile_script(file)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment