diff --git a/bin/check_newline_eof b/bin/check_newline_eof new file mode 100755 index 0000000..26b33fa --- /dev/null +++ b/bin/check_newline_eof @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +# Finds files without final newlines +# Pass "-f" to also fix those files +# +# Always have a new line marker at the end of every file comprised of text. +# In a POSIX system a file missing a final new line is technically not a text file. +# Some tools will not parse them, or parse them in unexpected ways. +# See: +# Official POSIX Standard: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206 +# Discussion of "why newline at EOF?": https://stackoverflow.com/q/729692/213191 +# Primary Source: https://stackoverflow.com/a/67426395/213191 +# Author: ppar, https://stackoverflow.com/users/9983387/ppar +# Documentation & theory behind `-not \( -path ... \)`: https://stackoverflow.com/a/69830768/213191 +# Author: Gabriel Staples, https://stackoverflow.com/users/4561887/gabriel-staples +# License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/ +fix_flag="$([ "$1" == "-f" ] && echo -true || echo -false)" +(test $(find . \ + -type f \ + -not \( -name "*.css" \) \ + -not \( -name "*.scss" \) \ + -not \( -name "*.csv" \) \ + -not \( -name "*.json" \) \ + -not \( -name "VERSION*" \) \ + -not \( -path "*/.bundle/*" -prune \) \ + -not \( -path "*/.git/*" -prune \) \ + -not \( -path "*/.idea/*" -prune \) \ + -not \( -path "*/.vscode/*" -prune \) \ + -not \( -path "*/checksums/*" -prune \) \ + -not \( -path "*/coverage/*" -prune \) \ + -not \( -path "*/doc/*" -prune \) \ + -not \( -path "*/log/*" -prune \) \ + -not \( -path "*/node_modules/*" -prune \) \ + -not \( -path "*/public/*" -prune \) \ + -not \( -path "*/results/*" -prune \) \ + -not \( -path "*/test-results/*" -prune \) \ + -not \( -path "*/tmp/*" -prune \) \ + -not \( -path "*/vendor/*" -prune \) \ + -exec sh -c 'file -b "{}" | grep -q text' \; \ + -exec sh -c '[ "$(tail -c 1 "{}" | od -An -a | tr -d "[:space:]")" != "nl" ]' \; \ + -print \ + $fix_flag \ + -exec sh -c 'echo >> "{}"' \; | tee pb_ruby_newlines_missing.txt | wc -l) -eq "0" && \ + rm pb_ruby_newlines_missing.txt) || \ + (cat pb_ruby_newlines_missing.txt && rm pb_ruby_newlines_missing.txt && exit 1) diff --git a/bin/check_ruby_syntax b/bin/check_ruby_syntax new file mode 100755 index 0000000..3a2615e --- /dev/null +++ b/bin/check_ruby_syntax @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +ruby -wc -- **/*.rb diff --git a/bin/check_tab_chars b/bin/check_tab_chars new file mode 100755 index 0000000..4b6ab8f --- /dev/null +++ b/bin/check_tab_chars @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# Absolutely no tab characters in any *.rb, *.yml, or *.yaml files. +(test $(grep -nR $'\t' --include="*.rb" --include="*.yml" --include="*.yaml" | tee pb_ruby_tabs_found.txt | wc -l) -eq "0" && \ + rm pb_ruby_tabs_found.txt) || \ + (cat pb_ruby_tabs_found.txt && rm pb_ruby_tabs_found.txt && exit 1) diff --git a/bin/checksum b/bin/checksum deleted file mode 100755 index be917ee..0000000 --- a/bin/checksum +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -require "digest/sha2" -gems = Dir["*.gem"] -puts "Found: #{gems.inspect}" -raise "No Gems" if gems.empty? -raise "Too Many Gems" if gems.length > 1 - -built_gem_path = gems.first -checksum512 = Digest::SHA512.new.hexdigest(File.read(built_gem_path)) -checksum512_path = "checksums/#{built_gem_path}.sha512" -File.write(checksum512_path, checksum512) - -checksum256 = Digest::SHA256.new.hexdigest(File.read(built_gem_path)) -checksum256_path = "checksums/#{built_gem_path}.sha256" -File.write(checksum256_path, checksum256) - -puts "You must now git add and commit '#{checksum256_path}' and '#{checksum512_path}'" diff --git a/bin/checksums b/bin/checksums new file mode 100755 index 0000000..5498152 --- /dev/null +++ b/bin/checksums @@ -0,0 +1,67 @@ +#!/usr/bin/env ruby + +# Script from https://github.com/rubygems/guides/pull/325 +require "digest/sha2" + +# Final clause of Regex `(?=\.gem)` is a positive lookahead assertion +# See: https://learnbyexample.github.io/Ruby_Regexp/lookarounds.html#positive-lookarounds +# Used to pattern match against a gem package name, which always ends with .gem. +# The positive lookahead ensures it is present, and prevents it from being captured. +VERSION_REGEX = /((\d+\.\d+\.\d+)([-.][0-9A-Za-z-]+)*)(?=\.gem)/ + +gem_path_parts = ARGV.first&.split("/") + +if gem_path_parts&.any? + gem_name = gem_path_parts.last + gem_pkg = File.join(gem_path_parts) + puts "Looking for: #{gem_pkg.inspect}" + gems = Dir[gem_pkg] + puts "Found: #{gems.inspect}" +else + gem_pkgs = File.join("pkg", "*.gem") + puts "Looking for: #{gem_pkgs.inspect}" + gems = Dir[gem_pkgs] + raise "Unable to find gems #{gem_pkgs}" if gems.empty? + + # Sort by newest last + # [ "my_gem-2.3.9.gem", "my_gem-2.3.11.pre.alpha.4.gem", "my_gem-2.3.15.gem", ... ] + gems.sort_by! { |gem| Gem::Version.new(gem[VERSION_REGEX]) } + gem_pkg = gems.last + gem_path_parts = gem_pkg.split("/") + gem_name = gem_path_parts.last + puts "Found: #{gems.length} gems; latest is #{gem_name}" +end + +checksum512 = Digest::SHA512.new.hexdigest(File.read(gem_pkg)) +checksum512_path = "checksums/#{gem_name}.sha512" +File.write(checksum512_path, checksum512) + +checksum256 = Digest::SHA256.new.hexdigest(File.read(gem_pkg)) +checksum256_path = "checksums/#{gem_name}.sha256" +File.write(checksum256_path, checksum256) + +version = gem_name[VERSION_REGEX] + +git_cmd = <<~GIT_MSG + git add checksums/* && \ + git commit -m "🔒️ Checksums for v#{version}" +GIT_MSG + +puts <<~RESULTS + [ GEM: #{gem_name} ] + [ VERSION: #{version} ] + [ GEM PKG LOCATION: #{gem_pkg} ] + [ CHECKSUM SHA-256: #{checksum256} ] + [ CHECKSUM SHA-512: #{checksum512} ] + [ CHECKSUM SHA-256 PATH: #{checksum256_path} ] + [ CHECKSUM SHA-512 PATH: #{checksum512_path} ] + + ... Running ... + + #{git_cmd} +RESULTS + +# This will replace the current process with the git process, and exit. +# Any command placed after this will not be run: +# See: https://www.akshaykhot.com/call-shell-commands-in-ruby +exec(git_cmd)