Skip to content

Commit

Permalink
wip: added script to generate painless autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Oct 27, 2020
1 parent 00efc3c commit 7ad74a6
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/kbn-monaco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"babel-loader": "^8.0.6",
"css-loader": "^3.4.2",
"del": "^5.1.0",
"minimist": "^1.2.5",
"ora": "^5.1.0",
"raw-loader": "^3.1.0",
"rimraf": "^3.0.2",
"semver": "^7.3.2",
"supports-color": "^7.0.0",
"typescript": "4.0.2",
"webpack": "^4.41.5",
Expand Down
88 changes: 88 additions & 0 deletions packages/kbn-monaco/scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 rimraf = require('rimraf');
const { cloneAndCheckout, formatData } = require('./utils');

const SUPPORTED_CONTEXTS = [
'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',
];

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');

log.text = 'Cleaning autocomplete folder...';
rimraf.sync(join(autocompleteOutputFolder, '*.ts'));

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

const generatedFolderContents = readdirSync(generatedFolder);

generatedFolderContents
.filter((file) => {
const contextName = file.split('.')[0].split('whitelist-').pop();
return SUPPORTED_CONTEXTS.includes(contextName);
})
.forEach((file) => {
try {
const { name, classes: painlessClasses } = JSON.parse(
readFileSync(join(generatedFolder, file), 'utf8')
);
const filePath = join(autocompleteOutputFolder, `${name}.ts`);
const code = formatData(name, painlessClasses);
writeFileSync(filePath, code, { encoding: 'utf8' });
} catch (err) {
log.fail(err.message);
}
});

log.succeed('Painless autocomplete definitions generated successfully');
});
}
130 changes: 130 additions & 0 deletions packages/kbn-monaco/scripts/utils/clone_es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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');

// TODO: Temporarily pointing to Stu's fork
const esRepo = 'https://github.com/stu-elastic/elasticsearch.git';

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

function cloneAndCheckout(opts, callback) {
const { tag, branch } = opts;
withTag(tag, callback);

/**
* Sets the elasticsearch repository to the given tag.
* If the repository is not present in `esFolder` it will
* clone the repository and the checkout the tag.
* If the repository is already present but it cannot checkout to
* the given tag, it will perform a pull and then try again.
* @param {string} tag
* @param {function} callback
*/
function withTag(tag, callback) {
let fresh = false;
let retry = 0;

if (!pathExist(esFolder)) {
if (!createFolder(esFolder)) {
return;
}
fresh = true;
}

const git = simpleGit(esFolder);

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

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

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

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

/**
* Checks if the given path exists
* @param {string} path
* @returns {boolean} true if exists, false if not
*/
function 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
*/
function createFolder(name) {
try {
mkdirSync(name);
return true;
} catch (err) {
return false;
}
}
}

module.exports = cloneAndCheckout;
158 changes: 158 additions & 0 deletions packages/kbn-monaco/scripts/utils/format_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 getTypes = (data) => {
return data
.filter(
({
static_fields: staticFields,
fields,
static_methods: staticMethods,
methods,
constructors,
}) => {
if (
staticMethods.length === 0 &&
methods.length === 0 &&
staticFields.length === 0 &&
fields.length === 0 &&
constructors.length === 0
) {
return true;
}
}
)
.map((type) => type.name);
};

const parameterIndexToLetterMap = {
0: 'a',
1: 'b',
2: 'c',
3: 'd',
4: 'e',
5: 'f',
};

const getMethodDescription = (methodName, parameters, returnValue) => {
const parameterDescription = parameters.reduce((description, parameterType, index) => {
const newParameterDescription = `${parameterType} ${parameterIndexToLetterMap[index]}`;
const isLastParameter = parameters.length - 1 === index;

description = `${description}${newParameterDescription}${isLastParameter ? '' : ', '}`;

return description;
}, '');

// Final format will look something like this:
// pow(double a, double b): double
return `${methodName}(${parameterDescription}): ${returnValue}`;
};

const getPainlessClassToAutocomplete = (painlessClass) => {
const { staticFields, fields, staticMethods, methods } = painlessClass;

const staticFieldsAutocomplete = staticFields.map(({ name, type }) => {
return {
label: name,
kind: 'property',
documentation: `${name}: ${type}`,
insertText: name,
};
});

const fieldsAutocomplete = fields.map(({ name, type }) => {
return {
label: name,
kind: 'property',
documentation: `${name}: ${type}`,
insertText: name,
};
});

const staticMethodsAutocomplete = staticMethods.map(
({ name, parameters, return: returnValue }) => {
return {
label: name,
kind: 'method',
documentation: getMethodDescription(name, parameters, returnValue),
insertText: name,
};
}
);

const methodsAutocomplete = methods.map(({ name, parameters, return: returnValue }) => {
return {
label: name,
kind: 'method',
documentation: getMethodDescription(name, parameters, returnValue),
insertText: name,
};
});

return [
...staticFieldsAutocomplete,
...staticMethodsAutocomplete,
...methodsAutocomplete,
...fieldsAutocomplete,
];
};

const formatData = (contextName, painlessClasses) => {
const formattedData = painlessClasses.map(
({ name, static_fields: staticFields, fields, static_methods: staticMethods, methods }) => {
const displayName = name.split('.').pop() || name; // TODO ES to add "displayName" field so this won't be necessary
const isType = getTypes(painlessClasses).includes(name);

return {
label: displayName,
kind: isType ? 'type' : 'class',
documentation: isType ? `Primitive: ${displayName}` : `Class: ${displayName}`,
insertText: displayName,
children: getPainlessClassToAutocomplete({ staticFields, fields, staticMethods, methods }),
};
}
);

const stringifiedData = JSON.stringify(formattedData);
return `
/*
* 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.
*/
export const ${contextName} = ${stringifiedData};
`;
};

module.exports = formatData;
Loading

0 comments on commit 7ad74a6

Please sign in to comment.