Skip to content

Commit

Permalink
Support for painless language autocomplete within monaco (elastic#80577
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Nov 30, 2020
1 parent 91a2b68 commit 9bc8d30
Show file tree
Hide file tree
Showing 43 changed files with 430,052 additions and 9 deletions.
1 change: 1 addition & 0 deletions .i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"src/plugins/management"
],
"maps_legacy": "src/plugins/maps_legacy",
"monaco": "packages/kbn-monaco/src",
"indexPatternManagement": "src/plugins/index_pattern_management",
"advancedSettings": "src/plugins/advanced_settings",
"kibana_legacy": "src/plugins/kibana_legacy",
Expand Down
52 changes: 52 additions & 0 deletions packages/kbn-monaco/scripts/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module.exports = {
licenseHeader: `/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
`,
supportedContexts: [
'boolean_script_field_script_field',
'date_script_field',
'double_script_field_script_field',
'filter',
'ip_script_field_script_field',
'long_script_field_script_field',
'painless_test',
'processor_conditional',
'score',
'string_script_field_script_field',
],
};
102 changes: 102 additions & 0 deletions packages/kbn-monaco/scripts/generate_autocomplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { join } = require('path');
const { readdirSync, readFileSync, writeFileSync } = require('fs');
const minimist = require('minimist');
const semver = require('semver');
const ora = require('ora');
const del = require('del');

const {
cloneAndCheckout,
createAutocompleteDefinitions,
createAutocompleteExports,
} = require('./utils');
const { supportedContexts } = require('./constants');

start(
minimist(process.argv.slice(2), {
string: ['tag', 'branch'],
})
);

function start(opts) {
const log = ora('Loading Elasticsearch repository').start();

if (opts.branch == null && semver.valid(opts.tag) === null) {
log.fail(`Missing or invalid tag: ${opts.tag}`);
return;
}

const autocompleteOutputFolder = join(
__dirname,
'..',
'src',
'painless',
'autocomplete_definitions'
);

log.text = 'Cleaning autocomplete definitions folder';
del.sync([`${autocompleteOutputFolder}/**`]);

cloneAndCheckout(
{ log, tag: opts.tag, branch: opts.branch },
(err, { esPainlessContextFolder }) => {
if (err) {
log.fail(err.message);
return;
}

const painlessContextFolderContents = readdirSync(esPainlessContextFolder);

// Generate autocomplete definitions
painlessContextFolderContents
.filter((file) => {
// Expected filename format: whitelist-<contextName>.json
const contextName = file.split('.')[0].split('whitelist-').pop();
return supportedContexts.includes(contextName);
})
.forEach((file) => {
try {
const { name, classes: painlessClasses } = JSON.parse(
readFileSync(join(esPainlessContextFolder, file), 'utf8')
);
const filePath = join(autocompleteOutputFolder, `${name}.json`);
const code = JSON.stringify(
{ suggestions: createAutocompleteDefinitions(painlessClasses) },
null,
2
);
writeFileSync(filePath, code, { encoding: 'utf8' });
} catch (err) {
log.fail(err.message);
}
});

// Create index.ts file for autocomplete definitions
const indexFilePath = join(autocompleteOutputFolder, 'index.ts');
const indexCode = createAutocompleteExports();

writeFileSync(indexFilePath, indexCode, { encoding: 'utf8' });

log.succeed('Painless autocomplete definitions generated successfully');
}
);
}
149 changes: 149 additions & 0 deletions packages/kbn-monaco/scripts/utils/clone_es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { accessSync, mkdirSync } = require('fs');
const { join } = require('path');
const simpleGit = require('simple-git');

// Note: The generated whitelists have not yet been merged to master
// so this script may fail until code in this branch has been merged:
// https://github.com/stu-elastic/elasticsearch/tree/scripting/whitelists
const esRepo = 'https://github.com/elastic/elasticsearch.git';

const esFolder = join(__dirname, '..', '..', 'elasticsearch');
const esPainlessContextFolder = join(
esFolder,
'modules',
'lang-painless',
'src',
'main',
'generated'
);

/**
* Checks if the given path exists
* @param {string} path
* @returns {boolean} true if exists, false if not
*/
const pathExist = (path) => {
try {
accessSync(path);
return true;
} catch (err) {
return false;
}
};

/**
* Creates the given folder
* @param {string} name
* @returns {boolean} true on success, false on failure
*/
const createFolder = (name) => {
try {
mkdirSync(name);
return true;
} catch (err) {
return false;
}
};

/**
* Sets the Elasticsearch repository to the given branch or tag.
* If the repository is not present in `esFolder` it will
* clone the repository and the checkout the branch or tag.
* If the repository is already present but it cannot checkout to
* the given tag, it will perform a pull and then try again.
*
* This code was largely borrowed from the ES JS client:
* https://github.com/elastic/elasticsearch-js/blob/master/scripts/utils/clone-es.js
*
* @param {object} options
* @param {function} callback
*/
const cloneAndCheckout = (opts, callback) => {
const { log, tag, branch } = opts;

let fresh = false;
let retry = 0;

if (!pathExist(esFolder)) {
if (!createFolder(esFolder)) {
log.fail('Failed to create ES folder');
return;
}
fresh = true;
}

const git = simpleGit(esFolder);

const pull = (cb) => {
log.text = 'Pulling Elasticsearch repository';

git.pull((err) => {
if (err) {
callback(err, { esPainlessContextFolder });
return;
}
cb();
});
};

const clone = (cb) => {
log.text = 'Cloning Elasticsearch repository';

git.clone(esRepo, esFolder, (err) => {
if (err) {
callback(err, { esPainlessContextFolder });
return;
}
cb();
});
};

const checkout = (alsoPull = false) => {
if (branch) {
log.text = `Checking out branch '${branch}'`;
} else {
log.text = `Checking out tag '${tag}'`;
}

git.checkout(branch || tag, (err) => {
if (err) {
if (retry++ > 0) {
callback(new Error(`Cannot checkout '${branch || tag}'`), { esPainlessContextFolder });
return;
}
return pull(checkout);
}
if (alsoPull) {
return pull(checkout);
}
callback(null, { esPainlessContextFolder });
});
};

if (fresh) {
clone(checkout);
} else {
checkout(Boolean(opts.branch));
}
};

module.exports = cloneAndCheckout;
Loading

0 comments on commit 9bc8d30

Please sign in to comment.