From 7d753a9d1e94a8140ab11f84d382aae75279055e Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Thu, 18 Nov 2021 09:53:09 -0500 Subject: [PATCH] simplify spring setup --- README.md | 3 +- USAGE.md | 12 +++++-- exe/packwerk | 8 ++++- lib/packwerk.rb | 5 --- lib/packwerk/cli.rb | 5 ++- lib/packwerk/generators/binstub.rb | 42 ---------------------- lib/packwerk/generators/templates/packwerk | 21 ----------- lib/packwerk/spring_command.rb | 1 + test/unit/generators/binstub_test.rb | 36 ------------------- 9 files changed, 21 insertions(+), 112 deletions(-) delete mode 100644 lib/packwerk/generators/binstub.rb delete mode 100644 lib/packwerk/generators/templates/packwerk delete mode 100644 test/unit/generators/binstub_test.rb diff --git a/README.md b/README.md index 82a679950..020f36b92 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ Or install it yourself as: $ gem install packwerk -3. Run `bundle exec packwerk init` to generate the configuration files +2. Run `bundle binstub packwerk` to generate the binstub +3. Run `bin/packwerk init` to generate the configuration files ## Usage diff --git a/USAGE.md b/USAGE.md index 499bd8224..aa76abe13 100644 --- a/USAGE.md +++ b/USAGE.md @@ -42,9 +42,12 @@ The [package principles](https://en.wikipedia.org/wiki/Package_principles) page ## Getting started -After including Packwerk in the Gemfile, you can generate the necessary files to get Packwerk running by executing: +After including Packwerk in the Gemfile, you will first want to generate a binstub: +You can do this by running `bundle binstub packwerk`, which will generate a [binstub](https://bundler.io/man/bundle-binstubs.1.html#DESCRIPTION) for packwerk. - bundle exec packwerk init +Then, you can generate the necessary files to get Packwerk running by executing: + + bin/packwerk init Here is a list of files generated: @@ -52,7 +55,6 @@ Here is a list of files generated: |-----------------------------|--------------|------------| | Packwerk configuration | packwerk.yml | See [Setting up the configuration file](#Setting-up-the-configuration-file) | | Root package | package.yml | A package for the root folder | -| Bin script | bin/packwerk | A binstub for executing packwerk with spring (if available) | Custom inflections | config/inflections.yml | A custom inflections file is only required if you have custom inflections in `inflections.rb`, see [Inflections](#Inflections) | After that, you may begin creating packages for your application. See [Defining packages](#Defining-packages) @@ -70,6 +72,10 @@ Packwerk reads from the `packwerk.yml` configuration file in the root directory. | custom_associations | N/A | list of custom associations, if any | | parallel | true | when true, fork code parsing out to subprocesses | +## Setting up Spring + +[Spring](https://github.com/rails/spring) is a preloader for Rails. Because `packwerk` loads `Rails`, it can be sped up dramatically by enabling spring. Packwerk supports the usage of Spring. To enable Spring, first run `bin/spring binstub packwerk` which will "springify" the generated binstub. Secondly, spring needs to know about the packwerk spring command when spring is loading. To do that, add `require 'packwerk/spring_command'` to the bottom of `config/spring.rb` in your application. + ### Using a custom ERB parser You can specify a custom ERB parser if needed. For example, if you're using `<%graphql>` tags from https://github.com/github/graphql-client in your ERBs, you can use a custom parser subclass to comment them out so that Packwerk can parse the rest of the file: diff --git a/exe/packwerk b/exe/packwerk index 526cc0315..5f01582e1 100755 --- a/exe/packwerk +++ b/exe/packwerk @@ -3,4 +3,10 @@ require "packwerk" -Packwerk::Cli.new(style: Packwerk::OutputStyles::Coloured.new).run(ARGV.dup) +# 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 +cli = Packwerk::Cli.new(style: Packwerk::OutputStyles::Coloured.new) +cli.run(packwerk_argv) diff --git a/lib/packwerk.rb b/lib/packwerk.rb index 525e5c672..769251fc4 100644 --- a/lib/packwerk.rb +++ b/lib/packwerk.rb @@ -5,11 +5,6 @@ require "active_support" require "fileutils" -# See https://github.com/jonleighton/spring-commands-rspec for an example of this pattern -if defined?(Spring.register_command) - require "packwerk/spring_command" -end - module Packwerk extend ActiveSupport::Autoload diff --git a/lib/packwerk/cli.rb b/lib/packwerk/cli.rb index ade013a30..b0dfef358 100644 --- a/lib/packwerk/cli.rb +++ b/lib/packwerk/cli.rb @@ -92,14 +92,13 @@ 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 && binstub + success = configuration_file && inflections_file && root_package result = if success <<~EOS - 🎉 Packwerk is ready to be used. You can start defining packages and run `packwerk check`. + 🎉 Packwerk is ready to be used. You can start defining packages and run `bin/packwerk check`. For more information on how to use Packwerk, see: https://github.com/Shopify/packwerk/blob/main/USAGE.md EOS else diff --git a/lib/packwerk/generators/binstub.rb b/lib/packwerk/generators/binstub.rb deleted file mode 100644 index 7db3fb00e..000000000 --- a/lib/packwerk/generators/binstub.rb +++ /dev/null @@ -1,42 +0,0 @@ -# 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 deleted file mode 100644 index dfd517c0c..000000000 --- a/lib/packwerk/generators/templates/packwerk +++ /dev/null @@ -1,21 +0,0 @@ -#!/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/lib/packwerk/spring_command.rb b/lib/packwerk/spring_command.rb index 2a97c17f4..1e6b88257 100644 --- a/lib/packwerk/spring_command.rb +++ b/lib/packwerk/spring_command.rb @@ -24,5 +24,6 @@ def call end end + puts "registering command" Spring.register_command("packwerk", SpringCommand.new) end diff --git a/test/unit/generators/binstub_test.rb b/test/unit/generators/binstub_test.rb deleted file mode 100644 index c5f7cc0c2..000000000 --- a/test/unit/generators/binstub_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -# 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