Class: Mmi::Interactive::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/mmi/interactive/updater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, file_path = nil) ⇒ Updater

Returns a new instance of Updater.



7
8
9
10
# File 'lib/mmi/interactive/updater.rb', line 7

def initialize(processor, file_path=nil)
	self.processor = processor
	self.file_path = file_path
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/mmi/interactive/updater.rb', line 5

def file_path
  @file_path
end

#processorObject

Returns the value of attribute processor.



4
5
6
# File 'lib/mmi/interactive/updater.rb', line 4

def processor
  @processor
end

Instance Method Details

#add_assetObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mmi/interactive/updater.rb', line 69

def add_asset
	source_type = CLI::UI::Prompt.ask('Choose a source type.') do |handler|
		[
			'github',
		].each do |type|
			handler.option(type, &:to_sym)
		end
		
		handler.option('quit', &:to_sym)
	end
	
	case source_type
	when :quit
		false
	when :github
		options = {
			'source' => {
				'type'     => 'github',
				'asset_id' => 0,
			}
		}
		
		options['source']['owner'      ] = CLI::UI::Prompt.ask('Who is the owner of the source repository?').strip
		options['source']['repo'       ] = CLI::UI::Prompt.ask('What is the name of the source repository?').strip
		options['source']['install_dir'] = CLI::UI::Prompt.ask('In which directory should the asset be placed?', default: 'mods').strip
		options['source']['filename'   ] = CLI::UI::Prompt.ask('Under which filename should the asset be saved? (leave empty for release asset name)', allow_empty: true).strip.then do |filename|
			filename == '' ? nil : filename
		end
		
		options['source'].compact!
		
		source = Mmi::Source::Github.new(options['source'])
		
		if update_asset(source)
			self.processor.content['assets'] ||= []
			
			self.processor.content['assets'].push(options)
			self.processor.assets.assets.push(source)
			
			true
		else
			CLI::UI.puts('Aborting asset addition. No change will be made.', color: CLI::UI::Color::RED)
			
			false
		end
	end
end

#run!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mmi/interactive/updater.rb', line 12

def run!
	while true
		to_update = CLI::UI::Prompt.ask('What do you want to update?') do |handler|
			[
				['assets'                , :assets      ],
				['quit & save changes'   , :quit_save   ],
				['quit & discard changes', :quit_discard],
			].each do |name, result|
				handler.option(name) do |s|
					result
				end
			end
		end
		
		case to_update
		when :assets
			update_assets
		when :quit_save
			file_path = CLI::UI::Prompt.ask('Filename', default: self.file_path, is_file: true)
			yaml      = self.processor.content.to_yaml
			
			File.write(File.expand_path(file_path, Dir.pwd), yaml)
			break
		when :quit_discard
			break
		else
			raise 'Consider yourself lucky, you found a bug.'
		end
	end
end

#update_asset(asset) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mmi/interactive/updater.rb', line 117

def update_asset(asset)
	case asset
	when Mmi::Source::Github
		releases = ::Github::Client::Repos::Releases.new.list(owner: asset.owner, repo: asset.repo, per_page: 100)
		
		release = CLI::UI::Prompt.ask('Choose a release.') do |handler|
			releases.select do |release|
				release.assets.any?
			end.each do |release|
				handler.option(release.name) do |s|
					release
				end
			end
			
			handler.option('quit', &:to_sym)
		end
		
		case release
		when :quit
			false
		else
			release_asset = CLI::UI::Prompt.ask('Choose an asset.') do |handler|
				release.assets.each do |a|
					handler.option(a.name) do |s|
						a
					end
				end
				
				handler.option('quit', &:to_sym)
			end
			
			case release_asset
			when :quit
				false
			else
				asset.options.delete('release')
				asset.options.delete('file'   )
				
				asset.options['asset_id'] = release_asset.id
				
				true
			end
		end
	else
		CLI::UI.puts('This asset cannot be updated.', color: CLI::UI::Color::RED)
		
		false
	end
end

#update_assetsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mmi/interactive/updater.rb', line 43

def update_assets
	while true
		assets = processor.assets.assets
		
		choice = CLI::UI::Prompt.ask('Which asset do you want to change?') do |handler|
			assets.each do |asset|
				handler.option(asset.display_name) do |s|
					asset
				end
			end
			
			handler.option('add' , &:to_sym)
			handler.option('quit', &:to_sym)
		end
		
		case choice
		when :quit
			break
		when :add
			add_asset
		else
			update_asset(choice)
		end
	end
end