lit-cli 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/bin/lit +1 -1
  3. data/lib/config.rb +10 -7
  4. data/lib/lit_cli.rb +26 -9
  5. data/lib/lit_pry.rb +88 -49
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 839f03037b67dd56c7b7d4c38ba26138082594823cc32dbc7217c78363d34088
4
- data.tar.gz: 3ab13437fcb2e6884ccd9465d85c72c0f3f7623b2e4076cd21ea22ba369a160b
3
+ metadata.gz: a7edc257dc260a2ad0eccff8e71eff6217b0e89fde2acd7ea127500cefc58afb
4
+ data.tar.gz: 9f0726175f5f79794f96694e18bae016a819bffe66fed34226ad4ffa694adc1a
5
5
  SHA512:
6
- metadata.gz: 97d737397a07cfd123166f9c233757e0db4606072c75f22235c85d421474bc05bf94ef2ec8e216420b3c1cca9aecd1615c9ad20ade4b76ee6dde3770d36ba89f
7
- data.tar.gz: 7e832c959dd947b99609f2e0ff8e30bb45a8f117b13a617a4671bb4236ee6a1a455f595b8eb35d9d682bc06abe9411e336b2f82553e328cd574215481b565770
6
+ metadata.gz: 323f05467ab8cfa3c23d51fae4a738be4382e7a8d5bad44d9d9a2efc0376d6c139ee970792b35a9749226b913e308556209592d2ffb87318c14a6e6ae272d5b0
7
+ data.tar.gz: facde086d4de3198fbfef692367d4cb31be51e26445ea6527d4f31c6084872e3283d8d43c2c43569b33ac879c7ff764ab2c3281a593474eb6ba46d0829744991
data/bin/lit CHANGED
@@ -8,7 +8,7 @@
8
8
  # lit rails server
9
9
  ################################################################################
10
10
 
11
- ENV['LIT_ENABLED'] = 'true'
11
+ ENV['LIT_ENABLED'] = Time.now.to_i.to_s
12
12
 
13
13
  # Get flags from arguments.
14
14
  args = []
data/lib/config.rb CHANGED
@@ -33,12 +33,12 @@ module LitCLI
33
33
  def initialize()
34
34
 
35
35
  @statuses = {
36
- :info => { icon: "ℹ", color: :blue },
37
- :pass => { icon: "✔", color: :green },
38
- :warn => { icon: "⚠", color: :yellow },
39
- :fail => { icon: "⨯", color: :red },
40
- :error => { icon: "!", color: :red },
41
- :debug => { icon: "?", color: :purple },
36
+ :info => { icon: "ℹ", color: :blue, styles: [:upcase] },
37
+ :pass => { icon: "✔", color: :green, styles: [:upcase] },
38
+ :warn => { icon: "⚠", color: :yellow, styles: [:upcase] },
39
+ :fail => { icon: "⨯", color: :red, styles: [:upcase] },
40
+ :error => { icon: "!", color: :red, styles: [:upcase] },
41
+ :debug => { icon: "?", color: :purple, styles: [:upcase] },
42
42
  }
43
43
 
44
44
  @types = nil
@@ -70,7 +70,10 @@ module LitCLI
70
70
 
71
71
  # Override config from command line.
72
72
  def cli_configure()
73
- @enabled = true if ENV['LIT_ENABLED'] == 'true'
73
+
74
+ # Enable lit via the command line.
75
+ @enabled = true if ENV['LIT_ENABLED'].to_i >= (Time.now.to_i() - 1)
76
+ return unless @enabled
74
77
 
75
78
  # Convert flag string to hash.
76
79
  flags = {}
data/lib/lit_cli.rb CHANGED
@@ -12,41 +12,58 @@ module LitCLI
12
12
  if @@config.enabled
13
13
  return if LitCLI.filter_status? status
14
14
  return if LitCLI.filter_type? type
15
+
15
16
  LitCLI.render(message, status, type, context)
17
+
16
18
  LitCLI.step()
19
+ yield if block_given?
20
+
17
21
  LitCLI.delay()
18
22
  end
19
23
  end
20
24
  alias 🔥 lit
21
25
 
22
26
  def self.render(message, status, type, context)
23
- text = "🔥"
27
+ output = "🔥"
24
28
 
25
29
  # Time.
26
30
  time = LitCLI.format(Time.now().strftime("%k:%M"), color: :cyan)
27
- text << " #{time}"
31
+ output << " #{time}"
28
32
 
29
33
  # Status.
30
34
  config = @@config.statuses[status]
31
- text << LitCLI.format(" #{config[:icon]} #{status.to_s}", config)
35
+ output << LitCLI.format(" #{config[:icon]} #{status.to_s}", config)
32
36
 
33
37
  # Type.
34
38
  if !@@config.types.nil? && @@config.types.has_key?(type)
35
39
  config = @@config.types[type]
36
40
  if config.has_key? :icon
37
- text << LitCLI.format(" #{config[:icon]} #{type.to_s}", config)
41
+ output << LitCLI.format(" #{config[:icon]} #{type.to_s}", config)
38
42
  else
39
- text << LitCLI.format(" #{type.to_s}", config)
43
+ output << LitCLI.format(" #{type.to_s}", config)
40
44
  end
41
45
  end
42
46
 
43
47
  # Context.
44
- text << LitCLI.format(" #{context}", styles: [:bold, :dim])
48
+ output << LitCLI.format(" #{context}", styles: [:bold, :dim])
45
49
 
46
50
  # Message.
47
- text << " #{message}"
51
+ while message.start_with?('^')
52
+ message.delete_prefix!('^').strip!
53
+ unless @@config.status || @@config.type
54
+ output = "\n" + output
55
+ end
56
+ end
57
+
58
+ while message.start_with?('>')
59
+ message.delete_prefix!('>').strip!
60
+ unless @@config.status || @@config.type
61
+ output = ' ' + output
62
+ end
63
+ end
48
64
 
49
- puts text
65
+ output << " #{message}"
66
+ puts output
50
67
  end
51
68
 
52
69
  def self.step()
@@ -145,7 +162,7 @@ module LitCLI
145
162
  def self.configure()
146
163
  yield(@@config)
147
164
 
148
- # Override config from command line.
165
+ # Override config from flags on command line.
149
166
  @@config.cli_configure()
150
167
  end
151
168
 
data/lib/lit_pry.rb CHANGED
@@ -1,70 +1,109 @@
1
1
  # Only override kernel methods when Lit's @step flag is true.
2
2
  if ENV['LIT_FLAGS'] && ENV['LIT_FLAGS'].include?('step')
3
+ require 'set'
4
+
5
+ # TODO: Investigate RubyGems `require` before overriding.
6
+ # SEE: https://github.com/ruby/ruby/blob/v2_6_3/lib/rubygems/core_ext/kernel_require.rb
7
+
3
8
  module Kernel
4
9
 
5
- def require_relative relative_path
6
- filename = relative_path
10
+ @@lit_processed_paths = Set.new
7
11
 
8
- # Get directory of the file that called this file.
9
- absolute_path = caller_locations.first.absolute_path.split('/')
10
- absolute_path.pop
12
+ def require_relative relative_path, current_directory = nil
13
+ return false if relative_path.nil?
11
14
 
12
- # When relative path is current directory.
13
- if relative_path.start_with?('./')
14
- filename = relative_path.delete_prefix! './'
15
- end
15
+ # Handle absolute path.
16
+ if relative_path.start_with?('/') && File.exist?(relative_path)
17
+ absolute_path = relative_path.split('/')
18
+ file_name = absolute_path.pop
19
+ # Handle relative path.
20
+ else
21
+ # Get directory of the file that called this file.
22
+ if current_directory.nil?
23
+ absolute_path = caller_locations.first.absolute_path.split('/')
24
+ absolute_path.pop
25
+ else
26
+ absolute_path = current_directory.split('/')
27
+ end
28
+
29
+ # When path is current directory.
30
+ if relative_path.start_with?('./')
31
+ relative_path.delete_prefix! './'
32
+ end
33
+
34
+ # When path is parent directory.
35
+ while relative_path.start_with?('../')
36
+ relative_path.delete_prefix! '../'
37
+ absolute_path.pop
38
+ end
39
+
40
+ # When path contains child directories.
41
+ if relative_path.split('/').count > 1
42
+ # Add child directories to absolute path and remove from relative path.
43
+ child_path = relative_path.split('/')
44
+ relative_path = child_path.pop
45
+ child_path.each do |dir|
46
+ absolute_path << dir
47
+ end
48
+ end
16
49
 
17
- # When relative path is parent directory.
18
- while relative_path.start_with?('../')
19
- relative_path.delete_prefix! '../'
20
- absolute_path.pop
50
+ file_name = relative_path
21
51
  end
22
52
 
23
53
  # Append default extension.
24
- unless filename.end_with? '.rb'
25
- filename << '.rb'
54
+ unless file_name.end_with? '.rb'
55
+ file_name << '.rb'
26
56
  end
27
57
 
28
- filepath = File.join(absolute_path.join('/'), relative_path)
58
+ file_path = File.join(absolute_path.join('/'), file_name)
29
59
 
30
- # Add pry binding beneath each lit message.
31
- new_lines = ''
32
- File.foreach(filepath) do |line|
33
- new_lines << line
34
- if line.strip.start_with? 'lit "'
35
- new_lines << "binding.pry if LitCLI.is_prying?\n"
36
- new_lines << "@@is_prying = false\n"
60
+ unless @@lit_processed_paths.include? file_path
61
+ @@lit_processed_paths.add file_path
62
+
63
+ new_lines = ''
64
+ line_count = 0
65
+ new_lines_count = 0
66
+
67
+ File.foreach(file_path) do |line|
68
+ line_count = line_count + 1
69
+
70
+ # Pass current directory into the next file's requires.
71
+ if line.strip.start_with? 'require_relative '
72
+ line = line.strip + ", '#{absolute_path.join('/')}'\n"
73
+ new_lines << line
74
+ # Add pry binding on each lit method.
75
+ elsif lit_line = Kernel.add_lit_binding(line)
76
+ new_lines << lit_line
77
+ else
78
+ new_lines << line
79
+ end
37
80
  end
81
+
82
+ eval(new_lines, get_binding(__dir__ = absolute_path), file_path)
83
+
84
+ return true
85
+ # Path has already been required.
86
+ else
87
+ return false
38
88
  end
89
+ end
39
90
 
40
- eval(new_lines)
91
+ def get_binding(__dir__)
92
+ # Don't go to this binding when Pry session activated.
93
+ # The variables have already been evaluated by eval(). NEEDS CONFIRMATION.
94
+ # Setting file_path in eval() negates this fix but will keep just in case.
95
+ return binding unless LitCLI.is_prying?
41
96
  end
42
97
 
43
- # TODO: Investigate RubyGems `require` before overriding.
44
- # SEE: https://github.com/ruby/ruby/blob/v2_6_3/lib/rubygems/core_ext/kernel_require.rb
45
- #
46
- # def require path
47
- # absolute_path = File.join(Dir.pwd, path)
48
- # #p "requiring #{absolute_path}"
49
- # #p __dir__
50
- # #p File.dirname(File.realpath(__FILE__))
51
- # #p File.realpath(__FILE__)
52
- #
53
- # # Split client-side and server-side code.
54
- # new_lines = []
55
- # binding_line = "binding.pry if LitCLI.is_prying?"
56
- #
57
- # # Add pry binding beneath each lit message.
58
- # p path
59
- # File.foreach(path) do |line|
60
- # new_lines << line
61
- # if line.strip.start_with? 'lit "'
62
- # new_lines << binding_line
63
- # end
64
- # end
65
- #
66
- # eval(new_lines.join)
67
- # end
98
+ def self.add_lit_binding(line)
99
+ ['lit ', 'lit(', '🔥 ', '🔥(', '🔥'].each do |needle|
100
+ if line.strip.start_with? needle
101
+ args = line.strip.delete_prefix(needle).delete_suffix(")").chomp
102
+ return "lit(#{args}) { binding.pry if LitCLI.is_prying?; @@is_prying = false } \n"
103
+ end
104
+ end
68
105
 
106
+ false
107
+ end
69
108
  end
70
109
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-08 00:00:00.000000000 Z
11
+ date: 2021-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel