1#!/bin/bash
2
3# Copyright 2014 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7set -e
8
9if [ "$#" -ne 2 ]
10then
11  echo "Usage: $0 <src_dir> <dst_dir>"
12  echo
13  echo "Copies <src_dir> to <dst_dir> and extracts all inline scripts from" \
14       "Polymer HTML files found in the destination directory to separate JS" \
15       "files. A JS file extracted from the file with name 'foo.html' will" \
16       "have a name 'foo-extracted.js'. Inclusion of the script file will be" \
17       "added to 'foo.html': '<script src=\"foo-extracted.js\"></script>'."
18  exit 1
19fi
20
21src="$1"
22dst="$2"
23
24if [ -e "$dst" ]
25then
26  echo "ERROR: '$dst' already exists. Please remove it before running the" \
27      "script." 1>&2
28  exit 1
29fi
30
31cp -r "$src" "$dst"
32find "$dst" -name "*.html" \
33            -not -path "*/demos/*" \
34            -not -name "demo*.html" \
35            -not -name "index.html" \
36            -not -name "metadata.html" | \
37xargs grep -l "<script>" | \
38while read original_html_name
39do
40  dir=$(dirname "$original_html_name")
41  name=$(basename "$original_html_name" .html)
42
43  html_without_js="$dir/$name-extracted.html"
44  extracted_js="$dir/$name-extracted.js"
45  vulcanize -o "$html_without_js" --csp --config vulcanize_config.json \
46      "$original_html_name" 1>&2
47  mv "$html_without_js" "$original_html_name"
48done
49