Class: Effuse

Inherits:
Object
  • Object
show all
Defined in:
lib/effuse.rb

Constant Summary collapse

VERSION =
'2.1.0'

Instance Method Summary collapse

Constructor Details

#initializeEffuse

Returns a new instance of Effuse.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/effuse.rb', line 8

def initialize
  @dest_dir = Dir.home
  @import   = false
  @clean    = false
  @confirm  = true
  @backup   = true
  @prefix   = ''
  @verbose  = false
  @exclude  = %w[*~ .*~ .*.sw? .effuseignore effuse.yml *.effuse .*.effuse
                 .git .gitignore .gitmodules]

  ignore_file
  effuse_file
  options

  vputs self.inspect
end

Instance Method Details

#backup(src) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/effuse.rb', line 117

def backup(src)
  if @backup
    vputs "renaming '#{src}' -> '#{src}.effuse'"
    File.rename(src, src + '.effuse')
  else
    vputs "deleting '#{src}'"
    File.delete(src)
  end
end

#clean(files) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/effuse.rb', line 157

def clean(files)
  files.each do |src, dest|
    if File.exist?(dest) && File.identical?(src, dest)
      puts dest
      File.delete(dest)
    else
      vputs "'#{dest}' not symlinked"
    end
  end
end

#confirm?(s) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
# File 'lib/effuse.rb', line 107

def confirm? s
  return true unless @confirm
  loop do
    print "#{s} [Y/n] "
    input = $stdin.gets.chomp
    return true if input.empty? || input[0].downcase == ?y
    return false if input[0].downcase == ?n
  end
end

#effuse_fileObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/effuse.rb', line 34

def effuse_file
  return unless File.file? 'effuse.yml'
  yaml = YAML.load_file('effuse.yml')

  if yaml.include? 'destination'
    @dest_dir = File.expand_path(yaml['destination'])
  end
  @prefix = yaml['prefix'] if yaml.include? 'prefix'

  exclude = yaml['exclude'] || yaml['ignore']
  @exclude.concat(exclude) if exclude
  @exclude -= yaml['include'] if yaml.include? 'include'
end

#executeObject



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/effuse.rb', line 199

def execute
  if File.identical? @dest_dir, '.'
    puts 'error: destination directory is current directory'
    exit 1
  end

  if @clean
    clean(scan)
  else
    symlink(scan)
  end
end

#ignore_fileObject



26
27
28
29
30
31
32
# File 'lib/effuse.rb', line 26

def ignore_file
  return unless File.file? '.effuseignore'
  puts 'warning: ignore file is deprecated, use effuse.yml instead'
  File.readlines('.effuseignore').each do |glob|
    @exclude << glob.chomp
  end
end

#optionsObject



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/effuse.rb', line 48

def options
  OptionParser.new do |o|
    o.banner = 'usage: effuse [OPTIONS...] [DEST]'

    o.on('-i', '--import', 'Import existing files') do
      @import = true
    end
    o.on('-c', '--clean', 'Remove symlinks') do
      @clean = true
    end

    o.separator ''

    o.on('-y', '--no-confirm', 'Do not ask before replacing files') do
      @confirm = false
    end
    o.on('-n', '--no-backup', 'Do not create backup files') do
      @backup = false
    end

    o.separator ''

    o.on('--exclude GLOB', 'Exclude GLOB from symlinking') do |glob|
      @exclude << glob
    end
    o.on('--include GLOB', 'Include GLOB in symlinking') do |glob|
      @exclude.delete(glob)
    end

    o.separator ''

    o.on('-p', '--prefix', 'Prefix symlink paths with PREFIX') do |prefix|
      @prefix = prefix
    end

    o.separator ''

    o.on('-v', '--verbose', 'Show verbose output') do
      @verbose = true
    end

    o.separator ''

    o.on_tail('--version', 'Show version') do
      puts "effuse #{VERSION}"
      exit
    end
    o.on_tail('-h', '--help', 'Show this message') do
      puts o
      exit
    end
  end.parse!
  @dest_dir = ARGV.first if ARGV.first
end

#scanObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/effuse.rb', line 127

def scan
  dirs = ['.']
  files = {}

  dirs.each do |dir|
    vputs "scanning '#{dir}'"
    Dir.foreach(dir) do |entry|
      next if %w[. ..].include? entry

      path = File.join(dir, entry)

      if @exclude.any? {|g| File.fnmatch(g, entry) }
        vputs "excluding '#{path}'"
        next
      end

      if File.directory? path
        dirs << path
        next
      end

      vputs "adding '#{path}'"
      relpath = @prefix + File.join(path.split('/').drop(1))
      files[File.absolute_path(path)] = File.join(@dest_dir, relpath)
    end
  end

  files
end


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/effuse.rb', line 168

def symlink(files)
  files.each do |src, dest|
    if File.exist? dest
      if File.identical? src, dest
        vputs "'#{dest}' already symlinked"
        next
      elsif @import
        if confirm? "'#{dest}' already exists. Import it?"
          backup(src)
          vputs "renaming '#{dest}' -> '#{src}'"
          File.rename(dest, src)
        else
          vputs "skipping '#{dest}'"
          next
        end
      else # Replace destination file
        if confirm? "'#{dest}' already exists. Replace it?"
          backup(dest)
        else
          vputs "skipping '#{dest}'"
          next
        end
      end
    end

    puts "'#{dest}' -> '#{src}'"
    FileUtils.mkdir_p(File.dirname(dest))
    File.symlink(src, dest)
  end
end

#vputs(s) ⇒ Object



103
104
105
# File 'lib/effuse.rb', line 103

def vputs(s)
  puts(s) if @verbose
end