Flutter Engine
The Flutter Engine
tools
templater
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
17
namespace
flutter
{
18
19
bool
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)) {
24
FML_LOG
(
ERROR
)
25
<<
"Input template path not specified. Use --templater-input."
;
26
return
false
;
27
}
28
if
(!command_line.
GetOptionValue
(
"templater-output"
, &
output_path
)) {
29
FML_LOG
(
ERROR
)
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(),
55
false
,
fml::FilePermission::kReadWrite
);
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
69
int
main
(
int
argc,
char
const
*
argv
[]) {
70
return
flutter::TemplaterMain
(
fml::CommandLineFromArgcArgv
(argc,
argv
))
71
? EXIT_SUCCESS
72
: EXIT_FAILURE;
73
}
fml::CommandLine
Definition:
command_line.h:56
fml::CommandLine::options
const std::vector< Option > & options() const
Definition:
command_line.h:95
fml::CommandLine::GetOptionValue
bool GetOptionValue(std::string_view name, std::string *value) const
Definition:
command_line.cc:51
fml::FileMapping::CreateReadOnly
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition:
mapping.cc:20
fml::NonOwnedMapping
Definition:
mapping.h:115
FML_LOG
#define FML_LOG(severity)
Definition:
logging.h:82
argv
char ** argv
Definition:
library.h:9
dart_profiler_symbols.output
output
Definition:
dart_profiler_symbols.py:51
env
Definition:
__init__.py:1
flutter
Definition:
asset_manager.cc:10
flutter::TemplaterMain
bool TemplaterMain(const fml::CommandLine &command_line)
Definition:
templater_main.cc:19
fml::CommandLineFromArgcArgv
CommandLine CommandLineFromArgcArgv(int argc, const char *const *argv)
Definition:
command_line.h:233
fml::WriteAtomically
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
Definition:
file_posix.cc:191
fml::OpenDirectory
fml::UniqueFD OpenDirectory(const char *path, bool create_if_necessary, FilePermission permission)
Definition:
file_posix.cc:97
fml::FilePermission::kReadWrite
@ kReadWrite
gn.bazel_build.output_path
output_path
Definition:
bazel_build.py:39
pkg-config.env
env
Definition:
pkg-config.py:140
main
int main(int argc, char const *argv[])
Definition:
templater_main.cc:69
ERROR
#define ERROR(message)
Definition:
elf_loader.cc:260
Generated on Sun Jun 23 2024 21:56:53 for Flutter Engine by
1.9.4