repackage 1.0.30 → 1.1.8

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,934 @@
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
+ # === set_extract_to
381
+ #
382
+ # Set the @extract_to variable here.
383
+ # ========================================================================= #
384
+ def set_extract_to(
385
+ i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY # Must be the initial constant as default.
386
+ )
387
+ case i
388
+ # ======================================================================= #
389
+ # === :default
390
+ # ======================================================================= #
391
+ when :default,
392
+ nil
393
+ i = DEFAULT_EXTRACT_TO_THIS_DIRECTORY
394
+ end
395
+ i = i.to_s.dup
396
+ # ======================================================================= #
397
+ # The next clause is an override, in particular for my home system.
398
+ # ======================================================================= #
399
+ if File.directory? '/home/Temp/'
400
+ i = '/home/Temp/repackage/'
401
+ end
402
+ i = '/tmp/' if i.empty? # Hardcoded rescue-step in this case.
403
+ i << '/' unless i.end_with? '/' # A directory has a trailing /.
404
+ @internal_hash[:extract_to_this_directory] = i
405
+ end; alias set_extract_to_this_directory set_extract_to # === set_extract_to_this_directory
406
+
407
+ # ========================================================================= #
408
+ # === extract_to?
409
+ #
410
+ # Defaults to /home/Temp/ on my home system.
411
+ # ========================================================================= #
412
+ def extract_to?
413
+ @internal_hash[:extract_to_this_directory]
414
+ end; alias extract_to extract_to? # === extract_to
415
+ alias extract_to_this_directory? extract_to? # === extract_to_this_directory?
416
+ alias extracted_to? extract_to? # === extracted_to?
417
+
418
+ # ========================================================================= #
419
+ # === orange
420
+ # ========================================================================= #
421
+ def orange(i)
422
+ ::Repackage.orange(i)
423
+ end
424
+
425
+ # ========================================================================= #
426
+ # === shall_we_delete_the_old_archive?
427
+ # ========================================================================= #
428
+ def shall_we_delete_the_old_archive?
429
+ @internal_hash[:shall_we_delete_the_old_archive]
430
+ end
431
+
432
+ # ========================================================================= #
433
+ # === consider_copying_the_new_archive_into_the_current_working_directory
434
+ # ========================================================================= #
435
+ def consider_copying_the_new_archive_into_the_current_working_directory
436
+ _ = away_with_the_archive_type(
437
+ extracted_to?+ # This part is like "/home/Temp/repackage/".
438
+ repackage_which_file?
439
+ )+
440
+ repackage_to_which_format?
441
+ if File.exist?(_)
442
+ cd(:to_the_original_dir)
443
+ new_target = return_pwd+File.basename(_)
444
+ opn; e 'Now copying '+sfile(_)+' to the current directory.'
445
+ opn; e 'It will then reside at '+sfile(new_target)+'.'
446
+ copy_file(_, new_target)
447
+ if File.exist? new_target
448
+ opn; e 'Done! The file can be found '\
449
+ 'at '+sfile(new_target)+' now.'
450
+ set_the_final_location_is_at(File.absolute_path(new_target))
451
+ if shall_we_delete_the_old_archive?
452
+ remove_the_old_archive
453
+ end
454
+ else
455
+ opn; e 'No file at '+sfile(new_target)+' exists, '\
456
+ 'thus we can not move anything.'
457
+ end
458
+ end
459
+ end
460
+
461
+ # ========================================================================= #
462
+ # === start_dir?
463
+ # ========================================================================= #
464
+ def start_dir?
465
+ @internal_hash[:start_dir]
466
+ end
467
+
468
+ # ========================================================================= #
469
+ # === set_start_dir
470
+ # ========================================================================= #
471
+ def set_start_dir(
472
+ i = return_pwd
473
+ )
474
+ @internal_hash[:start_dir] = i
475
+ end
476
+
477
+ # ========================================================================= #
478
+ # === copy_file
479
+ # ========================================================================= #
480
+ def copy_file(this_file, to_that_location)
481
+ FileUtils.cp(this_file, to_that_location)
482
+ end
483
+
484
+ # ========================================================================= #
485
+ # === create_archive_from_this_directory (create tag)
486
+ # ========================================================================= #
487
+ def create_archive_from_this_directory(
488
+ this_directory,
489
+ repackage_to_this_format = repackage_to_which_format?
490
+ )
491
+ esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} #{this_directory}#{repackage_to_this_format} #{File.basename(this_directory)}"
492
+ end; alias package_this_directory create_archive_from_this_directory # === package_this_directory
493
+
494
+ # ========================================================================= #
495
+ # === set_repackage_to_this_format
496
+ #
497
+ # We will repackage to this format here.
498
+ # ========================================================================= #
499
+ def set_repackage_to_this_format(
500
+ i = :default_format
501
+ )
502
+ i = i.downcase if i and i.is_a?(String) # Only want it downcased.
503
+ case i # case tag
504
+ # ======================================================================= #
505
+ # === nil
506
+ # ======================================================================= #
507
+ when nil, # Assume a default here.
508
+ :default,
509
+ :default_format,
510
+ :to_the_default_format
511
+ i = DEFAULT_TARGET_FORMAT_TYPE
512
+ # ======================================================================= #
513
+ # === .tar.xz
514
+ # ======================================================================= #
515
+ when 'xz',
516
+ 'tar.xz'
517
+ i = '.tar.xz'
518
+ # ======================================================================= #
519
+ # === .tar.bz2
520
+ # ======================================================================= #
521
+ when 'bz2',
522
+ '.tar.bz2'
523
+ i = '.tar.bz2'
524
+ # ======================================================================= #
525
+ # === .tar.gz
526
+ # ======================================================================= #
527
+ when 'targz',
528
+ 'gz',
529
+ 'tar.gz'
530
+ i = '.tar.gz'
531
+ else # else tag
532
+ # warn 'Did not find registered format type.'
533
+ end
534
+ @internal_hash[:repackage_to_this_format] = i
535
+ end; alias set_target_format_type set_repackage_to_this_format # === set_target_format_type
536
+ alias format= set_repackage_to_this_format # === format=
537
+
538
+ # ========================================================================= #
539
+ # === is_it_already_in_the_correct_archive_format?
540
+ # ========================================================================= #
541
+ def is_it_already_in_the_correct_archive_format?(this_file)
542
+ this_file.include? target_format?
543
+ end
544
+
545
+ # ========================================================================= #
546
+ # === repackage_to_this_format?
547
+ # ========================================================================= #
548
+ def repackage_to_this_format?
549
+ @internal_hash[:repackage_to_this_format]
550
+ end; alias repackage_to_which_format? repackage_to_this_format? # === repackage_to_which_format?
551
+ alias target_format? repackage_to_this_format? # === target_format?
552
+ alias target_format_type? repackage_to_this_format? # === target_format_type?
553
+
554
+ # ========================================================================= #
555
+ # === determine_file_size
556
+ # ========================================================================= #
557
+ def determine_file_size(
558
+ i = repackage_which_file?
559
+ )
560
+ if i and File.exist?(i)
561
+ @internal_hash[:file_size] = File.stat(i).size? # Obtain some information.
562
+ end
563
+ end
564
+
565
+ # ========================================================================= #
566
+ # === set_repackage_this_file
567
+ # ========================================================================= #
568
+ def set_repackage_this_file(this_file)
569
+ this_file = this_file.first if this_file.is_a? Array
570
+ case this_file
571
+ # ======================================================================= #
572
+ # === :default
573
+ # ======================================================================= #
574
+ when :default,
575
+ nil
576
+ # ===================================================================== #
577
+ # Since as of May 2013 we try to fetch a random file from a list.
578
+ # ===================================================================== #
579
+ _ = Dir['*'].reject {|entry| File.directory? entry}
580
+ this_file = _.first if _.size == 1 # if we only have one entry, continue here.
581
+ # ======================================================================= #
582
+ # === --last
583
+ # ======================================================================= #
584
+ when /^-?-?last(-|_)?downloaded$/i,
585
+ /^-?-?last$/i,
586
+ '-l'
587
+ this_file = File.readlines(LAST_DOWNLOADED_FILE).first
588
+ # ===================================================================== #
589
+ # The format of the file has changed a bit. We have to check
590
+ # whether it includes a '#' character. If so then we discard
591
+ # all that comes after said '#' token.
592
+ # ===================================================================== #
593
+ if this_file.include? '#'
594
+ this_file = this_file[0 .. (this_file.index('#')-1)].strip
595
+ end
596
+ end
597
+ this_file = this_file.to_s.dup # Keep a copy.
598
+ if File.directory?(this_file) and !this_file.end_with?('/')
599
+ this_file << '/'
600
+ end
601
+ if this_file.nil? or this_file.empty?
602
+ raise 'Please provide a valid archive to repackage.'
603
+ end
604
+ @internal_hash[:repackage_this_file] = this_file
605
+ if this_file and File.exist?(this_file)
606
+ # ===================================================================== #
607
+ # === :original_directory_where_the_archive_was_kept
608
+ #
609
+ # This entry point will always refer to the directory where the
610
+ # archive was originally situated in.
611
+ # ===================================================================== #
612
+ @internal_hash[:original_directory_where_the_archive_was_kept] = rds(
613
+ File.absolute_path(
614
+ File.dirname(this_file)
615
+ )+'/'
616
+ )
617
+ end
618
+ end; alias set_package set_repackage_this_file # === set_package
619
+
620
+ # ========================================================================= #
621
+ # === opn
622
+ # ========================================================================= #
623
+ def opn
624
+ if Object.const_defined? :Opn
625
+ Opn.opn(namespace: NAMESPACE)
626
+ else
627
+ # else do nothing.
628
+ end
629
+ end
630
+
631
+ # ========================================================================= #
632
+ # === the_final_location_is_at?
633
+ # ========================================================================= #
634
+ def the_final_location_is_at?
635
+ @internal_hash[:the_new_file_is_at]
636
+ end; alias the_new_file_is_at? the_final_location_is_at? # === the_new_file_is_at?
637
+ alias the_file_is_where? the_final_location_is_at? # === the_file_is_where?
638
+
639
+ # ========================================================================= #
640
+ # === set_the_new_file_is_at
641
+ # ========================================================================= #
642
+ def set_the_new_file_is_at(i = nil)
643
+ @internal_hash[:the_new_file_is_at] = i
644
+ end; alias set_final_location set_the_new_file_is_at # === set_final_location
645
+ alias set_the_final_location_is_at set_the_new_file_is_at # === set_the_final_location_is_at
646
+
647
+ # ========================================================================= #
648
+ # === dir_where_the_archive_resides?
649
+ # ========================================================================= #
650
+ def dir_where_the_archive_resides?
651
+ @internal_hash[:original_directory_where_the_archive_was_kept]
652
+ end; alias the_original_dir? dir_where_the_archive_resides? # === the_original_dir?
653
+
654
+ # ========================================================================= #
655
+ # === menu
656
+ # ========================================================================= #
657
+ def menu(
658
+ i = commandline_arguments?
659
+ )
660
+ if i.is_a? Array
661
+ i.select {|inner_entry|
662
+ inner_entry.start_with?('--')
663
+ }.each {|entry| menu(entry) }
664
+ else
665
+ case i
666
+ # ===================================================================== #
667
+ # Let the user determine the format type here.
668
+ # ===================================================================== #
669
+ when /-?-?format(-|_)?type=(.+)$/i
670
+ set_target_format_type(
671
+ $2.to_s.dup
672
+ )
673
+ end
674
+ end
675
+ end
676
+
677
+ # ========================================================================= #
678
+ # === warn_about_missing_file_then_exit
679
+ # ========================================================================= #
680
+ def warn_about_missing_file_then_exit(
681
+ i = target_file?
682
+ )
683
+ opn;e swarn('The file `')+sfile(i)+
684
+ swarn('` does not exist. Must provide a valid path here.')
685
+ raise "The argument given must be the path to an existing "\
686
+ "(local) file.\nAs the path given does not appear "\
687
+ "to exist, this class can not continue."
688
+ end
689
+
690
+ # ========================================================================= #
691
+ # === run_the_proper_to_archive_command
692
+ #
693
+ # The argument to this method should be a String representing a
694
+ # directory.
695
+ # ========================================================================= #
696
+ def run_the_proper_to_archive_command(
697
+ on_this_directory
698
+ )
699
+ on_this_directory = on_this_directory.dup if on_this_directory.frozen?
700
+ target_format = repackage_to_which_format?
701
+ case target_format # case tag
702
+ # ======================================================================= #
703
+ # === .tar.xz
704
+ # ======================================================================= #
705
+ when /\.tar\.xz/i
706
+ esystem "#{COMMAND_TO_CREATE_A_TAR_XZ_ARCHIVE} "\
707
+ "#{on_this_directory}#{repackage_to_which_format?} "\
708
+ "#{File.basename(on_this_directory)}"
709
+ # ======================================================================= #
710
+ # === .tar.gz
711
+ # ======================================================================= #
712
+ when /\.tar\.gz/i
713
+ esystem "#{COMMAND_TO_CREATE_A_TAR_GZ_ARCHIVE} "\
714
+ "#{on_this_directory}#{repackage_to_which_format?} "\
715
+ "#{File.basename(on_this_directory)}"
716
+ # ======================================================================= #
717
+ # === .tar.bz2
718
+ # ======================================================================= #
719
+ when /\.tar\.bz2/i
720
+ esystem "#{COMMAND_TO_CREATE_A_TAR_BZ2_ARCHIVE} "\
721
+ "#{on_this_directory}#{repackage_to_which_format?} "\
722
+ "#{File.basename(on_this_directory)}"
723
+ # ======================================================================= #
724
+ # === .zip
725
+ # ======================================================================= #
726
+ when /\.zip/i
727
+ esystem "#{COMMAND_TO_CREATE_A_ZIP_ARCHIVE} "\
728
+ "#{on_this_directory}#{repackage_to_which_format?} "\
729
+ "#{File.basename(on_this_directory)}"
730
+ else
731
+ e 'Unknown target format: '+format.to_s
732
+ end
733
+ end
734
+
735
+ # ========================================================================= #
736
+ # === remove_the_old_archive
737
+ #
738
+ # Only call this when we are sure to remove the old source. I recommend
739
+ # to delete the old source. Of course we must make sure to delete the
740
+ # right package.
741
+ # ========================================================================= #
742
+ def remove_the_old_archive(
743
+ i = dir_where_the_archive_resides?+filename?
744
+ )
745
+ opn; e "Removing the old archive at #{sfile(i)}"
746
+ opn; e "next, as requested via the "\
747
+ "#{::Repackage.steelblue('SHALL_WE_DELETE_THE_OLD_ARCHIVE')} "\
748
+ "constant."
749
+ remove(i)
750
+ end
751
+
752
+ # ========================================================================= #
753
+ # === extract (extract tag)
754
+ #
755
+ # Extract it here. Before we do so, though, we must check if the target
756
+ # does not exist yet.
757
+ # ========================================================================= #
758
+ def extract(
759
+ what = filename?,
760
+ extract_to = extract_to?
761
+ )
762
+ what = what.dup if what.frozen?
763
+ unless what.include? '/'
764
+ what = dir_where_the_archive_resides?+File.basename(what)
765
+ end
766
+ remove_extracted_data
767
+ Extracter.extract_what_to(what, extract_to)
768
+ end
769
+
770
+ # ========================================================================= #
771
+ # === do_repackage_the_assigned_files
772
+ # ========================================================================= #
773
+ def do_repackage_the_assigned_files(
774
+ i = @work_on_these_files
775
+ )
776
+ # ======================================================================= #
777
+ # The input may look like this:
778
+ #
779
+ # ["Python-3.10.7.tar.gz"]
780
+ #
781
+ # ======================================================================= #
782
+ i.flatten.compact.each {|this_file|
783
+ # ===================================================================== #
784
+ # Clean up :the_new_file_is_at first:
785
+ # ===================================================================== #
786
+ @internal_hash[:the_new_file_is_at] = nil
787
+ set_the_final_location_is_at(nil) # This is also cleanup.
788
+ cliner
789
+ set_repackage_this_file(this_file)
790
+ determine_file_size if File.exist? this_file
791
+ if File.exist? this_file
792
+ # =================================================================== #
793
+ # === Consider refusing the repackage-action
794
+ #
795
+ # Check whether the file is already in the correct target archive
796
+ # format.
797
+ # =================================================================== #
798
+ if is_it_already_in_the_correct_archive_format?(this_file)
799
+ opn; e rev+'The file at '+sfile(this_file)+' is already in '\
800
+ 'the desired target format ('+target_format?+').'
801
+ opn; e "Repackaging into the same target format makes no sense, "\
802
+ "thus aborting now."
803
+ else
804
+ opn; e 'We will try to repackage this archive into the `'+
805
+ simp(target_format_type?)+'`'
806
+ opn; e 'format type.'
807
+ opn; e 'It will be extracted into the directory '+
808
+ sdir(extract_to?)+'.'
809
+ # ================================================================= #
810
+ # Here we know that the file exists and that it can be repackaged
811
+ # into another target format. Thus, we can repackage it. We
812
+ # will delegate towards a method for this.
813
+ # ================================================================= #
814
+ repackage_this_particular_file(this_file)
815
+ end
816
+ else
817
+ # =================================================================== #
818
+ # Ok - in this case the file does not exist. We will first try
819
+ # to check if we have a number as input; if so we then we may
820
+ # try a glob-action. And if not, we will raise an error.
821
+ #
822
+ # This can be invoked like so:
823
+ #
824
+ # repackagerb 1
825
+ #
826
+ # =================================================================== #
827
+ if this_file and (this_file =~ /^\d$/) # If input is a (positional) number like "3"
828
+ _ = Dir['*'].select {|entry| File.file?(entry) }.
829
+ sort[this_file.to_i - 1]
830
+ do_repackage_these_files([_])
831
+ else
832
+ # ================================================================= #
833
+ # Else we can still try a glob, before giving up.
834
+ # ================================================================= #
835
+ _ = try_glob(this_file) if this_file
836
+ if _.empty?
837
+ warn_about_missing_file_then_exit
838
+ else
839
+ first = _.first
840
+ if first and File.file?(first)
841
+ do_repackage_these_files([first])
842
+ end
843
+ end
844
+ end
845
+ end
846
+ cliner
847
+ }
848
+ end; alias do_repackage_these_files do_repackage_the_assigned_files # === do_repackage_these_files
849
+
850
+ # ========================================================================= #
851
+ # === remove_archive_from
852
+ #
853
+ # The proper order does matter in this method.
854
+ # ========================================================================= #
855
+ def remove_archive_from(i)
856
+ i.delete_suffix('.xz').
857
+ delete_suffix('.gz').
858
+ delete_suffix('.bz2').
859
+ delete_suffix('.zip').
860
+ delete_suffix('.tgz').
861
+ delete_suffix('.tar')
862
+ end; alias remove_extension remove_archive_from # === remove_extensions
863
+ alias remove_file_extension_from remove_archive_from # === remove_file_extension_from
864
+ alias away_with_the_archive_type remove_archive_from # === away_with_the_archive_type
865
+
866
+ # ========================================================================= #
867
+ # === repackage_this_particular_file
868
+ # ========================================================================= #
869
+ def repackage_this_particular_file(this_file)
870
+ opn; e rev+'The file '+sfile(this_file)+' will be repackaged '\
871
+ 'into the '+orange(repackage_to_which_format?)+' format.'
872
+ copy_this_file_to_the_working_directory(this_file)
873
+ working_directory = working_directory?
874
+ cd(working_directory) # Go to the working directory.
875
+ set_start_dir(working_directory)
876
+ opn; e 'Working from the directory `'+sdir(working_directory)+'` next.'
877
+ absolute_path = File.absolute_path(this_file)
878
+ opn; e 'The actual file that will be used for the '\
879
+ 'repackaging'
880
+ opn; e 'step now resides at: '
881
+ e
882
+ e " #{sdir(absolute_path)}"
883
+ e
884
+ # ======================================================================= #
885
+ # Ok, now we have to extract it.
886
+ # ======================================================================= #
887
+ if File.exist? absolute_path
888
+ extract(absolute_path)
889
+ if DELETE_CODE_OF_CONDUCT_FILE_IF_IT_EXISTS
890
+ target = return_pwd+
891
+ remove_archive_from(
892
+ File.basename(repackage_which_file?)
893
+ )
894
+ all_files = Dir[target+'/*']
895
+ if all_files.any? {|file_path| file_path.include?('CODE_OF_CONDUCT') }
896
+ # In this case we found at the least one code-of-conduct files.
897
+ # Delete it now.
898
+ selection = all_files.select {|file_path|
899
+ file_path.include?('CODE_OF_CONDUCT') and File.file?(file_path)
900
+ }
901
+ selection.each {|this_file|
902
+ opn; e 'Now deleting the file '+sfile(this_file)
903
+ opn; e 'as requested by the user.'
904
+ delete_file(this_file)
905
+ }
906
+ end
907
+ end
908
+ # ===================================================================== #
909
+ # If all went well then we can now create the new desired target
910
+ # format. So if that format is '.tar.xz' then we will have to
911
+ # run the proper command that will create such a tarball for
912
+ # us next.
913
+ # ===================================================================== #
914
+ run_the_proper_to_archive_command(
915
+ remove_file_extension_from(
916
+ absolute_path # This here is e. g. "/home/Temp/repackage/htop-3.1.2.tar.gz".
917
+ )
918
+ )
919
+ consider_copying_the_new_archive_into_the_current_working_directory
920
+ end
921
+ end
922
+
923
+ # ========================================================================= #
924
+ # === run (run tag)
925
+ # ========================================================================= #
926
+ def run
927
+ menu
928
+ consider_loading_the_colours_gem
929
+ consider_creating_the_working_directory
930
+ set_work_on_these_files(@commandline_arguments)
931
+ do_repackage_the_assigned_files
932
+ end
933
+
934
+ end; end