Class: Psdk::Cli::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/psdk/cli/configuration.rb

Overview

Class holding the configuration of the Cli

Constant Summary collapse

PROJECT_CONFIGURATION_FILENAME =

Filename of the project configuration

'.psdk-cli.yml'
PATH =

Path where all the global configuration are stored

File.join(ENV.fetch('PSDK_CLI_DIR', Dir.home || ENV['USERPROFILE'] || '~'), '.psdk-cli')
GLOBAL_CONFIGURATION_FILENAME =

Filename of the global configuration

File.join(PATH, 'config.yml')

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Configuration

Create a new configuration

Parameters:

  • hash (Hash)

    configuration hash



20
21
22
23
24
25
26
27
28
29
# File 'lib/psdk/cli/configuration.rb', line 20

def initialize(hash)
  hash = {} unless hash.is_a?(Hash)
  # @type [String]
  @studio_path = ''
  # @type [Array<String>]
  @project_paths = []

  self.studio_path = hash[:studio_path] if hash.key?(:studio_path)
  self.project_paths = hash[:project_paths] if hash.key?(:project_paths)
end

Class Attribute Details

.project_pathString | nil (readonly)

Get the project path

Returns:

  • (String | nil)


78
79
80
# File 'lib/psdk/cli/configuration.rb', line 78

def project_path
  @project_path
end

Instance Attribute Details

#project_pathsArray<String>

Get the project paths

Returns:

  • (Array<String>)


48
49
50
# File 'lib/psdk/cli/configuration.rb', line 48

def project_paths
  @project_paths
end

#studio_pathString

Get the Pokémon Studio path

Returns:

  • (String)


33
34
35
# File 'lib/psdk/cli/configuration.rb', line 33

def studio_path
  @studio_path
end

Class Method Details

.get(type) ⇒ Configuration

Get the configuration

Parameters:

  • type (:global | :local)

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/psdk/cli/configuration.rb', line 83

def get(type)
  @global ||= Configuration.new(load_hash(GLOBAL_CONFIGURATION_FILENAME))
  return @global if type == :global

  project_path = find_project_path
  @local = nil if @project_path != project_path
  @project_path = project_path
  @local ||= Configuration.new(
    @global.to_h.merge(load_hash(File.join(*@project_path, PROJECT_CONFIGURATION_FILENAME)))
  )

  return @local
end

.saveObject

Save the configuration



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/psdk/cli/configuration.rb', line 98

def save
  make_config_global_dir
  File.write(GLOBAL_CONFIGURATION_FILENAME, YAML.dump(@global.to_h))
  return unless @local && @project_path

  local_configuration = @local.to_h
  # Delete global configuration keys
  local_configuration.delete(:project_paths)

  File.write(File.join(@project_path, PROJECT_CONFIGURATION_FILENAME), YAML.dump(local_configuration))
end

Instance Method Details

#to_hObject



65
66
67
68
69
70
# File 'lib/psdk/cli/configuration.rb', line 65

def to_h
  return {
    studio_path: @studio_path,
    project_paths: @project_paths
  }
end