diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 64502afd..9696481b 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -9,15 +9,33 @@ require 'rubygems' require 'bundler/setup' require_relative '../utils/devtool/copyright' -source_files_exts = ['.S', '.rs', '.rb'] +def copyright_check(staged_files) + source_files_exts = ['.S', '.rs', '.rb'] -staged_files = `git --no-pager diff --name-only --cached --diff-filter=d`.split(/\n/) -staged_files.select! do |f| - next if f.include?('build.rs') + staged_files = staged_files.select do |f| + next if f.include?('build.rs') + + f.include?('Makefile') || + f.include?('Dockerfile') || + source_files_exts.include?(File.extname(f)) + end + return true if staged_files.empty? - f.include?('Makefile') || - f.include?('Dockerfile') || - source_files_exts.include?(File.extname(f)) + copyright_check_files(staged_files) end -exit(1) unless copyright_check_files(staged_files) +##-------------------------------------------------------------------------------------------------- +## Execution starts here +##-------------------------------------------------------------------------------------------------- +staged_files = `git --no-pager diff --name-only --cached --diff-filter=d`.split(/\n/) +root_dir = `git rev-parse --show-toplevel`.strip + +# Copyright must be fixed manually. +exit(1) unless copyright_check(staged_files) + +# Brute-force format. Don't care if it affects non-staged files as well, since we only add back the +# staged ones. +Dir.chdir(root_dir) { system('ruby utils/devtool.rb fmt') } + +staged_files.each { |f| system("git add #{f}") } +exit(0) diff --git a/utils/devtool.rb b/utils/devtool.rb index 73579922..9a4952ca 100755 --- a/utils/devtool.rb +++ b/utils/devtool.rb @@ -41,8 +41,8 @@ class TutorialCrate Dir.chdir(@folder) { exit(1) unless system("BSP=#{bsp} make clippy") } end - def fmt(args) - print 'Format '.light_blue + def fmt_cargo_rust(args) + print 'Rust cargo fmt '.light_blue print "#{args} ".light_blue unless args.nil? puts @folder @@ -86,6 +86,7 @@ end # Forks commands to all applicable receivers class DevTool def initialize + @user_has_supplied_crates = false @bsp = bsp_from_env || SUPPORTED_BSPS.first cl = user_supplied_crate_list || Dir['*/Cargo.toml'].sort @@ -120,14 +121,16 @@ class DevTool end end - def fmt(check: false) - args = '-- --check' if check - - @crates.each { |c| c.fmt(args) } + def fmt + fmt_cargo_rust(check: false) + puts + fmt_prettier(check: false) end def fmt_check - fmt(check: true) + fmt_cargo_rust(check: true) + puts + fmt_prettier(check: true) end def make(bsp = nil) @@ -166,7 +169,7 @@ class DevTool def rubocop puts 'Rubocop'.light_blue - exit(1) unless system('rubocop') + exit(1) unless system('bundle exec rubocop') end def ready_for_publish @@ -177,6 +180,7 @@ class DevTool clippy('rpi4') clippy('rpi3') copyright + diff clean make('rpi4') @@ -184,7 +188,6 @@ class DevTool make_xtra test_unit test_integration - diff clean end @@ -209,6 +212,29 @@ class DevTool nil end + def fmt_cargo_rust(check: false) + args = '-- --check' if check + + @crates.each { |c| c.fmt_cargo_rust(args) } + end + + def fmt_prettier(check: false) + args = if check + '--check' + else + '--write' + end + + args += if @user_has_supplied_crates + " #{@crates.map(&:folder).join(' ')}" + else + ' .' + end + + puts 'Prettier:'.light_blue + exit(1) unless system("./node_modules/.bin/prettier #{args}") + end + def user_supplied_crate_list folders = ARGV.drop(1)