Class: ReseedTask

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

Instance Method Summary collapse

Instance Method Details

#download_and_extract(url_to_zip) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/reseed/reseed_task.rb', line 93

def download_and_extract url_to_zip
   puts "#{@current_task} : Downloading #{url_to_zip}"

   temp_dir = Dir.mktmpdir

   open url_to_zip do |f|
      zip_path = f.path

      Zip::ZipFile.open(zip_path) do |zip|
         zip.each do |z|
            dest = File.join temp_dir, z.name
            FileUtils.mkdir_p(File.dirname(dest))
            zip.extract z, dest
         end
      end
   end

   temp_dir
end

#execute(name, seeds) ⇒ Object



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
# File 'lib/reseed/reseed_task.rb', line 11

def execute name, seeds
   @current_task = name

   seeds.each do |s|
      files_to_reseed = []

      if s[:file]
         files_to_reseed = [s]
      elsif s[:files]
         arg = s.delete :files
         
         if arg.is_a? String
            files = Dir.glob arg
         elsif arg.is_a? Array
            files = arg         
         else
            return 
         end
         files_to_reseed = files.map { |f| { :file => f }.merge(s) }
      end

      reseed_files files_to_reseed
   end

   @current_task = nil
end

#reseed(source, dest) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/reseed/reseed_task.rb', line 79

def reseed source, dest
   base_name = File.basename dest
   if File.exist? source
      begin
         FileUtils.cp source, dest
         puts "#{@current_task} : #{base_name}"
      rescue
         puts "#{@current_task} : (Unable to copy) #{base_name}"
      end
   else
      puts "#{@current_task} : (Doesn't exist) #{base_name}"
   end
end

#reseed_files(files) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reseed/reseed_task.rb', line 38

def reseed_files files
   files.each do |args|
      if args[:latest_dir]
         reseed_from_latest_dir args[:latest_dir], args[:file]
      elsif args[:dir]
         reseed_from_dir args[:dir], args[:file]
      elsif args[:web]
         reseed_from_web args[:web], args[:file]
      end
   end
end

#reseed_from_dir(dir, file) ⇒ Object



55
56
57
58
# File 'lib/reseed/reseed_task.rb', line 55

def reseed_from_dir dir, file
   source = File.join(dir, File.basename(file))
   reseed source, file 
end

#reseed_from_latest_dir(base_dir, file) ⇒ Object



50
51
52
53
# File 'lib/reseed/reseed_task.rb', line 50

def reseed_from_latest_dir base_dir, file
   source = File.join base_dir, Dir.entries(base_dir).sort.reverse.take(1).first
   reseed_from_dir source, file
end

#reseed_from_web(url, file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/reseed/reseed_task.rb', line 60

def reseed_from_web url, file
   if /\.zip$/i.match url
      extracted_to = download_and_extract url
      reseed_from_dir extracted_to, file
      return
   elsif /(\\|\/)$/.match url
      url += File.basename file
   end

   begin
      open url do |x|
         reseed x.path, file
      end
   rescue
      base_name = File.basename file
      puts "#{@current_task} : (Doesn't exist) #{base_name}"
   end
end