Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
12#include "flutter/fml/file.h"
13#include "flutter/fml/logging.h"
14#include "flutter/fml/mapping.h"
16#include "inja/inja.hpp"
17
18namespace flutter {
19
20bool TemplaterMain(const fml::CommandLine& command_line) {
21 std::string input_path;
22 std::string output_path;
23
24 if (!command_line.GetOptionValue("templater-input", &input_path)) {
25 FML_LOG(ERROR)
26 << "Input template path not specified. Use --templater-input.";
27 return false;
28 }
29 if (!command_line.GetOptionValue("templater-output", &output_path)) {
30 FML_LOG(ERROR)
31 << "Input template path not specified. Use --templater-output.";
32 return false;
33 }
34
35 auto input = fml::FileMapping::CreateReadOnly(input_path);
36 if (!input) {
37 FML_LOG(ERROR) << "Could not open input file: " << input_path;
38 return false;
39 }
40
41 nlohmann::json arguments;
42 for (const auto& option : command_line.options()) {
43 arguments[option.name] = option.value;
44 }
45 inja::Environment env;
46 auto rendered_template = env.render(
47 std::string_view{reinterpret_cast<const char*>(input->GetMapping()),
48 input->GetSize()},
49 arguments);
50 auto output = fml::NonOwnedMapping{
51 reinterpret_cast<const uint8_t*>(rendered_template.data()),
52 rendered_template.size()};
53
54 auto current_dir = fml::OpenDirectory(
55 fml::PathToUtf8(std::filesystem::current_path()).c_str(), false,
57 if (!current_dir.is_valid()) {
58 FML_LOG(ERROR) << "Could not open current directory.";
59 return false;
60 }
61 if (!fml::WriteAtomically(current_dir, output_path.c_str(), output)) {
62 FML_LOG(ERROR) << "Could not write output to path: " << output_path;
63 return false;
64 }
65 return true;
66}
67
68} // namespace flutter
69
70int main(int argc, char const* argv[]) {
72 ? EXIT_SUCCESS
73 : EXIT_FAILURE;
74}
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
static int input(yyscan_t yyscanner)
#define FML_LOG(severity)
Definition logging.h:101
char ** argv
Definition library.h:9
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
std::string PathToUtf8(const std::filesystem::path &path)
int main(int argc, char const *argv[])