ditto_code 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/dittoc +117 -0
- data/lib/ditto_code.rb +11 -0
- data/lib/dittocode/exec.rb +29 -0
- data/lib/dittocode/parse.rb +155 -0
- data/lib/dittocode/talk.rb +12 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 94116e826c5c80e1677bb93fa0995931aa91a846
|
4
|
+
data.tar.gz: a7bb58d325d8ba77a5397e9bf46939ed8d6c73e8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ffc4edeb0b21be03a544bde6efb05653554cc510f719b52506f7b39b1af62f0e02ca88cc9758cffc9ffc9f91a8ffdb6e4803ce5d7f8706079a045e8ac46839bb
|
7
|
+
data.tar.gz: e3d830ff566898824715a0728e61ec574a19c883f873c054ccdb19d0e29baa3f4a1c34071dbf07470766cba2edd547bcaf01f4729b500272c4523eaee43613de
|
data/bin/dittoc
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'ditto_code'
|
3
|
+
|
4
|
+
# ===============================================================
|
5
|
+
# FUNCTIONS
|
6
|
+
# ===============================================================
|
7
|
+
|
8
|
+
def usage(s)
|
9
|
+
$stderr.puts("Ditto say -> [Err] " + s)
|
10
|
+
$stderr.puts("Ditto say -> [Usage] #{File.basename($0)}: [-v <verbose>] [-f <folder>] [-r <recursive>] [-o <override files>] ENVIRONMENT file|folder")
|
11
|
+
exit(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Check if the filepath is a file or a directory
|
15
|
+
def isFile?(filepath)
|
16
|
+
if File.file?(filepath)
|
17
|
+
true
|
18
|
+
else
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Check if the filepath is a directory
|
24
|
+
def isDirectory?(filepath)
|
25
|
+
if File.directory?(filepath)
|
26
|
+
true
|
27
|
+
else
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def iterateFolder(folder)
|
33
|
+
# Is folder?
|
34
|
+
Dir.foreach("#{folder}") do |rb_file|
|
35
|
+
# Jump on . and ..
|
36
|
+
next if rb_file == '.' or rb_file == '..' or rb_file == './'
|
37
|
+
|
38
|
+
filepath = "#{folder}#{rb_file}"
|
39
|
+
|
40
|
+
if isFile?(filepath) && rb_file.end_with?('.rb')
|
41
|
+
@ditto.transformation(filepath)
|
42
|
+
elsif isDirectory?(filepath) && @recursive
|
43
|
+
# Recursive
|
44
|
+
iterateFolder("#{filepath}/")
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# ===============================================================
|
51
|
+
# VARIAVLEs
|
52
|
+
# ===============================================================
|
53
|
+
|
54
|
+
$isFolder = false;
|
55
|
+
$folder = "";
|
56
|
+
$environment = "";
|
57
|
+
$verbose = false;
|
58
|
+
$override = false;
|
59
|
+
@recursive = false
|
60
|
+
|
61
|
+
# ===============================================================
|
62
|
+
# INITIATION
|
63
|
+
# ===============================================================
|
64
|
+
|
65
|
+
while ARGV[0]
|
66
|
+
case ARGV[0]
|
67
|
+
when '-f'
|
68
|
+
ARGV.shift
|
69
|
+
$isFolder = true
|
70
|
+
when '-v'
|
71
|
+
ARGV.shift
|
72
|
+
$verbose = ARGV.shift
|
73
|
+
when '-o'
|
74
|
+
ARGV.shift
|
75
|
+
$override = true
|
76
|
+
when '-r'
|
77
|
+
ARGV.shift
|
78
|
+
@recursive = true
|
79
|
+
when /^-/
|
80
|
+
usage("Unknown option: #{ARGV[0].inspect}");
|
81
|
+
else
|
82
|
+
|
83
|
+
if $environment.empty?
|
84
|
+
$environment = ARGV.shift;
|
85
|
+
else
|
86
|
+
$folder = ARGV.shift;
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# ===============================================================
|
93
|
+
# EXECUTION
|
94
|
+
# ===============================================================
|
95
|
+
|
96
|
+
# Initiate ditto
|
97
|
+
@ditto = DittoCode::Parse.new($environment, $override)
|
98
|
+
|
99
|
+
# Check if the user has introduce a folder/file
|
100
|
+
if $folder.empty?
|
101
|
+
|
102
|
+
usage("There aren't any folder/file")
|
103
|
+
|
104
|
+
else
|
105
|
+
|
106
|
+
# Check if is a folder
|
107
|
+
if $isFolder
|
108
|
+
# Check /
|
109
|
+
unless $folder.end_with?("/") then $folder = $folder + "/" end
|
110
|
+
# Iterate inside the folder
|
111
|
+
iterateFolder($folder)
|
112
|
+
else
|
113
|
+
# Only a file
|
114
|
+
ditto.transformation($folder)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
data/lib/ditto_code.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'dittocode/parse'
|
2
|
+
require 'dittocode/exec'
|
3
|
+
require 'dittocode/talk'
|
4
|
+
include DittoCode::Exec
|
5
|
+
include DittoCode::Talk
|
6
|
+
|
7
|
+
module DittoCode
|
8
|
+
def self.symbolize_keys h
|
9
|
+
h.inject({}) { |opts,(k,v)| opts[(k.to_sym rescue k) || k] = v; opts }
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DittoCode
|
2
|
+
|
3
|
+
@initialize = false
|
4
|
+
|
5
|
+
module Exec
|
6
|
+
def self.if (environment)
|
7
|
+
|
8
|
+
# check the env code
|
9
|
+
unless @initialize
|
10
|
+
check_env
|
11
|
+
end
|
12
|
+
|
13
|
+
if ENV["DITTOCODE_ENV"] == environment
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Check the presence of the environment constant
|
21
|
+
def check_env
|
22
|
+
if ENV["DITTOCODE_ENV"].nil? || ENV["DITTOCODE_ENV"].empty?
|
23
|
+
puts 'Ditto say -> Environment isn\'t declared'
|
24
|
+
end
|
25
|
+
@initialize = true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
module DittoCode
|
2
|
+
class Parse
|
3
|
+
|
4
|
+
# Require
|
5
|
+
require 'indentation'
|
6
|
+
|
7
|
+
# Say hello ditto
|
8
|
+
def self.hi
|
9
|
+
say "Hi man! What's up?"
|
10
|
+
end
|
11
|
+
|
12
|
+
# Initialize the environment
|
13
|
+
def initialize(environment, override)
|
14
|
+
@env = environment;
|
15
|
+
@override = override;
|
16
|
+
end
|
17
|
+
|
18
|
+
# Transform the file based in the environment
|
19
|
+
def transformation(file_path)
|
20
|
+
|
21
|
+
# Start to read the file
|
22
|
+
file = File.open(file_path)
|
23
|
+
@file_name = File.basename(file_path, ".rb")
|
24
|
+
@dir_name = File.dirname(file_path) + "/"
|
25
|
+
|
26
|
+
# Generate the new archive
|
27
|
+
out_file = initiateFile(file_path)
|
28
|
+
|
29
|
+
dittos = 0;
|
30
|
+
actions = { atack: false }
|
31
|
+
|
32
|
+
# Get the text
|
33
|
+
# Protect against nil or directory when it's a file
|
34
|
+
begin
|
35
|
+
text = file.read
|
36
|
+
|
37
|
+
# Each line........
|
38
|
+
text.each_line do | line |
|
39
|
+
|
40
|
+
m = /[\s]*DittoCode::Exec.if[\s]+['|"](?<environment>[a-zA-Z]+)['|"] do/.match(line)
|
41
|
+
|
42
|
+
if m
|
43
|
+
actions[:env] = m[:environment]
|
44
|
+
actions[:atack] = true;
|
45
|
+
actions[:ends] = 1;
|
46
|
+
else
|
47
|
+
|
48
|
+
if !actions[:atack]
|
49
|
+
out_file.puts(line)
|
50
|
+
else
|
51
|
+
# He is transforming
|
52
|
+
dittos += 1
|
53
|
+
|
54
|
+
# Check if the line is end
|
55
|
+
if isEnd? line
|
56
|
+
|
57
|
+
# If is the last end, it's coincide with the end of the block
|
58
|
+
if actions[:ends] == 1
|
59
|
+
actions[:atack] = false
|
60
|
+
elsif actions[:env] == @env
|
61
|
+
# Only how if we must mantain it
|
62
|
+
out_file.puts(line.indent(-1))
|
63
|
+
end
|
64
|
+
|
65
|
+
actions[:ends] -= 1
|
66
|
+
|
67
|
+
else
|
68
|
+
|
69
|
+
# prints of coincide with the environment
|
70
|
+
if actions[:env] == @env
|
71
|
+
out_file.puts(line.indent(-1))
|
72
|
+
end
|
73
|
+
|
74
|
+
# Check if we need to add a new end
|
75
|
+
new_ends = moreEnds? line
|
76
|
+
actions[:ends] += new_ends
|
77
|
+
dittos += new_ends
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
say "[Ok] #{dittos} lines ditted on #{@dir_name}#{@file_name}!"
|
87
|
+
closeFile(out_file)
|
88
|
+
|
89
|
+
rescue => e
|
90
|
+
|
91
|
+
if e.class == Errno::EISDIR
|
92
|
+
say "[Err] If you wants to use a directory use the option -f"
|
93
|
+
else
|
94
|
+
say "[Err] Oh no! I have an error :("
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Detect an end line
|
103
|
+
def isEnd?(line)
|
104
|
+
|
105
|
+
# Coincide with an end!
|
106
|
+
if /^[\s]*end/.match(line)
|
107
|
+
return true
|
108
|
+
end
|
109
|
+
|
110
|
+
return false
|
111
|
+
end
|
112
|
+
|
113
|
+
# Detect if we need to add a new end
|
114
|
+
def moreEnds?(line)
|
115
|
+
|
116
|
+
# Get the initializers of blocks
|
117
|
+
initializers = line.scan(/^(if|do|def)[\s]+/).size + line.scan(/[\s]+(if|do|def)[\s]+/).size + line.scan(/[\s]+(if|do|def)$/).size
|
118
|
+
# Get the ends blocks
|
119
|
+
finals = line.scan(/[\s]+(end)[\s]+/).size + line.scan(/^(end)[\s]+/).size + line.scan(/[\s]+(end)$/).size
|
120
|
+
|
121
|
+
# Return the difference
|
122
|
+
initializers - finals
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
# Open the file
|
127
|
+
def initiateFile(file_path)
|
128
|
+
|
129
|
+
if(@override)
|
130
|
+
File.new("#{@dir_name}#{@file_name}_tmp.rb", "w")
|
131
|
+
else
|
132
|
+
File.new("#{@dir_name}#{@file_name}_#{@env}.rb", "w")
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
# Close the file
|
138
|
+
#
|
139
|
+
# file => file to close
|
140
|
+
# file_name => name of the file
|
141
|
+
# file_path => path of the original file
|
142
|
+
def closeFile(file)
|
143
|
+
|
144
|
+
# Close the file
|
145
|
+
file.close;
|
146
|
+
|
147
|
+
if(@override)
|
148
|
+
File.delete(file)
|
149
|
+
File.rename(file, "#{@dir_name}#{@file_name}.rb")
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ditto_code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Angel M Miguel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: indentation
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.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.1.1
|
27
|
+
description: Transform your ruby code based on a custom variable.
|
28
|
+
email: angel@laux.es
|
29
|
+
executables:
|
30
|
+
- dittoc
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- bin/dittoc
|
35
|
+
- lib/ditto_code.rb
|
36
|
+
- lib/dittocode/exec.rb
|
37
|
+
- lib/dittocode/parse.rb
|
38
|
+
- lib/dittocode/talk.rb
|
39
|
+
homepage: https://github.com/Angelmmiguel/ditto_code
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.2.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: ditto
|
63
|
+
test_files: []
|