central 0.2.7 → 0.2.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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/bin/central +17 -3
  3. data/lib/central.rb +44 -6
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2c7c6eb1c4333daf4ee604010033b355478fb24a
4
- data.tar.gz: 58c97710c8336a5b88a6365519142a061c879b99
2
+ SHA256:
3
+ metadata.gz: 673d289d151554b977cdbc987fbd5ea65323e605a9e5b193d5a1908b3738ea62
4
+ data.tar.gz: a4a52750319b99b127496db875ddbe5df46378a8021c5b8b77bf6ac0f9457859
5
5
  SHA512:
6
- metadata.gz: 962163fac50d3d75242f9f2d1795c2fa78821217bfb33591ddcacb058ddc154beab2a20b1ddaf64b3f486fd7c0cd89baaca3eae2dc24f2333b3a21f84d8c94d8
7
- data.tar.gz: 23b8b04c1a7f392860bc884a2c764ea96dcbf5eb4dbb4ae9cd502f980d49f78c889b5ad7c2c8403b32bcff2e46a6fe1020304812dfd08d77da8250a883fb39ce
6
+ metadata.gz: c1dafe44846a27daf9911281bf721ff68bddc004df2e14822fb7b3e4dd1fa7bb87a8a9cedc197e0ec6c2519aa95c4f73ac8b0ccc85d652685bf46ca0f763e339
7
+ data.tar.gz: '016499d9cd74e3a1a6bb78a123a60f44ddff74bb25198bb205eda2d8f90834467f4b3cf274544ef4d181cde77ed99e3ebf702f68d328314162808f2761633550'
@@ -10,12 +10,26 @@ require 'central'
10
10
 
11
11
  unless ARGV.empty?
12
12
  if ARGV[0] == '-v' || ARGV[0] == '--version' || ARGV[0] == '-version'
13
- puts 'central v0.2.7'
13
+ puts 'central v0.2.8'
14
14
  exit 0
15
15
  end
16
16
  if ARGV[0] == '-h' || ARGV[0] == '--help' || ARGV[0] == '-help'
17
- puts 'central [path/to/configuration.rb]'
17
+ puts 'central v0.2.8 - created by Dmitry Geurkov (d.geurkov@gmail.com)'
18
+ puts ' and licensed under LGPLv3'
19
+ puts ''
20
+ puts 'Usage: central [monitor] [path/to/configuration.rb]'
21
+ puts ''
22
+ puts 'Description: [monitor] - will monitor erb files for changes'
23
+ puts ' reprocess them automatically, disabled by default'
24
+ puts ' if no [path/to/configuration.rb] is specified'
25
+ puts ' it will use configuration.rb in current working directory'
18
26
  exit 0
19
27
  end
20
28
  end
21
- central(ARGV)
29
+ if ARGV[0] == 'monitor'
30
+ ARGV.shift
31
+ run_central(ARGV)
32
+ run_monitors
33
+ else
34
+ run_central(ARGV)
35
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -------------------------------------------------------------------------
2
4
  # # central - dot files manager licensed under LGPLv3 (see LICENSE file) |
3
5
  # # written in Ruby by Dmitry Geurkov (d.geurkov@gmail.com) |
@@ -12,6 +14,9 @@ require 'fileutils'
12
14
  COLOR_RED = 31
13
15
  COLOR_GREEN = 32
14
16
 
17
+ # monitors
18
+ $monitors = {}
19
+
15
20
  # putsc, puts with color
16
21
  def color(message, color)
17
22
  "\e[#{color}m#{message}\e[0m"
@@ -74,10 +79,10 @@ end
74
79
  def shell(command, verbose: false, silent: true)
75
80
  info 'Executing', command if verbose
76
81
  exit_code = nil
77
- stdout = ''
78
- stdout_line = ''
79
- stderr = ''
80
- stderr_line = ''
82
+ stdout = String.new
83
+ stdout_line = String.new
84
+ stderr = String.new
85
+ stderr_line = String.new
81
86
  Open3.popen3(command) do |_, o, e, t|
82
87
  stdout_open = true
83
88
  stderr_open = true
@@ -384,13 +389,16 @@ def copy(from, to)
384
389
  end
385
390
 
386
391
  # process erb template into an output_file
387
- def erb(file, output_file = nil)
392
+ def erb(file, output_file = nil, monitor = true)
388
393
  file = abs(file)
389
394
  fail 'No erb file found', file unless file_exists?(file)
390
395
 
391
396
  if output_file.nil?
392
397
  output_file = file.end_with?('.erb') ? file[0...-4] : file + '.out'
393
398
  end
399
+
400
+ $monitors[file] = proc { erb(file, output_file, false) } if monitor
401
+
394
402
  out = ERB.new(File.read(file)).result
395
403
  return if File.exist?(output_file) && File.read(output_file) == out
396
404
 
@@ -398,6 +406,14 @@ def erb(file, output_file = nil)
398
406
  info 'Processed erb', "#{file} → #{output_file}"
399
407
  end
400
408
 
409
+ # monitor file for changes and execute proc if file changed
410
+ def monitor(file, &block)
411
+ file = abs(file)
412
+ fail 'No file found', file unless file_exists?(file)
413
+
414
+ $monitors[file] = block
415
+ end
416
+
401
417
  # run configuration.rb file
402
418
  def run(file)
403
419
  cwd = pwd
@@ -417,7 +433,7 @@ def run_if_exists(file)
417
433
  end
418
434
 
419
435
  # run central configuration
420
- def central(configurations)
436
+ def run_central(configurations)
421
437
  if configurations.instance_of?(Array) && !configurations.empty?
422
438
  configurations.each { |configuration| run configuration }
423
439
  elsif configurations.instance_of?(String)
@@ -426,3 +442,25 @@ def central(configurations)
426
442
  run 'configuration.rb'
427
443
  end
428
444
  end
445
+
446
+ # run monitors
447
+ def run_monitors
448
+ info 'Monitoring files for changes (press Ctrl-C to stop)'
449
+ file_mtimes = {}
450
+ $monitors.keys.each { |f| file_mtimes[f] = File.mtime(f) }
451
+ loop do
452
+ $monitors.keys.each do |f|
453
+ file_mtime = File.mtime(f)
454
+ next if file_mtime == file_mtimes[f]
455
+
456
+ info 'File modified', f
457
+ $monitors[f].call
458
+ file_mtimes[f] = file_mtime
459
+ end
460
+ begin
461
+ sleep(0.5)
462
+ rescue Interrupt
463
+ exit 0
464
+ end
465
+ end
466
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: central
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Geurkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-26 00:00:00.000000000 Z
11
+ date: 2020-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: central dotfile management system
14
14
  email: d.geurkov@gmail.com
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.5.2.1
42
+ rubygems_version: 2.7.6
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: central dotfile management