forked from Homebrew/homebrew-cask-fonts
/
cask2formula
executable file
Β·208 lines (201 loc) Β· 7.17 KB
/
cask2formula
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env ruby
require "parslet"
def replace_cask_version_properties(input_string)
input_string
.gsub(".no_dots", '.gsub(".", "")')
.gsub(".no_hyphens", '.gsub("-", "")')
.gsub(".dots_to_underscores", '.gsub(".", "_")')
.gsub(".dots_to_slashes", '.gsub(".", "/")')
.gsub(".dots_to_hyphens", '.gsub(".", "-")')
.gsub(".major", '.sub(/\\..*/, "")')
.gsub(".minor", '.sub(/.*\\./, "")')
.gsub(".before_comma", '.sub(/,.*/, "")')
.gsub(".after_comma", '.sub(/.*,/, "")')
.gsub(".csv.first", '.sub(/,.*/, "")')
.gsub(".csv.second", '.sub(/.*,/, "")')
.gsub("version.", "version.to_s.")
end
class CaskParser < Parslet::Parser
rule(:space) {
match("[ \t\n\r]").repeat(1)
}
rule(:string) {
((str('"') >>
((str('#{') >> (str('}').absent? >> any).repeat >> str('}')) |
(str('"').absent? >> any)).repeat >>
str('"')) |
(str("'") >>
(str("'").absent? >> any).repeat >>
str("'"))).as(:string)
}
rule(:word) {
match("[0-9a-zA-Z_]").repeat(1)
}
rule(:keyword) {
(str(":") >> word).as(:keyword)
}
rule(:pair) {
(word >> str(":")).as(:left) >> space >> (string | keyword | word).as(:right)
}
rule(:doblock) {
(str("do") >> space >>
((space >> doblock) | (match("[ \t\n\r]+end").absent? >> any)).repeat >>
space >> str("end"))
}
rule(:heredoc) {
(str("<<~EOS") >> (str("EOS").absent? >> any).repeat >> str("EOS")).as(:heredoc)
}
rule(:value) {
(string | keyword | pair | heredoc | doblock.as(:doblock))
}
rule(:font) {
str("font") >> space >> string.as(:font)
}
rule(:command) {
(str("font").absent? >> word >> str("!").maybe).as(:command) >> space >>
value.as(:first_argument) >>
(str(",") >> space >> value).repeat.as(:rest_arguments)
}
rule(:comment) {
(str("#") >> (str("\n").absent? >> any).repeat).as(:comment)
}
rule(:cask) {
str("cask") >> space >> string.as(:name) >> space >> str("do") >> space >>
((command | comment) >> space).repeat.as(:before) >>
(font >> space).repeat.as(:fonts) >>
((command | comment) >> space).repeat.as(:after) >>
str("end") >> str("\n")
}
root :cask
end
class CaskTransform < Parslet::Transform
@@no_check = false
@@latest = false
@@only_path = ""
rule(:string => simple(:string)) {
string.to_s.sub(/^'([^"]*)'$/) { '"'+$1+'"' }
}
rule(:keyword => simple(:keyword)) {
keyword.to_s
}
rule(:heredoc => simple(:heredoc)) {
heredoc.to_s
}
rule(:doblock => simple(:doblock)) {
doblock.to_s
}
rule(:left => simple(:left), :right => simple(:right) ) {
left + " " + right
}
rule(:comment => simple(:comment)) {
comment.to_s + "\n"
}
rule(:font => simple(:font)) {
afont = font.to_s
afont = afont.sub(/"(.*\/)/, '"#{parent}\1')
if @@only_path != "" && !afont.include?('#{parent}') then
afont = "\"#{@@only_path}/\" + " + afont
end
afont = replace_cask_version_properties(afont)
"(share/\"fonts\").install #{afont}"
}
rule(:name => simple(:name),
:before => sequence(:before),
:fonts => sequence(:fonts),
:after => sequence(:after)) {
commands1 = before.select{|c| c != ""}
camelcase = name.to_s.gsub('"', "").gsub(/(^|-)(\w)/) { $2.upcase }
commands2 = after.select{|c| c != ""}+["test do\n end"]
parent = "parent = File.dirname(Dir.pwd) != (ENV['HOMEBREW_TEMP'] || '/tmp') ? '../' : ''\n "
<<~EOS
class #{camelcase} < Formula
#{commands1.join("\n ")}
def install
#{parent if fonts.to_s.include? "\#{parent}"}#{fonts.join("\n ")}
end
#{commands2.join("\n ")}
end
EOS
}
rule(:command => simple(:command),
:first_argument => simple(:first_argument),
:rest_arguments => sequence(:rest_arguments)) {
arguments = rest_arguments.unshift(first_argument)
if command == "caveats" then
"def creavat; #{arguments.join(", ")}\n end"
elsif command == "name" then
"desc #{arguments.join(", ")}"
elsif command == "depends_on" then
# Delete depends_on macos ...
# "depends_on #{arguments.join(",").gsub("formula: ","")}"
""
elsif command == "appcast" || command == "container" then #TODO: "container type:""
""
elsif command == "sha256" && arguments.join(", ") == ":no_check" then
@@no_check = true
""
elsif command == "version" && arguments.join(", ") == ":latest" then
@@latest = true
""
elsif command == "url" && @@latest && @@no_check then
@@latest = false
@@no_check = false
if arguments.find{ |arg| arg.to_s.include? "only_path" } then
@@only_path = arguments
.find{ |arg| arg.to_s.include? "only_path" }
.to_s
.gsub(/only_path:\s*"(.*)"/, '\1')
p @@only_path
else
@@only_path = ""
end
"head #{arguments.join(", ")}"
elsif command == "livecheck" then
""
elsif command == "deprecate!" || command == "disable!" then
command + " " + arguments.join(", ").gsub(":discontinued", ":unsupported")
else
command + " " + replace_cask_version_properties(arguments.join(", ").to_s)
end
}
end
class CaskConverter
def convert
transform = CaskTransform.new(true)
parser = CaskParser.new
ignores = IO.readlines('.caskignore', chomp: true)
Dir.glob('./homebrew-cask-fonts/Casks/*.rb').select{|file|
ignores.all? { |ignore| !File.fnmatch(ignore, file) }
}.each do |cask|
p "< #{cask}"
recipe = File.read(cask, encoding: 'UTF-8')
recipe = parser.parse(recipe)
recipe = transform.apply(recipe)
formula = cask.sub(%r{homebrew-cask-fonts/Casks}, 'Formula')
File.write(formula, recipe)
p "> #{formula}"
rescue Parslet::ParseFailed => failure
$stderr.puts failure.parse_failure_cause.ascii_tree
exit 1
end
end
def commit
`git diff --name-only; git ls-files --others --exclude-standard`.split(/\s+/).each do |file|
if file != "homebrew-cask-fonts" then
remote_source = "https://github.com/homebrew/#{file.sub(/Formula/, "homebrew-cask-fonts/blob/master/Casks")}"
commit_id = `git -C homebrew-cask-fonts log -n 1 --pretty=format:%H -- #{file.sub(/Formula/, 'Casks')}`
system 'git', 'add', file
system 'git', 'commit', '-m', "import #{remote_source} from #{commit_id}"
end
end
system 'git', 'add', 'homebrew-cask-fonts'
system 'git', 'commit', '-m', 'Update submodule'
end
end
Dir.chdir(__dir__)
converter = CaskConverter.new
case ARGV[0]
when "convert" then converter.convert
when "commit" then converter.commit
else p "ERROR!"
end