rub_a_scripts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjRkODY1Y2NmYmNhZWI4NDFkYmE4NGU3M2U1ODQxM2YzYjE5ZGNiZQ==
5
+ data.tar.gz: !binary |-
6
+ MWY4ODk2NzFlZjU4NWU2MDcxNTYwZTQ0YmM1ZDJmY2QyYTVjOTY5Nw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmY3ODhjOTRkY2FlYzg2MTE0ODk3OWRhMTA5ZGY4MjM1NzRmMmFhMTEzYmQz
10
+ NmY5MjA3OTkwMGZkNmI1YTYyY2E5MjJiOTM5MGU0ZTdmNmMwNWNmMzA1OGU3
11
+ MDljYWNhMjZjYTMxZGQzNGNlYTE4MTdjNjg4YmY0ZjY0MmNlZDk=
12
+ data.tar.gz: !binary |-
13
+ MjM5ZmNiZGFiOWRjZTc2ZjJhMzhkYjVlOWNjMDJlZTY2ZjgzY2Q0OWVlM2I5
14
+ N2U4OTg2Zjg3ZWYxZWJiZWE2ODk0ZmZjMTY0NjU2MTE3OTg5NGUzNTRhMmYw
15
+ YWNjOTRjMjI2ZTJkYWJlZDY1MjdmNjhkY2QyYTQxYjAxYzkzNGI=
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+
3
+ task :build_gem do
4
+ File.open("#{Dir.pwd}/batch_jobs/build_doc.bat", 'w') {|file|
5
+ file.write("cd #{Dir.pwd}\n")
6
+ file.write("rdoc --op #{Dir.pwd}/doc\n")
7
+ }
8
+ system("#{Dir.pwd}/batch_jobs/build_doc.bat")
9
+ FileUtils.rm_rf(Dir.glob("batch_jobs/build_doc.bat"))
10
+ system("#{Dir.pwd}/batch_jobs/build_gem.bat")
11
+ system("#{Dir.pwd}/batch_jobs/install_gem.bat")
12
+
13
+ #gem_files = Dir.glob("rub_a_scripts*.gem")
14
+ #FileUtils.rm_rf(gem_files)
15
+ end
@@ -0,0 +1 @@
1
+ require 'rub_a_scripts'
@@ -0,0 +1,238 @@
1
+ require "inline"
2
+ require 'Date'
3
+ module Inline
4
+
5
+ ##
6
+ # VBScript Inline - allow simple inline of VBScript within Ruby.
7
+ #
8
+ class VBScript
9
+
10
+ ##
11
+ # extra_args: Allow the user to specify any other arguments they want to add. By default //Nologo is added.
12
+ #
13
+ attr_accessor :extra_args
14
+
15
+ def initialize(mod)
16
+ @context = mod
17
+ @src = ""
18
+ @imports = []
19
+ @sigs = []
20
+ @file_name = ''
21
+ @extra_args = '//Nologo'
22
+ end
23
+
24
+ ##
25
+ # Use by RubyInline to designate new file or to reuse cache. For VBScript
26
+ # we simply create a new file every times so this will always be set to false.
27
+ # Also crapped in here a method to clear out the inline folder if files are
28
+ # more than 3 days old.
29
+ #
30
+ def load_cache
31
+ clear_inline_folder
32
+ false
33
+ end
34
+
35
+ ##
36
+ # This method will clear out the inline directory if any files is more than 3 days old.
37
+ #
38
+ def clear_inline_folder
39
+ files = Dir.glob("#{Inline.directory}/VBScript*")
40
+ files.each do |file|
41
+ file_time = File.ctime(file)
42
+ today_date = Date.today
43
+ if ((file_time.day - today_date.day) >= 2) and (file_time.month >= today_date.month) and (file_time.year >= today_date.year)
44
+ FileUtils.rm_rf(file)
45
+ end
46
+ end
47
+ end
48
+
49
+ ##
50
+ # Parse your VBScript to find a function name. Store them sor Ruby
51
+ # can create a Ruby method with the exact name and parameters list.
52
+ #
53
+ def vbscript(src)
54
+ @src << src << "\n"
55
+ signature = src.match(/Function\W+([a-zA-Z0-9_]+)\((.*)\)/)
56
+ @sigs << [signature[1], signature[2], signature[3]] unless !signature
57
+ if !signature
58
+ signature = src.match(/Sub\W+([a-zA-Z0-9_]+)\((.*)\)/)
59
+ @sigs << [signature[1], signature[2], signature[3]] unless !signature
60
+ end
61
+ end
62
+
63
+ ##
64
+ # @build. Build source VBScript file to be later used for execution.
65
+ # - grabbing all your vbscript source code
66
+ # - creating a vbscript file and then inputing in the code
67
+ # - it will also add an extra vbscript function that will
68
+ # later be called to pass in parameters and output result to
69
+ # a file so Ruby can pick up.
70
+ #
71
+ def build
72
+ @load_name = @name = "VBScript#{@src.hash.abs}"
73
+ @file_name = "#{Inline.directory}/#{@name}.vbs"
74
+ @src << extra_input_fx << "\n"
75
+ File.open(@file_name, 'w') {|file| file.write(@src)}
76
+ end
77
+
78
+ ##
79
+ # This method is used to dynamically create all the Ruby methods
80
+ # that is associate with their VBScript methods. No doubt the
81
+ # most important method of this module as it's reponsible for
82
+ # creating handling interactions between Ruby and VBScript.
83
+ #
84
+ def load
85
+ @context.module_eval "const_set :#{@name}, Inline::VBScript;"
86
+
87
+ @context.module_eval "
88
+ def clean_cache
89
+ files = Dir.glob(\"\#{Inline.directory}/VBScript*\")
90
+ files.each {|file| FileUtils.rm_rf(file)}
91
+ end
92
+ def delete_file
93
+ File.delete('#{@file_name}') if File.exist?('#{@file_name}');
94
+ end
95
+ "
96
+
97
+ @context.module_eval "
98
+ def construct_arguments(*args)
99
+ @argument_to_send = ''
100
+ if args.class == Array
101
+ args.each_with_index do |top_level_item, args_index|
102
+ @argument_to_send += \",\" if args_index > 0
103
+ if top_level_item.class == Array
104
+ @argument_to_send += \"Array(\"
105
+ top_level_item.each_with_index do |inner_item, top_level_index|
106
+ @argument_to_send += \",\" if top_level_item.count != top_level_index and top_level_index > 0
107
+ @argument_to_send += construct_arguments(inner_item)
108
+ end
109
+ @argument_to_send += \")\"
110
+ else
111
+ case
112
+ when top_level_item.class == String
113
+ @argument_to_send += %q[\"] + %Q[\#{top_level_item.to_s}] + %q[\"]
114
+ when top_level_item.class == Fixnum
115
+ @argument_to_send += top_level_item.to_s
116
+ when top_level_item.class == TrueClass
117
+ @argument_to_send += \"True\"
118
+ when top_level_item.class == FalseClass
119
+ @argument_to_send += \"False\"
120
+ else
121
+ @argument_to_send += top_level_item.to_s
122
+ end
123
+ end
124
+ end
125
+ end
126
+ return @argument_to_send
127
+ end
128
+ "
129
+
130
+ @sigs.each do |sig|
131
+ @context.module_eval "
132
+ def #{sig[0]}(*args);
133
+ @arguments_to_send = construct_arguments(args)
134
+ @arguments_to_send.sub!(\"Array(\",\"\").chomp!(\")\")
135
+ @arguments_to_send.gsub!(/\"/, \'\\'\')
136
+ @arguments_to_send.chomp!
137
+ execution_str = 'cscript #{@file_name} \"#{sig[0]}' + '(' + @arguments_to_send + ')\" #{@extra_args}';
138
+
139
+ IO.popen(execution_str).each do |line|
140
+ case
141
+ when (line.strip == \"nothing\" or line.strip == \"null\" or line.strip == \"Empty\")
142
+ @result = nil
143
+ when line.strip == \"False\"
144
+ @result = false
145
+ when line.strip == \"True\"
146
+ @result = true
147
+ when line.strip[/^[-+]?[0-9]+$/]
148
+ @result = line.strip.to_i
149
+ when line.strip[/^[-+]?[\\d]+(\\.[\\d]+){0,1}$/]
150
+ @result = line.strip.to_f
151
+ when line.strip[/Array\\[.*\\]/]
152
+ begin
153
+ @result = eval(line.strip.gsub(\"Array\",\"\").gsub(\"True\",\"true\").gsub(\"False\",\"false\"))
154
+ rescue => e
155
+ @result = line.strip;
156
+ warn(e)
157
+ end
158
+ else
159
+ @result = line.strip;
160
+ end
161
+ end.close
162
+ return @result
163
+ end"
164
+ end
165
+ end
166
+
167
+ ##
168
+ # The inside man. This source code and methods is added and inject into the VBScript.
169
+ # It's responsible for picking up arguments and executing the VBScript function
170
+ # being called. Will also write result to stdout so that Ruby can pick up.
171
+ # A few basic mapping and error handling is also being done here vs the load method on the Ruby side.
172
+ #
173
+ def extra_input_fx()
174
+ my_str1 = '
175
+ Function fxWriteOutput
176
+ InputArgs = WScript.Arguments.Item(0)
177
+ InputArgs = Replace(InputArgs, "\'", chr(34))
178
+ if WScript.Arguments.Count() > 0 Then
179
+ On Error Resume Next
180
+ fx_value = Eval(InputArgs)
181
+ If Err.Number <> 0 Then
182
+ If Err.Description = "Object doesn\'t support this property or method" Then
183
+ fx_value = "Error: " & Err.Description & ". Sorry!!! Object are not mapped."
184
+ End If
185
+ Err.Clear
186
+ End If
187
+ On Error Goto 0
188
+ ' + "\n"
189
+ my_str_3 = '
190
+ if IsArray(fx_value) = False Then
191
+ if fx_value = True Then
192
+ WScript.Echo "True"
193
+ elseif IsEmpty(fx_value) Then
194
+ WScript.Echo "Empty"
195
+ elseif fx_value = False Then
196
+ WScript.Echo "False"
197
+ else
198
+ WScript.Echo fx_value
199
+ End If
200
+ else
201
+ my_array_list = "Array" & abcExpandArray(fx_value)
202
+ WScript.Echo my_array_list
203
+ End if
204
+ End if
205
+ End Function
206
+ fxWriteOutput()
207
+
208
+ Function abcExpandArray(fx_value)
209
+ If IsArray(fx_value) Then
210
+ For i = (lbound(fx_value)) to ubound(fx_value)
211
+ if i > 0 Then
212
+ finalStr = finalStr & "," & abcExpandArray(fx_value(i))
213
+ Else
214
+ finalStr = finalStr & abcExpandArray(fx_value(i))
215
+ End If
216
+ Next
217
+ abcExpandArray = "[" & finalStr & "]"
218
+ Else
219
+ If IsNumeric(fx_value) Then
220
+ abcExpandArray = fx_value
221
+ ElseIf fx_value = True Then
222
+ abcExpandArray = "True"
223
+ ElseIf IsEmpty(fx_value) Then
224
+ abcExpandArray = "Empty"
225
+ ElseIf fx_value = False Then
226
+ abcExpandArray = "False"
227
+ Else
228
+ abcExpandArray = """" & fx_value & """"
229
+ End If
230
+ End If
231
+ End Function
232
+ '
233
+ return (my_str1 + my_str_3)
234
+ end
235
+
236
+ end
237
+
238
+ end
@@ -0,0 +1,194 @@
1
+ require 'rub_a_scripts'
2
+ require 'minitest'
3
+ require 'minitest/autorun'
4
+
5
+ class MyVBScriptClass
6
+
7
+ inline :VBScript do |builder|
8
+ builder.vbscript '
9
+ Function fnSum(var1,var2)
10
+ vrSum = var1 + var2
11
+ fnSum = vrSum
12
+ End Function
13
+ '
14
+
15
+ builder.vbscript '
16
+ Function fnSubtract(var1,var2)
17
+ vrSubtract = var1 - var2
18
+ fnSubtract = vrSubtract
19
+ End Function
20
+ '
21
+
22
+ builder.vbscript '
23
+ Function fnString(input)
24
+ fnString = input & " World!!!"
25
+ End Function
26
+ '
27
+
28
+ builder.vbscript '
29
+ Function fnBoolean(input)
30
+ if input = True then
31
+ fnBoolean = False
32
+ else
33
+ fnBoolean = True
34
+ End If
35
+ End Function
36
+ '
37
+
38
+ builder.vbscript'
39
+ \'WScript.Echo "Hello Cruel Cruel World!!!"
40
+ '
41
+
42
+ builder.vbscript '
43
+ Function fnArray(input)
44
+ fnArray = input(2)
45
+ End Function
46
+ '
47
+
48
+ builder.vbscript '
49
+ Function fn2Array(input, input2)
50
+ fn2Array = input2(5)
51
+ End Function
52
+ '
53
+
54
+ builder.vbscript '
55
+ Function fnNull()
56
+ fnNull = "nothing"
57
+ End Function
58
+ '
59
+
60
+ builder.vbscript '
61
+ Function fnDecimal()
62
+ fnDecimal = 123.13
63
+ End Function
64
+ '
65
+
66
+ builder.vbscript '
67
+ Function fnDecimalNegative()
68
+ fnDecimalNegative = -1234567.13
69
+ End Function
70
+ '
71
+
72
+ builder.vbscript '
73
+ Function fnReturnArray()
74
+ fnReturnArray = Array(3,9,4,0,8,2)
75
+ End Function
76
+ '
77
+
78
+ builder.vbscript '
79
+ Function fnReturn2DArray()
80
+ array1 = Array(1,3)
81
+ array2 = Array("Hello", "World")
82
+ fnReturn2DArray = Array(array1,array2,True)
83
+ End Function
84
+ '
85
+
86
+ builder.vbscript '
87
+ Function fnProcessMultiDimArray(input)
88
+ fnProcessMultiDimArray = input
89
+ End Function
90
+ '
91
+
92
+ builder.vbscript '
93
+ Function fnObject()
94
+ set fso = CreateObject("Scripting.FileSystemObject")
95
+ Set fnObject = fso
96
+ End Function
97
+ '
98
+
99
+ builder.vbscript '
100
+ Sub subString()
101
+ WScript.Echo "I am inside a Procedures!!!"
102
+ response.write("I was written by a sub procedure")
103
+ End Sub
104
+ '
105
+
106
+ end
107
+
108
+ end
109
+
110
+ class TestRubAScript < Minitest::Test
111
+ def setup
112
+ @vb_script_obj = MyVBScriptClass.new()
113
+ @my_str = "Hello"
114
+ @my_array = ['a', 'b', 'c', 'd', 'e', 'f']
115
+ @my_array2 = ["1", "2", "3", "4", "5", "6"]
116
+ @my_array3 = [9,[0],[[1],[[2],[3]],[4]],"Hello World"]
117
+ @error_str = "Error: Object doesn't support this property or method. Sorry!!! Object are not mapped."
118
+ end
119
+
120
+ def test_fnSum
121
+ assert_equal 8, @vb_script_obj.fnSum(3,5)
122
+ end
123
+
124
+ def test_fnSubtract
125
+ assert_equal 5, @vb_script_obj.fnSubtract(8,3)
126
+ end
127
+
128
+ def test_fnSubtractNegative
129
+ assert_equal -5, @vb_script_obj.fnSubtract(3,8)
130
+ end
131
+
132
+ def test_fnString
133
+ assert_equal "Hello World!!!", @vb_script_obj.fnString('Hello')
134
+ end
135
+
136
+ def test_fnString_with_var
137
+ assert_equal "Hello World!!!", @vb_script_obj.fnString(@my_str)
138
+ end
139
+
140
+ def test_fnBoolean_true
141
+ assert_equal false, @vb_script_obj.fnBoolean(true)
142
+ end
143
+
144
+ def test_fnBoolean_false
145
+ assert_equal true, @vb_script_obj.fnBoolean(false)
146
+ end
147
+
148
+ def test_fnArray
149
+ assert_equal "c", @vb_script_obj.fnArray(@my_array)
150
+ end
151
+
152
+ def test_2fnArray
153
+ assert_equal 6, @vb_script_obj.fn2Array(@my_array, @my_array2)
154
+ end
155
+
156
+ def test_fnNull
157
+ assert_equal nil, @vb_script_obj.fnNull
158
+ end
159
+
160
+ def test_fnDecimal
161
+ assert_equal 123.13, @vb_script_obj.fnDecimal
162
+ end
163
+
164
+ def test_fnDecimal
165
+ assert_equal -1234567.13, @vb_script_obj.fnDecimalNegative
166
+ end
167
+
168
+ def test_fnReturnArray
169
+ assert_equal [3,9,4,0,8,2], @vb_script_obj.fnReturnArray()
170
+ end
171
+
172
+ def test_fnReturn2DArray
173
+ assert_equal [[1,3],['Hello', 'World'], true], @vb_script_obj.fnReturn2DArray()
174
+ end
175
+
176
+ def test_fnProcessMultiDimArray
177
+ assert_equal @my_array3, @vb_script_obj.fnProcessMultiDimArray(@my_array3)
178
+ end
179
+
180
+ def test_fnObject
181
+ assert_equal @error_str, @vb_script_obj.fnObject
182
+ end
183
+
184
+ def test_subString()
185
+ assert_equal nil, @vb_script_obj.subString()
186
+ end
187
+
188
+ end
189
+
190
+ Minitest.after_run {
191
+ @test = MyVBScriptClass.new()
192
+ @test.delete_file
193
+ @test.clean_cache
194
+ }
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rub_a_scripts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kent T. Le
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An extension of RubyInline. Allow for the use of VBScript in Ruby.
14
+ email: Le.78@osu.edu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Rakefile
20
+ - bin/rub_a_scripts
21
+ - lib/rub_a_scripts.rb
22
+ - test/test_rub_a_scripts.rb
23
+ homepage: https://github.com/KentViet/rub_a_scripts
24
+ licenses: []
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ - test
31
+ - bin
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: An extension of RubyInline gem. rub_a_scripts allows you to write VBScript
48
+ within Ruby code. The extensions are then automatically loaded into the class/module
49
+ that defines it. Allowing ruby to call on VBScript function and procedure. Basic
50
+ data types are mapped as such facilitate the exchange of data between the two languages.
51
+ test_files:
52
+ - test/test_rub_a_scripts.rb