repackage 1.0.30 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,936 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'repackage/class/misc.rb'
6
+ # =========================================================================== #
7
+ module Repackage
8
+
9
+ class Repackage # === Repackage::Repackage
10
+
11
+ # ========================================================================= #
12
+ # === return_pwd
13
+ # ========================================================================= #
14
+ def return_pwd
15
+ "#{Dir.pwd}/".squeeze('/')
16
+ end
17
+
18
+ # ========================================================================= #
19
+ # === register_sigint
20
+ # ========================================================================= #
21
+ def register_sigint
22
+ Signal.trap('SIGINT') { exit }
23
+ end
24
+
25
+ # ========================================================================= #
26
+ # === remove
27
+ #
28
+ # Remove a file or a directory with this method.
29
+ # ========================================================================= #
30
+ def remove(i)
31
+ if File.directory?(i)
32
+ FileUtils.rm_rf(i) unless i.strip == '/' # A tiny safeguard.
33
+ elsif File.file?(i)
34
+ delete_file(i)
35
+ end
36
+ end; alias delete remove # === delete
37
+
38
+ # ========================================================================= #
39
+ # === delete_file
40
+ # ========================================================================= #
41
+ def delete_file(i)
42
+ File.delete(i)
43
+ end
44
+
45
+ # ========================================================================= #
46
+ # === rds
47
+ # ========================================================================= #
48
+ def rds(i)
49
+ i.squeeze '/'
50
+ end
51
+
52
+ # ========================================================================= #
53
+ # === cliner
54
+ # ========================================================================= #
55
+ def cliner
56
+ e rev+('=' * 80)
57
+ end
58
+
59
+ # ========================================================================= #
60
+ # === try_glob
61
+ #
62
+ # Try a glob with this method.
63
+ # ========================================================================= #
64
+ def try_glob(i)
65
+ return Dir[i+'*']
66
+ end
67
+
68
+ # ========================================================================= #
69
+ # === initialize
70
+ #
71
+ # The first argument given to this class should be the name, or the path,
72
+ # to a locally existing file, such as "foobar-1.0.tar.gz".
73
+ # ========================================================================= #
74
+ def initialize(
75
+ commandline_arguments = nil,
76
+ run_already = true
77
+ )
78
+ register_sigint
79
+ reset
80
+ set_commandline_arguments(
81
+ commandline_arguments
82
+ )
83
+ # ======================================================================= #
84
+ # === Handle given blocks next
85
+ # ======================================================================= #
86
+ if block_given?
87
+ yielded = yield
88
+ case yielded
89
+ # ===================================================================== #
90
+ # === :do_not_delete_the_old_source
91
+ # ===================================================================== #
92
+ when :do_not_delete_the_old_source
93
+ set_shall_we_delete_old_source(false)
94
+ # ===================================================================== #
95
+ # === :run_already
96
+ # ===================================================================== #
97
+ when :run_already
98
+ run_already = true
99
+ else
100
+ if yielded.is_a? Hash
101
+ # ================================================================= #
102
+ # === :run
103
+ # ================================================================= #
104
+ if yielded.has_key? :run
105
+ run_already = yielded[:run]
106
+ end
107
+ # ================================================================= #
108
+ # === :delete_code_of_conduct
109
+ # ================================================================= #
110
+ if yielded.has_key? :delete_code_of_conduct
111
+ set_delete_code_of_conduct(
112
+ yielded[:delete_code_of_conduct]
113
+ )
114
+ end
115
+ end
116
+ end
117
+ end
118
+ run if run_already
119
+ end
120
+
121
+ # ========================================================================= #
122
+ # === reset (reset tag)
123
+ # ========================================================================= #
124
+ def reset
125
+ # ======================================================================= #
126
+ # === @namespace
127
+ # ======================================================================= #
128
+ @namespace = NAMESPACE
129
+ # ======================================================================= #
130
+ # === @internal_hash
131
+ # ======================================================================= #
132
+ @internal_hash = {}
133
+ # ======================================================================= #
134
+ # === :delete_code_of_conduct_file_if_it_exists
135
+ # ======================================================================= #
136
+ @internal_hash[:delete_code_of_conduct_file_if_it_exists] = DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS
137
+ # ======================================================================= #
138
+ # === :file_size
139
+ # ======================================================================= #
140
+ @internal_hash[:file_size] = 0
141
+ set_extract_to_this_directory(:default)
142
+ set_start_dir
143
+ # ======================================================================= #
144
+ # === :repackage_into_this_format
145
+ #
146
+ # .tar.xz is the default target format.
147
+ # ======================================================================= #
148
+ set_repackage_to_this_format(:to_the_default_format)
149
+ # ======================================================================= #
150
+ # === :the_new_file_is_at
151
+ #
152
+ # This entry will keep track at which new location the file can be
153
+ # found.
154
+ # ======================================================================= #
155
+ @internal_hash[:the_new_file_is_at] = nil
156
+ set_shall_we_delete_the_old_archive(:default)
157
+ end
158
+
159
+ # ========================================================================= #
160
+ # === set_shall_we_delete_the_old_archive
161
+ # ========================================================================= #
162
+ def set_shall_we_delete_the_old_archive(
163
+ i = :default
164
+ )
165
+ case i
166
+ # ======================================================================= #
167
+ # === :default
168
+ # ======================================================================= #
169
+ when :default,
170
+ nil
171
+ i = SHALL_WE_DELETE_THE_OLD_ARCHIVE
172
+ if is_on_roebe?
173
+ i = true
174
+ end
175
+ end
176
+ @internal_hash[:shall_we_delete_the_old_archive] = i
177
+ end; alias set_shall_we_delete_old_source set_shall_we_delete_the_old_archive # === set_shall_we_delete_old_source
178
+
179
+ # ========================================================================= #
180
+ # === do_delete_code_of_conduct_file
181
+ # ========================================================================= #
182
+ def do_delete_code_of_conduct_file
183
+ @internal_hash[:delete_code_of_conduct_file_if_it_exists] = true
184
+ end
185
+
186
+ # ========================================================================= #
187
+ # === set_delete_code_of_conduct
188
+ # ========================================================================= #
189
+ def set_delete_code_of_conduct(i)
190
+ @internal_hash[:delete_code_of_conduct_file_if_it_exists] = i
191
+ end
192
+
193
+ # ========================================================================= #
194
+ # === repackage_into_which_format?
195
+ # ========================================================================= #
196
+ def repackage_into_which_format?
197
+ @internal_hash[:repackage_into_this_format]
198
+ end; alias repackage_to_which_format? repackage_into_which_format? # === repackage_to_which_format?
199
+ alias format? repackage_into_which_format? # === format?
200
+ alias target_format? repackage_into_which_format? # === target_format?
201
+ alias target_format_type? repackage_into_which_format? # === target_format_type?
202
+
203
+ # ========================================================================= #
204
+ # === set_commandline_arguments
205
+ # ========================================================================= #
206
+ def set_commandline_arguments(i = '')
207
+ i = [i].flatten.compact
208
+ @commandline_arguments = i
209
+ end
210
+
211
+ # ========================================================================= #
212
+ # === commandline_arguments?
213
+ # ========================================================================= #
214
+ def commandline_arguments?
215
+ @commandline_arguments
216
+ end
217
+
218
+ # ========================================================================= #
219
+ # === first_argument?
220
+ # ========================================================================= #
221
+ def first_argument?
222
+ @commandline_arguments.first
223
+ end; alias first? first_argument? # === first?
224
+
225
+ # ========================================================================= #
226
+ # === internal_hash?
227
+ # ========================================================================= #
228
+ def internal_hash?
229
+ @internal_hash
230
+ end; alias hash? internal_hash? # === hash?
231
+
232
+ # ========================================================================= #
233
+ # === is_on_roebe?
234
+ # ========================================================================= #
235
+ def is_on_roebe?
236
+ ENV['IS_ROEBE'].to_s == '1'
237
+ end
238
+
239
+ # ========================================================================= #
240
+ # === set_work_on_these_files
241
+ # ========================================================================= #
242
+ def set_work_on_these_files(
243
+ i = @commandline_arguments
244
+ )
245
+ i = [i].flatten.compact
246
+ i.reject! {|entry| entry.start_with?('--') }
247
+ @work_on_these_files = i
248
+ end
249
+
250
+ # ========================================================================= #
251
+ # === cd (cd tag)
252
+ # ========================================================================= #
253
+ def cd(
254
+ where_to = extract_to?
255
+ )
256
+ case where_to
257
+ # ======================================================================= #
258
+ # === :to_the_start_dir
259
+ # ======================================================================= #
260
+ when :to_the_start_dir,
261
+ :start_dir
262
+ where_to = start_dir?
263
+ # ======================================================================= #
264
+ # === :to_the_original_dir
265
+ # ======================================================================= #
266
+ when :to_the_original_dir
267
+ where_to = the_original_dir?
268
+ end
269
+ Dir.chdir(where_to) if File.directory?(where_to)
270
+ end; alias chdir cd # === chdir
271
+ alias change_dir cd # === change_dir
272
+ alias change_dir_to cd # === change_dir_to
273
+
274
+ # ========================================================================= #
275
+ # === consider_creating_the_working_directory
276
+ # ========================================================================= #
277
+ def consider_creating_the_working_directory(
278
+ i = working_directory?
279
+ )
280
+ unless File.directory? i
281
+ FileUtils.mkdir_p(i)
282
+ end
283
+ end
284
+
285
+ # ========================================================================= #
286
+ # === copy_this_file_to_the_working_directory
287
+ # ========================================================================= #
288
+ def copy_this_file_to_the_working_directory(
289
+ this_file
290
+ )
291
+ target = "#{working_directory?}#{File.basename(this_file)}"
292
+ File.delete(target) if File.exist?(target)
293
+ FileUtils.cp(
294
+ this_file, target
295
+ )
296
+ end
297
+
298
+ # ========================================================================= #
299
+ # === rev
300
+ # ========================================================================= #
301
+ def rev
302
+ ::Repackage.rev
303
+ end
304
+
305
+ # ========================================================================= #
306
+ # === extract_to_this_directory?
307
+ # ========================================================================= #
308
+ def extract_to_this_directory?
309
+ @internal_hash[:extract_to_this_directory]
310
+ end; alias working_directory? extract_to_this_directory? # === working_directory?
311
+
312
+ # ========================================================================= #
313
+ # === simp
314
+ # ========================================================================= #
315
+ def simp(i)
316
+ ::Repackage.simp(i)
317
+ end
318
+
319
+ # ========================================================================= #
320
+ # === sdir
321
+ # ========================================================================= #
322
+ def sdir(i)
323
+ ::Repackage.sdir(i)
324
+ end
325
+
326
+ # ========================================================================= #
327
+ # === sfile
328
+ # ========================================================================= #
329
+ def sfile(i)
330
+ ::Repackage.sfile(i)
331
+ end
332
+
333
+ # ========================================================================= #
334
+ # === swarn
335
+ # ========================================================================= #
336
+ def swarn(i)
337
+ ::Repackage.swarn(i)
338
+ end
339
+
340
+ # ========================================================================= #
341
+ # === consider_loading_the_colours_gem
342
+ # ========================================================================= #
343
+ def consider_loading_the_colours_gem
344
+ ::Repackage.consider_requiring_the_colours_gem
345
+ end
346
+
347
+ # ========================================================================= #
348
+ # === esystem
349
+ # ========================================================================= #
350
+ def esystem(i)
351
+ e i
352
+ system i
353
+ end
354
+
355
+ # ========================================================================= #
356
+ # === repackage_which_file?
357
+ # ========================================================================= #
358
+ def repackage_which_file?
359
+ @internal_hash[:repackage_this_file]
360
+ end; alias filename? repackage_which_file? # === filename?
361
+
362
+ # ========================================================================= #
363
+ # === remove_old_extracted_directory_should_it_exist
364
+ #
365
+ # This method will remove any extracted archive that exits
366
+ # at the target location. This will avoid "unclean"
367
+ # directories.
368
+ # ========================================================================= #
369
+ def remove_old_extracted_directory_should_it_exist(
370
+ i = rds("#{extract_to?}/#{remove_extension(filename?)}")
371
+ )
372
+ if File.directory?(i) # We remove a possibly-existing, extracted directory first.
373
+ e 'Removing the existing directory '\
374
+ 'at `'+sdir(i)+'` next.'
375
+ remove(i)
376
+ end
377
+ end; alias remove_extracted_data remove_old_extracted_directory_should_it_exist # === remove_extracted_data
378
+
379
+ # ========================================================================= #
380
+ # === extract_to?
381
+ #
382
+ # Defaults to /home/x/Temp/ on my home system.
383
+ # ========================================================================= #
384
+ def extract_to?
385
+ @internal_hash[:extract_to_this_directory]
386
+ end; alias extract_to extract_to? # === extract_to
387
+ alias extract_to_this_directory? extract_to? # === extract_to_this_directory?
388
+ alias extracted_to? extract_to? # === extracted_to?
389
+
390
+ # ========================================================================= #
391
+ # === orange
392
+ # ========================================================================= #
393
+ def orange(i)
394
+ ::Repackage.orange(i)
395
+ end
396
+
397
+ # ========================================================================= #
398
+ # === shall_we_delete_the_old_archive?
399
+ # ========================================================================= #
400
+ def shall_we_delete_the_old_archive?
401
+ @internal_hash[:shall_we_delete_the_old_archive]
402
+ end
403
+
404
+ # ========================================================================= #
405
+ # === consider_copying_the_new_archive_into_the_current_working_directory
406
+ # ========================================================================= #
407
+ def consider_copying_the_new_archive_into_the_current_working_directory
408
+ _ = away_with_the_archive_type(
409
+ extracted_to?+ # This part is like "/home/Temp/repackage/".
410
+ repackage_which_file?
411
+ )+
412
+ repackage_to_which_format?
413
+ if File.exist?(_)
414
+ cd(:to_the_original_dir)
415
+ new_target = return_pwd+File.basename(_)
416
+ opn; e 'Now copying '+sfile(_)+' to the current directory.'
417
+ opn; e 'It will then reside at '+sfile(new_target)+'.'
418
+ copy_file(_, new_target)
419
+ if File.exist? new_target
420
+ opn; e 'Done! The file can be found '\
421
+ 'at '+sfile(new_target)+' now.'
422
+ set_the_final_location_is_at(File.absolute_path(new_target))
423
+ if shall_we_delete_the_old_archive?
424
+ remove_the_old_archive
425
+ end
426
+ else
427
+ opn; e 'No file at '+sfile(new_target)+' exists, '\
428
+ 'thus we can not move anything.'
429
+ end
430
+ end
431
+ end
432
+
433
+ # ========================================================================= #
434
+ # === start_dir?
435
+ # ========================================================================= #
436
+ def start_dir?
437
+ @internal_hash[:start_dir]
438
+ end
439
+
440
+ # ========================================================================= #
441
+ # === set_start_dir
442
+ # ========================================================================= #
443
+ def set_start_dir(
444
+ i = return_pwd
445
+ )
446
+ @internal_hash[:start_dir] = i
447
+ end
448
+
449
+ # ========================================================================= #
450
+ # === copy_file
451
+ # ========================================================================= #
452
+ def copy_file(this_file, to_that_location)
453
+ FileUtils.cp(this_file, to_that_location)
454
+ end
455
+
456
+ # ========================================================================= #
457
+ # === create_archive_from_this_directory (create tag)
458
+ # ========================================================================= #
459
+ def create_archive_from_this_directory(
460
+ this_directory,
461
+ repackage_to_this_format = repackage_to_which_format?
462
+ )
463
+ esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} #{this_directory}#{repackage_to_this_format} #{File.basename(this_directory)}"
464
+ end; alias package_this_directory create_archive_from_this_directory # === package_this_directory
465
+
466
+ # ========================================================================= #
467
+ # === set_repackage_to_this_format
468
+ #
469
+ # We will repackage to this format here.
470
+ # ========================================================================= #
471
+ def set_repackage_to_this_format(
472
+ i = :default_format
473
+ )
474
+ i = i.downcase if i and i.is_a?(String) # Only want it downcased.
475
+ case i # case tag
476
+ # ======================================================================= #
477
+ # === nil
478
+ # ======================================================================= #
479
+ when nil, # Assume a default here.
480
+ :default,
481
+ :default_format,
482
+ :to_the_default_format
483
+ i = DEFAULT_TARGET_FORMAT_TYPE
484
+ # ======================================================================= #
485
+ # === .tar.xz
486
+ # ======================================================================= #
487
+ when 'xz',
488
+ 'tar.xz'
489
+ i = '.tar.xz'
490
+ # ======================================================================= #
491
+ # === .tar.bz2
492
+ # ======================================================================= #
493
+ when 'bz2',
494
+ '.tar.bz2'
495
+ i = '.tar.bz2'
496
+ # ======================================================================= #
497
+ # === .tar.gz
498
+ # ======================================================================= #
499
+ when 'targz',
500
+ 'gz',
501
+ 'tar.gz'
502
+ i = '.tar.gz'
503
+ else # else tag
504
+ # warn 'Did not find registered format type.'
505
+ end
506
+ @internal_hash[:repackage_to_this_format] = i
507
+ end; alias set_target_format_type set_repackage_to_this_format # === set_target_format_type
508
+ alias format= set_repackage_to_this_format # === format=
509
+
510
+ # ========================================================================= #
511
+ # === is_it_already_in_the_correct_archive_format?
512
+ # ========================================================================= #
513
+ def is_it_already_in_the_correct_archive_format?(this_file)
514
+ this_file.include? target_format?
515
+ end
516
+
517
+ # ========================================================================= #
518
+ # === repackage_to_this_format?
519
+ # ========================================================================= #
520
+ def repackage_to_this_format?
521
+ @internal_hash[:repackage_to_this_format]
522
+ end; alias repackage_to_which_format? repackage_to_this_format? # === repackage_to_which_format?
523
+ alias target_format? repackage_to_this_format? # === target_format?
524
+ alias target_format_type? repackage_to_this_format? # === target_format_type?
525
+
526
+ # ========================================================================= #
527
+ # === determine_file_size
528
+ # ========================================================================= #
529
+ def determine_file_size(
530
+ i = repackage_which_file?
531
+ )
532
+ if i and File.exist?(i)
533
+ @internal_hash[:file_size] = File.stat(i).size? # Obtain some information.
534
+ end
535
+ end
536
+
537
+ # ========================================================================= #
538
+ # === set_repackage_this_file
539
+ # ========================================================================= #
540
+ def set_repackage_this_file(this_file)
541
+ this_file = this_file.first if this_file.is_a? Array
542
+ case this_file
543
+ # ======================================================================= #
544
+ # === :default
545
+ # ======================================================================= #
546
+ when :default,
547
+ nil
548
+ # ===================================================================== #
549
+ # Since as of May 2013 we try to fetch a random file from a list.
550
+ # ===================================================================== #
551
+ _ = Dir['*'].reject {|entry| File.directory? entry}
552
+ this_file = _.first if _.size == 1 # if we only have one entry, continue here.
553
+ # ======================================================================= #
554
+ # === --last
555
+ # ======================================================================= #
556
+ when /^-?-?last(-|_)?downloaded$/i,
557
+ /^-?-?last$/i,
558
+ '-l'
559
+ this_file = File.readlines(LAST_DOWNLOADED_FILE).first
560
+ # ===================================================================== #
561
+ # The format of the file has changed a bit. We have to check
562
+ # whether it includes a '#' character. If so then we discard
563
+ # all that comes after said '#' token.
564
+ # ===================================================================== #
565
+ if this_file.include? '#'
566
+ this_file = this_file[0 .. (this_file.index('#')-1)].strip
567
+ end
568
+ end
569
+ this_file = this_file.to_s.dup # Keep a copy.
570
+ if File.directory?(this_file) and !this_file.end_with?('/')
571
+ this_file << '/'
572
+ end
573
+ if this_file.nil? or this_file.empty?
574
+ raise 'Please provide a valid archive to repackage.'
575
+ end
576
+ @internal_hash[:repackage_this_file] = this_file
577
+ if this_file and File.exist?(this_file)
578
+ # ===================================================================== #
579
+ # === :original_directory_where_the_archive_was_kept
580
+ #
581
+ # This entry point will always refer to the directory where the
582
+ # archive was originally situated in.
583
+ # ===================================================================== #
584
+ @internal_hash[:original_directory_where_the_archive_was_kept] = rds(
585
+ File.absolute_path(
586
+ File.dirname(this_file)
587
+ )+'/'
588
+ )
589
+ end
590
+ end; alias set_package set_repackage_this_file # === set_package
591
+
592
+ # ========================================================================= #
593
+ # === opn
594
+ # ========================================================================= #
595
+ def opn
596
+ if Object.const_defined? :Opn
597
+ Opn.opn(namespace: NAMESPACE)
598
+ else
599
+ # else do nothing.
600
+ end
601
+ end
602
+
603
+ # ========================================================================= #
604
+ # === the_final_location_is_at?
605
+ # ========================================================================= #
606
+ def the_final_location_is_at?
607
+ @internal_hash[:the_new_file_is_at]
608
+ end; alias the_new_file_is_at? the_final_location_is_at? # === the_new_file_is_at?
609
+ alias the_file_is_where? the_final_location_is_at? # === the_file_is_where?
610
+
611
+ # ========================================================================= #
612
+ # === set_the_new_file_is_at
613
+ # ========================================================================= #
614
+ def set_the_new_file_is_at(i = nil)
615
+ @internal_hash[:the_new_file_is_at] = i
616
+ end; alias set_final_location set_the_new_file_is_at # === set_final_location
617
+ alias set_the_final_location_is_at set_the_new_file_is_at # === set_the_final_location_is_at
618
+
619
+ # ========================================================================= #
620
+ # === dir_where_the_archive_resides?
621
+ # ========================================================================= #
622
+ def dir_where_the_archive_resides?
623
+ @internal_hash[:original_directory_where_the_archive_was_kept]
624
+ end; alias the_original_dir? dir_where_the_archive_resides? # === the_original_dir?
625
+
626
+ # ========================================================================= #
627
+ # === menu
628
+ # ========================================================================= #
629
+ def menu(
630
+ i = commandline_arguments?
631
+ )
632
+ if i.is_a? Array
633
+ i.select {|inner_entry|
634
+ inner_entry.start_with?('--')
635
+ }.each {|entry| menu(entry) }
636
+ else
637
+ case i
638
+ # ===================================================================== #
639
+ # Let the user determine the format type here.
640
+ # ===================================================================== #
641
+ when /-?-?format(-|_)?type=(.+)$/i
642
+ set_target_format_type(
643
+ $2.to_s.dup
644
+ )
645
+ end
646
+ end
647
+ end
648
+
649
+ # ========================================================================= #
650
+ # === warn_about_missing_file_then_exit
651
+ # ========================================================================= #
652
+ def warn_about_missing_file_then_exit(
653
+ i = target_file?
654
+ )
655
+ opn;e swarn('The file `')+sfile(i)+
656
+ swarn('` does not exist. Must provide a valid path here.')
657
+ raise "The argument given must be the path to an existing "\
658
+ "(local) file.\nAs the path given does not appear "\
659
+ "to exist, this class can not continue."
660
+ end
661
+
662
+ # ========================================================================= #
663
+ # === run_the_proper_to_archive_command
664
+ #
665
+ # The argument to this method should be a String representing a
666
+ # directory.
667
+ # ========================================================================= #
668
+ def run_the_proper_to_archive_command(
669
+ on_this_directory
670
+ )
671
+ on_this_directory = on_this_directory.dup if on_this_directory.frozen?
672
+ target_format = repackage_to_which_format?
673
+ case target_format # case tag
674
+ # ======================================================================= #
675
+ # === .tar.xz
676
+ # ======================================================================= #
677
+ when /\.tar\.xz/i
678
+ esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} "\
679
+ "#{on_this_directory}#{repackage_to_which_format?} "\
680
+ "#{File.basename(on_this_directory)}"
681
+ # ======================================================================= #
682
+ # === .tar.gz
683
+ # ======================================================================= #
684
+ when /\.tar\.gz/i
685
+ esystem "#{COMMAND_TO_CREATE_A_TAR_GZ_ARCHIVE} "\
686
+ "#{on_this_directory}#{repackage_to_which_format?} "\
687
+ "#{File.basename(on_this_directory)}"
688
+ # ======================================================================= #
689
+ # === .tar.bz2
690
+ # ======================================================================= #
691
+ when /\.tar\.bz2/i
692
+ esystem "#{COMMAND_TO_CREATE_A_TAR_BZ2_ARCHIVE} "\
693
+ "#{on_this_directory}#{repackage_to_which_format?} "\
694
+ "#{File.basename(on_this_directory)}"
695
+ # ======================================================================= #
696
+ # === .zip
697
+ # ======================================================================= #
698
+ when /\.zip/i
699
+ esystem "#{COMMAND_TO_CREATE_A_ZIP_ARCHIVE} "\
700
+ "#{on_this_directory}#{repackage_to_which_format?} "\
701
+ "#{File.basename(on_this_directory)}"
702
+ else
703
+ e 'Unknown target format: '+format.to_s
704
+ end
705
+ end
706
+
707
+ # ========================================================================= #
708
+ # === remove_the_old_archive
709
+ #
710
+ # Only call this when we are sure to remove the old source. I recommend
711
+ # to delete the old source. Of course we must make sure to delete the
712
+ # right package.
713
+ # ========================================================================= #
714
+ def remove_the_old_archive(
715
+ i = dir_where_the_archive_resides?+filename?
716
+ )
717
+ opn; e "Removing the old archive at #{sfile(i)}"
718
+ opn; e "next, as requested via the "\
719
+ "#{::Repackage.steelblue('SHALL_WE_DELETE_THE_OLD_ARCHIVE')} "\
720
+ "constant."
721
+ remove(i)
722
+ end
723
+
724
+ # ========================================================================= #
725
+ # === extract (extract tag)
726
+ #
727
+ # Extract it here. Before we do so, though, we must check if the target
728
+ # does not exist yet.
729
+ # ========================================================================= #
730
+ def extract(
731
+ what = filename?,
732
+ extract_to = extract_to?
733
+ )
734
+ what = what.dup if what.frozen?
735
+ unless what.include? '/'
736
+ what = dir_where_the_archive_resides?+File.basename(what)
737
+ end
738
+ remove_extracted_data
739
+ Extracter.extract_what_to(what, extract_to)
740
+ end
741
+
742
+ # ========================================================================= #
743
+ # === do_repackage_the_assigned_files
744
+ # ========================================================================= #
745
+ def do_repackage_the_assigned_files(
746
+ i = @work_on_these_files
747
+ )
748
+ # ======================================================================= #
749
+ # The input may look like this:
750
+ #
751
+ # ["Python-3.10.7.tar.gz"]
752
+ #
753
+ # ======================================================================= #
754
+ i.flatten.compact.each {|this_file|
755
+ # ===================================================================== #
756
+ # Clean up :the_new_file_is_at first:
757
+ # ===================================================================== #
758
+ @internal_hash[:the_new_file_is_at] = nil
759
+ set_the_final_location_is_at(nil) # This is also cleanup.
760
+ cliner
761
+ set_repackage_this_file(this_file)
762
+ determine_file_size if File.exist? this_file
763
+ if File.exist? this_file
764
+ # =================================================================== #
765
+ # === Consider refusing the repackage-action
766
+ #
767
+ # Check whether the file is already in the correct target archive
768
+ # format.
769
+ # =================================================================== #
770
+ if is_it_already_in_the_correct_archive_format?(this_file)
771
+ opn; e rev+'The file at '+sfile(this_file)+' is already in '\
772
+ 'the desired target format ('+target_format?+').'
773
+ opn; e "Repackaging into the same target format makes no sense, "\
774
+ "thus aborting now."
775
+ else
776
+ opn; e 'We will try to repackage this archive into the `'+
777
+ simp(target_format_type?)+'`'
778
+ opn; e 'format type.'
779
+ opn; e 'It will be extracted into the directory '+
780
+ sdir(extract_to?)+'.'
781
+ # ================================================================= #
782
+ # Here we know that the file exists and that it can be repackaged
783
+ # into another target format. Thus, we can repackage it. We
784
+ # will delegate towards a method for this.
785
+ # ================================================================= #
786
+ repackage_this_particular_file(this_file)
787
+ end
788
+ else
789
+ # =================================================================== #
790
+ # Ok - in this case the file does not exist. We will first try
791
+ # to check if we have a number as input; if so we then we may
792
+ # try a glob-action. And if not, we will raise an error.
793
+ #
794
+ # This can be invoked like so:
795
+ #
796
+ # repackagerb 1
797
+ #
798
+ # =================================================================== #
799
+ if this_file and (this_file =~ /^\d$/) # If input is a (positional) number like "3"
800
+ _ = Dir['*'].select {|entry| File.file?(entry) }.
801
+ sort[this_file.to_i - 1]
802
+ do_repackage_these_files([_])
803
+ else
804
+ # ================================================================= #
805
+ # Else we can still try a glob, before giving up.
806
+ # ================================================================= #
807
+ _ = try_glob(this_file) if this_file
808
+ if _.empty?
809
+ warn_about_missing_file_then_exit
810
+ else
811
+ first = _.first
812
+ if first and File.file?(first)
813
+ do_repackage_these_files([first])
814
+ end
815
+ end
816
+ end
817
+ end
818
+ cliner
819
+ }
820
+ end; alias do_repackage_these_files do_repackage_the_assigned_files # === do_repackage_these_files
821
+
822
+ # ========================================================================= #
823
+ # === remove_archive_from
824
+ #
825
+ # The proper order does matter in this method.
826
+ # ========================================================================= #
827
+ def remove_archive_from(i)
828
+ i.delete_suffix('.xz').
829
+ delete_suffix('.gz').
830
+ delete_suffix('.bz2').
831
+ delete_suffix('.zip').
832
+ delete_suffix('.tgz').
833
+ delete_suffix('.tar')
834
+ end; alias remove_extension remove_archive_from # === remove_extensions
835
+ alias remove_file_extension_from remove_archive_from # === remove_file_extension_from
836
+ alias away_with_the_archive_type remove_archive_from # === away_with_the_archive_type
837
+
838
+ # ========================================================================= #
839
+ # === repackage_this_particular_file
840
+ # ========================================================================= #
841
+ def repackage_this_particular_file(this_file)
842
+ opn; e rev+'The file '+sfile(this_file)+' will be repackaged '\
843
+ 'into the '+orange(repackage_to_which_format?)+' format.'
844
+ copy_this_file_to_the_working_directory(this_file)
845
+ working_directory = working_directory?
846
+ cd(working_directory) # Go to the working directory.
847
+ set_start_dir(working_directory)
848
+ opn; e 'Working from the directory `'+sdir(working_directory)+'` next.'
849
+ absolute_path = File.absolute_path(this_file)
850
+ opn; e 'The actual file that will be used for the '\
851
+ 'repackaging'
852
+ opn; e 'step now resides at: '
853
+ e
854
+ e " #{sdir(absolute_path)}"
855
+ e
856
+ # ======================================================================= #
857
+ # Ok, now we have to extract it.
858
+ # ======================================================================= #
859
+ if File.exist? absolute_path
860
+ extract(absolute_path)
861
+ if DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS
862
+ target = return_pwd+
863
+ remove_archive_from(
864
+ File.basename(repackage_which_file?)
865
+ )
866
+ all_files = Dir[target+'/*']
867
+ if all_files.any? {|file_path| file_path.include?('CODE_OF_CONDUCT') }
868
+ # In this case we found at the least one code-of-conduct files.
869
+ # Delete it now.
870
+ selection = all_files.select {|file_path|
871
+ file_path.include?('CODE_OF_CONDUCT') and File.file?(file_path)
872
+ }
873
+ selection.each {|this_file|
874
+ opn; e 'Now deleting the file '+sfile(this_file)
875
+ opn; e 'as requested by the user.'
876
+ delete_file(this_file)
877
+ }
878
+ end
879
+ end
880
+ # ===================================================================== #
881
+ # If all went well then we can now create the new desired target
882
+ # format. So if that format is '.tar.xz' then we will have to
883
+ # run the proper command that will create such a tarball for
884
+ # us next.
885
+ # ===================================================================== #
886
+ run_the_proper_to_archive_command(
887
+ remove_file_extension_from(
888
+ absolute_path # This here is e. g. "/home/x/Temp/repackage/htop-3.1.2.tar.gz".
889
+ )
890
+ )
891
+ consider_copying_the_new_archive_into_the_current_working_directory
892
+ end
893
+ end
894
+
895
+ # ========================================================================= #
896
+ # === set_extract_to
897
+ #
898
+ # Set the @extract_to variable here.
899
+ # ========================================================================= #
900
+ def set_extract_to(
901
+ i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY # Must be the initial constant as default.
902
+ )
903
+ case i
904
+ # ======================================================================= #
905
+ # === :default
906
+ # ======================================================================= #
907
+ when :default,
908
+ nil
909
+ i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY
910
+ end
911
+ i = i.to_s.dup
912
+ # ======================================================================= #
913
+ # The next clause is an override, in particular for my home system.
914
+ # ======================================================================= #
915
+ if File.directory? '/home/x/Temp/'
916
+ i = '/home/x/Temp/repackage/'
917
+ elsif File.directory? '/home/Temp/'
918
+ i = '/home/Temp/repackage/'
919
+ end
920
+ i = '/tmp/' if i.empty? # Hardcoded rescue-step in this case.
921
+ i << '/' unless i.end_with? '/' # A directory has a trailing /.
922
+ @internal_hash[:extract_to_this_directory] = i
923
+ end; alias set_extract_to_this_directory set_extract_to # === set_extract_to_this_directory
924
+
925
+ # ========================================================================= #
926
+ # === run (run tag)
927
+ # ========================================================================= #
928
+ def run
929
+ menu
930
+ consider_loading_the_colours_gem
931
+ consider_creating_the_working_directory
932
+ set_work_on_these_files(@commandline_arguments)
933
+ do_repackage_the_assigned_files
934
+ end
935
+
936
+ end; end