summaryrefslogtreecommitdiffstats
path: root/tools/gn/bundle_file_rule.cc
blob: 7684196c35646e5cf8887a28041e85b5ec1f59ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/bundle_file_rule.h"

#include "tools/gn/output_file.h"
#include "tools/gn/settings.h"
#include "tools/gn/substitution_pattern.h"
#include "tools/gn/substitution_writer.h"
#include "tools/gn/target.h"

BundleFileRule::BundleFileRule(const std::vector<SourceFile> sources,
                               const SubstitutionPattern& pattern)
    : sources_(sources), pattern_(pattern) {}

BundleFileRule::BundleFileRule(const BundleFileRule& other) = default;

BundleFileRule::~BundleFileRule() {}

SourceFile BundleFileRule::ApplyPatternToSource(
    const Settings* settings,
    const BundleData& bundle_data,
    const SourceFile& source_file) const {
  std::string output_path;
  for (const auto& subrange : pattern_.ranges()) {
    switch (subrange.type) {
      case SUBSTITUTION_LITERAL:
        output_path.append(subrange.literal);
        break;
      case SUBSTITUTION_BUNDLE_ROOT_DIR:
        output_path.append(bundle_data.root_dir());
        break;
      case SUBSTITUTION_BUNDLE_RESOURCES_DIR:
        output_path.append(bundle_data.resources_dir());
        break;
      case SUBSTITUTION_BUNDLE_EXECUTABLE_DIR:
        output_path.append(bundle_data.executable_dir());
        break;
      case SUBSTITUTION_BUNDLE_PLUGINS_DIR:
        output_path.append(bundle_data.plugins_dir());
        break;
      default:
        output_path.append(SubstitutionWriter::GetSourceSubstitution(
            settings, source_file, subrange.type,
            SubstitutionWriter::OUTPUT_ABSOLUTE, SourceDir()));
        break;
    }
  }
  return SourceFile(SourceFile::SWAP_IN, &output_path);
}

OutputFile BundleFileRule::ApplyPatternToSourceAsOutputFile(
    const Settings* settings,
    const BundleData& bundle_data,
    const SourceFile& source_file) const {
  return OutputFile(settings->build_settings(),
                    ApplyPatternToSource(settings, bundle_data, source_file));
}