Skip to content

Commit 139fb39

Browse files
committed
buildx: split install from download and build methods
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 67957d8 commit 139fb39

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

__tests__/buildx/install.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ describe('download', () => {
4343
])(
4444
'acquires %p of buildx (standalone: %p)', async (version, standalone) => {
4545
const install = new Install({standalone: standalone});
46-
const buildxBin = await install.download(version, tmpDir);
46+
const toolPath = await install.download(version);
47+
expect(fs.existsSync(toolPath)).toBe(true);
48+
let buildxBin: string;
49+
if (standalone) {
50+
buildxBin = await install.installStandalone(toolPath, tmpDir);
51+
} else {
52+
buildxBin = await install.installPlugin(toolPath, tmpDir);
53+
}
4754
expect(fs.existsSync(buildxBin)).toBe(true);
4855
},
4956
100000
@@ -65,7 +72,7 @@ describe('download', () => {
6572
jest.spyOn(osm, 'platform').mockImplementation(() => os);
6673
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
6774
const install = new Install();
68-
const buildxBin = await install.download('latest', tmpDir);
75+
const buildxBin = await install.download('latest');
6976
expect(fs.existsSync(buildxBin)).toBe(true);
7077
},
7178
100000
@@ -82,14 +89,18 @@ describe('build', () => {
8289
// eslint-disable-next-line jest/no-disabled-tests
8390
it.skip('builds refs/pull/648/head', async () => {
8491
const install = new Install();
85-
const buildxBin = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir);
92+
const toolPath = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head');
93+
expect(fs.existsSync(toolPath)).toBe(true);
94+
const buildxBin = await install.installStandalone(toolPath, tmpDir);
8695
expect(fs.existsSync(buildxBin)).toBe(true);
8796
}, 100000);
8897

8998
// eslint-disable-next-line jest/no-disabled-tests
9099
it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => {
91100
const install = new Install();
92-
const buildxBin = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir);
101+
const toolPath = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14');
102+
expect(fs.existsSync(toolPath)).toBe(true);
103+
const buildxBin = await install.installPlugin(toolPath, tmpDir);
93104
expect(fs.existsSync(buildxBin)).toBe(true);
94105
}, 100000);
95106
});

src/buildx/install.ts

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Install {
4646
this._standalone = opts?.standalone;
4747
}
4848

49-
public async download(version: string, dest?: string): Promise<string> {
49+
public async download(version: string): Promise<string> {
5050
const release: GitHubRelease = await Install.getRelease(version);
5151
const fversion = release.tag_name.replace(/^v+|v+$/g, '');
5252
core.debug(`Install.download version: ${fversion}`);
@@ -62,15 +62,10 @@ export class Install {
6262
}
6363
core.debug(`Install.download toolPath: ${toolPath}`);
6464

65-
dest = dest || ((await this.isStandalone()) ? this.context.tmpDir() : Docker.configDir);
66-
core.debug(`Install.download dest: ${dest}`);
67-
if (await this.isStandalone()) {
68-
return this.setStandalone(toolPath, dest);
69-
}
70-
return this.setPlugin(toolPath, dest);
65+
return toolPath;
7166
}
7267

73-
public async build(gitContext: string, dest?: string): Promise<string> {
68+
public async build(gitContext: string): Promise<string> {
7469
// eslint-disable-next-line prefer-const
7570
let [repo, ref] = gitContext.split('#');
7671
if (ref.length == 0) {
@@ -103,12 +98,38 @@ export class Install {
10398
});
10499
}
105100

101+
return toolPath;
102+
}
103+
104+
public async installStandalone(toolPath: string, dest?: string): Promise<string> {
105+
dest = dest || this.context.tmpDir();
106+
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
107+
const binDir = path.join(dest, 'bin');
108+
if (!fs.existsSync(binDir)) {
109+
fs.mkdirSync(binDir, {recursive: true});
110+
}
111+
const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx';
112+
const buildxPath: string = path.join(binDir, filename);
113+
fs.copyFileSync(toolBinPath, buildxPath);
114+
fs.chmodSync(buildxPath, '0755');
115+
core.addPath(binDir);
116+
core.debug(`Install.installStandalone buildxPath: ${buildxPath}`);
117+
return buildxPath;
118+
}
119+
120+
public async installPlugin(toolPath: string, dest?: string): Promise<string> {
106121
dest = dest || Docker.configDir;
107-
core.debug(`Install.build dest: ${dest}`);
108-
if (await this.isStandalone()) {
109-
return this.setStandalone(toolPath, dest);
122+
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
123+
const pluginsDir: string = path.join(dest, 'cli-plugins');
124+
if (!fs.existsSync(pluginsDir)) {
125+
fs.mkdirSync(pluginsDir, {recursive: true});
110126
}
111-
return this.setPlugin(toolPath, dest);
127+
const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
128+
const pluginPath: string = path.join(pluginsDir, filename);
129+
fs.copyFileSync(toolBinPath, pluginPath);
130+
fs.chmodSync(pluginPath, '0755');
131+
core.debug(`Install.installPlugin pluginPath: ${pluginPath}`);
132+
return pluginPath;
112133
}
113134

114135
private async buildCommand(gitContext: string, outputDir: string): Promise<{args: Array<string>; command: string}> {
@@ -148,35 +169,6 @@ export class Install {
148169
return standalone;
149170
}
150171

151-
private async setStandalone(toolPath: string, dest: string): Promise<string> {
152-
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
153-
const binDir = path.join(dest, 'bin');
154-
if (!fs.existsSync(binDir)) {
155-
fs.mkdirSync(binDir, {recursive: true});
156-
}
157-
const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx';
158-
const buildxPath: string = path.join(binDir, filename);
159-
fs.copyFileSync(toolBinPath, buildxPath);
160-
fs.chmodSync(buildxPath, '0755');
161-
core.addPath(binDir);
162-
core.debug(`Install.setStandalone buildxPath: ${buildxPath}`);
163-
return buildxPath;
164-
}
165-
166-
private async setPlugin(toolPath: string, dest: string): Promise<string> {
167-
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
168-
const pluginsDir: string = path.join(dest, 'cli-plugins');
169-
if (!fs.existsSync(pluginsDir)) {
170-
fs.mkdirSync(pluginsDir, {recursive: true});
171-
}
172-
const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
173-
const pluginPath: string = path.join(pluginsDir, filename);
174-
fs.copyFileSync(toolBinPath, pluginPath);
175-
fs.chmodSync(pluginPath, '0755');
176-
core.debug(`Install.setPlugin pluginPath: ${pluginPath}`);
177-
return pluginPath;
178-
}
179-
180172
private async fetchBinary(version: string): Promise<string> {
181173
const targetFile: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
182174
const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, this.filename(version));

0 commit comments

Comments
 (0)