@@ -15,16 +15,6 @@
|
||||
|
||||
namespace {
|
||||
|
||||
std::string Fetch(const std::string& url) {
|
||||
base::FilePath path(url);
|
||||
std::string source;
|
||||
if (!base::ReadFileToString(path, &source)) {
|
||||
fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str());
|
||||
exit(1);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
base::FilePath SimplifyPath(const base::FilePath& path) {
|
||||
std::vector<base::FilePath::StringType> components;
|
||||
path.GetComponents(&components);
|
||||
@@ -42,6 +32,34 @@ base::FilePath SimplifyPath(const base::FilePath& path) {
|
||||
return result;
|
||||
}
|
||||
|
||||
class Fetcher {
|
||||
public:
|
||||
std::string Fetch(const std::string& url);
|
||||
const std::set<std::string>& dependencies() const { return dependencies_; }
|
||||
private:
|
||||
std::set<std::string> dependencies_;
|
||||
};
|
||||
|
||||
std::string Fetcher::Fetch(const std::string& url) {
|
||||
base::FilePath path(url);
|
||||
std::string source;
|
||||
if (!base::ReadFileToString(path, &source)) {
|
||||
fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str());
|
||||
exit(1);
|
||||
}
|
||||
dependencies_.insert(url);
|
||||
return source;
|
||||
}
|
||||
|
||||
Fetcher* g_fetcher = nullptr;
|
||||
|
||||
Fetcher& GetFetcher() {
|
||||
if (!g_fetcher) {
|
||||
g_fetcher = new Fetcher();
|
||||
}
|
||||
return *g_fetcher;
|
||||
}
|
||||
|
||||
class Loader {
|
||||
public:
|
||||
Loader(const base::FilePath& package_root);
|
||||
@@ -80,14 +98,14 @@ Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Import(Dart_Handle url) {
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
|
||||
Dart_Handle source = StringToDart(GetFetcher().Fetch(StringFromDart(url)));
|
||||
Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
|
||||
Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
|
||||
Dart_Handle source = StringToDart(GetFetcher().Fetch(StringFromDart(url)));
|
||||
Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
|
||||
LogIfError(result);
|
||||
return result;
|
||||
@@ -128,5 +146,10 @@ Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
|
||||
void LoadScript(const std::string& url) {
|
||||
LogIfError(
|
||||
Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0));
|
||||
Dart_LoadScript(StringToDart(url), StringToDart(GetFetcher().Fetch(url)),
|
||||
0, 0));
|
||||
}
|
||||
|
||||
const std::set<std::string>& GetDependencies() {
|
||||
return GetFetcher().dependencies();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
#define SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "dart/runtime/include/dart_api.h"
|
||||
@@ -13,5 +14,6 @@ Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
|
||||
Dart_Handle library,
|
||||
Dart_Handle url);
|
||||
void LoadScript(const std::string& url);
|
||||
const std::set<std::string>& GetDependencies();
|
||||
|
||||
#endif // SKY_TOOLS_SKY_SNAPSHOT_LOADER_H_
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
void Usage() {
|
||||
std::cerr << "Usage: sky_snapshot"
|
||||
<< " --" << switches::kPackageRoot << " --" << switches::kSnapshot
|
||||
<< " --" << switches::kDepfile << " --" << switches::kBuildOutput
|
||||
<< " <lib/main.dart>" << std::endl;
|
||||
}
|
||||
|
||||
@@ -33,6 +34,21 @@ void WriteSnapshot(base::FilePath path) {
|
||||
size);
|
||||
}
|
||||
|
||||
void WriteDependencies(base::FilePath path,
|
||||
const std::string& build_output,
|
||||
const std::set<std::string>& deps) {
|
||||
base::FilePath current_directory;
|
||||
CHECK(base::GetCurrentDirectory(¤t_directory));
|
||||
std::string output = build_output + ":";
|
||||
for (const auto& i : deps) {
|
||||
output += " ";
|
||||
output += current_directory.Append(i).MaybeAsASCII();
|
||||
}
|
||||
const char* data = output.c_str();
|
||||
const intptr_t data_length = output.size();
|
||||
CHECK_EQ(base::WriteFile(path, data, data_length), data_length);
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
base::AtExitManager exit_manager;
|
||||
base::EnableTerminationOnHeapCorruption();
|
||||
@@ -63,5 +79,13 @@ int main(int argc, const char* argv[]) {
|
||||
CHECK(command_line.HasSwitch(switches::kSnapshot)) << "Need --snapshot";
|
||||
WriteSnapshot(command_line.GetSwitchValuePath(switches::kSnapshot));
|
||||
|
||||
if (command_line.HasSwitch(switches::kDepfile)) {
|
||||
CHECK(command_line.HasSwitch(switches::kBuildOutput)) <<
|
||||
"Need --build-output with --depfile";
|
||||
WriteDependencies(command_line.GetSwitchValuePath(switches::kDepfile),
|
||||
command_line.GetSwitchValueASCII(switches::kBuildOutput),
|
||||
GetDependencies());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
namespace switches {
|
||||
|
||||
const char kBuildOutput[] = "build-output";
|
||||
const char kDepfile[] = "depfile";
|
||||
const char kHelp[] = "help";
|
||||
const char kPackageRoot[] = "package-root";
|
||||
const char kSnapshot[] = "snapshot";
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
namespace switches {
|
||||
|
||||
extern const char kBuildOutput[];
|
||||
extern const char kDepfile[];
|
||||
extern const char kHelp[];
|
||||
extern const char kPackageRoot[];
|
||||
extern const char kSnapshot[];
|
||||
|
||||
Reference in New Issue
Block a user