snipgem 0.0.5

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 900f13cdab2b7bc759bf4d189b8b3ff6548becfa
4
+ data.tar.gz: c8e4ce5352867021b0f7f2a57dcdbc9fbf640bde
5
+ SHA512:
6
+ metadata.gz: 2d73b6b38d8dc5fab8d306b5e0097b8201f73e31dde617c497d5c211d089dd860c79aea118365741682c5234e392d35d1a966c344de2d7d0d4cf79ec4feb77c9
7
+ data.tar.gz: 4cd33c7bd54192de3050760eb422fb4fe53d0bed91bd9ecbc8159ff82c596c5e3e4ef9b770fd964110947ebcb62d8ef6f9254bfdb89e4a7f8f379398ca5f6575
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in snip.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 jgerminario
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require "bundler/gem_tasks"
2
+ require 'fileutils'
3
+
4
+
5
+ require 'rake'
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc "seed test files"
9
+ task :seed do
10
+ test_str1 = "# <snip>\nclass OrangeTree\n attr_reader :age, :height\n def initialize\n @age = 0\n @oranges = []\n end\n#</snip>\n\n# test <snip> TITLE\n def age!\n @oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5\n end\n#</snip>\n\n# ewre w<snip> test\n def any_oranges?\n !@oranges.empty?\n end\n# test </snip> test\n\n def pick_an_orange!\n"
11
+ test_str2 = "#<$> Title ggg\ndef method\n \"code\"\nend\n#</$>\n\n#<$> Title 2323\ndef method\n \"code\"\nend\n#</$>\n\n#<$> Title 4\ndef method\n \"code\"\nend\n#</$>"
12
+ test_str3 = "# <snip> Title of first snip\n desc \"create the database\"\n task :create do\n puts \"Creating file \#{DB_PATH} if it doesn't exist...\"\n touch DB_PATH\n end\n# </snip>\n\n# <snip> Title of this snip\n desc \"drop the database\"\n task :drop do\n puts \"Deleting \#{DB_PATH}...\"\n rm_f DB_PATH\n end\n# </snip>\n\n# <snip> Title of first snip\n desc \"create the database\"\n task :create do\n puts \"Creating file \#{DB_PATH} if it doesn't exist...\"\n touch DB_PATH\n end\n# </snip>\n\n# <snip> Title of this snip\n desc \"drop the database\"\n task :drop do\n puts \"Deleting \#{DB_PATH}...\"\n rm_f DB_PATH\n end\n# </snip>"
13
+ test_str_js = "//<$> Title ggg\nvar test1 = 44;\nvar test2 = 22;\n//</$>\n\n//<$>\nvar test4 = \"Hola\";\n//</$>\n\n"
14
+
15
+ FileUtils.rm_rf('rspec/test_snip_files')
16
+ Dir.mkdir("rspec/test_snip_files")
17
+ Dir.mkdir("rspec/test_snip_files/subfolder")
18
+
19
+ File.open("my_snips.rb", "w+") do |file|
20
+ file << ""
21
+ end
22
+
23
+ File.open("rspec/test_snip_files/test.rb", "w+") do |file|
24
+ file << test_str1
25
+ end
26
+ File.open("rspec/test_snip_files/test2.rb", "w+") do |file|
27
+ file << test_str2
28
+ end
29
+ File.open("rspec/test_snip_files/subfolder/test3.rb", "w+") do |file|
30
+ file << test_str3
31
+ end
32
+ File.open("rspec/test_snip_files/subfolder/test_js.js", "w+") do |file|
33
+ file << test_str_js
34
+ end
35
+ end
36
+
37
+ desc "Run the specs"
38
+ task :specs do
39
+ RSpec::Core::RakeTask.new(:spec)
40
+ end
41
+
42
+ task :default => :specs
@@ -0,0 +1,20 @@
1
+ require_relative '../models/utils/sourcefilereader'
2
+ require_relative '../models/utils/codescanner'
3
+ require_relative '../models/utils/destinationfilewriter'
4
+
5
+ module CommandLineController
6
+
7
+ extend self
8
+
9
+ def run(file)
10
+ file_read = SourceFileReaderWriter.new(file)
11
+ to_run = file_read.convert_to_array_of_lines
12
+ CodeScanner.run(to_run, SourceFileReaderWriter.file_to_open)
13
+ DestinationFileWriter.run(Snippet.snippet_array)
14
+ DestinationFileWriter.run(Snippet.rb_snippets, "rb") if Snippet.rb_snippets.any?
15
+ DestinationFileWriter.run(Snippet.js_snippets, "js") if Snippet.js_snippets.any?
16
+ file_read.overwrite_existing_snips
17
+
18
+ Snippet.snippet_array = []
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ class Snippet
2
+ attr_accessor :snippet_array
3
+
4
+ @@snippet_array = []
5
+ @@snippet_counter = 0
6
+ attr_reader :code, :title, :line, :filename
7
+
8
+ def self.snippet_array
9
+ @@snippet_array
10
+ end
11
+
12
+ def self.snippet_counter
13
+ @@snippet_counter
14
+ end
15
+
16
+ def self.snippet_array=(arg)
17
+ @@snippet_array = arg
18
+ end
19
+
20
+ def self.rb_snippets
21
+ @@snippet_array.select {|snippet| snippet.filename.end_with?(".rb")}
22
+ end
23
+
24
+ def self.js_snippets
25
+ @@snippet_array.select {|snippet| snippet.filename.end_with?(".js")}
26
+ end
27
+
28
+ def initialize(args = {})
29
+ @code = args[:code]
30
+ @title = args[:title]
31
+ @@snippet_array << self
32
+ @@snippet_counter += 1
33
+ @line = args[:line]
34
+ @filename = args[:filename]
35
+ end
36
+
37
+ end
@@ -0,0 +1,11 @@
1
+ class BatchProcessing
2
+
3
+ def self.process(directory)
4
+ directory = directory.chomp('/')
5
+ Dir.glob(directory + "/**/*.{rb,js}") do |file|
6
+ next if file == '.' or file == '..'
7
+ CommandLineController.run(file)
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,58 @@
1
+ # Input ex ("\n" and whitespace are necessary to keep to preserve formatting):
2
+ #["#<snip>\n", " def age!\n", " @oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5\n", " end\n", "#</snip>\n", "\n", "#<snip>\n", " def any_oranges?\n", " !@oranges.empty?\n", " end\n", "#</snip>\n", "\n"]
3
+
4
+ require_relative "../snippet"
5
+
6
+ class CodeScanner
7
+ @scan_array = []
8
+ class << self; attr_reader :scan_array end
9
+ def self.run(scan_array, filename)
10
+ @scan_array = scan_array
11
+ while @scan_array.join.include?('<snip>') || @scan_array.join.include?('<$>')
12
+ Snippet.new(code: array_range, title: @title, line: @line, filename: filename)
13
+ end
14
+ end
15
+
16
+ def self.find_begin_range
17
+ @scan_array.each_with_index do |line, index|
18
+ if line.include?('<snip>') || line.include?('<$>')
19
+ find_title(index)
20
+ @line = index+1
21
+ strip_snip_tag(index)
22
+ return @begin_scan = index
23
+ end
24
+ end
25
+ return false
26
+ end
27
+
28
+ def self.find_end_range
29
+ @scan_array.each_with_index do |line, index|
30
+ if line.include?('</snip>') || line.include?('</$>')
31
+ strip_snip_tag(index)
32
+ return @end_scan = index
33
+ end
34
+ end
35
+ return false
36
+ end
37
+
38
+ def self.array_range
39
+ find_begin_range
40
+ find_end_range
41
+ @scan_array[@begin_scan+1..@end_scan-1].join
42
+ end
43
+
44
+ def self.strip_snip_tag(index)
45
+ @scan_array[index].sub!(/<snip>/,'<*snip*>')
46
+ @scan_array[index].sub!(/<\/snip>/,'</*snip*>')
47
+ @scan_array[index].sub!(/<\$>/,'<*$*>')
48
+ @scan_array[index].sub!(/<\/\$>/,'</*$*>')
49
+ end
50
+
51
+ def self.find_title(index)
52
+ matches = @scan_array[index].match(/(<snip>|<\$>)(.+)/)
53
+ if matches
54
+ @title = matches[2].strip
55
+ end
56
+ @title
57
+ end
58
+ end
@@ -0,0 +1,100 @@
1
+ # Input ex:
2
+ #[#<Snippet:0x007fc98c9cbd20 @code_array=[" def age!\n", " @oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5\n", " end\n"]>, #<Snippet:0x007fc98c9ca678 @code_array=[" def any_oranges?\n", " !@oranges.empty?\n", " end\n"]>]
3
+
4
+
5
+ require_relative '../../views/viewformatter'
6
+ CONFIG_PATH = File.expand_path("../../../../config/filepath.config", __FILE__)
7
+ LOG_PATH = File.expand_path("../../../../log/snip.log", __FILE__)
8
+
9
+ module DestinationFileWriter
10
+
11
+ extend self
12
+
13
+ def check_config_file_for_snip_file
14
+ if snip_filepath
15
+ check_if_file_still_exists
16
+ else
17
+ abort(ViewFormatter.specify_path)
18
+ end
19
+ end
20
+
21
+ def snip_filepath
22
+ File.readlines(CONFIG_PATH)[0]
23
+ end
24
+
25
+ def check_if_file_still_exists
26
+ if File.exist?(snip_filepath)
27
+ @snip_file_name = snip_filepath
28
+ else
29
+ abort(ViewFormatter.output_file_not_found(snip_file))
30
+ end
31
+ end
32
+
33
+ def run(snippet_array, type=nil)
34
+ directory = File.dirname(@snip_file_name)
35
+ case type
36
+ when "rb"
37
+ filename = directory + "/my_snips.rb"
38
+ File.new(filename, 'w') unless File.exist?(filename)
39
+ when "js"
40
+ filename = directory + "/my_snips.js"
41
+ File.new(filename, 'w') unless File.exist?(filename)
42
+ else
43
+ filename = @snip_file_name
44
+ end
45
+ write_file(snippet_array, filename, type)
46
+ end
47
+
48
+
49
+ def save_file_path_to_config_file(filedir)
50
+ snip_file = File.absolute_path(filedir).chomp('/') + "/my_snips.txt"
51
+ File.open(CONFIG_PATH, "w+") do |f|
52
+ f << snip_file
53
+ end
54
+ unless File.exist?(snip_file)
55
+ File.new(snip_file, 'w')
56
+ abort(ViewFormatter.new_file(snip_file))
57
+ end
58
+ end
59
+
60
+ def determine_next_index(filename)
61
+ last_snip_scan = File.readlines(filename).reverse.join
62
+ if last_snip_scan.match(/\*\*\*\* Snippet (\d+):/)
63
+ last_snip_scan.match(/\*\*\*\* Snippet (\d+):/).captures[0].to_i+1
64
+ else
65
+ 1
66
+ end
67
+ end
68
+
69
+ def write_file(snippet_array, filename, type)
70
+ File.open(filename, "a") do |file|
71
+ snippet_array.each_with_index do |snip_object, index|
72
+ file << ViewFormatter.snippet_indexer(determine_next_index(filename)+index, snip_object.title, type)
73
+ file << ViewFormatter.status_line(snip_object.line, type)
74
+ file << "\n"
75
+ file << snip_object.code
76
+ file << "\n\n"
77
+ unless type
78
+ log = ViewFormatter.snip_terminal_status(snip_object.filename, snip_object.line)
79
+ puts log
80
+ write_to_log_file(log)
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ def write_to_log_file(log)
87
+ File.open(LOG_PATH, "a") do |file|
88
+ file << Time.now.strftime("%m-%d-%Y").to_s + " " + log + "\n"
89
+ end
90
+ end
91
+
92
+ def full_file_directory
93
+ File.absolute_path(@snip_file_name)
94
+ end
95
+
96
+ def log_filepath
97
+ LOG_PATH
98
+ end
99
+
100
+ end
@@ -0,0 +1,63 @@
1
+ # Input ex.
2
+ # #<snip>
3
+ # def age!
4
+ # @oranges += Array.new(rand(1..10)) { Orange.new } if @age > 5
5
+ # end
6
+ # #</snip>
7
+
8
+ # #<snip>
9
+ # def any_oranges?
10
+ # !@oranges.empty?
11
+ # end
12
+ # #</snip>
13
+
14
+ class SourceFileReaderWriter
15
+ attr_reader :file_to_open
16
+
17
+ def self.file_to_open
18
+ @@file_to_open
19
+ end
20
+
21
+ def initialize(file_to_open)
22
+ @@file_to_open = file_to_open
23
+ @array_of_lines = []
24
+ @overwrite = []
25
+ end
26
+
27
+ def convert_to_array_of_lines
28
+ File.open(@@file_to_open, "r").each do |line|
29
+ @array_of_lines << line
30
+ end
31
+ @array_of_lines
32
+ end
33
+
34
+ def overwrite_existing_snips
35
+ File.open(@@file_to_open, "r+").each do |line|
36
+ @overwrite << line
37
+ end
38
+ overwrite
39
+ rewrite_whole_file
40
+ end
41
+
42
+ def overwrite
43
+ @overwrite.each_with_index do |element, index|
44
+ if element.include?('<snip>') || element.include?('<$>')
45
+ @overwrite[index].sub!(/<snip>/,'<*snip*>')
46
+ @overwrite[index].sub!(/<\$>/,'<*$*>')
47
+ end
48
+ if element.include?('</snip>') || element.include?('</$>')
49
+ @overwrite[index].sub!(/<\/snip>/,'</*snip*>')
50
+ @overwrite[index].sub!(/<\/\$>/,'</*$*>')
51
+ end
52
+ end
53
+ end
54
+
55
+ def rewrite_whole_file
56
+ File.open(@@file_to_open, "w") do |file|
57
+ @overwrite.each do |line|
58
+ file << line
59
+ end
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,133 @@
1
+ # View component for file writing operations and possibly terminal output
2
+ require 'date'
3
+
4
+ module ViewFormatter
5
+
6
+ extend self
7
+
8
+ def snippet_indexer(index, title, type)
9
+ if type == "js"
10
+ "// **** Snippet " + (index).to_s + ": #{title} **** \n"
11
+ else
12
+ "# **** Snippet " + (index).to_s + ": #{title} **** \n"
13
+ end
14
+ end
15
+
16
+ def file_not_found
17
+ "File not found, please try again."
18
+ end
19
+
20
+ def invalid_file
21
+ "File must be a .js or .rb file."
22
+ end
23
+
24
+ def success_message(filedir)
25
+ if Snippet.snippet_counter > 0
26
+ "Your snippet file has been successfully updated with #{Snippet.snippet_counter} new snips at: '#{filedir}'"
27
+ else
28
+ "No snips found in the specified files or directories."
29
+ end
30
+ end
31
+
32
+ def status_line(line, type)
33
+ if type == "js"
34
+ "// Snipped from #{SourceFileReaderWriter.file_to_open}:#{line} on #{Time.now.strftime("%m-%d-%Y")}"
35
+ else
36
+ "# Snipped from #{SourceFileReaderWriter.file_to_open}:#{line} on #{Time.now.strftime("%m-%d-%Y")}"
37
+ end
38
+ end
39
+
40
+ def snip_terminal_status(filename, line)
41
+ "#{File.absolute_path(filename)}:#{line} snipped"
42
+ end
43
+
44
+ def specify_path
45
+ "Please specify where you would like to save your snippets by running 'snip -f <filepath>'"
46
+ end
47
+
48
+ def output_file_not_found(file)
49
+ "Output file '#{file}' not found, please specify existing file location or desired location for new file by running 'snip -f <filepath>'"
50
+ end
51
+
52
+ def new_file(filename)
53
+ "New snippet file created at #{File.absolute_path(filename)}"
54
+ end
55
+
56
+ def terminal_help_message
57
+ "
58
+
59
+ *** First time setup: ***
60
+
61
+ snip -f <directory>
62
+
63
+ This specifies the directory where you would like to save your snippets. 'my_snips.txt' will be automatically created if it doesn't already exist. If you move your my_snips.txt file, you will need to run 'snip -f' again.
64
+
65
+ Ruby and JavaScript snippet files will be automatically created in the same directory as the .txt file.
66
+
67
+
68
+ *** To add snip tags into your code: ***
69
+
70
+ Ruby:
71
+
72
+ # <snip> Title goes here
73
+ 5.times do {|x| puts x}
74
+ # </snip>
75
+
76
+ JS:
77
+
78
+ // <snip> Title goes here
79
+ var test = 1234;
80
+ // </snip>
81
+
82
+ You can alternately use <$> and </$>. Your tags will be replaced with <*snip*> and/or <*$*> after running 'snip'.
83
+
84
+
85
+ *** Ongoing usage: ***
86
+
87
+ snip <filename> - process a single file
88
+ snip <directory> - process a directory recursively
89
+ snip -l - view log history if you need to debug previous snips
90
+ snip -d - display your snips in terminal
91
+
92
+ Visit https://github.com/jgerminario/snip for more information or to submit bug reports/feature requests.
93
+
94
+ "
95
+ end
96
+
97
+ def new_file_path
98
+ "Snippet file location saved as #{File.absolute_path(ARGV[1])}/my_snips.txt"
99
+ end
100
+
101
+ def no_args_message
102
+ "Welcome to snip. Type 'snip --help' for help"
103
+ end
104
+
105
+ def specify_filename
106
+ "Please specify a filename after '-f'"
107
+ end
108
+
109
+ def need_to_specify_directory
110
+ "Please specify an existing directory to save your snippets to"
111
+ end
112
+
113
+ def show_log(log_file)
114
+ if File.readlines(log_file)[0]
115
+ File.readlines(log_file)
116
+ else
117
+ "Log file is empty"
118
+ end
119
+ end
120
+
121
+ def show_snips(file)
122
+ if file
123
+ if File.readlines(file)[0]
124
+ File.readlines(file)
125
+ else
126
+ "Snippet file is empty"
127
+ end
128
+ else
129
+ "Snippet file doesn't exist. Specify where you would like to save your snippets by running 'snip -f <filepath>"
130
+ end
131
+ end
132
+
133
+ end
data/bin/snip ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'snip'
4
+
5
+ runner = Snip::Run.new
6
+ runner.execute
File without changes
@@ -0,0 +1,3 @@
1
+ module Snip
2
+ VERSION = "0.0.5"
3
+ end
data/lib/snip.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "snip/version"
2
+ require_relative '../app/controllers/controller'
3
+ require_relative '../app/models/utils/batchprocessing'
4
+
5
+ module Snip
6
+ class Run
7
+ def execute
8
+ unless ARGV[0]
9
+ abort(ViewFormatter.no_args_message)
10
+ end
11
+
12
+ if ARGV[0] == "--help"
13
+ abort(ViewFormatter.terminal_help_message)
14
+ end
15
+
16
+ if ARGV[0] == "-f"
17
+ if !ARGV[1].nil?
18
+ if File.directory?(ARGV[1])
19
+ DestinationFileWriter.save_file_path_to_config_file(ARGV[1])
20
+ abort(ViewFormatter.new_file_path)
21
+ else
22
+ abort(ViewFormatter.need_to_specify_directory)
23
+ end
24
+ else
25
+ abort(ViewFormatter.specify_filename)
26
+ end
27
+ end
28
+
29
+ if ARGV[0] == "-l"
30
+ puts ViewFormatter.show_log(DestinationFileWriter.log_filepath)
31
+ abort
32
+ end
33
+
34
+ if ARGV[0] == "-d"
35
+ puts ViewFormatter.show_snips(DestinationFileWriter.snip_filepath)
36
+ abort
37
+ end
38
+
39
+ DestinationFileWriter.check_config_file_for_snip_file
40
+
41
+ if File.directory?(ARGV[0])
42
+ BatchProcessing.process(ARGV[0])
43
+ elsif File.file?(ARGV[0])
44
+ if ARGV[0].end_with?(".rb") || ARGV[0].end_with?(".js")
45
+ CommandLineController.run(ARGV[0])
46
+ else
47
+ abort(ViewFormatter.invalid_file)
48
+ end
49
+ else
50
+ abort(ViewFormatter.file_not_found)
51
+ end
52
+
53
+ puts ViewFormatter.success_message(DestinationFileWriter.full_file_directory)
54
+ end
55
+ end
56
+ end
data/my_snips.rb ADDED
File without changes
Binary file