code_caser 0.0.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +8 -0
- data/LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +0 -0
- data/lib/code_caser/caser.rb +78 -0
- data/lib/code_caser/converters.rb +29 -0
- data/lib/code_caser/version.rb +3 -0
- data/lib/code_caser.rb +12 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e4a7c32894314d1eb738386aa4aee1cc5ed4c59c
|
4
|
+
data.tar.gz: 496e85e0b72e0c2535133a7779310fcd8a5d4c6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbce0c53b959a3ef7023c02f2325c3a5d502928ddf8fd8230bb3211376a48de0a42d1668638f8d128b46e558f6d9c6387e46b621fb5fe0143b39aee0fe865715
|
7
|
+
data.tar.gz: 361ffca0b4338bb90bf736731d0319308a584e623d11afc811853f666fbde82d92fa239184b25f95016d1bc4da0d06aa03598c50848500ca8be7f5fb184a5617
|
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2016 Stephen J. Lovell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# CodeCaser
|
2
|
+
|
3
|
+
A simple Ruby command line utility that converts files from camelCase to snake_case and vice-versa.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
To install:
|
8
|
+
```
|
9
|
+
$ gem install code_caser
|
10
|
+
```
|
11
|
+
## Usage
|
12
|
+
```
|
13
|
+
$ code_caser
|
14
|
+
Commands:
|
15
|
+
code_caser help [COMMAND] # Describe available commands or one specific command
|
16
|
+
code_caser to_camel --path=PATH # converts files in PATH from snake_case to camelCase.
|
17
|
+
code_caser to_snake --path=PATH # converts files in PATH from camelCase to snake_case.
|
18
|
+
```
|
19
|
+
To use, pass in the path to the file or folder:
|
20
|
+
```
|
21
|
+
$ code_caser to_camel --path=/path/to/example_file.js
|
22
|
+
```
|
23
|
+
Passing a folder will convert all files in that folder (non-recursive):
|
24
|
+
```
|
25
|
+
$ code_caser to_camel --path=/path/to/example_folder
|
26
|
+
```
|
27
|
+
code_caser will happily convert files in any directory with the required permissions, so **exercise caution when passing in a directory!** You will be presented a confirmation dialog before any files are converted.
|
28
|
+
|
29
|
+
You may also pass in a globbed directory to convert only files that match a given pattern:
|
30
|
+
```
|
31
|
+
$ code_caser to_camel --path/to/example_folder/*.js
|
32
|
+
```
|
33
|
+
## Options
|
34
|
+
Use the ```--verbose``` flag to print any changes made to each file to the terminal.
|
35
|
+
|
36
|
+
By default, backup copies of each file converted will be saved to a timestamped backup folder in the ```--path``` directory. You can prevent backups from being created by passing in the ```--no-save``` flag.
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module CodeCaser
|
5
|
+
|
6
|
+
class Caser
|
7
|
+
def initialize(opts = {})
|
8
|
+
@converter = opts[:converter]
|
9
|
+
@path = File.directory?(opts[:path]) ? File.join(opts[:path], "*") : opts[:path]
|
10
|
+
@save = opts[:save] || true
|
11
|
+
@verbose = opts[:verbose] || false
|
12
|
+
end
|
13
|
+
|
14
|
+
def start
|
15
|
+
files = get_files
|
16
|
+
if files.empty?
|
17
|
+
puts "File or folder not found.\n"
|
18
|
+
return
|
19
|
+
elsif user_aborted?(files)
|
20
|
+
puts "File conversion aborted.\n"
|
21
|
+
return
|
22
|
+
end
|
23
|
+
convert_files(files)
|
24
|
+
puts "\n#{files.count} file(s) converted.".colorize(:green)
|
25
|
+
puts "Backup copies of the original files can be found here:".colorize(:green)
|
26
|
+
puts "#{backup_folder}\n"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def get_files
|
32
|
+
Dir.glob(File.expand_path(@path))
|
33
|
+
end
|
34
|
+
|
35
|
+
def backup_folder
|
36
|
+
@backup_folder ||= File.dirname(@path) + "_backup_#{Time.new.to_i}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def convert_files(files)
|
40
|
+
# cache the original file in a backup folder.
|
41
|
+
FileUtils.mkdir_p(backup_folder)
|
42
|
+
files.each { |f| convert_file(f) if File.file?(f) }
|
43
|
+
FileUtils.rm_r(backup_folder) unless @save
|
44
|
+
end
|
45
|
+
|
46
|
+
def convert_file(file_path)
|
47
|
+
file_name = File.basename(file_path)
|
48
|
+
puts "-> Converting #{file_name}...".colorize(:blue) if @verbose
|
49
|
+
backup_file_path = File.join(backup_folder, file_name)
|
50
|
+
FileUtils.cp(file_path, backup_file_path)
|
51
|
+
# Replace the file with its converted equivalent.
|
52
|
+
FileUtils.rm(file_path)
|
53
|
+
f = File.new(file_path, "w+")
|
54
|
+
IO.foreach(backup_file_path) do |line|
|
55
|
+
f.puts(convert_line(line))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def convert_line(line)
|
60
|
+
if (converted_line = @converter.convert_line(line)) != line && @verbose
|
61
|
+
puts " " + line.strip
|
62
|
+
puts " " + converted_line.strip.colorize(:green)
|
63
|
+
end
|
64
|
+
converted_line
|
65
|
+
end
|
66
|
+
|
67
|
+
def user_aborted?(files)
|
68
|
+
puts "Warning: This will convert all files listed below from #{@converter.description}.\n".colorize(:yellow)
|
69
|
+
puts "No back-ups of these files will be created.\n".colorize(:yellow) unless @save
|
70
|
+
puts files
|
71
|
+
puts ("\nMake sure your files are checked in to source control before converting." +
|
72
|
+
"\nTo confirm, type 'CONVERT':").colorize(:yellow)
|
73
|
+
STDIN.gets.chomp != "CONVERT"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module CodeCaser
|
2
|
+
class CamelConverter
|
3
|
+
def convert_line(str)
|
4
|
+
match = false
|
5
|
+
output = str.reverse.gsub(/([a-z]+[A-Z]\B)(.)(?!\w*[A-Z]\b)/) { |s|
|
6
|
+
match = true
|
7
|
+
($1.to_s + '_' + $2.to_s)
|
8
|
+
}.gsub(/([A-Z])([a-z0-9])(?!\w*[A-Z]\b)/) { |s|
|
9
|
+
match = true
|
10
|
+
($1.to_s + '_' + $2.to_s)
|
11
|
+
}.reverse
|
12
|
+
match ? output.downcase : output
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
"camelCase to snake_case"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class SnakeConverter
|
21
|
+
def convert_line(str)
|
22
|
+
str.gsub(/([a-z0-9])_([a-z0-9])/) { |s| $1 + $2.upcase }
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
"snake_case to camelCase"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/code_caser.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
Dir["./lib/code_caser/*.rb"].each {|file| require file }
|
3
|
+
|
4
|
+
module CodeCaser
|
5
|
+
def self.to_camel(opts)
|
6
|
+
Caser.new(opts.merge({ converter: SnakeConverter.new })).start
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.to_snake(opts)
|
10
|
+
Caser.new(opts.merge({ converter: CamelConverter.new })).start
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: code_caser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Lovell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.19.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.19.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.8.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: A simple utility gem to convert files from snake_case to camelCase and
|
56
|
+
back.
|
57
|
+
email:
|
58
|
+
- sjlovell34@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files:
|
62
|
+
- LICENSE
|
63
|
+
- README.md
|
64
|
+
- CHANGELOG.md
|
65
|
+
files:
|
66
|
+
- CHANGELOG.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- lib/code_caser.rb
|
72
|
+
- lib/code_caser/caser.rb
|
73
|
+
- lib/code_caser/converters.rb
|
74
|
+
- lib/code_caser/version.rb
|
75
|
+
homepage: https://github.com/stephenjlovell/code_caser
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.6.8
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Convert files from snake_case to camelCase and vice versa.
|
99
|
+
test_files: []
|