-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsevenzip_command_builder.rb
78 lines (61 loc) · 2.16 KB
/
sevenzip_command_builder.rb
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
module Ark
class SevenZipCommandBuilder
def unpack
sevenzip_command
end
def dump
sevenzip_command_builder(resource.path, 'e')
end
def cherry_pick
"#{sevenzip_command_builder(resource.path, 'e')} -r #{resource.creates}"
end
def initialize(resource)
@resource = resource
end
private
attr_reader :resource
def node
resource.run_context.node
end
def sevenzip_command
if resource.strip_components <= 0
return sevenzip_command_builder(resource.path, 'x')
end
tmpdir = make_temp_directory
cmd = sevenzip_command_builder(tmpdir, 'e')
cmd += ' && '
currdir = tmpdir.tr('/', '\\')
1.upto(resource.strip_components).each do |count|
cmd += "for /f %#{count} in ('dir /ad /b \"#{currdir}\"') do "
currdir += "\\%#{count}"
end
cmd += "#{ENV.fetch('SystemRoot')}\\System32\\xcopy \"#{currdir}\" \"#{resource.home_dir}\" /s /e"
end
def sevenzip_binary
@tar_binary ||= (node['ark']['sevenzip_binary'] || sevenzip_path_from_registry)
end
def sevenzip_path_from_registry
begin
basepath = ::Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe').read_s('Path')
# users like pretty errors
rescue ::Win32::Registry::Error
raise 'Failed to find the path of 7zip binary by searching checking HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe\Path. Make sure to install 7zip before using this resource. If 7zip is installed and you still receive this message you can also specify the 7zip binary path by setting node["ark"]["sevenzip_binary"]'
end
"#{basepath}7z.exe"
end
def sevenzip_command_builder(dir, command)
"#{sevenzip_binary} #{command} \"#{resource.release_file}\"#{extension_is_tar} -o\"#{dir}\" -uy"
end
def extension_is_tar
if resource.extension =~ /tar.gz|tgz|tar.bz2|tbz|tar.xz|txz/
" -so | #{sevenzip_binary} x -aoa -si -ttar"
else
''
end
end
def make_temp_directory
require 'tmpdir'
Dir.mktmpdir
end
end
end