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



34
35
36
37
38
39
40
41
# File 'lib/jsduck/app.rb', line 34

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



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jsduck/app.rb', line 117

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jsduck/app.rb', line 135

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/jsduck/app.rb', line 158

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]}")
    begin
      {
        :doc => class_formatter.format(cls.internal_doc),
        :images => doc_formatter.images
      }
    rescue
      Logger.instance.fatal("Error while formatting #{cls[:name]}", $!)
      exit(1)
    end
  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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jsduck/app.rb', line 104

def parallel_parse(filenames)
  @parallel.map(filenames) do |fname|
    Logger.instance.log("Parsing", fname)
    begin
      SourceFile.new(JsDuck::IO.read(fname), fname, @opts)
    rescue
      Logger.instance.fatal("Error while parsing #{fname}", $!)
      exit(1)
    end
  end
end

#runObject

Call this after input parameters set



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

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,
      :examples => ExamplesExporter,
    }
    cw = ClassWriter.new(exporters[@opts.export], @relations, @opts)
    cw.write(@opts.output_dir, ".json")
    if @opts.export == :examples
      gw = GuideWriter.new(exporters[@opts.export], @assets.guides, @opts)
      gw.write(@opts.output_dir, ".json")
    end
    Stdout.instance.flush
  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.
    if @opts.source
      source_writer = SourceWriter.new(parsed_files, @parallel)
      source_writer.write(@opts.output_dir + "/source")
    end
    format_classes

    if @opts.tests
      examples = InlineExamples.new
      examples.add_classes(@relations)
      examples.add_guides(@assets.guides)
      examples.write(@opts.output_dir+"/inline-examples.js")
    end

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

    @assets.write
  end
end