summaryrefslogtreecommitdiff
path: root/stdlib.rb
blob: 3b414ce4f2ca3a9ef5db1f9a5454ce93271a0a2f (plain)
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
defop :MkDir, [:dir] do
  define_method :check do
    File.directory? @dir
  end
  define_method :do do
    mkdir @dir
  end
  define_method :undo do
    rmdir @dir if File.exist? @dir and Dir.empty? @dir
  end
  define_method :inspect do
    "#<MkDir '#{@dir}'>"
  end
end

defop :SymLink, [:from, :to] do
  define_method :check do
    if File.exist? @to or File.symlink? @to  # broken symlink should be kept
      if !(File.symlink? @to) or (File.readlink @to) != @from
        raise "File '#{@to}' exists but is not a symlink to '#{@from}'"
      else true
      end
    end
  end
  define_method :do do
    ln @from, @to
  end
  define_method :undo do
    rm @to
  end
  define_method :inspect do
    "#<SymLink '#{@from}' -> '#{to}>"
  end
end

defdeploy :tree do |src:, dst:, ignores: [/~$/], stops: []|
  sp, dp = [src, dst].map {|p| File.expand_path p }
  src = lambda {|path| File.join sp, path }
  dst = lambda {|path| File.join dp, path }
  recur = lambda do |path|
    return if ignores.any? {|pat| path =~ pat }
    if File.directory?(src[path])
      if stops.include? path
        op SymLink, from: src[path], to: dst[path]
      else
        op MkDir, dir: dst[path]
        (ls src[path]).each {|p| recur[File.join path, p] }
      end
    else
      op SymLink, from: src[path], to: dst[path]
    end
  end
  (ls sp).each {|path| recur[path] }
end