require 'system' require 'find' require 'fileutils' require 'pathname' module Install MYSELF = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ DAT_LIB = File.expand_path(File.dirname(MYSELF)) DAT_ROOT = Pathname.new(DAT_LIB).parent HOME = ENV["HOME"] def self.base_update() install_symlinks end def self.base_install() #uninstall install_symlinks System.install(["neovim", "zsh"]) #install_ohmyzsh create_zshrc end def self.uninstall() system("rm -rf \"#{HOME}/.oh-my-zsh\"") end def self.create_zshrc() content = <<~TEXT src() { [[ -e $1 ]] && source $1 } export DAT_ROOT="#{DAT_ROOT}" export RUBYLIB="#{DAT_LIB}" export PATH="$HOME/.local/share/dat/bin:$HOME/.local/bin:$PATH" src .zshrc-ohmyzsh TEXT File.write("#{ENV["HOME"]}/.zshrc", content) system("source .zshrc") end def self.install_ohmyzsh() puts "installing ohmyzsh ====" url_execute 'https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh' content = <<~TEXT export ZSH="$HOME/.oh-my-zsh" ZSH_THEME="robbyrussell" plugins=(git) source $ZSH/oh-my-zsh.sh TEXT File.write("#{ENV["HOME"]}/.zshrc-ohmyzsh", content) end def self.url_execute(url) require 'net/http' require 'uri' require 'tempfile' uri = URI.parse(url) response = Net::HTTP.get_response(uri) puts "downloading ended" if response.is_a?(Net::HTTPSuccess) Tempfile.create(['install', '.sh']) do |file| file.write(response.body) file.flush File.chmod(0755, file.path) system("/bin/sh", file.path) puts "=======> step 1" end else puts "Failed to download script: #{response.code} #{response.message}" end puts "=======> step 2" end # this creates search path for workspace def self.workspace_search_paths workspace_path = File.join(DAT_ROOT, "workspace") Dir["#{workspace_path}/*/bin"].map { |path| "\"#{path}\"" }.join(" ") end def self.symlink_dir(from, to) walk_files from do |path| puts path create_symlink File.join(from, path), File.join(to, path) end end def self.install_symlinks() link_to = File.join(DAT_ROOT, "home") symlink_dir link_to, HOME end def self.create_symlink(source, destination) destination_dir = File.dirname destination FileUtils.mkdir_p destination_dir unless File.exist? destination_dir if File.symlink? destination File.delete destination elsif File.exist? destination File.delete destination end File.symlink source, destination end def self.walk_files(base_path) raise ArgumentError, "Base path must be a directory" unless File.directory? base_path Find.find base_path do |path| rel_path = path.sub "#{base_path}/" , "" if File.file? path yield rel_path if block_given? end end end end