-
Notifications
You must be signed in to change notification settings - Fork 34
Expose a "glean" command #105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2f605f
Expose a "glean" command
Dexterp37 49a64e5
Make sure to build CLI before publishing
Dexterp37 50e9338
Use the 'glean' command in the JS web-extension sample
Dexterp37 0b1d675
Use the 'glean' command in the typescript web-ext sample
Dexterp37 2d8ccd8
Add a changelog entry
Dexterp37 1c0b020
Adding Python3 as a requirement.
Dexterp37 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| //require('ts-node').register(); | ||
| //require('./glean.ts'); | ||
|
|
||
| import * as exec from "child_process"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import { argv, platform } from "process"; | ||
| import { promisify } from "util"; | ||
|
|
||
| // Define an async/await version of "exec". | ||
| const execAsync = promisify(exec.exec); | ||
|
|
||
| // The name of the directory which will contain the Python virtual environment | ||
| // used to run the glean-parser. | ||
| const VIRTUAL_ENVIRONMENT_DIR = ".venv"; | ||
|
|
||
| // The version of glean_parser to install from PyPI. | ||
| const GLEAN_PARSER_VERSION = "2.5.0"; | ||
|
|
||
| // This script runs a given Python module as a "main" module, like | ||
| // `python -m module`. However, it first checks that the installed | ||
| // package is at the desired version, and if not, upgrades it using `pip`. | ||
| // | ||
| // ** IMPORTANT** | ||
| // Keep this script in sync with the one in the Glean SDK (Gradle Plugin). | ||
| // | ||
| // Note: Groovy doesn't support embedded " in multi-line strings, so care | ||
| // should be taken to use ' everywhere in this code snippet. | ||
| const PYTHON_SCRIPT = ` | ||
| import importlib | ||
| import subprocess | ||
| import sys | ||
| offline = sys.argv[1] == 'offline' | ||
| module_name = sys.argv[2] | ||
| expected_version = sys.argv[3] | ||
| try: | ||
| module = importlib.import_module(module_name) | ||
| except ImportError: | ||
| found_version = None | ||
| else: | ||
| found_version = getattr(module, '__version__') | ||
| if found_version != expected_version: | ||
| if not offline: | ||
| subprocess.check_call([ | ||
| sys.executable, | ||
| '-m', | ||
| 'pip', | ||
| 'install', | ||
| '--upgrade', | ||
| f'{module_name}=={expected_version}' | ||
| ]) | ||
| else: | ||
| print(f'Using Python environment at {sys.executable},') | ||
| print(f'expected glean_parser version {expected_version}, found {found_version}.') | ||
| sys.exit(1) | ||
| try: | ||
| subprocess.check_call([ | ||
| sys.executable, | ||
| '-m', | ||
| module_name | ||
| ] + sys.argv[4:]) | ||
| except: | ||
| # We don't need to show a traceback in this helper script. | ||
| # Only the output of the subprocess is interesting. | ||
| sys.exit(1) | ||
| `; | ||
|
|
||
| /** | ||
| * Gets the name of the Python binary, based on the host OS. | ||
| * | ||
| * @returns the name of the Python executable. | ||
| */ | ||
| function getSystemPythonBinName(): string { | ||
| return (platform === "win32") ? "python.exe" : "python3"; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the full path to the directory containing the python | ||
| * binaries in the virtual environment. | ||
| * | ||
| * Note that this directory changes depending on the host OS. | ||
| * | ||
| * @param venvRoot the root path of the virtual environment. | ||
| * | ||
| * @returns the full path to the directory containing the python | ||
| * binaries in the virtual environment. | ||
| */ | ||
| function getPythonVenvBinariesPath(venvRoot: string): string { | ||
| if (platform === "win32") { | ||
| return path.join(venvRoot, "Scripts"); | ||
| } | ||
|
|
||
| return path.join(venvRoot, "bin"); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a Python virtual environment is available. | ||
| * | ||
| * @param venvPath the Python virtual environment directory. | ||
| * | ||
| * @returns `true` if the Python virtual environment exists and | ||
| * is accessible, `false` otherwise. | ||
| */ | ||
| async function checkPythonVenvExists(venvPath: string): Promise<boolean> { | ||
| console.log(`Checking for a Glean virtual environment at ${venvPath}`); | ||
|
|
||
| const venvPython = | ||
| path.join(getPythonVenvBinariesPath(venvPath), getSystemPythonBinName()); | ||
|
|
||
| const access = promisify(fs.access); | ||
|
|
||
| try { | ||
| await access(venvPath, fs.constants.F_OK); | ||
| await access(venvPython, fs.constants.F_OK); | ||
|
|
||
| return true; | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Uses the system's Python interpreter to create a Python3 virtual environment. | ||
| * | ||
| * @param venvPath the directory in which to create the virtual environment. | ||
| * | ||
| * @returns `true` if the environment was correctly created, `false` otherwise. | ||
| */ | ||
| async function createPythonVenv(venvPath: string): Promise<boolean> { | ||
| console.log(`Creating a Glean virtual environment at ${venvPath}`); | ||
|
|
||
| const pipFilename = (platform === "win32") ? "pip3.exe" : "pip3"; | ||
| const venvPip = | ||
| path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), pipFilename); | ||
|
|
||
| const pipCmd = `${venvPip} install wheel`; | ||
| const venvCmd = `${getSystemPythonBinName()} -m venv ${VIRTUAL_ENVIRONMENT_DIR}`; | ||
|
|
||
| for (const cmd of [venvCmd, pipCmd]) { | ||
| try { | ||
| await execAsync(cmd); | ||
| } catch (e) { | ||
| console.error(e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a virtual environment for running the glean_parser exists, | ||
| * otherwise it creates it. | ||
| * | ||
| * @param projectRoot the project's root directory. | ||
| */ | ||
| async function setup(projectRoot: string) { | ||
| const venvRoot = path.join(projectRoot, VIRTUAL_ENVIRONMENT_DIR); | ||
|
|
||
| const venvExists = await checkPythonVenvExists(venvRoot); | ||
| if (venvExists) { | ||
| console.log(`Using Glean virtual environment at ${venvRoot}`); | ||
| } else if (!await createPythonVenv(venvRoot)){ | ||
| console.error(`Failed to createa a Glean virtual environment at ${venvRoot}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs the glean_parser with the provided options. | ||
| * | ||
| * @param projectRoot the project's root directory. | ||
| * @param parserArgs the list of arguments passed to this command. | ||
| */ | ||
| async function runGlean(projectRoot: string, parserArgs: string[]) { | ||
| const venvRoot = path.join(projectRoot, VIRTUAL_ENVIRONMENT_DIR); | ||
| const pythonBin = path.join(getPythonVenvBinariesPath(venvRoot), getSystemPythonBinName()); | ||
| const cmd = `${pythonBin} -c "${PYTHON_SCRIPT}" online glean_parser ${GLEAN_PARSER_VERSION} ${parserArgs.join(" ")}`; | ||
| try { | ||
| await execAsync(cmd); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs the command. | ||
| * | ||
| * @param args the arguments passed to this process. | ||
| */ | ||
| async function run(args: string[]) { | ||
| if (args.length < 3) { | ||
| throw new Error("Not enough arguments. Please refer to https://mozilla.github.io/glean_parser/readme.html"); | ||
| } | ||
|
|
||
| const projectRoot = process.cwd(); | ||
| try { | ||
| await setup(projectRoot); | ||
| } catch (err) { | ||
| console.error("Failed to setup the Glean build environment", err); | ||
| } | ||
|
|
||
| await runGlean(projectRoot, args.slice(2)); | ||
| } | ||
|
|
||
| run(argv).catch(e => { | ||
| console.error("There was an error running Glean", e); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "extends": "./base.json", | ||
| "include": [ | ||
| "../src/cli.ts" | ||
| ], | ||
| "compilerOptions": { | ||
| "outDir": "../dist/cli" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.