blob: 95c920d871b9764db9f64e110342a6c50ea899aa [file] [log] [blame]
// Copyright 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert';
import 'dart:io' as io;
import 'package:crypto/crypto.dart';
import 'package:file/file.dart';
import 'command.dart';
import 'package.dart';
import 'provenance.dart';
/// A [Package] whose artifacts are uploaded to "go/cipd".
/// The [upload] method uploads the files in the artifacts directory to a CIPD
/// package. The [name] becomes the packages CIPD path.
/// In [dryRun] mode, the [upload] method merely prints the 1st level contents
/// instead of uploading them.
abstract class CipdPackage extends Package {
final String version;
CipdPackage(String name, Uri source, this.version) : super(name, source);
/// Upload the files in the [artifacts] directory to go/cipd.
Future<void> upload(Directory artifacts, bool provenance) async {
if (await isAlreadyUploaded()) {
throw StateError('Attempted to re-upload existing CIPD version');
final zip = artifacts.parent.childFile('${artifacts.basename}.zip');
final jsonOutput = artifacts.parent.childFile('${artifacts.basename}.json');
final buildArguments = [
];'Running cipd ${buildArguments.join(' ')}...');
final buildResult = await Command('cipd', buildArguments).run(raise: true);
final instanceId =
jsonDecode(await jsonOutput.readAsString())['result']['instance_id'];
final digest = sha256.convert(await zip.readAsBytes()).toString();
final registerArguments = [
];'Produced $name instance $instanceId with digest $digest');'Running cipd ${registerArguments.join(' ')}...');
if (isDryRun) {
final files = await artifacts.list(recursive: true).toList();"Files that would've been included:\n $files");
} else {
final result = await Command('cipd', registerArguments).run(raise: true);
if (provenance) {
provenanceReportCipd(name, digest, instanceId);
}'Finished running cipd create for $name.');
Future<bool> isAlreadyUploaded() async {
final arguments = [
];'Running cipd ${arguments.join(' ')}...');
final result = await Command('cipd', arguments).run();
return result.exitCode == 0;