-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup
executable file
·84 lines (69 loc) · 2.18 KB
/
setup
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env ruby
# rubocop:disable Layout/SpaceAroundOperators, Style/MixinUsage, Style/NumericLiteralPrefix, Lint/MissingCopEnableDirective
# frozen_string_literal: true
require "fileutils"
require "pathname"
def dry_run?
@dry_run = (ARGV.include?("-d") || ARGV.include?("--dry-run")) if @dry_run.nil?
@dry_run
end
if dry_run?
warn "==> Dry run mode."
include FileUtils::DryRun
else
include FileUtils::Verbose
end
OS = `uname`.chomp
HOSTNAME = `hostname`.chomp
ROOT = Pathname.new(__dir__).expand_path
DOTFILES_HOME = ROOT/"home"
DOTFILES_OVERWRITE = ROOT/"overwrite"
HOME = Pathname.new(Dir.home)
def overwrite_path(src)
Pathname.new(src.to_s.sub(DOTFILES_HOME.to_s, DOTFILES_OVERWRITE.to_s))
end
def link_file(src, dst_dir)
mkdir_p dst_dir unless dst_dir.directory?
return if src.basename.to_s == ".DS_Store"
return if src.to_s.start_with?(DOTFILES_HOME.to_s) && overwrite_path(src).exist?
ln_sf src, dst_dir
end
def link_dir(src_dir, dst_dir, skip_directory: false)
overwrite_dir = overwrite_path(src_dir)
files = []
files += src_dir.children
files += overwrite_dir.children if overwrite_dir.exist?
files.sort.each do |file|
next if skip_directory && file.directory?
link_file file, dst_dir
end
end
def clean(dir)
dir.children.sort.each do |file|
next if file.exist?
next unless file.symlink?
rm file if file.readlink.to_s.start_with? ROOT.to_s
end
end
# Link files
link_dir DOTFILES_HOME, HOME, skip_directory: true
link_dir DOTFILES_HOME/".config", HOME/".config"
link_dir DOTFILES_HOME/".local/bin", HOME/".local/bin"
link_dir DOTFILES_HOME/".local/share/cargo", HOME/".local/share/cargo"
link_dir DOTFILES_HOME/".ssh", HOME/".ssh"
if OS == "Darwin"
link_dir DOTFILES_HOME/".gnupg", HOME/".gnupg"
link_file DOTFILES_HOME/".config/Code/User", HOME/"Library/Application Support/Code"
link_file DOTFILES_HOME/".config/pypoetry/config.toml", HOME/"Library/Application Support/pypoetry"
end
# Cleanup broken symlinks
clean HOME
clean HOME/".config"
clean HOME/".gnupg"
clean HOME/".local/bin"
clean HOME/".local/share/cargo"
clean HOME/".ssh"
# Post linking
chmod 0700, HOME/".ssh"
chmod 0600, (HOME/".ssh/config").realpath
chmod 0700, HOME/".gnupg"