Skip to content

Commit

Permalink
postinstall: GoDependency: refactor
Browse files Browse the repository at this point in the history
This should make it easier to handle custom paths / subcommands.

For example, we may want:

new goUtils.GoDependency('networking/cmd/vm', { outputPath: 'staging/vm-switch'})

Signed-off-by: Mark Yen <[email protected]>
  • Loading branch information
mook-as committed Jul 8, 2024
1 parent fa44f72 commit 0c3e88b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
76 changes: 42 additions & 34 deletions scripts/dependencies/go-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@ import path from 'path';
import { AlpineLimaISOVersion, Dependency, DownloadContext } from 'scripts/lib/dependencies';
import { simpleSpawn } from 'scripts/simple_process';

type GoDependencyOptions = {
/**
* The output file name, relative to the platform-specific resources directory.
* If this does not contain any directory separators ('/'), it is assumed to
* be a directory name (defaults to `bin`) and the leaf name of the source
* path is appended as the executable name.
*/
outputPath: string;
/**
* Additional environment for the go compiler; e.g. for GOARCH overrides.
*/
env?: NodeJS.ProcessEnv;
};

/**
* GoDependency represents a golang binary that is built from the local source
* code.
*/
export class GoDependency implements Dependency {
constructor(path: string | string[], dir: 'bin' | 'internal' | 'host' | 'staging' = 'bin') {
this.name = Array.isArray(path) ? path[path.length - 1] : path;
this.path = Array.isArray(path) ? path : [path];
this.dir = dir;
/**
* Construct a new GoDependency.
* @param sourcePath The path to be compiled, relative to .../src/go
* @param options Additional configuration option; if a string is given, this
* is the outputPath option, defaulting to `bin`.
*/
constructor(sourcePath: string, options: string | GoDependencyOptions = 'bin') {
this.sourcePath = sourcePath;
this.options = typeof options === 'string' ? { outputPath: options } : options;
}

get name(): string {
if (this.options.outputPath.includes('/')) {
return path.basename(this.options.outputPath);
}

return path.basename(this.sourcePath);
}

name: string;
path: string[];
dir: 'bin' | 'internal' | 'host' | 'staging';
sourcePath: string;
options: GoDependencyOptions;

async download(context: DownloadContext): Promise<void> {
// Rather than actually downloading anything, this builds the source code.
const sourceDir = path.join(process.cwd(), 'src', 'go', ...this.path);
const sourceDir = path.join(process.cwd(), 'src', 'go', this.sourcePath);
const outFile = this.outFile(context);

console.log(`Building go utility \x1B[1;33;40m${ this.name }\x1B[0m from ${ sourceDir } to ${ outFile }...`);
Expand All @@ -35,17 +61,19 @@ export class GoDependency implements Dependency {
...process.env,
GOOS: context.goPlatform,
GOARCH: context.isM1 ? 'arm64' : 'amd64',
...this.options.env ?? {},
};
}

outFile(context: DownloadContext): string {
const target = context.platform === 'win32' ? `${ this.name }.exe` : this.name;
const suffix = context.platform === 'win32' ? '.exe' : '';
let outputPath = `${ this.options.outputPath }${ suffix }`;

if (this.dir === 'host') {
return path.join(context.resourcesDir, this.dir, target);
if (!this.options.outputPath.includes('/')) {
outputPath = `${ this.options.outputPath }/${ this.name }${ suffix }`;
}

return path.join(context.resourcesDir, context.platform, this.dir, target);
return path.join(context.resourcesDir, context.platform, outputPath);
}

getAvailableVersions(includePrerelease?: boolean | undefined): Promise<string[]> {
Expand Down Expand Up @@ -83,34 +111,14 @@ export class RDCtl extends GoDependency {
}
}

export class ExtensionProxy extends GoDependency {
constructor() {
super('extension-proxy', 'staging');
}

override environment(context: DownloadContext): NodeJS.ProcessEnv {
return {
...super.environment(context),
CGO_ENABLED: '0',
};
}
}

export class WSLHelper extends GoDependency {
constructor() {
super('wsl-helper', 'internal');
super('wsl-helper', { outputPath: 'internal', env: { CGO_ENABLED: '0' } });
}

dependencies(context: DownloadContext): string[] {
return ['mobyOpenAPISpec:win32'];
}

override environment(context: DownloadContext) {
return {
...super.environment(context),
CGO_ENABLED: '0',
};
}
}

export class NerdctlStub extends GoDependency {
Expand All @@ -124,6 +132,6 @@ export class NerdctlStub extends GoDependency {
// easier to handle permissions for Linux-in-WSL.
const leafName = context.platform === 'win32' ? 'nerdctl.exe' : 'nerdctl-stub';

return path.join(context.resourcesDir, context.platform, this.dir, leafName);
return path.join(context.resourcesDir, context.platform, 'bin', leafName);
}
}
2 changes: 1 addition & 1 deletion scripts/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const vmDependencies = [
new tools.WasmShims(),
new tools.CertManager(),
new tools.SpinOperator(),
new goUtils.ExtensionProxy(),
new goUtils.GoDependency('extension-proxy', { outputPath: 'staging', env: { CGO_ENABLED: '0' } }),
new ExtensionProxyImage(),
];

Expand Down

0 comments on commit 0c3e88b

Please sign in to comment.