Skip to content

Commit

Permalink
update dependencies and add groovy beautify vendor files
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmariecom committed Sep 15, 2024
1 parent c13f047 commit f0c0582
Show file tree
Hide file tree
Showing 29 changed files with 1,573 additions and 635 deletions.
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ PATH
ostruct
parallel
syntax_tree
syntax_tree-css
syntax_tree-haml
syntax_tree-json
syntax_tree-xml
terminal-table
w_syntax_tree-erb
yaml
Expand Down Expand Up @@ -143,10 +146,19 @@ GEM
securerandom (0.3.1)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
syntax_tree-css (0.1.0)
prettier_print
syntax_tree (>= 2.0.1)
syntax_tree-haml (4.0.3)
haml (>= 5.2)
prettier_print (>= 1.2.1)
syntax_tree (>= 6.0.0)
syntax_tree-json (0.3.0)
prettier_print
syntax_tree (>= 2.0.1)
syntax_tree-xml (0.1.0)
prettier_print
syntax_tree (>= 2.0.1)
temple (0.10.3)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
Expand Down
3 changes: 3 additions & 0 deletions dorian.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Gem::Specification.new do |s|
s.add_dependency "parallel"
s.add_dependency "syntax_tree"
s.add_dependency "syntax_tree-haml"
s.add_dependency "syntax_tree-xml"
s.add_dependency "syntax_tree-json"
s.add_dependency "syntax_tree-css"
s.add_dependency "terminal-table"
s.add_dependency "w_syntax_tree-erb"
s.add_dependency "yaml"
Expand Down
60 changes: 48 additions & 12 deletions lib/dorian/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
require "syntax_tree"
require "syntax_tree/erb"
require "syntax_tree/haml"
require "syntax_tree/xml"
require "syntax_tree/json"
require "syntax_tree/css"
require "tempfile"
require "terminal-table"
require "uri"
Expand Down Expand Up @@ -372,17 +375,24 @@ def command_format

root = File.expand_path("../../", __dir__)

prettier_path = File.join(root, "vendor/prettier/standalone.js")
prettier_js = File.read(prettier_path)
context.eval(prettier_js)

sql_path = File.join(root, "vendor/sql-formatter.js")
sql_js = File.read(sql_path)
context.eval("self = {};")
context.eval(sql_js)
context.eval("sqlFormatter = self.sqlFormatter;")

prettier_path = File.join(root, "vendor/prettier/standalone.js")
prettier_js = File.read(prettier_path)
context.eval(prettier_js)
groovy_path = File.join(root, "vendor/groovy-beautify/dist/cjs/index.js")
groovy_js = File.read(groovy_path)
context.eval("module = { exports: {} };")
context.eval("exports = module.exports;")
context.eval(groovy_js)
context.eval("groovyBeautify = module.exports;")

plugins = %w[babel estree typescript postcss html markdown]
plugins = %w[babel estree typescript html markdown]

plugins.each do |plugin|
path = File.join(root, "vendor/prettier/plugins/#{plugin}.js")
Expand All @@ -402,7 +412,9 @@ def command_format
let after;
if (parser === "sql") {
after = sqlFormatter.format(before, { parser, plugins });
after = sqlFormatter.format(before);
} else if (parser === "groovy") {
after = groovyBeautify(before);
} else {
after = await prettier.format(before, { parser, plugins });
}
Expand All @@ -420,7 +432,9 @@ def command_format
if files.any?
each(files) { |file| format(file, context:) }
else
each(Git.open(".").ls_files.map(&:first)) { |file| format(file, context:) }
each(
Git.open(".").ls_files.map(&:first)
) { |file| format(file, context:) }
end
end

Expand Down Expand Up @@ -1522,8 +1536,14 @@ def filetype(path)
return :env if path.start_with?(".env.")
return :sh if path == "Dockerfile"
return :sh if ext == ".sh"
return :env if ext == ".enc"
return :enc if ext == ".enc"
return :enc if ext == ".keystore"
return :pro if ext == ".pro"
return :txt if ext == ".txt"
return :xml if ext == ".xml"
return :xml if ext == ".plist"
return :xml if ext == ".storyboard"
return :groovy if ext == ".gradle"
return unless File.exist?(path)
first_line = File.open(path, &:gets).to_s
first_line = first_line.encode("UTF-8", invalid: :replace).strip
Expand All @@ -1548,7 +1568,11 @@ def format(path, context:)
when :erb
after = SyntaxTree::ERB.format(before)
when :json
after = JSON.pretty_generate(sort(JSON.parse(before)))
after = SyntaxTree::JSON.format(before)
when :xml
after = SyntaxTree::XML.format(before)
when :css
after = SyntaxTree::CSS.format(before)
when :jsonl
after = before.lines.map { |line| JSON.parse(line).to_json }.join("\n")
when :csv
Expand All @@ -1572,8 +1596,6 @@ def format(path, context:)
context.eval("format(#{path.to_json}, 'html')")
when :md
context.eval("format(#{path.to_json}, 'markdown')")
when :css
context.eval("format(#{path.to_json}, 'css')")
when :slim, :fish
# unsupported
when :sql
Expand All @@ -1590,7 +1612,7 @@ def format(path, context:)
else
warn "run: `brew install shfmt` for #{path}"
end
when :raw, :directory, :symlink, :env, :enc, :txt
when :raw, :directory, :symlink, :env, :enc, :txt, :pro
when :png, :jpeg, :webp, :heic, :ico
image = MiniMagick::Image.open(path)
image.strip
Expand All @@ -1601,11 +1623,25 @@ def format(path, context:)
doc.trailer.info.each { |key, value| doc.trailer.info.delete(key) }
doc.write(path, update_fields: false)
after = File.read(path)
when :tex
if system("command -v latexindent > /dev/null 2>&1")
command = ["latexindent", path, "--logfile", "/dev/null"].shelljoin
stdout, stderr, status = Open3.capture3(command)
if stderr.empty? && status.success?
after = stdout.gsub("\t", " ")
else
raise stderr
end
else
warn "run: `brew install latexindent` for #{path}"
end
when :groovy
context.eval("format(#{path.to_json}, 'groovy')")
else
case File.basename(path)
when ".gitignore", ".node-version", ".prettierignore", ".ruby-version",
".tool-versions", "Gemfile.lock", "LICENSE", "VERSION", ".rspec",
"Procfile", "Procfile.dev", "Podfile.lock", "xcode.env", "CNAME"
"Procfile", "Procfile.dev", "Podfile.lock", ".xcode.env", "CNAME"
when ".keep"
File.write(path, "")
else
Expand Down
Loading

0 comments on commit f0c0582

Please sign in to comment.