-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlpx_links.rb
executable file
·103 lines (84 loc) · 2.68 KB
/
lpx_links.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env ruby
require "json"
require "fileutils"
require "pathname"
require "uri"
require "optparse"
require_relative "lib/file_helpers"
# read the plist, create a json & parse a list of links
module LpxLinks
module_function
$app_name = "LOGIC" # default application name to read a list of packages from
OptionParser.new do |opts|
opts.on("-nAPP_NAME", "--name=APP_NAME", "[ Logic | Mainstage ] Default is Logic") do |n|
if (n.upcase! == "LOGIC" || n == "MAINSTAGE")
$app_name = n
else
print "Application name can only be Logic or Mainstage\n"
puts opts
exit
end
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
def run
begin
create_dirs
convert_plist_to_json
print_file(FileHelpers.all_download_links, download_links)
print_file(FileHelpers.mandatory_download_links, download_links(true))
print_file(FileHelpers.json_file, JSON.pretty_generate(packages))
open_lpx_download_links
rescue RuntimeError => e
puts "Error: #{e.message}"
puts "Please ensure that #{$app_name} is installed correctly on your system."
exit 1
end
end
def open_lpx_download_links
`open #{FileHelpers.links_dir}`
end
def create_dirs
FileUtils.mkdir_p(FileHelpers.links_dir)
FileUtils.mkdir_p(FileHelpers.json_dir)
end
def convert_plist_to_json
`plutil -convert json \'#{FileHelpers.plist_file_path($app_name)}\' -o /tmp/lgp_content.json`
end
def packages
@packages ||= read_packages
end
def read_packages
JSON.parse(File.read("/tmp/lgp_content.json"))["Packages"]
end
def download_links(only_mandatory = false)
links = []
packages.each do |i|
next if only_mandatory && !i[1]["IsMandatory"]
# Use File.join to concatenate URL and remove redundant separators (i.e. //)
unresolved_download_url = File.join(FileHelpers.url, i[1]["DownloadName"])
# Convert to URI
download_uri = URI(unresolved_download_url)
# Extract path
unresolved_download_uri_path = download_uri.path
# Resolve "../../" relative paths in the download URI path
resolved_download_uri_path = Pathname.new(unresolved_download_uri_path).cleanpath.to_s
# Set download URI path to the resolved path
download_uri.path = resolved_download_uri_path
# Convert download URI to URL string
resolved_download_url = download_uri.to_s
# Add the resolved download URL to the array
links << "#{resolved_download_url}\n"
end
links.sort
end
def print_file(file, content)
f = File.open(file, "w")
f.puts content
f.close
end
end
LpxLinks.run if $PROGRAM_NAME == __FILE__