Class: NeptuneCoffee::Generator
- Inherits:
-
Object
- Object
- NeptuneCoffee::Generator
- Defined in:
- lib/neptune_coffee/generator.rb
Constant Summary collapse
- SAFE_GENERATE_FIRST_LINE =
"// Generated by NeptuneCoffee"
Instance Attribute Summary collapse
-
#current_files ⇒ Object
Returns the value of attribute current_files.
-
#dirs ⇒ Object
Returns the value of attribute dirs.
-
#force ⇒ Object
Returns the value of attribute force.
-
#generated_files ⇒ Object
Returns the value of attribute generated_files.
-
#overwrite ⇒ Object
Returns the value of attribute overwrite.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#skipped_files ⇒ Object
Returns the value of attribute skipped_files.
-
#to_generate_files ⇒ Object
Returns the value of attribute to_generate_files.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #error(message) ⇒ Object
- #file_was_generated_by_neptune_coffee(file) ⇒ Object
-
#generate_all ⇒ Object
returns: { current_files: [Pathnames…] generated_files: [Pathnames…] skipped_files: [Pathnames…] }.
- #generate_module(dir) ⇒ Object
- #generate_namespace(dir) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(options) ⇒ Generator
constructor
A new instance of Generator.
- #load_dirs ⇒ Object
- #reset_file_info ⇒ Object
-
#safe_generate(file) ⇒ Object
writes to “file” whatever the block yields IF the file was originally generated by NeptuneCoffee (unless @overwrite) the file contents changed (unless @force).
- #show_generated(contents) ⇒ Object
- #success(message) ⇒ Object
- #warning(message) ⇒ Object
Constructor Details
#initialize(options) ⇒ Generator
Returns a new instance of Generator.
14 15 16 17 18 19 20 21 22 |
# File 'lib/neptune_coffee/generator.rb', line 14 def initialize() raise ArgumentError.new(":root option required, must be a string and valid path") unless Pathname.new([:root]).directory? @root = Pathname.new [:root] @force = [:force] @verbose = [:verbose] @overwrite = [:overwrite] @quiet = [:quiet] reset_file_info end |
Instance Attribute Details
#current_files ⇒ Object
Returns the value of attribute current_files.
8 9 10 |
# File 'lib/neptune_coffee/generator.rb', line 8 def current_files @current_files end |
#dirs ⇒ Object
Returns the value of attribute dirs.
9 10 11 |
# File 'lib/neptune_coffee/generator.rb', line 9 def dirs @dirs end |
#force ⇒ Object
Returns the value of attribute force.
9 10 11 |
# File 'lib/neptune_coffee/generator.rb', line 9 def force @force end |
#generated_files ⇒ Object
Returns the value of attribute generated_files.
8 9 10 |
# File 'lib/neptune_coffee/generator.rb', line 8 def generated_files @generated_files end |
#overwrite ⇒ Object
Returns the value of attribute overwrite.
9 10 11 |
# File 'lib/neptune_coffee/generator.rb', line 9 def overwrite @overwrite end |
#quiet ⇒ Object
Returns the value of attribute quiet.
9 10 11 |
# File 'lib/neptune_coffee/generator.rb', line 9 def quiet @quiet end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
10 11 12 |
# File 'lib/neptune_coffee/generator.rb', line 10 def root @root end |
#skipped_files ⇒ Object
Returns the value of attribute skipped_files.
8 9 10 |
# File 'lib/neptune_coffee/generator.rb', line 8 def skipped_files @skipped_files end |
#to_generate_files ⇒ Object
Returns the value of attribute to_generate_files.
8 9 10 |
# File 'lib/neptune_coffee/generator.rb', line 8 def to_generate_files @to_generate_files end |
#verbose ⇒ Object
Returns the value of attribute verbose.
9 10 11 |
# File 'lib/neptune_coffee/generator.rb', line 9 def verbose @verbose end |
Instance Method Details
#error(message) ⇒ Object
32 |
# File 'lib/neptune_coffee/generator.rb', line 32 def error() Guard::UI.error "NeptuneCoffee: "+; end |
#file_was_generated_by_neptune_coffee(file) ⇒ Object
69 70 71 |
# File 'lib/neptune_coffee/generator.rb', line 69 def file_was_generated_by_neptune_coffee file !file.exist? || (file.read(SAFE_GENERATE_FIRST_LINE.length) == SAFE_GENERATE_FIRST_LINE) end |
#generate_all ⇒ Object
returns:
{
current_files: [Pathnames...]
generated_files: [Pathnames...]
skipped_files: [Pathnames...]
}
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/neptune_coffee/generator.rb', line 48 def generate_all reset_file_info load_dirs success "generating all files in: #{root}" success "#{@dirs.length} directories found" @dirs.all.each do |dir| generate_module dir unless dir == root generate_namespace dir end success "#{@current_files.length}/#{@to_generate_files.length} files current" success "#{@generated_files.length} files generated" # if @generated_files.length > 0 warning "#{@skipped_files.length} files skipped (this is a name conflict. We recommend renaming your source file(s)." if @skipped_files.length> 0 return { current_files: @current_files.keys, generated_files: @generated_files.keys, skipped_files: @skipped_files.keys } end |
#generate_module(dir) ⇒ Object
103 104 105 106 107 |
# File 'lib/neptune_coffee/generator.rb', line 103 def generate_module dir subdirs = @dirs.subdirs(dir) files = dir.children.select {|f| f.extname.downcase == ".js" && !subdirs.index(f.sub_ext(""))} safe_generate(dir.sub_ext(".js")) {JavascriptGenerator.new(root, dir).module(@dirs.subdirs(dir), files)} end |
#generate_namespace(dir) ⇒ Object
109 110 111 |
# File 'lib/neptune_coffee/generator.rb', line 109 def generate_namespace dir safe_generate(dir + "namespace.js") {JavascriptGenerator.new(root, dir).namespace(@dirs.subdirs(dir))} end |
#info(message) ⇒ Object
30 |
# File 'lib/neptune_coffee/generator.rb', line 30 def info() Guard::UI.info "NeptuneCoffee: "+ unless @quiet; end |
#load_dirs ⇒ Object
24 25 26 27 |
# File 'lib/neptune_coffee/generator.rb', line 24 def load_dirs @dirs = SimpleDirectoryStructure.new @root @dirs.add_all end |
#reset_file_info ⇒ Object
34 35 36 37 38 39 |
# File 'lib/neptune_coffee/generator.rb', line 34 def reset_file_info @generated_files = {} @to_generate_files = {} @current_files = {} @skipped_files = {} end |
#safe_generate(file) ⇒ Object
writes to “file” whatever the block yields IF
the file was originally generated by NeptuneCoffee (unless @overwrite)
the file contents changed (unless @force)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/neptune_coffee/generator.rb', line 81 def safe_generate file @to_generate_files[file] = true info_file = file #join @root, file.split(@root)[-1] if @overwrite || file_was_generated_by_neptune_coffee(file) new_contents = SAFE_GENERATE_FIRST_LINE + " #{NeptuneCoffee::VERSION}\n" + "// path: #{file.relative_path_from @root}\n" + yield if @force || !file.exist? || file.read != new_contents @generated_files[file] = true success "generating: #{info_file}" show_generated new_contents if @verbose file.open("w") {|f| f.write new_contents} else @current_files[file] = true end else @skipped_files[file] = true warning "skipping: #{info_file}" end end |
#show_generated(contents) ⇒ Object
73 74 75 76 |
# File 'lib/neptune_coffee/generator.rb', line 73 def show_generated contents highlighted = CodeRay.scan(new_contents, :javascript).terminal info "output:\n "+highlighted.gsub("\n","\n ") end |