Class: Root

Inherits:
Object
  • Object
show all
Includes:
Sys::Client
Defined in:
lib/sub/root.rb

Defined Under Namespace

Classes: Processes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sys::Client

#run, #say, #svn, #sys, #verbosity, #verbosity=

Constructor Details

#initialize(root_path, clean = false) ⇒ Root

Returns a new instance of Root.



6
7
8
9
10
# File 'lib/sub/root.rb', line 6

def initialize(root_path, clean = false)
  @root_path = root_path
  @clean = clean
  @status = nil
end

Instance Attribute Details

#root_pathObject (readonly)

Returns the value of attribute root_path.



4
5
6
# File 'lib/sub/root.rb', line 4

def root_path
  @root_path
end

Instance Method Details

#all_externalsObject



130
131
132
133
134
135
136
# File 'lib/sub/root.rb', line 130

def all_externals
  exts = []
  directories_containing_externals.collect do |parent|
    exts += External.externals_of(parent)
  end
  exts
end

#checkout_external(external) ⇒ Object



78
79
80
81
82
# File 'lib/sub/root.rb', line 78

def checkout_external(external)
  @processes.launch do
    external.checkout
  end
end

#directories_containing_externalsObject



138
139
140
141
142
143
144
145
146
# File 'lib/sub/root.rb', line 138

def directories_containing_externals
  status.externals.collect do |path|
    if (path !~ /\//)
      root_path
    else
      path.gsub(/\/[^\/]*$/, '')
    end
  end.uniq
end

#remove_deleted_externalsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sub/root.rb', line 84

def remove_deleted_externals
  directories_containing_externals.each do |dir|
    deleted_something = false
    workspace_externals = External.externals_of(dir)
    Dir.chdir(root_path) do
      head_property = svn("propget -r HEAD svn:externals #{dir}", true)
      head_externals = External.externals_of(dir, head_property)
      deleted_externals = workspace_externals.select do |workspace_external|
        head_externals.select { |head_external| head_external.name == workspace_external.name }.empty?
      end
      unless deleted_externals.empty?
        deleted_externals.each do |external|
          # todo: check for local changes
          say("Deleting external #{external.path}")
          FileUtils.rm_rf(external.path)
        end
        # update the property first
        svn("up -N #{dir}")
        # then update all the deleted former externals
        svn("up " + deleted_externals.map{|external| "#{external.path}"}.join(" "))
      end
    end
    if deleted_something
      svn("up -N #{dir}")
      svn("up --ignore-externals #{dir}")
      puts "pausing"
      sleep(9999)
    end
  end
end

#remove_unversionedObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/sub/root.rb', line 115

def remove_unversioned
  status.unversioned.each do |path|
    if File.directory?(path)
      say "Removing unversioned directory #{path}"
    else
      say "Removing unversioned file #{path}"
    end
    FileUtils.rm_rf(path)
  end
end

#statusObject



126
127
128
# File 'lib/sub/root.rb', line 126

def status
  @status ||= Status.new(svn("st #{root_path}", true))
end

#updateObject



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
# File 'lib/sub/root.rb', line 30

def update
  if @clean
    remove_unversioned
  end

  remove_deleted_externals

  externals_before = all_externals

  svn("up --ignore-externals #{root_path}")
  # for some reason (array - array) doesn't work right here
  # so i had to write my own subtract method
  removed_externals = externals_before.subtract(all_externals)
  removed_externals.each do |ext|
    say "Removed external #{ext}"
    FileUtils.rm_rf(ext.path)
  end

  @processes = Processes.new

  already_up_to_date = []
  all_externals.each do |external|
    if external.should_update?
      if File.exists?(external.path)
        update_external(external)
      else
        checkout_external(external)
      end
    else
      already_up_to_date << external
    end
  end

  unless already_up_to_date.empty?
    say("External#{'s' if already_up_to_date.size > 1} " +
      already_up_to_date.collect {|external| external.name}.join(", ") +
      " already up to date")
  end

  @processes.join
end

#update_external(external) ⇒ Object



72
73
74
75
76
# File 'lib/sub/root.rb', line 72

def update_external(external)
  @processes.launch do
    external.update
  end
end