Skip to content

Commit

Permalink
feat(cli): convert flutter (#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Oct 12, 2023
1 parent e83e6df commit 478f06d
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 51 deletions.
19 changes: 19 additions & 0 deletions docs/custom-registries.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.413/dotnet-sdk-6.0.413-linux-x64.
https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.413/dotnet-sdk-6.0.413-linux-arm64.tar.gz
```

## flutter

Flutter releases are downloaded from:

- `https://github.com/containerbase/flutter-prebuild/releases/download`
- `https://github.com/flutter/flutter.git`

The first url is preferred and the second is used as fallback for older versions.

Samples:

```txt
https://github.com/containerbase/flutter-prebuild/releases/download/3.13.7/flutter-3.13.7-x86-64.tar.xz
https://github.com/containerbase/flutter-prebuild/releases/download/3.13.7/flutter-3.13.7-x86-64.tar.xz.sha512
https://github.com/containerbase/flutter-prebuild/releases/download/3.13.7/flutter-3.13.7-aarch64.tar.xz
https://github.com/containerbase/flutter-prebuild/releases/download/3.13.7/flutter-3.13.7-aarch64.tar.xz.sha512
https://github.com/flutter/flutter.git
```

## flux

Flux releases are downloaded from:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"pretty-ms": "8.0.0",
"reflect-metadata": "0.1.13",
"semver": "7.5.4",
"simple-git": "3.20.0",
"tar": "6.2.0",
"typanion": "3.14.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InstallBunService } from '../tools/bun';
import { InstallDartService } from '../tools/dart';
import { InstallDockerService } from '../tools/docker';
import { InstallDotnetService } from '../tools/dotnet';
import { InstallFlutterService } from '../tools/flutter';
import { InstallFluxService } from '../tools/flux';
import { InstallMavenService } from '../tools/java/maven';
import { InstallNodeService } from '../tools/node';
Expand Down Expand Up @@ -47,6 +48,7 @@ function prepareContainer(): Container {
container.bind(INSTALL_TOOL_TOKEN).to(InstallDartService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallDockerService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallDotnetService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallFlutterService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallFluxService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallLernaService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallMavenService);
Expand Down
2 changes: 2 additions & 0 deletions src/cli/prepare-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { rootContainer } from '../services';
import { PrepareDartService } from '../tools/dart';
import { PrepareDockerService } from '../tools/docker';
import { PrepareDotnetService } from '../tools/dotnet';
import { PrepareFlutterService } from '../tools/flutter';
import { logger } from '../utils';
import { PrepareLegacyToolsService } from './prepare-legacy-tools.service';
import { PREPARE_TOOL_TOKEN, PrepareToolService } from './prepare-tool.service';
Expand All @@ -20,6 +21,7 @@ function prepareContainer(): Container {
container.bind(PREPARE_TOOL_TOKEN).to(PrepareDartService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareDotnetService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareDockerService);
container.bind(PREPARE_TOOL_TOKEN).to(PrepareFlutterService);

logger.trace('preparing container done');
return container;
Expand Down
140 changes: 140 additions & 0 deletions src/cli/tools/flutter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import fs from 'node:fs/promises';
import { join } from 'node:path';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { CleanOptions, ResetMode, simpleGit } from 'simple-git';
import { InstallToolBaseService } from '../install-tool/install-tool-base.service';
import { PrepareToolBaseService } from '../prepare-tool/prepare-tool-base.service';
import {
CompressionService,
EnvService,
HttpService,
PathService,
} from '../services';
import { logger } from '../utils';

@injectable()
export class PrepareFlutterService extends PrepareToolBaseService {
readonly name = 'flutter';

constructor(@inject(EnvService) private readonly envSvc: EnvService) {
super();
}

override async execute(): Promise<void> {
const flutter = join(this.envSvc.userHome, '.flutter');
await fs.writeFile(flutter, '{ "firstRun": false, "enabled": false }');
await fs.chown(flutter, this.envSvc.userId, 0);
await fs.chmod(flutter, 0o664);

await fs.writeFile(
join(this.envSvc.rootDir, '/root/.flutter'),
'{ "firstRun": false, "enabled": false }',
);
}
}

@injectable()
export class InstallFlutterService extends InstallToolBaseService {
readonly name = 'flutter';

private get ghArch(): string {
switch (this.envSvc.arch) {
case 'arm64':
return 'aarch64';
case 'amd64':
return 'x86_64';
}
}

constructor(
@inject(EnvService) envSvc: EnvService,
@inject(PathService) pathSvc: PathService,
@inject(HttpService) private http: HttpService,
@inject(CompressionService) private compress: CompressionService,
) {
super(pathSvc, envSvc);
}

override async install(version: string): Promise<void> {
const name = this.name;
const filename = `${name}-${version}-${this.ghArch}.tar.xz`;
const url = `https://github.com/containerbase/${name}-prebuild/releases/download/${version}/${filename}`;
const checksumFileUrl = `${url}.sha512`;
const isOnGithub = await this.http.exists(checksumFileUrl);

if (isOnGithub) {
logger.info(`using github prebuild`);
const checksumFile = await this.http.download({ url: checksumFileUrl });
const expectedChecksum = (
await fs.readFile(checksumFile, 'utf-8')
).trim();
const file = await this.http.download({
url,
checksumType: 'sha512',
expectedChecksum,
});
await this.compress.extract({ file, cwd: await this.getToolPath() });
} else {
logger.info(`using github source repo`);
await this.getToolPath();
const path = await this.pathSvc.createVersionedToolPath(
this.name,
version,
);
const git = simpleGit({ baseDir: path });
await git.clone('https://github.com/flutter/flutter.git', '.', {
'--filter': 'blob:none',
'--branch': 'stable',
});

await git.reset(ResetMode.HARD, [version]);

await git.addConfig(
'safe.directory',
path,
true,
this.envSvc.isRoot ? 'system' : 'global',
);

// init flutter
await execa(`./bin/flutter`, ['--version'], { cwd: path });
await execa(`./bin/flutter`, ['pub', 'get', '--help'], { cwd: path });

// cleanup
await git.clean(CleanOptions.FORCE + CleanOptions.IGNORED_INCLUDED, [
'--',
'**/.packages',
]);
await git.clean(CleanOptions.FORCE + CleanOptions.IGNORED_INCLUDED, [
'--',
'**/.dart_tool/',
]);
await fs.rm(join(path, '.pub-cache/git'), {
recursive: true,
force: true,
});

// fix permrmissions
await execa('chmod', ['-R', 'g+w', path]);
}
}

override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');
await this.shellwrapper({ srcDir: src });
}

override async test(_version: string): Promise<void> {
await execa('flutter', ['--version'], {
stdio: ['inherit', 'inherit', 1],
});
}

private async getToolPath(): Promise<string> {
return (
(await this.pathSvc.findToolPath(this.name)) ??
(await this.pathSvc.createToolPath(this.name))
);
}
}
46 changes: 0 additions & 46 deletions src/usr/local/containerbase/tools/v2/flutter.sh

This file was deleted.

10 changes: 5 additions & 5 deletions test/flutter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ RUN prepare-tool flutter
#--------------------------------------
FROM build as testa

# renovate: datasource=flutter-version
RUN install-tool flutter 1.22.6
# EOL, use source, not prebuild
RUN install-tool flutter 1.22.5

USER 1000

RUN set -ex; \
ls -la /opt/containerbase/tools/flutter/1.22.6 $USER_HOME; \
ls -la /opt/containerbase/tools/flutter/1.22.5 $USER_HOME; \
cd a; \
flutter pub upgrade;

Expand All @@ -55,7 +55,7 @@ FROM build as testb

USER 1000

# renovate: datasource=flutter-version
# EOL
RUN install-tool flutter 1.22.6

RUN set -ex; \
Expand All @@ -74,7 +74,7 @@ FROM build as testc

USER 1000

# renovate: datasource=flutter-version
# EOL
RUN install-tool flutter 2.10.5

RUN set -ex; \
Expand Down
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ __metadata:
languageName: node
linkType: hard

"@kwsites/file-exists@npm:^1.1.1":
version: 1.1.1
resolution: "@kwsites/file-exists@npm:1.1.1"
dependencies:
debug: ^4.1.1
checksum: 4ff945de7293285133aeae759caddc71e73c4a44a12fac710fdd4f574cce2671a3f89d8165fdb03d383cfc97f3f96f677d8de3c95133da3d0e12a123a23109fe
languageName: node
linkType: hard

"@kwsites/promise-deferred@npm:^1.1.1":
version: 1.1.1
resolution: "@kwsites/promise-deferred@npm:1.1.1"
checksum: 07455477a0123d9a38afb503739eeff2c5424afa8d3dbdcc7f9502f13604488a4b1d9742fc7288832a52a6422cf1e1c0a1d51f69a39052f14d27c9a0420b6629
languageName: node
linkType: hard

"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"
Expand Down Expand Up @@ -2751,6 +2767,7 @@ __metadata:
semantic-release: 22.0.5
semver: 7.5.4
shelljs: 0.8.5
simple-git: 3.20.0
tar: 6.2.0
tsx: 3.13.0
typanion: 3.14.0
Expand Down Expand Up @@ -7992,6 +8009,17 @@ __metadata:
languageName: node
linkType: hard

"simple-git@npm:3.20.0":
version: 3.20.0
resolution: "simple-git@npm:3.20.0"
dependencies:
"@kwsites/file-exists": ^1.1.1
"@kwsites/promise-deferred": ^1.1.1
debug: ^4.3.4
checksum: 56b50c574a950a8ddc87033083e073216708ccf6aaa3c904da05a5b3e52502287049dde49df1df42d5ec072c1050ef8219733e6cfebf71885081917ffcdbb54c
languageName: node
linkType: hard

"sirv@npm:^2.0.3":
version: 2.0.3
resolution: "sirv@npm:2.0.3"
Expand Down

0 comments on commit 478f06d

Please sign in to comment.