Skip to content

Commit

Permalink
🔨 bin scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Mar 23, 2024
1 parent d03d93a commit ef7e437
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 19 deletions.
45 changes: 45 additions & 0 deletions bin/check_newline_eof
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions bin/check_ruby_syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

ruby -wc -- **/*.rb
6 changes: 6 additions & 0 deletions bin/check_tab_chars
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 0 additions & 19 deletions bin/checksum

This file was deleted.

67 changes: 67 additions & 0 deletions bin/checksums
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ef7e437

Please sign in to comment.