Class: Roebe::CreateFileSkeleton
- Inherits:
-
Base
show all
- Defined in:
- lib/roebe/classes/create_file_skeleton/constants.rb,
lib/roebe/classes/create_file_skeleton/run.rb,
lib/roebe/classes/create_file_skeleton/reset.rb,
lib/roebe/classes/create_file_skeleton/generate_c_string.rb,
lib/roebe/classes/create_file_skeleton/generate_cpp_string.rb,
lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb,
lib/roebe/classes/create_file_skeleton/generate_ruby_string.rb
Overview
Roebe::CreateFileSkeleton
Constant Summary
collapse
- THRESHOLD =
#
Roebe::CreateFileSkeleton::THRESHOLD
#
76
- USE_THIS_ENCODING =
#
USE_THIS_ENCODING
We have to use a constant here because we may want to generate a file with ANOTHER encoding eventually.
#
Roebe.encoding?
- ARRAY_REQUIRE_THESE_LIBRARIES_FOR_RB_FILES =
#
ARRAY_REQUIRE_THESE_LIBRARIES_FOR_RB_FILES
Which libraries to require:
#
%w(
colours
opn
esystem/method
save_file
)
- PATH_TO_SYSTEM_RUBY =
'#!/usr/bin/ruby'
- PATH_TO_SYSTEM_PHP =
'<?php'
- SHALL_WE_DISPLAY_THE_GENERATED_DATA =
#
SHALL_WE_DISPLAY_THE_GENERATED_DATA
#
false
- FROZEN_STRING_LITERAL_TRUE_OR_FALSE =
#
FROZEN_STRING_LITERAL_TRUE_OR_FALSE
We store this as a String, since it will be embedded into an autogenerated file.
#
'true'
- SHALL_WE_ALSO_OPEN_THE_CREATED_FILE =
#
SHALL_WE_ALSO_OPEN_THE_CREATED_FILE
If true we open the file in our editor as well.
#
true
Constants inherited
from Base
Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE
Instance Method Summary
collapse
Methods inherited from Base
#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #show_help, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into
#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments
Constructor Details
#initialize(filename_to_be_generated = 'test.rb', run_already = true) ⇒ CreateFileSkeleton
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 40
def initialize(
filename_to_be_generated = 'test.rb',
run_already = true
)
reset
set_filename(filename_to_be_generated)
case run_already
when :dont_run_yet
run_already = false
end
run if run_already
end
|
Instance Method Details
#_(input, append_newline = true) ⇒ Object
#
_ (append tag)
Use this method here to append to @_, which keeps track of our main String.
#
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 442
def _(
input, append_newline = true
) case append_newline
when :no_newline
append_newline = false
end
if append_newline
if input.frozen?
input = input.dup
end
input << N
end
@_ << input
end
|
#
This will append a comment to a file, capped at 80 characters in total.
#
426
427
428
429
430
431
432
433
434
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 426
def (
optional_padding = padding?
)
s = optional_padding.dup
s << '# '
s << '=' * (THRESHOLD - optional_padding.size)
s << ' #'
return s
end
|
#
The add_header() method will add a proper Header.
If the second argument contains a key called :pad then we will pad to the most-right area.
#
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 250
def (
= '',
optional_hash = {}
)
append_this = ''.dup
if optional_hash.has_key? :pad
append_this << optional_hash.delete(:pad)
end
= .to_s
_ N+
line = padding?+(, '')
unless append_this.empty?
count = (width_length? - append_this.size)
append_this = append_this.rjust(count)
line << append_this
end
_ line
_
end
|
#add_newline ⇒ Object
461
462
463
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 461
def add_newline
_ ''
end
|
#be_verbose? ⇒ Boolean
133
134
135
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 133
def be_verbose?
@be_verbose
end
|
#
This will add a comment.
#
90
91
92
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 90
def
padding?+'#'
end
|
222
223
224
225
226
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 222
def (
i, use_this_padding = padding?
)
"#{use_this_padding}# === #{i}"
end
|
#consider_opening_generated_file ⇒ Object
#
consider_opening_generated_file
With this method we consider opening the newly generated file.
This allows the user to not have to manually open that generated file.
#
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 196
def consider_opening_generated_file
if @shall_we_also_open_the_created_file
opne "Also opening the generated file in the "\
"editor #{sfancy(@editor)}#{rev}."
_ = @editor+' '+filename?
e
if @use_colours
e "#{rev} #{yellow(_)}"
else
e " #{_}"
end
system(_)
e
end
end
|
#consider_showing_main_string ⇒ Object
#
consider_showing_main_string
Since Jun 2013 we honour an extra setting, the constant called SHALL_WE_DISPLAY_THE_GENERATED_DATA.
#
175
176
177
178
179
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 175
def consider_showing_main_string
if SHALL_WE_DISPLAY_THE_GENERATED_DATA
e @_ if be_verbose?
end
end
|
#content? ⇒ Boolean
59
60
61
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 59
def content?
@_
end
|
#disable_colours ⇒ Object
184
185
186
187
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 184
def disable_colours
@use_colours = false
Opn.disable_colours if Object.const_defined? :Opn
end
|
#do_not_save_file ⇒ Object
352
353
354
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 352
def do_not_save_file
@save_file = false
end
|
#empty_main_string ⇒ Object
Also known as:
ems
66
67
68
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 66
def empty_main_string
@_ = ''.dup
end
|
#filename? ⇒ Boolean
231
232
233
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 231
def filename?
@filename
end
|
#generate ⇒ Object
Also known as:
generate_string
#
generate
General generate method.
#
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 278
def generate
name_of_the_extension = File.extname(filename?).delete('.')
case name_of_the_extension
when 'java'
if be_verbose?
opne 'Now creating a Java File.'
end
generate_java_string
when 'rb' if be_verbose?
opne 'Now creating a Ruby File.'
end
generate_ruby_string
when 'php'
if be_verbose?
opne 'Now creating a PHP File.'
end
generate_php_string
when 'cgi'
if be_verbose?
opne 'Now creating a CGI File.'
end
generate_cgi_string
when 'cpp'
if be_verbose?
opne 'Now creating a C++ File.'
end
generate_cpp_string
when 'c' if be_verbose?
opne 'Now creating a C File.'
end
generate_c_string
when 'rtf' if be_verbose?
opne 'Now creating a RTF File.'
end
generate_rtf_string
else _ ''
end
end
|
#generate_c_string ⇒ Object
#
generate_c_string (c tag)
Generate C files.
Commandline invocation example:
rubygen foobar.c
#
21
22
23
24
25
26
27
28
29
|
# File 'lib/roebe/classes/create_file_skeleton/generate_c_string.rb', line 21
def generate_c_string
empty_main_string
_ '#include <stdio.h>'+N
_ 'int main() {'
_ ' printf("Hello world!\n");'
_ ' return 0;'
_ '}'+N
_ '// gcc '+real_filename?+'.c', :no_newline
end
|
#generate_cgi_string ⇒ Object
#
generate_cgi_string (cgi tag)
Generate the string required for new .cgi files.
If the user has the Cyberweb project installed then we will generate via Cyberweb.generate_cgi() instead.
#
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 375
def generate_cgi_string
begin
require 'cyberweb'
rescue LoadError; end
if Object.const_defined? :Cyberweb
Cyberweb.generate_cgi(filename?)
do_not_save_file
else
empty_main_string
_ PATH_TO_SYSTEM_RUBY+' -w'
_ '# Encoding: '+@use_this_encoding
_ '# frozen_string_literal: '.dup+FROZEN_STRING_LITERAL_TRUE_OR_FALSE
_ ''
_ '#'
_ ''
_ "require 'cyberweb/autoinclude'"+N
_ "w {"
_ "css_style :template1"
_ "fav 'PC/RUBY/RUBY_FAVICON.png'"
_ "body_css_class 's12px padt4px marpad2px VERDANAs'"
_ "body_css_style 'background-color: #d3d2d1;'"
_ "font_size 'def' # or something like: '0.9em'"
_ "# The following is optional:"
_ "background_color '#ffffee'"
_ "background_image 'NJOY/VeronicaZemanova_01.jpg'"
_ "}"
_ 'doc {'
_ ' # Your stuff goes in here.'
_ '}'
end
end
|
#generate_cpp_string ⇒ Object
#
generate_cpp_string (cpp tag)
Generate C++ files.
Commandline invocation example:
rubygen foobar.cpp
#
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/roebe/classes/create_file_skeleton/generate_cpp_string.rb', line 21
def generate_cpp_string
empty_main_string
_ '#include <iostream>'+N
_ 'using namespace std;'+N
_ 'int main() {'
_ ' cout << "Hello world!" << endl;'
_ ' return 0;'
_ '}'+N
_ '// g++ '+real_filename?+'.cpp'
_ '// run SAVE_AS_IS', :no_newline
end
|
#generate_java_string ⇒ Object
#
generate_java_string (java tag)
#
359
360
361
362
363
364
365
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 359
def generate_java_string
empty_main_string
require 'roebe/classes/java_header.rb'
result = Roebe.(false)
_ result.to_s
add_newline
end
|
#generate_php_string ⇒ Object
#
generate_php_string
We generate a PHP string here.
#
121
122
123
124
125
126
127
128
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 121
def generate_php_string
empty_main_string
_ PATH_TO_SYSTEM_PHP+' -w'
_ "div('pad2px');"
_ 'cdiv();'
_ '$w->hfin();'
_ '?>', false
end
|
#generate_rtf_string ⇒ Object
#
generate_rtf_string
This will generate a .rtf file.
#
99
100
101
102
103
104
105
106
107
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 99
def generate_rtf_string
ems
if Object.const_defined? :GenerateRtfFile
string = GenerateRtfFile.new(nil, false).content
_ string
else
opn; e 'class GenerateRtfFile does not exist.'
end
end
|
#generate_ruby_string ⇒ Object
#
generate_ruby_string (ruby tag)
This method will generate a ruby string. This string holds the “typical” content for a .rb file, that is, the skeleton that can be used to generate a default ruby file from scratch.
#
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/roebe/classes/create_file_skeleton/generate_ruby_string.rb', line 16
def generate_ruby_string
empty_main_string
classname = to_camelcase(real_filename?)
_ "#{PATH_TO_SYSTEM_RUBY} -w"
_ '# Encoding: '+@use_this_encoding.to_s
_ '# frozen_string_literal: '.dup+
FROZEN_STRING_LITERAL_TRUE_OR_FALSE
_ ''
_ '# === '+classname
_ '#'
_ '# Usage example:'
_ '#'
_ '# '+classname+'.new(ARGV)'
_ '#'
_ ''
_ "# require '"+real_filename?+".rb'"
_ ''
ARRAY_REQUIRE_THESE_LIBRARIES_FOR_RB_FILES.sort.each {|entry|
if entry.start_with? 'begin'
_ entry
else
_ "require '#{entry}'"
end
}
_ N+'class '+classname+" # < Base # === Foo::"+classname+N
_ ' include Colours'+N
_ ' # ========================================================================== #'
_ ' # === NAMESPACE'
_ ' # ========================================================================== #'
_ ' NAMESPACE = inspect'
:initialize
_ ' def initialize('
_ ' commandline_arguments = nil,'
_ ' run_already = true'
_ ' )'
_ ' reset'
_ ' set_commandline_arguments('
_ ' commandline_arguments'
_ ' )'
_ ' run if run_already'
_ ' end'
(:reset, pad: '(reset tag)') _ ' def reset'
_ ' super() if respond_to?(:super)' _ ' # ======================================================================== #'
_ ' # === @namespace'
_ ' # ======================================================================== #'
_ ' @namespace = NAMESPACE'
_ ' # infer_the_namespace'
_ ' end'
:set_commandline_arguments _ " def set_commandline_arguments(i = '')"
_ ' i = [i].flatten.compact'
_ ' @commandline_arguments = i'
_ ' end'
:commandline_arguments?
_ ' def commandline_arguments?'
_ ' @commandline_arguments'
_ ' end'
:first_argument?
_ ' def first_argument?'
_ ' @commandline_arguments.first'
_ ' end; alias first? first_argument? # === first?'
(:run, pad: '(run tag)')
_ ' def run'
_ 'pp self # For debugging. Should be removed / replaced.'
_ ' end'
classname+'[]'
_ " def self.[](i = '')"
_ ' new(i)'
_ " end#{N}"
_ 'end'+N
_ 'if __FILE__ == $PROGRAM_NAME'
_ ' '+classname+'.new(ARGV)'
_ 'end # '+real_filename?+'.rb', :no_newline
end
|
#padding? ⇒ Boolean
215
216
217
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 215
def padding?
@padding
end
|
#real_filename? ⇒ Boolean
112
113
114
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 112
def real_filename?
@real_filename
end
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/roebe/classes/create_file_skeleton/reset.rb', line 14
def reset
super()
infer_the_namespace
if ENV['EDITOR']
@editor = ENV['EDITOR'].dup
else
@editor = 'vim'
end
@padding = ' '
@use_colours = true
@shall_we_also_open_the_created_file =
SHALL_WE_ALSO_OPEN_THE_CREATED_FILE
@save_file = true
@use_this_encoding = 'UTF-8'
@use_this_encoding = USE_THIS_ENCODING
empty_main_string
set_be_verbose
set_filename
end
|
#return_filename ⇒ Object
165
166
167
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 165
def return_filename
sfile(File.basename(__FILE__))
end
|
#
run (run tag)
Here we match the different file-types to a specific action.
#
16
17
18
19
20
21
|
# File 'lib/roebe/classes/create_file_skeleton/run.rb', line 16
def run
generate consider_showing_main_string
save_file if save_file?
consider_opening_generated_file
end
|
#save_file ⇒ Object
Also known as:
save
#
save_file
Use this method to save the file.
#
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 142
def save_file
e return_filename+
rev+': Now saving this string into `'+sfile(filename?)+rev+'`.' if be_verbose?
if File.exist?(filename?) and File.directory?(filename?)
set_filename(filename?+'.rb')
generate
end
if File.exist?(filename?)
File.delete(filename?) if File.zero?(filename?)
end
write_what_into(@_.chomp, filename?)
end
|
#save_file? ⇒ Boolean
345
346
347
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 345
def save_file?
@save_file
end
|
#set_filename(i = nil) ⇒ Object
#
set_filename
If the input does not include
#
75
76
77
78
79
80
81
82
83
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 75
def set_filename(i = nil)
i = i.join(' ').strip if i.is_a? Array i = i.to_s unless i.include?('.')
i = i.to_s+'.rb' end if i
@filename = i
@real_filename = i.split('.')[0..-2].join('.') if i end
|
#width_length? ⇒ Boolean
238
239
240
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 238
def width_length?
THRESHOLD
end
|
#yellow(i) ⇒ Object
413
414
415
416
417
418
419
|
# File 'lib/roebe/classes/create_file_skeleton/create_file_skeleton.rb', line 413
def yellow(i)
if Object.const_defined? :BYEL
BYEL+i
else
i
end
end
|