Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion lib/psdk/cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class Configuration
# Filename of the project configuration
PROJECT_CONFIGURATION_FILENAME = '.psdk-cli.yml'

# Path where all the global configuration are stored
PATH = ENV.fetch('PSDK_CLI_DIR', Dir.home || ENV['USERPROFILE'] || '~')

# Filename of the global configuration
GLOBAL_CONFIGURATION_FILENAME = File.join(Dir.home || ENV['USERPROFILE'] || '~', '.psdk-cli/config.yml')
GLOBAL_CONFIGURATION_FILENAME = File.join(PATH, '.psdk-cli/config.yml')

# Create a new configuration
# @param hash [Hash] configuration hash
Expand Down
32 changes: 32 additions & 0 deletions lib/psdk/helpers/psdk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative '../cli/configuration'

module Psdk
module Cli
# Module holding all the utility to interact with PSDK repository
module PSDK
# Default URL to the PSDK repository
MAIN_REPOSITORY_URL = 'https://gitlab.com/pokemonsdk/pokemonsdk.git'

module_function

# Ensure the PSDK module is cloned
def ensure_repository_cloned
return if Dir.exist?(File.join(repository_path, '.git'))

res = system('git', 'clone', MAIN_REPOSITORY_URL, chdir: Configuration::PATH)
return if res

puts "[Error] Failed to setup pokemonsdk repository in `#{Configuration::PATH}`"
exit(1)
end

# Get the repository path
# @return [String]
def repository_path
return File.join(Configuration::PATH, 'pokemonsdk')
end
end
end
end
32 changes: 8 additions & 24 deletions lib/psdk/helpers/studio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ module Studio
module_function

# Find and Save Pokemon studio path
def find_and_save_path
# @param type [:global | :local] where to save the located studio path
def find_and_save_path(type)
locations = common_studio_location.select { |l| Dir.exist?(l) }
binaries_locations = psdk_binaries_locations
studio_path = locations.find { |l| binaries_locations.any? { |b| Dir.exist?(File.join(l, b)) } }
return ask_and_save_studio_path unless studio_path
unless studio_path
puts '[Error] failed to locate Pokemon Studio, please set it up manually'
exit(1)
end

puts "\rLocated Pokemon Studio in `#{studio_path}`"
Configuration.get(:global).studio_path = studio_path
puts "Located Pokemon Studio in `#{studio_path}`"
Configuration.get(type).studio_path = studio_path
Configuration.save
end

Expand All @@ -30,26 +34,6 @@ def psdk_binaries_path(path)
return File.join(path, valid_path)
end

# Ask and save Pokemon Studio path
def ask_and_save_studio_path
print "\rCould not automatically find Pokémon Studio path, please enter it:"
path = $stdin.gets.chomp
check_psdk_binaries_in_provided_location(path)
Configuration.get(:global).studio_path = path
Configuration.save
rescue ArgumentError
retry
end

# Check if a provided path contains the psdk-binaries
# @param path [String]
def check_psdk_binaries_in_provided_location(path)
return if psdk_binaries_locations.any? { |l| Dir.exist?(File.join(path, l)) }

puts 'Provided path does not contain psdk-binaries'
raise ArgumentError
end

# Get all the common Pokemon Studio location
# @return [Array<String>]
def common_studio_location
Expand Down
40 changes: 22 additions & 18 deletions lib/psdk/helpers/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../cli/configuration'
require_relative 'studio'
require_relative 'psdk'

module Psdk
module Cli
Expand All @@ -15,7 +16,7 @@ def run(no_psdk_version)
puts "psdk-cli v#{VERSION}"
return if no_psdk_version

print 'Searching for PSDK version...'
print "Searching for PSDK version...\r"
search_and_show_psdk_version
end

Expand All @@ -27,23 +28,18 @@ def search_and_show_psdk_version

# Search and show the global PSDK version
def search_and_show_global_psdk_version
global_config = Configuration.get(:global)
Studio.find_and_save_path if global_config.studio_path.empty?
psdk_binaries_path = Studio.psdk_binaries_path(global_config.studio_path)
return show_global_psdk_version(psdk_binaries_path) if psdk_binaries_path

puts "\r[Error] Current Pokemon Studio path does not contain psdk-binaries"
global_config.studio_path = ''
raise ArgumentError
rescue ArgumentError
retry
PSDK.ensure_repository_cloned
psdk_path = PSDK.repository_path
show_global_psdk_version(psdk_path)
git_data = load_git_data(psdk_path)
puts "Global PSDK git Target: #{git_data}"
end

# Show the global PSDK version
# @param psdk_binaries_path [String]
def show_global_psdk_version(psdk_binaries_path)
version_string = version_to_string(load_version_integer(File.join(psdk_binaries_path, 'pokemonsdk')))
puts "\rGlobal PSDK version: #{version_string} "
# @param psdk_path [String] Path to the PSDK repository
def show_global_psdk_version(psdk_path)
version_string = version_to_string(load_version_integer(psdk_path))
puts "Global PSDK version: #{version_string} "
end

# Search and show the local PSDK version
Expand All @@ -62,7 +58,15 @@ def search_and_show_local_psdk_version

# Show that there's no local PSDK version
def show_no_local_psdk_version
puts "Project PSDK Version: Studio's PSDK version"
Studio.find_and_save_path(:local) if Configuration.get(:local).studio_path.empty?
psdk_binaries_path = Studio.psdk_binaries_path(Configuration.get(:local).studio_path)
unless psdk_binaries_path
puts 'Project PSDK Version: Cannot locate Pokémon Studio or local repository...'
exit(1)
end

version_string = version_to_string(load_version_integer(File.join(psdk_binaries_path, 'pokemonsdk')))
puts "Project PSDK Version: #{version_string} (Pokémon Studio)"
end

# Load the Git data if any
Expand All @@ -72,8 +76,8 @@ def load_git_data(path)
Dir.chdir(path) do
return '' unless Dir.exist?('.git') || Dir.exist?('../.git')

commit = `git log --oneline -n 1`
branch = `git branch --show-current`
commit = `git log --oneline -n 1`.chomp
branch = `git branch --show-current`.chomp
return "[#{branch}] #{commit}" unless branch.empty?

return "[!detached] #{commit}"
Expand Down
71 changes: 16 additions & 55 deletions spec/psdk/helpers/studio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,6 @@
expect(Psdk::Cli::Studio.common_studio_location).to eq(options)
end

it 'checks if psdk-binaries exists in provided location' do
allow(Dir).to receive(:exist?) { |path| path == '/path/resources/psdk-binaries' }

expect { Psdk::Cli::Studio.check_psdk_binaries_in_provided_location('/path') }.not_to raise_error
end

it 'raises ArgumentError if the psdk-binaries does not exists in provided location' do
allow(Dir).to receive(:exist?) { false }
expect(Psdk::Cli::Studio).to receive(:puts).with('Provided path does not contain psdk-binaries')

expect { Psdk::Cli::Studio.check_psdk_binaries_in_provided_location('/path') }.to raise_error(ArgumentError)
end

it 'asks for Studio path' do
configuration = Psdk::Cli::Configuration.new({})
allow(Dir).to receive(:exist?) { |path| path == '/path/resources/psdk-binaries' }
allow($stdin).to receive(:gets).and_return("/path\n")
expect(Psdk::Cli::Studio).to receive(:print).with(
"\rCould not automatically find Pokémon Studio path, please enter it:"
)
expect(Psdk::Cli::Configuration).to receive(:get).with(:global).and_return(configuration)
expect(configuration).to receive(:studio_path=).with('/path')
expect(Psdk::Cli::Configuration).to receive(:save)

Psdk::Cli::Studio.ask_and_save_studio_path
end

it 'asks for Studio path until a valid one is provided' do
configuration = Psdk::Cli::Configuration.new({})
i = 0
allow(Dir).to receive(:exist?) { |path| path == '/valid_path/resources/psdk-binaries' }
allow($stdin).to receive(:gets) { (i += 1) == 1 ? "/path\n" : "/valid_path\n" }
expect(Psdk::Cli::Studio).to receive(:puts).with('Provided path does not contain psdk-binaries').exactly(1).times
expect(Psdk::Cli::Studio).to(
receive(:print).with("\rCould not automatically find Pokémon Studio path, please enter it:").exactly(2).times
)
expect(Psdk::Cli::Configuration).to receive(:get).with(:global).and_return(configuration).exactly(1).times
expect(configuration).to receive(:studio_path=).with('/valid_path')
expect(Psdk::Cli::Configuration).to receive(:save)

Psdk::Cli::Studio.ask_and_save_studio_path
end

it 'returns the psdk-binaries path based on studio path' do
allow(Dir).to receive(:exist?) { |path| path == '/path/resources/psdk-binaries' }

Expand All @@ -89,33 +46,37 @@
valid_paths = ['/Applications/PokemonStudio.app',
'/Applications/PokemonStudio.app/Contents/Resources/psdk-binaries']
allow(Dir).to receive(:exist?) { |path| valid_paths.include?(path) }
expect(Psdk::Cli::Studio).to receive(:puts).with("\rLocated Pokemon Studio in `/Applications/PokemonStudio.app`")
expect(Psdk::Cli::Studio).to receive(:puts).with('Located Pokemon Studio in `/Applications/PokemonStudio.app`')
expect(Psdk::Cli::Configuration).to receive(:get).with(:global).and_return(configuration)
expect(configuration).to receive(:studio_path=).with('/Applications/PokemonStudio.app')
expect(Psdk::Cli::Configuration).to receive(:save)

Psdk::Cli::Studio.find_and_save_path
Psdk::Cli::Studio.find_and_save_path(:global)
end

it 'ask Studio path if target folder does not contains psdk-binaries' do
it 'exits with error if target folder does not contains psdk-binaries' do
valid_paths = ['/Applications/PokemonStudio.app']
allow(Dir).to receive(:exist?) { |path| valid_paths.include?(path) }
expect(Psdk::Cli::Studio).not_to receive(:puts)
expect(Psdk::Cli::Configuration).not_to receive(:get).with(:global)
expect(Psdk::Cli::Configuration).not_to receive(:get).with(:local)
expect(Psdk::Cli::Configuration).not_to receive(:save)
expect(Psdk::Cli::Studio).to receive(:ask_and_save_studio_path)
expect(Psdk::Cli::Studio).to receive(:exit).with(1) { raise 'exit 1' }
expect(Psdk::Cli::Studio).to receive(:puts).with(
'[Error] failed to locate Pokemon Studio, please set it up manually'
)

Psdk::Cli::Studio.find_and_save_path
expect { Psdk::Cli::Studio.find_and_save_path(:local) }.to raise_error(RuntimeError, 'exit 1')
end

it 'ask Studio path if no common path is found' do
it 'exits with error Studio path if no common path is found' do
allow(Dir).to receive(:exist?) { false }
expect(Psdk::Cli::Studio).not_to receive(:puts)
expect(Psdk::Cli::Configuration).not_to receive(:get).with(:global)
expect(Psdk::Cli::Configuration).not_to receive(:get).with(:local)
expect(Psdk::Cli::Configuration).not_to receive(:save)
expect(Psdk::Cli::Studio).to receive(:ask_and_save_studio_path)
expect(Psdk::Cli::Studio).to receive(:exit).with(1) { raise 'exit 1' }
expect(Psdk::Cli::Studio).to receive(:puts).with(
'[Error] failed to locate Pokemon Studio, please set it up manually'
)

Psdk::Cli::Studio.find_and_save_path
expect { Psdk::Cli::Studio.find_and_save_path(:local) }.to raise_error(RuntimeError, 'exit 1')
end
end
# rubocop:enable Metrics/BlockLength
Loading