From f9f8638a599591f1a646f1df873414b1c35fe816 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Thu, 21 Oct 2021 17:00:26 -0400 Subject: [PATCH] generate binstub along with other commands --- lib/packwerk.rb | 1 + lib/packwerk/cli.rb | 3 +- lib/packwerk/generators/binstub.rb | 42 ++++++++++++++++++++++ lib/packwerk/generators/templates/packwerk | 21 +++++++++++ test/unit/generators/binstub_test.rb | 36 +++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 lib/packwerk/generators/binstub.rb create mode 100644 lib/packwerk/generators/templates/packwerk create mode 100644 test/unit/generators/binstub_test.rb diff --git a/lib/packwerk.rb b/lib/packwerk.rb index 89c439979..769251fc4 100644 --- a/lib/packwerk.rb +++ b/lib/packwerk.rb @@ -73,6 +73,7 @@ module Generators autoload :ConfigurationFile autoload :InflectionsFile autoload :RootPackage + autoload :Binstub end module ReferenceChecking diff --git a/lib/packwerk/cli.rb b/lib/packwerk/cli.rb index 1d5fa6d49..ade013a30 100644 --- a/lib/packwerk/cli.rb +++ b/lib/packwerk/cli.rb @@ -92,8 +92,9 @@ def generate_configs ) inflections_file = Packwerk::Generators::InflectionsFile.generate(root: @configuration.root_path, out: @out) root_package = Packwerk::Generators::RootPackage.generate(root: @configuration.root_path, out: @out) + binstub = Packwerk::Generators::Binstub.generate(root: @configuration.root_path, out: @out) - success = configuration_file && inflections_file && root_package + success = configuration_file && inflections_file && root_package && binstub result = if success <<~EOS diff --git a/lib/packwerk/generators/binstub.rb b/lib/packwerk/generators/binstub.rb new file mode 100644 index 000000000..7db3fb00e --- /dev/null +++ b/lib/packwerk/generators/binstub.rb @@ -0,0 +1,42 @@ +# typed: true +# frozen_string_literal: true + +module Packwerk + module Generators + class Binstub + class << self + def generate(root: ".", out: $stdout) + new(root, out: out).generate + end + end + + def initialize(root, out: $stdout) + @root = root + @out = out + end + + def generate + @out.puts("📦 Generating binstub...") + generate_packwerk_validate_script + end + + private + + def generate_packwerk_validate_script + destination_file_path = File.join(@root, "bin") + FileUtils.mkdir_p(destination_file_path) + + if File.exist?(File.join(destination_file_path, "packwerk")) + @out.puts("⚠️ Packwerk binstub already exists.") + return true + end + + source_file_path = File.expand_path("templates/packwerk", __dir__) + FileUtils.cp(source_file_path, destination_file_path) + + @out.puts("✅ Packwerk binstub generated in #{destination_file_path}") + true + end + end + end +end diff --git a/lib/packwerk/generators/templates/packwerk b/lib/packwerk/generators/templates/packwerk new file mode 100644 index 000000000..dfd517c0c --- /dev/null +++ b/lib/packwerk/generators/templates/packwerk @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# This file was auto-generated by Packwerk through `packwerk init` + +# Needs to be run in test environment in order to have test helper paths available in the autoload paths +ENV["RAILS_ENV"] = "test" + +# Command line arguments needs to be duplicated because spring modifies it +packwerk_argv = ARGV.dup + +begin + load(File.expand_path("spring", __dir__)) +rescue LoadError => e + raise unless e.message.include?("spring") +end + +require "packwerk" + +cli = Packwerk::Cli.new +cli.run(packwerk_argv) diff --git a/test/unit/generators/binstub_test.rb b/test/unit/generators/binstub_test.rb new file mode 100644 index 000000000..c5f7cc0c2 --- /dev/null +++ b/test/unit/generators/binstub_test.rb @@ -0,0 +1,36 @@ +# typed: false +# frozen_string_literal: true + +require "test_helper" + +module Packwerk + module Generators + class BinstubTest < Minitest::Test + setup do + @string_io = StringIO.new + @temp_dir = Dir.mktmpdir + FileUtils.mkdir_p(Pathname.new(@temp_dir).join("bin")) + @generated_file_path = File.join(@temp_dir, "bin", "packwerk") + end + + teardown do + FileUtils.remove_entry(@temp_dir) + end + + test ".generate creates a bin/packwerk file" do + success = Packwerk::Generators::Binstub.generate(root: @temp_dir, out: @string_io) + assert(File.exist?(@generated_file_path)) + assert success + assert_includes @string_io.string, "binstub generated" + end + + test ".generate does not create a bin/packwerk file if bin/packwerk already exists" do + File.open(File.join(@temp_dir, "bin/packwerk"), "w") do |_f| + success = Packwerk::Generators::Binstub.generate(root: @temp_dir, out: @string_io) + assert success + assert_includes @string_io.string, "binstub already exists" + end + end + end + end +end