Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly locate common proto files #46

Merged
merged 4 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ system-test/secrets.js
system-test/*key.json
*.lock
*-lock.js*
google/
16 changes: 6 additions & 10 deletions .jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ module.exports = {
template: './node_modules/ink-docstrap/template',
recurse: true,
verbose: true,
destination: './docs/'
destination: './docs/',
},
plugins: [
'plugins/markdown'
],
plugins: ['plugins/markdown'],
source: {
excludePattern: '(^|\\/|\\\\)[._]',
include: [

This comment was marked as spam.

This comment was marked as spam.

'src'
],
includePattern: '\\.js$'
include: ['src'],
includePattern: '\\.js$',
},
templates: {
copyright: 'Copyright 2017 Google, Inc.',
includeDate: false,
sourceFiles: false,
systemName: 'google-proto-files',
theme: 'lumen'
}
theme: 'lumen',
},
};
59 changes: 40 additions & 19 deletions load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,60 @@ var globby = require('globby');
var path = require('path');
var protobuf = require('protobufjs');

var COMMON_PROTO_DIRS = [
// This list of directories is defined here:
// https://github.com/googleapis/googleapis/blob/master/gapic/packaging/common_protos.yaml
'api',
path.join('logging', 'type'),
'longrunning',
'protobuf', // This is an additional path that the common protos depend on.
'rpc',
'type',
];

var COMMON_PROTO_GLOB_PATTERNS = COMMON_PROTO_DIRS.map(function(dir) {
return path.join('google', dir, '**', '*.proto');
});

var COMMON_PROTO_FILES = globby.sync(COMMON_PROTO_GLOB_PATTERNS);
let COMMON_PROTO_FILES;

class GoogleProtoFilesRoot extends protobuf.Root {
constructor() {
super([].slice.apply(arguments));
}

// Caches and returns an array of the local common/google core proto files
// exist on disk.
static getCommonProtoFiles() {
var commonProtoDirs = [
// This list of directories is defined here:
// https://github.com/googleapis/googleapis/blob/master/gapic/packaging/common_protos.yaml
'api',
path.join('logging', 'type'),
'longrunning',
'protobuf', // This is an additional path that the common protos depend on.
'rpc',
'type',
];

var commonProtoGlobPatterns = commonProtoDirs.map(dir =>
path.join(__dirname, 'google', dir, '**', '*.proto')
);

if (!COMMON_PROTO_FILES) {
COMMON_PROTO_FILES = globby
.sync(commonProtoGlobPatterns)
.map(path.normalize);
}

return COMMON_PROTO_FILES;
}

// Causes the loading of an included proto to check if it is a common
// proto. If it is a common proto, use the google-proto-files proto.
resolvePath(_, includePath) {
includePath = path.normalize(includePath);

This comment was marked as spam.

This comment was marked as spam.


// Fully qualified paths don't need to be resolved.
if (includePath.startsWith('/')) {
if (path.isAbsolute(includePath)) {
return includePath;
}

if (COMMON_PROTO_FILES.indexOf(includePath) > -1) {
return path.join(__dirname, includePath);
const fullIncludePath = path.join(
__dirname,
path.relative(__dirname, includePath)
);
const commonProtoFiles = GoogleProtoFilesRoot.getCommonProtoFiles();

if (commonProtoFiles.indexOf(fullIncludePath) > -1) {
return fullIncludePath;
}

return protobuf.util.path.resolve.apply(null, [].slice.call(arguments));
}
}
Expand Down
Loading