Skip to content

Commit

Permalink
Interactive CLI option for move_to_folder (#140)
Browse files Browse the repository at this point in the history
* first pass at creating a "move pack" interactive cli item

Co-authored-by: Perry Hertler <[email protected]>
Co-authored-by: Ashley Willard <[email protected]>
Co-authored-by: Teal Stannard <[email protected]>

* specs for interactive cli changes

* bump gem version

* formatting

* rubocop fix

* flatten dirs and add spec for pack dir selector

* use += instead of <<

* use match_array to ignore ordering

---------

Co-authored-by: Perry Hertler <[email protected]>
Co-authored-by: Ashley Willard <[email protected]>
Co-authored-by: Teal Stannard <[email protected]>
  • Loading branch information
4 people authored Jan 5, 2024
1 parent 19b4919 commit b48cfc4
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GIT
PATH
remote: .
specs:
packs (0.0.38)
packs (0.0.39)
code_ownership (>= 1.33.0)
packs-specification
packwerk
Expand Down
16 changes: 8 additions & 8 deletions lib/packs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
require 'code_ownership'

# Private implementation requires
require 'packs/private'
require 'packs/per_file_processor_interface'
require 'packs/rubocop_post_processor'
require 'packs/update_references_post_processor'
require 'packs/code_ownership_post_processor'
require 'packs/logging'
require 'packs/configuration'
require 'packs/cli'
require_relative 'packs/private'
require_relative 'packs/per_file_processor_interface'
require_relative 'packs/rubocop_post_processor'
require_relative 'packs/update_references_post_processor'
require_relative 'packs/code_ownership_post_processor'
require_relative 'packs/logging'
require_relative 'packs/configuration'
require_relative 'packs/cli'

module Packs
extend T::Sig
Expand Down
4 changes: 2 additions & 2 deletions lib/packs/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typed: strict

require 'packs/user_event_logger'
require 'packs/default_user_event_logger'
require_relative 'user_event_logger'
require_relative 'default_user_event_logger'

module Packs
class Configuration
Expand Down
6 changes: 3 additions & 3 deletions lib/packs/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
require 'rainbow'
require 'sorbet-runtime'

require 'packs/private/file_move_operation'
require 'packs/private/pack_relationship_analyzer'
require 'packs/private/interactive_cli'
require_relative 'private/file_move_operation'
require_relative 'private/pack_relationship_analyzer'
require_relative 'private/interactive_cli'

require 'date'

Expand Down
33 changes: 17 additions & 16 deletions lib/packs/private/interactive_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
# https://github.com/piotrmurach/tty-prompt
require 'tty-prompt'

require 'packs/private/interactive_cli/team_selector'
require 'packs/private/interactive_cli/pack_selector'
require 'packs/private/interactive_cli/file_selector'
require 'packs/private/interactive_cli/use_cases/interface'
require 'packs/private/interactive_cli/use_cases/create'
require 'packs/private/interactive_cli/use_cases/move'
require 'packs/private/interactive_cli/use_cases/add_dependency'
require 'packs/private/interactive_cli/use_cases/get_info'
require 'packs/private/interactive_cli/use_cases/query'
require 'packs/private/interactive_cli/use_cases/make_public'
require 'packs/private/interactive_cli/use_cases/move_to_parent'
require 'packs/private/interactive_cli/use_cases/rename'
require 'packs/private/interactive_cli/use_cases/check'
require 'packs/private/interactive_cli/use_cases/update'
require 'packs/private/interactive_cli/use_cases/validate'
require 'packs/private/interactive_cli/use_cases/lint_package_yml_files'
require_relative 'interactive_cli/team_selector'
require_relative 'interactive_cli/pack_selector'
require_relative 'interactive_cli/pack_directory_selector'
require_relative 'interactive_cli/file_selector'
require_relative 'interactive_cli/use_cases/interface'
require_relative 'interactive_cli/use_cases/create'
require_relative 'interactive_cli/use_cases/move'
require_relative 'interactive_cli/use_cases/move_pack'
require_relative 'interactive_cli/use_cases/add_dependency'
require_relative 'interactive_cli/use_cases/get_info'
require_relative 'interactive_cli/use_cases/query'
require_relative 'interactive_cli/use_cases/make_public'
require_relative 'interactive_cli/use_cases/rename'
require_relative 'interactive_cli/use_cases/check'
require_relative 'interactive_cli/use_cases/update'
require_relative 'interactive_cli/use_cases/validate'
require_relative 'interactive_cli/use_cases/lint_package_yml_files'

module Packs
module Private
Expand Down
28 changes: 28 additions & 0 deletions lib/packs/private/interactive_cli/pack_directory_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# typed: strict

module Packs
module Private
module InteractiveCli
class PackDirectorySelector
extend T::Sig

sig { params(prompt: TTY::Prompt, question_text: String).returns(String) }
def self.select(prompt, question_text: 'Select a directory')
directories = T.let([], T::Array[String])

Packs::Specification.config.pack_paths.each do |path|
directories += Dir.glob(path).select { |f| File.directory? f }
end

prompt.select(
question_text,
directories,
filter: true,
per_page: 10,
show_help: :always
)
end
end
end
end
end
70 changes: 70 additions & 0 deletions lib/packs/private/interactive_cli/use_cases/move_pack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# typed: strict

module Packs
module Private
module InteractiveCli
module UseCases
class MovePack
extend T::Sig
extend T::Helpers
include Interface

sig { override.params(prompt: TTY::Prompt).void }
def perform!(prompt)
move_type = prompt.select(
'What do you want to do?',
{
'Move a child pack to be nested under a parent pack' => :move_to_parent,
'Move a pack to a folder that is not a pack' =>
:move_to_folder
}
)

case move_type
when :move_to_parent
child_pack = PackSelector.single_pack_select(prompt, question_text: 'Please select the child pack that will be nested')
parent_pack = PackSelector.single_pack_select(prompt, question_text: 'Please select the pack that will be the parent')

Packs.move_to_parent!(
parent_name: parent_pack.name,
pack_name: child_pack.name,
per_file_processors: [Packs::RubocopPostProcessor.new, Packs::CodeOwnershipPostProcessor.new]
)
when :move_to_folder
pack = PackSelector.single_pack_select(prompt, question_text: 'Please select the pack that you want to move')
destination = PackDirectorySelector.select(prompt, question_text: "What directory do you want to move #{pack.name} to?")

if Packs.find(destination)
use_move_to_parent = prompt.select(
"The directory #{destination} contains a pack. Add #{pack.last_name} as a dependency?",
{ 'Yes' => true, 'No' => false }
)

if use_move_to_parent
Packs.move_to_parent!(
parent_name: destination,
pack_name: pack.name,
per_file_processors: [Packs::RubocopPostProcessor.new, Packs::CodeOwnershipPostProcessor.new]
)

return
end
end

Packs.move_to_folder!(
pack_name: pack.name,
destination: destination,
per_file_processors: [Packs::RubocopPostProcessor.new, Packs::CodeOwnershipPostProcessor.new]
)
end
end

sig { override.returns(String) }
def user_facing_name
'Move a pack'
end
end
end
end
end
end
31 changes: 0 additions & 31 deletions lib/packs/private/interactive_cli/use_cases/move_to_parent.rb

This file was deleted.

2 changes: 1 addition & 1 deletion packs.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'packs'
spec.version = '0.0.38'
spec.version = '0.0.39'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']

Expand Down
27 changes: 27 additions & 0 deletions spec/packs/private/interactive_cli/pack_directory_selector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'tty/prompt/test'

module Packs
RSpec.describe Private::InteractiveCli::PackDirectorySelector do
let(:prompt) { TTY::Prompt::Test.new }

it 'passes pack subdirectories to the prompt' do
write_file 'packs/admin/.keep'
write_file 'packs/utilities/subdir/.keep'
write_file 'not_packs/some_other_dir/.keep'

expected = [
'packs/admin',
'packs/utilities',
'packs/utilities/subdir'
]

expect(prompt).to receive(:select) do |_, directories, _, _, _|
expect(directories).to match_array(expected)
end.and_return(expected.first)

described_class.select(prompt)
end
end
end
57 changes: 57 additions & 0 deletions spec/packs/private/interactive_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,62 @@ module INPUTS
)
subject
end

it 'allows moving a pack to a folder' do
write_package_yml('packs/my_pack')
`mkdir packs/utilities/`
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::RETURN
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::RETURN
prompt.input << "my_pack\r"
prompt.input << "utilities\r"
prompt.input.rewind
expect(Packs).to receive(:move_to_folder!).with(
pack_name: 'packs/my_pack',
destination: 'packs/utilities',
per_file_processors: anything
)
subject
end

it 'allows moving a pack to a parent when originally attempting to move to folder' do
write_package_yml('packs/my_pack')
write_package_yml('packs/utilities')
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::RETURN
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::RETURN
prompt.input << "my_pack\r"
prompt.input << "utilities\r"
prompt.input << INPUTS::RETURN
prompt.input.rewind
expect(Packs).to receive(:move_to_parent!).with(
pack_name: 'packs/my_pack',
parent_name: 'packs/utilities',
per_file_processors: anything
)
subject
end

it 'allows moving a pack to a parent' do
write_package_yml('packs/child_pack')
write_package_yml('packs/parent_pack')
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::DOWN
prompt.input << INPUTS::RETURN
prompt.input << INPUTS::RETURN
prompt.input << "child\r"
prompt.input << "parent\r"
prompt.input.rewind
expect(Packs).to receive(:move_to_parent!).with(
pack_name: 'packs/child_pack',
parent_name: 'packs/parent_pack',
per_file_processors: anything
)
subject
end
end
end
2 changes: 1 addition & 1 deletion spec/verify_docs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

let(:expected_content) do
all_docs = []
Packs::CLI.all_commands.each do |_command_name, command|
Packs::CLI.all_commands.each_value do |command|
all_docs << <<~DOCUMENTATION
## #{command.description}
`bin/packs #{command.usage}`
Expand Down

0 comments on commit b48cfc4

Please sign in to comment.