Skip to content

Commit

Permalink
Merge pull request #3356 from fearthecowboy/v3
Browse files Browse the repository at this point in the history
allow version: to be specified in configuration
  • Loading branch information
fearthecowboy authored Dec 9, 2019
2 parents e79b800 + b94ffc7 commit 1d92bf5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
93 changes: 85 additions & 8 deletions autorest/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ import { isFile, readdir, rmdir, isDirectory } from '@azure-tools/async-io';
import { Exception, LazyPromise } from '@azure-tools/tasks';
import { homedir } from 'os';
import chalk from 'chalk';
import { join } from 'path';
import { join, dirname } from 'path';
import { gt } from 'semver';
import { availableVersions, newCorePackage, oldCorePackage, ensureAutorestHome, extensionManager, installedCores, networkEnabled, pkgVersion, resolvePathForLocalVersion, rootFolder, selectVersion, tryRequire } from './autorest-as-a-service';
import { availableVersions, newCorePackage, oldCorePackage, ensureAutorestHome, extensionManager, installedCores, networkEnabled, pkgVersion, resolvePathForLocalVersion, rootFolder, selectVersion, tryRequire, resolveEntrypoint } from './autorest-as-a-service';
import { color } from './coloring';
import { tmpdir } from 'os';
import * as vm from 'vm';

import { ResolveUri, ReadUri, EnumerateFiles } from '@azure-tools/uri';

// aliases, round one.
if (process.argv.indexOf('--no-upgrade-check') !== -1) {
Expand Down Expand Up @@ -181,6 +184,56 @@ async function clearTempData() {
await Promise.all(all);
}

async function configurationSpecifiedVersion(selectedVersion: any) {
try {
// we can either have a selectedVerison object or a path. See if we can find the AutoRest API
const autorestApi = await resolveEntrypoint(typeof selectedVersion === 'string' ? selectedVersion : await selectedVersion.modulePath, 'main');

// things we need in the sandbox.
const sandbox = {
require, console, rfs: {
EnumerateFileUris: async (folderUri: string): Promise<Array<string>> => {
return EnumerateFiles(folderUri, ['readme.md']);
},
ReadFile: async (uri: string): Promise<string> => {
return ReadUri(uri);
},
WriteFile: async (uri: string, content: string): Promise<void> => {
//return WriteString(uri, content);
}
},
cfgfile: ResolveUri(cwd, args.configFileOrFolder || '.'),
switches: args
};

// *sigh* ... there's a bug in most versions of autorest-core that to use the API you have to
// have the current directory set to the package location. We'll fix this in the future versions.
process.chdir(dirname(autorestApi));
const configSpecifiedVersion = await vm.runInNewContext(`
async function go() {
// load the autorest api library
const r = require('${autorestApi}');
const api = new r.AutoRest(rfs,cfgfile);
// don't let the version from the cmdline affect this!
delete switches.version;
api.AddConfiguration(switches);
// resolve the configuration and return the version if there is one.
return (await api.view).rawConfig.version;
}
go();
`, sandbox);

// if we got back a result, lets return that.
if (configSpecifiedVersion) {
selectedVersion = await selectVersion(configSpecifiedVersion, false);
}
return selectedVersion;
} catch {
return undefined;
}
}

/** Main Entrypoint for AutoRest Bootstrapper */
async function main() {
try {
Expand Down Expand Up @@ -226,12 +279,27 @@ async function main() {
let requestedVersion: string = args.version || (args.latest && 'latest') || (args.preview && 'preview') || 'latest-installed';

// check to see if local installed core is available.
const localVersion = resolvePathForLocalVersion(args.version ? requestedVersion : null);
let localVersion = resolvePathForLocalVersion(args.version ? requestedVersion : null);

// try to use a specified folder or one in node_modules if it is there.
process.chdir(cwd);
if (await tryRequire(localVersion, 'app.js')) {
return;
if (!args.version && localVersion) {
// they never specified a version on the cmdline, but we might have one in configuration
const cfgVersion = (await configurationSpecifiedVersion(localVersion)).version;

// if we got one back, we're going to set the requestedVersion to whatever they asked for.
if (cfgVersion) {
args.version = requestedVersion = cfgVersion;

// and not use the local version
localVersion = undefined;
}
}

// if this is still valid, then we're not overriding it from configuration.
if (localVersion) {
process.chdir(cwd);
if (await tryRequire(localVersion, 'app.js')) {
return;
}
}

// if the resolved local version is actually a file, we'll try that as a package when we get there.
Expand All @@ -253,7 +321,7 @@ async function main() {

// logic to resolve and optionally install a autorest core package.
// will throw if it's not doable.
const selectedVersion = await selectVersion(requestedVersion, force);
let selectedVersion = await selectVersion(requestedVersion, force);

// let's strip the extra stuff from the command line before we require the core module.
const oldArgs = process.argv;
Expand Down Expand Up @@ -282,7 +350,15 @@ async function main() {
if (args.debug) {
console.log(`Starting ${newCorePackage} from ${await selectedVersion.location}`);
}

// if they never said the version on the command line, we should make a check for the config version.
if (!args.version) {
selectedVersion = await configurationSpecifiedVersion(selectedVersion) || selectedVersion;
}

// reset the working folder to the correct place.
process.chdir(cwd);

const result = await tryRequire(await selectedVersion.modulePath, 'app.js');
if (!result) {
throw new Error(`Unable to start AutoRest Core from ${await selectedVersion.modulePath}`);
Expand All @@ -296,3 +372,4 @@ async function main() {
}

main();

6 changes: 3 additions & 3 deletions autorest/autorest-as-a-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ export async function resolveEntrypoint(localPath: string | null, entrypoint: st
if (args.debug) {
console.log(`special case: '${localPath}/${pkg.main}' .`);
}
return localPath;
return localPath.replace(/\\/g, '/');
}
}
const path = `${localPath}/${entrypoint}`;
if (await isFile(path)) {
if (args.debug) {
console.log(`Using Entrypoing: '${localPath}/${entrypoint}' .`);
console.log(`Using Entrypoint: '${localPath}/${entrypoint}' .`);
}
return path;
return path.replace(/\\/g, '/');
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion autorest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@azure-tools/async-io": "~3.0.0",
"@azure-tools/extension": "~3.0.0",
"@azure-tools/tasks": "~3.0.0",
"@azure-tools/uri": "~3.0.0",
"semver": "^5.5.1",
"chalk": "2.3.0",
"cpy-cli": "~2.0.0",
Expand All @@ -68,10 +69,11 @@
"entrypoints": [],
"dependencies": {
"@azure-tools/async-io": "~3.0.0",
"@azure-tools/uri": "~3.0.0",
"@azure-tools/extension": "~3.0.0",
"@azure-tools/tasks": "~3.0.0",
"semver": "^5.5.1",
"chalk": "2.3.0"
}
}
}
}

0 comments on commit 1d92bf5

Please sign in to comment.