Class: NeptuneCoffee::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/neptune_coffee/generator.rb

Constant Summary collapse

SAFE_GENERATE_FIRST_LINE =
"// Generated by NeptuneCoffee"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/neptune_coffee/generator.rb', line 14

def initialize(options)
  raise ArgumentError.new(":root option required, must be a string and valid path") unless Pathname.new(options[:root]).directory?
  @root = Pathname.new options[:root]
  @force = options[:force]
  @verbose = options[:verbose]
  @overwrite = options[:overwrite]
  @quiet = options[:quiet]
  reset_file_info
end

Instance Attribute Details

#current_filesObject

Returns the value of attribute current_files.



8
9
10
# File 'lib/neptune_coffee/generator.rb', line 8

def current_files
  @current_files
end

#dirsObject

Returns the value of attribute dirs.



9
10
11
# File 'lib/neptune_coffee/generator.rb', line 9

def dirs
  @dirs
end

#forceObject

Returns the value of attribute force.



9
10
11
# File 'lib/neptune_coffee/generator.rb', line 9

def force
  @force
end

#generated_filesObject

Returns the value of attribute generated_files.



8
9
10
# File 'lib/neptune_coffee/generator.rb', line 8

def generated_files
  @generated_files
end

#overwriteObject

Returns the value of attribute overwrite.



9
10
11
# File 'lib/neptune_coffee/generator.rb', line 9

def overwrite
  @overwrite
end

#quietObject

Returns the value of attribute quiet.



9
10
11
# File 'lib/neptune_coffee/generator.rb', line 9

def quiet
  @quiet
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/neptune_coffee/generator.rb', line 10

def root
  @root
end

#skipped_filesObject

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_filesObject

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

#verboseObject

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(message)    Guard::UI.error "NeptuneCoffee: "+message; 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_allObject

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(message)     Guard::UI.info "NeptuneCoffee: "+message unless @quiet; end

#load_dirsObject



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_infoObject



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

#success(message) ⇒ Object



31
# File 'lib/neptune_coffee/generator.rb', line 31

def success(message)  Guard::UI.success "NeptuneCoffee: "+message unless @quiet; end

#warning(message) ⇒ Object



29
# File 'lib/neptune_coffee/generator.rb', line 29

def warning(message)  Guard::UI.warning "NeptuneCoffee: "+message unless @quiet; end