Class: JsDuck::App

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/app.rb

Overview

The main application logic of jsduck

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ App

Initializes app with JsDuck::Options object



29
30
31
32
33
34
35
36
# File 'lib/jsduck/app.rb', line 29

def initialize(opts)
  @opts = opts
  # Sets the nr of parallel processes to use.
  # Set to 0 to disable parallelization completely.
  @parallel = ParallelWrap.new(:in_processes => @opts.processes)
  # Turn JSON pretty-printing on/off
  JsonDuck.pretty = @opts.pretty_json
end

Instance Method Details

#aggregate(parsed_files) ⇒ Object

Aggregates parsing results sequencially



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jsduck/app.rb', line 89

def aggregate(parsed_files)
  agr = Aggregator.new
  parsed_files.each do |file|
    Logger.instance.log("Aggregating", file.filename)
    agr.aggregate(file)
  end
  agr.classify_orphans
  agr.create_global_class
  agr.remove_ignored_classes
  agr.create_accessors
  agr.append_ext4_event_options
  agr.result
end

#filter_classes(docs) ⇒ Object

Turns all aggregated data into Class objects. Depending on –ignore-global either keeps or discards the global class. Warnings for global members are printed regardless of that setting, but of course can be turned off using –warnings=-global



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jsduck/app.rb', line 107

def filter_classes(docs)
  classes = []
  docs.each do |d|
    cls = Class.new(d)
    if d[:name] != "global"
      classes << cls
    else
      # add global class only if --ignore-global not specified
      classes << cls unless @opts.ignore_global

      # Print warning for each global member
      cls.all_local_members.each do |m|
        type = m[:tagname].to_s
        name = m[:name]
        file = m[:files][0]
        Logger.instance.warn(:global, "Global #{type}: #{name}", file[:filename], file[:linenr])
      end
    end
  end
  Relations.new(classes, @opts.external_classes)
end

#format_classesObject

Formats each class



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/jsduck/app.rb', line 130

def format_classes
  doc_formatter = DocFormatter.new(@relations, @opts)
  doc_formatter.img_path = "images"
  class_formatter = ClassFormatter.new(@relations, doc_formatter)
  # Don't format types when exporting
  class_formatter.include_types = !@opts.export
  # Format all doc-objects in parallel
  formatted_classes = @parallel.map(@relations.classes) do |cls|
    Logger.instance.log("Markdown formatting #{cls[:name]}")
    {
      :doc => class_formatter.format(cls.internal_doc),
      :images => doc_formatter.images
    }
  end
  # Then merge the data back to classes sequentially
  formatted_classes.each do |cls|
    @relations[cls[:doc][:name]].internal_doc = cls[:doc]
    cls[:images].each {|img| @assets.images.add(img) }
  end
end

#parallel_parse(filenames) ⇒ Object

Parses the files in parallel using as many processes as available CPU-s



81
82
83
84
85
86
# File 'lib/jsduck/app.rb', line 81

def parallel_parse(filenames)
  @parallel.map(filenames) do |fname|
    Logger.instance.log("Parsing", fname)
    SourceFile.new(IO.read(fname), fname, @opts)
  end
end

#runObject

Call this after input parameters set



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
# File 'lib/jsduck/app.rb', line 39

def run
  parsed_files = parallel_parse(@opts.input_files)
  result = aggregate(parsed_files)
  @relations = filter_classes(result)
  InheritDoc.new(@relations).resolve_all
  Lint.new(@relations).run

  # Initialize guides, videos, examples, ...
  @assets = Assets.new(@relations, @opts)

  # Give access to assets from all meta-tags
  MetaTagRegistry.instance.assets = @assets

  if @opts.export
    format_classes
    FileUtils.rm_rf(@opts.output_dir) unless @opts.output_dir == :stdout
    exporters = {:full => FullExporter, :api => ApiExporter}
    cw = ClassWriter.new(exporters[@opts.export], @relations, @opts)
    cw.write(@opts.output_dir, ".json")
  else
    FileUtils.rm_rf(@opts.output_dir)
    TemplateDir.new(@opts).write

    IndexHtml.new(@assets, @opts).write

    AppData.new(@relations, @assets, @opts).write(@opts.output_dir+"/data.js")

    # class-formatting is done in parallel which breaks the links
    # between source files and classes. Therefore it MUST to be done
    # after writing sources which needs the links to work.
    source_writer = SourceWriter.new(parsed_files, @parallel)
    source_writer.write(@opts.output_dir + "/source")
    format_classes

    cw = ClassWriter.new(AppExporter, @relations, @opts)
    cw.write(@opts.output_dir+"/output", ".js")

    @assets.write
  end
end