Set process.resourcesPath in C++

This commit is contained in:
Cheng Zhao 2015-02-13 13:32:58 +08:00
parent 0f15dd04af
commit 4834eed520
3 changed files with 27 additions and 25 deletions

View file

@ -3,10 +3,6 @@ path = require 'path'
module = require 'module' module = require 'module'
util = require 'util' util = require 'util'
# Expose information of current process.
process.type = 'browser'
process.resourcesPath = path.resolve process.argv[1], '..', '..', '..', '..'
# We modified the original process.argv to let node.js load the atom.js, # We modified the original process.argv to let node.js load the atom.js,
# we need to restore it here. # we need to restore it here.
process.argv.splice 1, 1 process.argv.splice 1, 1

View file

@ -7,6 +7,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
@ -14,6 +15,7 @@
#include "base/path_service.h" #include "base/path_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "native_mate/locker.h" #include "native_mate/locker.h"
#include "native_mate/dictionary.h"
#if defined(OS_WIN) #if defined(OS_WIN)
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
@ -110,6 +112,20 @@ std::vector<std::string> String16VectorToStringVector(
} }
#endif #endif
base::FilePath GetResourcesPath(CommandLine* command_line, bool is_browser) {
base::FilePath exec_path(command_line->argv()[0]);
PathService::Get(base::FILE_EXE, &exec_path);
base::FilePath resources_path =
#if defined(OS_MACOSX)
is_browser ? exec_path.DirName().DirName().Append("Resources") :
exec_path.DirName().DirName().DirName().DirName().DirName()
.Append("Resources");
#else
exec_path.DirName().Append(FILE_PATH_LITERAL("resources"));
#endif
return resources_path;
}
} // namespace } // namespace
node::Environment* global_env = nullptr; node::Environment* global_env = nullptr;
@ -158,31 +174,25 @@ node::Environment* NodeBindings::CreateEnvironment(
#endif #endif
// Feed node the path to initialization script. // Feed node the path to initialization script.
base::FilePath exec_path(command_line->argv()[0]); const char* process_type = is_browser_ ? "browser" : "renderer";
PathService::Get(base::FILE_EXE, &exec_path); base::FilePath resources_path = GetResourcesPath(command_line, is_browser_);
base::FilePath resources_path =
#if defined(OS_MACOSX)
is_browser_ ? exec_path.DirName().DirName().Append("Resources") :
exec_path.DirName().DirName().DirName().DirName().DirName()
.Append("Resources");
#else
exec_path.DirName().Append(FILE_PATH_LITERAL("resources"));
#endif
base::FilePath script_path = base::FilePath script_path =
resources_path.Append(FILE_PATH_LITERAL("atom.asar")) resources_path.Append(FILE_PATH_LITERAL("atom.asar"))
.Append(is_browser_ ? FILE_PATH_LITERAL("browser") : .Append(FILE_PATH_LITERAL(process_type))
FILE_PATH_LITERAL("renderer"))
.Append(FILE_PATH_LITERAL("lib")) .Append(FILE_PATH_LITERAL("lib"))
.Append(FILE_PATH_LITERAL("init.js")); .Append(FILE_PATH_LITERAL("init.js"));
std::string script_path_str = script_path.AsUTF8Unsafe(); std::string script_path_str = script_path.AsUTF8Unsafe();
args.insert(args.begin() + 1, script_path_str.c_str()); args.insert(args.begin() + 1, script_path_str.c_str());
scoped_ptr<const char*[]> c_argv = StringVectorToArgArray(args); scoped_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
return node::CreateEnvironment(context->GetIsolate(), node::Environment* env = node::CreateEnvironment(
uv_default_loop(), context->GetIsolate(), uv_default_loop(), context,
context, args.size(), c_argv.get(), 0, nullptr);
args.size(), c_argv.get(),
0, nullptr); mate::Dictionary process(context->GetIsolate(), env->process_object());
process.Set("type", process_type);
process.Set("resourcesPath", resources_path);
return env;
} }
void NodeBindings::LoadEnvironment(node::Environment* env) { void NodeBindings::LoadEnvironment(node::Environment* env) {

View file

@ -4,10 +4,6 @@ path = require 'path'
url = require 'url' url = require 'url'
Module = require 'module' Module = require 'module'
# Expose information of current process.
process.type = 'renderer'
process.resourcesPath = path.resolve process.argv[1], '..', '..', '..', '..'
# We modified the original process.argv to let node.js load the # We modified the original process.argv to let node.js load the
# atom-renderer.js, we need to restore it here. # atom-renderer.js, we need to restore it here.
process.argv.splice 1, 1 process.argv.splice 1, 1