Commit 54e68208 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Only copy to output dir the files which are actually needed.

Previously the whole contents of ~/template or ~/template-min
was copied over, which included files like README.md and build-js.html
that were only needed for development.

Now it's all a lot cleaner.
parent 2f80702a
Loading
Loading
Loading
Loading
+17 −15
Original line number Diff line number Diff line
@@ -13,18 +13,23 @@ module JsDuck
      @opts = opts
    end

    # In normal mode creates index.html.
    #
    # When --seo enabled, creates index.php, template.html and print-template.html.
    def write
      create_template_html
      create_print_template_html

      if !@opts.seo
        FileUtils.rm(@opts.output_dir+"/index.php")
        FileUtils.cp(@opts.output_dir+"/template.html", @opts.output_dir+"/index.html")
      if @opts.seo
        FileUtils.cp(@opts.template_dir+"/index.php", @opts.output_dir+"/index.php")
        create_template_html(@opts.template_dir+"/template.html", @opts.output_dir+"/template.html")
        create_print_template_html(@opts.template_dir+"/print-template.html", @opts.output_dir+"/print-template.html")
      else
        create_template_html(@opts.template_dir+"/template.html", @opts.output_dir+"/index.html")
      end
    end

    def create_template_html
      write_template("template.html", {
    private

    def create_template_html(in_file, out_file)
      write_template(in_file, out_file, {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
        "{footer}" => "<div id='footer-content' style='display: none'>#{@opts.footer}</div>",
@@ -40,23 +45,20 @@ module JsDuck
      })
    end

    def create_print_template_html
      write_template("print-template.html", {
    def create_print_template_html(in_file, out_file)
      write_template(in_file, out_file, {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
      })
    end

    # Opens file in template dir, replaces {keys} inside it, writes to output dir
    def write_template(filename, replacements)
      in_file = @opts.template_dir + '/' + filename
      out_file = @opts.output_dir + '/' + filename
    # Opens in_file, replaces {keys} inside it, writes to out_file
    def write_template(in_file, out_file, replacements)
      Logger.instance.log("Writing", out_file)
      html = IO.read(in_file)
      html.gsub!(/\{\w+\}/) do |key|
        replacements[key] ? replacements[key] : key
      end
      FileUtils.rm(out_file)
      File.open(out_file, 'w') {|f| f.write(html) }
    end

+24 −15
Original line number Diff line number Diff line
@@ -9,31 +9,40 @@ module JsDuck
  class TemplateDir
    def initialize(opts)
      @opts = opts
      @files = [
        "app",
        "app.js",
        "favicon.ico",
        "extjs",
        "resources",
      ]
    end

    def write
      FileUtils.mkdir(@opts.output_dir)
      if @opts.template_links
        link_files
        Logger.instance.log("Linking template files to", @opts.output_dir)
        move_files(:symlink)
      else
        copy_files
        Logger.instance.log("Copying template files to", @opts.output_dir)
        move_files(:cp_r)
      end

      if @opts.eg_iframe
        FileUtils.rm(@opts.output_dir+"/eg-iframe.html")
        FileUtils.cp(@opts.eg_iframe, @opts.output_dir+"/eg-iframe.html")
      end
      # always copy the eg-iframe file.
      eg_iframe = @opts.eg_iframe || @opts.template_dir+"/eg-iframe.html"
      FileUtils.cp(eg_iframe, @opts.output_dir+"/eg-iframe.html")
    end

    def copy_files
      Logger.instance.log("Copying template files to", @opts.output_dir)
      FileUtils.cp_r(@opts.template_dir, @opts.output_dir)
    end
    private

    def link_files
      Logger.instance.log("Linking template files to", @opts.output_dir)
      FileUtils.mkdir(@opts.output_dir)
      Dir.glob(@opts.template_dir + "/*").each do |file|
        File.symlink(File.expand_path(file), @opts.output_dir+"/"+File.basename(file))
    # moves files from one dir to another using a method of FileUtils module.
    def move_files(method)
      @files.each do |file|
        source = File.expand_path(@opts.template_dir+"/"+file)
        target = File.expand_path(@opts.output_dir+"/"+file)
        if File.exists?(source)
          FileUtils.send(method, source, target)
        end
      end
    end