Module: Psdk::Cli::Version

Defined in:
lib/psdk/helpers/version.rb

Overview

Module holding all the logic about the version command

Class Method Summary collapse

Class Method Details

.load_git_data(path) ⇒ String

Load the Git data if any

Parameters:

  • path (String)

    path to the PSDK installation

Returns:

  • (String)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/psdk/helpers/version.rb', line 75

def load_git_data(path)
  Dir.chdir(path) do
    return '' unless Dir.exist?('.git') || Dir.exist?('../.git')

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

    return "[!detached] #{commit}"
  end
end

.load_version_integer(path) ⇒ Integer

Get the version integer from a path

Parameters:

  • path (String)

    path where PSDK repository is

Returns:

  • (Integer)


97
98
99
100
101
102
# File 'lib/psdk/helpers/version.rb', line 97

def load_version_integer(path)
  filename = File.join(path, 'version.txt')
  return 0 unless File.exist?(filename)

  return File.read(filename).to_i
end

.run(no_psdk_version) ⇒ Object

Run the version command

Parameters:

  • no_psdk_version (Boolean)

    do not show PSDK version if true



15
16
17
18
19
20
21
# File 'lib/psdk/helpers/version.rb', line 15

def run(no_psdk_version)
  puts "psdk-cli v#{VERSION}"
  return if no_psdk_version

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

.search_and_show_global_psdk_versionObject

Search and show the global PSDK version



30
31
32
33
34
35
36
# File 'lib/psdk/helpers/version.rb', line 30

def search_and_show_global_psdk_version
  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

.search_and_show_local_psdk_versionObject

Search and show the local PSDK version



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/psdk/helpers/version.rb', line 46

def search_and_show_local_psdk_version
  Configuration.get(:local)
  return unless Configuration.project_path

  psdk_path = File.join(Configuration.project_path, 'pokemonsdk')
  return show_no_local_psdk_version unless Dir.exist?(psdk_path)

  version_string = version_to_string(load_version_integer(psdk_path))
  puts "Project PSDK version: #{version_string}"
  git_data = load_git_data(psdk_path)
  puts "Project's PSDK git target: #{git_data}" unless git_data.empty?
end

.search_and_show_psdk_versionObject

Search and show the PSDK version



24
25
26
27
# File 'lib/psdk/helpers/version.rb', line 24

def search_and_show_psdk_version
  search_and_show_global_psdk_version
  search_and_show_local_psdk_version
end

.show_global_psdk_version(psdk_path) ⇒ Object

Show the global PSDK version

Parameters:

  • psdk_path (String)

    Path to the PSDK repository



40
41
42
43
# File 'lib/psdk/helpers/version.rb', line 40

def show_global_psdk_version(psdk_path)
  version_string = version_to_string(load_version_integer(psdk_path))
  puts "Global PSDK version: #{version_string}       "
end

.show_no_local_psdk_versionObject

Show that there's no local PSDK version



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/psdk/helpers/version.rb', line 60

def show_no_local_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

.version_to_string(version) ⇒ String

Convert a version integer to a version string

Parameters:

  • version (Integer)

Returns:

  • (String)


90
91
92
# File 'lib/psdk/helpers/version.rb', line 90

def version_to_string(version)
  return [version].pack('I>').unpack('C*').join('.').gsub(/^(0\.)+/, '')
end