Commit 55df5902 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor App class.

Split the huge method into lots of smaller routines.
parent 56d6a113
Loading
Loading
Loading
Loading
+100 −41
Original line number Diff line number Diff line
@@ -39,67 +39,126 @@ module JsDuck

    # Call this after input parameters set
    def run
      batch_parser = BatchParser.new(@opts)
      @relations = batch_parser.run
      @batch_parser = BatchParser.new(@opts)
      @relations = @batch_parser.run

      apply_extra_processing

      init_assets

      if @opts.export
        generate_export
      else
        generate_web_page
      end
    end

    private

    def apply_extra_processing
      InheritDoc.new(@relations).resolve_all
      Importer.import(@opts.imports, @relations, @opts.new_since)
      ReturnValues.auto_detect(@relations)
      Lint.new(@relations).run
    end

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

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

      if @opts.export

    # -- export --


    def generate_export
      format_classes
        FileUtils.rm_rf(@opts.output_dir) unless @opts.output_dir == :stdout

      clean_output_dir unless @opts.output_dir == :stdout

      export_classes
      export_examples_in_guides if @opts.export == :examples

      Util::Stdout.flush if @opts.output_dir == :stdout
    end

    def export_classes
      cw = ClassWriter.new(get_exporter, @relations, @opts)
      cw.write(@opts.output_dir, ".json")
    end

    def get_exporter
      exporters = {
        :full => Exporter::Full,
        :api => Exporter::Api,
          :examples => ExamplesExporter::Examples,
        :examples => Exporter::Examples,
      }
        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)
      exporters[@opts.export]
    end

    def export_examples_in_guides
      gw = GuideWriter.new(Exporter::Examples, @assets.guides, @opts)
      gw.write(@opts.output_dir, ".json")
    end
        Util::Stdout.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")
    # -- web page --


    def generate_web_page
      clean_output_dir

      write_template_files
      write_app_data

      # 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 = Source::Writer.new(batch_parser.parsed_files)
      write_source if @opts.source
      format_classes

      write_inline_examples if @opts.tests

      write_classes

      @assets.write
    end

    def write_template_files
      TemplateDir.new(@opts).write
      IndexHtml.new(@assets, @opts).write
    end

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

    def write_source
      source_writer = Source::Writer.new(@batch_parser.parsed_files)
      source_writer.write(@opts.output_dir + "/source")
    end
        format_classes

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

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

        @assets.write
    def write_classes
      class_writer = ClassWriter.new(Exporter::App, @relations, @opts)
      class_writer.write(@opts.output_dir+"/output", ".js")
    end

    # -- util routines --

    def clean_output_dir
      FileUtils.rm_rf(@opts.output_dir)
    end

    # Formats each class
    def format_classes
      BatchFormatter.format_all!(@relations, @assets, @opts)
    end