Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
templater_main.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// FLUTTER_NOLINT: https://github.com/flutter/flutter/issues/105732
6
7#include <filesystem>
8#include <string>
9#include <string_view>
10
11#include "flutter/fml/command_line.h"
12#include "flutter/fml/file.h"
13#include "flutter/fml/logging.h"
14#include "flutter/fml/mapping.h"
15#include "inja/inja.hpp"
16
17namespace flutter {
18
19bool TemplaterMain(const fml::CommandLine& command_line) {
20 std::string input_path;
21 std::string output_path;
22
23 if (!command_line.GetOptionValue("templater-input", &input_path)) {
25 << "Input template path not specified. Use --templater-input.";
26 return false;
27 }
28 if (!command_line.GetOptionValue("templater-output", &output_path)) {
30 << "Input template path not specified. Use --templater-output.";
31 return false;
32 }
33
34 auto input = fml::FileMapping::CreateReadOnly(input_path);
35 if (!input) {
36 FML_LOG(ERROR) << "Could not open input file: " << input_path;
37 return false;
38 }
39
40 nlohmann::json arguments;
41 for (const auto& option : command_line.options()) {
42 arguments[option.name] = option.value;
43 }
44 inja::Environment env;
45 auto rendered_template = env.render(
46 std::string_view{reinterpret_cast<const char*>(input->GetMapping()),
47 input->GetSize()},
48 arguments);
49 auto output = fml::NonOwnedMapping{
50 reinterpret_cast<const uint8_t*>(rendered_template.data()),
51 rendered_template.size()};
52
53 auto current_dir =
54 fml::OpenDirectory(std::filesystem::current_path().u8string().c_str(),
56 if (!current_dir.is_valid()) {
57 FML_LOG(ERROR) << "Could not open current directory.";
58 return false;
59 }
60 if (!fml::WriteAtomically(current_dir, output_path.c_str(), output)) {
61 FML_LOG(ERROR) << "Could not write output to path: " << output_path;
62 return false;
63 }
64 return true;
65}
66
67} // namespace flutter
68
69int main(int argc, char const* argv[]) {
71 ? EXIT_SUCCESS
72 : EXIT_FAILURE;
73}
const std::vector< Option > & options() const
bool GetOptionValue(std::string_view name, std::string *value) const
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition mapping.cc:20
#define FML_LOG(severity)
Definition logging.h:82
char ** argv
Definition library.h:9
Definition __init__.py:1
bool TemplaterMain(const fml::CommandLine &command_line)
CommandLine CommandLineFromArgcArgv(int argc, const char *const *argv)
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
fml::UniqueFD OpenDirectory(const char *path, bool create_if_necessary, FilePermission permission)
Definition file_posix.cc:97
Definition main.py:1
#define ERROR(message)