Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Interactive CLI #24

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Interactive CLI client
  • Loading branch information
Alex Evanczuk committed Oct 9, 2022
commit 17a72a54629a9e46cda3ca2d0211eb2b2b3ab5f1
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ Style/AccessorGrouping:

Style/NumericPredicate:
Enabled: false

Layout/BlockEndNewline:
Enabled: false
11 changes: 9 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
use_packwerk (0.56.0)
use_packwerk (0.57.0)
code_ownership
colorize
package_protections
Expand Down Expand Up @@ -35,10 +35,11 @@ GEM
json (2.6.2)
method_source (1.0.0)
minitest (5.16.3)
package_protections (2.2.1)
package_protections (2.5.1)
activesupport
parse_packwerk
rubocop
rubocop-packs
rubocop-sorbet
sorbet-runtime
parallel (1.22.1)
Expand Down Expand Up @@ -88,6 +89,12 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-packs (0.0.10)
activesupport
parse_packwerk
rubocop
rubocop-sorbet
sorbet-runtime
rubocop-rspec (2.13.2)
rubocop (~> 1.33)
rubocop-sorbet (0.6.11)
Expand Down
5 changes: 5 additions & 0 deletions bin/packs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
# typed: strict

require_relative '../lib/use_packwerk'
UsePackwerk.start_interactive_mode!
2 changes: 1 addition & 1 deletion bin/use_packwerk
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# typed: strict

require_relative '../lib/use_packwerk'
UsePackwerk::InteractiveCli.start!
UsePackwerk::CLI.start(ARGV)
15 changes: 11 additions & 4 deletions lib/use_packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
require 'use_packwerk/logging'
require 'use_packwerk/configuration'
require 'use_packwerk/cli'
require 'use_packwerk/interactive_cli'

module UsePackwerk
extend T::Sig
Expand All @@ -31,22 +30,30 @@ module UsePackwerk
packs
], T::Array[String])

sig { void }
def self.start_interactive_mode!
Private::InteractiveCli.start!
end

sig do
params(
pack_name: String,
enforce_privacy: T::Boolean,
enforce_dependencies: T.nilable(T::Boolean)
enforce_dependencies: T.nilable(T::Boolean),
team: T.nilable(CodeTeams::Team)
).void
end
def self.create_pack!(
pack_name:,
enforce_privacy: true,
enforce_dependencies: nil
enforce_dependencies: nil,
team: nil
)
Private.create_pack!(
pack_name: pack_name,
enforce_privacy: enforce_privacy,
enforce_dependencies: enforce_dependencies
enforce_dependencies: enforce_dependencies,
team: team
)
end

Expand Down
78 changes: 0 additions & 78 deletions lib/use_packwerk/interactive_cli.rb

This file was deleted.

15 changes: 9 additions & 6 deletions lib/use_packwerk/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require 'use_packwerk/private/file_move_operation'
require 'use_packwerk/private/pack_relationship_analyzer'
require 'use_packwerk/private/interactive_cli'

module UsePackwerk
module Private
Expand Down Expand Up @@ -44,18 +45,19 @@ def self.replace_in_file(file:, find:, replace_with:)
params(
pack_name: String,
enforce_privacy: T::Boolean,
enforce_dependencies: T.nilable(T::Boolean)
enforce_dependencies: T.nilable(T::Boolean),
team: T.nilable(CodeTeams::Team)
).void
end
def self.create_pack!(pack_name:, enforce_privacy:, enforce_dependencies:)
def self.create_pack!(pack_name:, enforce_privacy:, enforce_dependencies:, team:)
Logging.section('👋 Hi!') do
intro = UsePackwerk.config.user_event_logger.before_create_pack(pack_name)
Logging.print_bold_green(intro)
end

pack_name = Private.clean_pack_name(pack_name)

package = create_pack_if_not_exists!(pack_name: pack_name, enforce_privacy: enforce_privacy, enforce_dependencies: enforce_dependencies)
package = create_pack_if_not_exists!(pack_name: pack_name, enforce_privacy: enforce_privacy, enforce_dependencies: enforce_dependencies, team: team)
add_public_directory(package)
add_readme_todo(package)

Expand Down Expand Up @@ -334,10 +336,11 @@ def self.add_readme_todo(package)
params(
pack_name: String,
enforce_privacy: T::Boolean,
enforce_dependencies: T.nilable(T::Boolean)
enforce_dependencies: T.nilable(T::Boolean),
team: T.nilable(CodeTeams::Team)
).returns(ParsePackwerk::Package)
end
def self.create_pack_if_not_exists!(pack_name:, enforce_privacy:, enforce_dependencies:)
def self.create_pack_if_not_exists!(pack_name:, enforce_privacy:, enforce_dependencies:, team: nil)
if PERMITTED_PACK_LOCATIONS.none? { |permitted_location| pack_name.start_with?(permitted_location) }
raise StandardError, "UsePackwerk only supports packages in the the following directories: #{PERMITTED_PACK_LOCATIONS.inspect}. Please make sure to pass in the name of the pack including the full directory path, e.g. `packs/my_pack`."
end
Expand All @@ -351,7 +354,7 @@ def self.create_pack_if_not_exists!(pack_name:, enforce_privacy:, enforce_depend
enforce_privacy: enforce_privacy,
dependencies: [],
metadata: {
'owner' => 'MyTeam'
'owner' => team.nil? ? 'MyTeam' : team.name
},
name: pack_name
)
Expand Down
48 changes: 48 additions & 0 deletions lib/use_packwerk/private/interactive_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# typed: strict

# https://github.com/piotrmurach/tty-prompt
require 'tty-prompt'

require 'use_packwerk/private/interactive_cli/team_selector'
require 'use_packwerk/private/interactive_cli/pack_selector'
require 'use_packwerk/private/interactive_cli/use_cases/interface'
require 'use_packwerk/private/interactive_cli/use_cases/create'
require 'use_packwerk/private/interactive_cli/use_cases/move'
require 'use_packwerk/private/interactive_cli/use_cases/add_dependency'
require 'use_packwerk/private/interactive_cli/use_cases/get_info'
require 'use_packwerk/private/interactive_cli/use_cases/query'
require 'use_packwerk/private/interactive_cli/use_cases/make_public'
require 'use_packwerk/private/interactive_cli/use_cases/nest'
require 'use_packwerk/private/interactive_cli/use_cases/rename'
require 'use_packwerk/private/interactive_cli/use_cases/update_deprecations'
require 'use_packwerk/private/interactive_cli/use_cases/validate'

module UsePackwerk
module Private
module InteractiveCli
extend T::Sig

sig { void }
def self.start!
prompt = TTY::Prompt.new(interrupt: lambda {
puts "\n\nGoodbye! I hope you have a good day."
exit 1 })
help_text = '(Press ↑/↓ arrow to move, Enter to select and letters to filter)'
choice = prompt.select('Hello! What would you like to do?',
cycle: true,
filter: true,
help: help_text,
show_help: :always,
per_page: 15) do |menu|
menu.enum '.'

UseCases::Interface.all.each do |use_case|
menu.choice use_case.user_facing_name, use_case
end
end

choice.perform!(prompt)
end
end
end
end
34 changes: 34 additions & 0 deletions lib/use_packwerk/private/interactive_cli/pack_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# typed: strict

module UsePackwerk
module Private
module InteractiveCli
class PackSelector
extend T::Sig

sig { params(prompt: TTY::Prompt, question_text: String).returns(ParsePackwerk::Package) }
def self.single_pack_select(prompt, question_text: 'Please select a pack')
packs = ParsePackwerk.all.to_h { |t| [t.name, t] }
prompt.select(
question_text,
packs,
filter: true,
per_page: 10,
show_help: :always
)
end

sig { params(prompt: TTY::Prompt, question_text: String).returns(T::Array[ParsePackwerk::Package]) }
def self.single_or_all_pack_multi_select(prompt, question_text: 'Please select one or more packs')
prompt.multi_select(
question_text,
ParsePackwerk.all.to_h { |t| [t.name, t] },
filter: true,
per_page: 10,
show_help: :always
)
end
end
end
end
end
35 changes: 35 additions & 0 deletions lib/use_packwerk/private/interactive_cli/team_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# typed: strict

module UsePackwerk
module Private
module InteractiveCli
class TeamSelector
extend T::Sig

sig { params(prompt: TTY::Prompt, question_text: String).returns(CodeTeams::Team) }
def self.single_select(prompt, question_text: 'Please select a team owner')
teams = CodeTeams.all.to_h { |t| [t.name, t] }
prompt.select(
question_text,
teams,
filter: true,
per_page: 10,
show_help: :always
)
end

sig { params(prompt: TTY::Prompt, question_text: String).returns(T::Array[CodeTeams::Team]) }
def self.multi_select(prompt, question_text: 'Please select team owners')
teams = CodeTeams.all.to_h { |t| [t.name, t] }
prompt.multi_select(
question_text,
teams,
filter: true,
per_page: 10,
show_help: :always
)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# typed: strict

module UsePackwerk
module Private
module InteractiveCli
module UseCases
class AddDependency
extend T::Sig
extend T::Helpers
include Interface

sig { override.params(prompt: TTY::Prompt).void }
def perform!(prompt)
dependent_pack = PackSelector.single_pack_select(prompt, question_text: 'Please select the pack you are adding a dependency to.')
dependency_pack = PackSelector.single_pack_select(prompt, question_text: "Please select the pack that #{dependent_pack.name} should depend on.")
UsePackwerk.add_dependency!(
pack_name: dependent_pack.name,
dependency_name: dependency_pack.name
)
end

sig { override.returns(String) }
def user_facing_name
'Add a dependency'
end
end
end
end
end
end
Loading