Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for updating arm64 versions of homebrew #97

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/src/formula.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2021, 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.

String updateFormula(String channel, String contents, String version,
Map<String, String> hashes) {
// Replace the version identifier. Formulas with stable and pre-release
// versions have multiple identifiers and only the right one should be
// updated.
var versionId = channel == 'stable'
? RegExp(r'version \"\d+\.\d+.\d+\"')
: RegExp(r'version \"\d+\.\d+.\d+\-.+\"');
contents = contents.replaceAll(versionId, 'version "$version"');

// Extract files and hashes that are stored in the formula in this format:
// url "<url base>/<channel>/release/<version>/sdk/<artifact>.zip"
// sha256 "<hash>"
var filesAndHashes = RegExp(
'channels/$channel/release'
r'/(\d[\w\d\-\.]*)/sdk/([\w\d\-\.]+)\"\n(\s+)sha256 \"[\da-f]+\"',
multiLine: true);
return contents.replaceAllMapped(filesAndHashes, (m) {
var currentVersion = m.group(1);
if (currentVersion == version) {
throw ArgumentError(
'Channel $channel is already at version $version in homebrew.');
}
var artifact = m.group(2);
var indent = m.group(3);
return 'channels/$channel/release/$version/sdk/$artifact"\n'
'${indent}sha256 "${hashes[artifact]}"';
});
}
38 changes: 5 additions & 33 deletions lib/src/impl.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright (c) 2019, 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.

part of '../update_homebrew.dart';

const _files = [
'dartsdk-macos-arm64-release.zip',
'dartsdk-macos-x64-release.zip',
'dartsdk-linux-x64-release.zip',
'dartsdk-linux-arm64-release.zip',
Expand All @@ -23,39 +28,6 @@ Future<String> _getHash256(
}
}

Future<void> _updateFormula(String channel, File file, String version,
Map<String, String> hashes) async {
var contents = await file.readAsString();

// Replace the version identifier. Formulas with stable and pre-release
// versions have multiple identifiers and only the right one should be
// updated.
var versionId = channel == 'stable'
? RegExp(r'version \"\d+\.\d+.\d+\"')
: RegExp(r'version \"\d+\.\d+.\d+\-.+\"');
contents = contents.replaceAll(versionId, 'version "$version"');

// Extract files and hashes that are stored in the formula in this format:
// url "<url base>/<channel>/release/<version>/sdk/<artifact>.zip"
// sha256 "<hash>"
var filesAndHashes = RegExp(
'channels/$channel/release'
r'/(\d[\w\d\-\.]*)/sdk/([\w\d\-\.]+)\"\n(\s+)sha256 \"[\da-f]+\"',
multiLine: true);
contents = contents.replaceAllMapped(filesAndHashes, (m) {
var currentVersion = m.group(1);
if (currentVersion == version) {
throw ArgumentError(
'Channel $channel is already at version $version in homebrew.');
}
var artifact = m.group(2);
var indent = m.group(3);
return 'channels/$channel/release/$version/sdk/$artifact"\n'
'${indent}sha256 "${hashes[artifact]}"';
});
await file.writeAsString(contents, flush: true);
}

Future<Map<String, String>> _getHashes(String channel, String version) async {
return <String, String>{
for (var file in _files) file: await _getHash256(channel, version, file)
Expand Down
10 changes: 9 additions & 1 deletion lib/update_homebrew.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2017, 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:async';
import 'dart:convert';
import 'dart:io';
Expand All @@ -7,6 +11,8 @@ import 'package:googleapis/storage/v1.dart' as storage;
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as p;

import 'src/formula.dart';

part 'src/impl.dart';

const githubRepo = 'dart-lang/homebrew-dart';
Expand All @@ -22,8 +28,10 @@ Iterable<String> get supportedChannels => formulaByChannel.keys;
Future<void> writeHomebrewInfo(
String channel, String version, String repository) async {
var formula = File(p.join(repository, formulaByChannel[channel]));
var contents = await formula.readAsString();
var hashes = await _getHashes(channel, version);
await _updateFormula(channel, formula, version, hashes);
var updated = updateFormula(channel, contents, version, hashes);
await formula.writeAsString(updated, flush: true);
}

Future<void> runGit(List<String> args, String repository,
Expand Down
79 changes: 79 additions & 0 deletions test/formula_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2021, 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 'package:test/test.dart';
import 'package:update_homebrew/src/formula.dart';

const _devFormula = '''
head do
version "2.15.0-65.0.dev"
if OS.mac? && Hardware::CPU.intel?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-macos-x64-release.zip"
sha256 "9c71429a806dd2ac7968542771764dd5d1b7c71fd03851c6870eb5c3f687fb1b"
elsif OS.mac? && Hardware::CPU.arm?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-macos-arm64-release.zip"
sha256 "754521d866bf2e878d2b4a33be96ece27a6381aa4b73a397d0b349bdd87b4eaa"
elsif OS.linux? && Hardware::CPU.intel?
if Hardware::CPU.is_64_bit?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-linux-x64-release.zip"
sha256 "af6537fe9b248f704420e05a88eedb0b2b0db9c8f3e996da90aabd2037543286"
else
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-linux-ia32-release.zip"
sha256 "a19f58988af2098a8f7ae4996fa25f95a86d43188fd0a796cc3740a5eeb2a855"
end
elsif OS.linux? && Hardware::CPU.arm?
if Hardware::CPU.is_64_bit?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-linux-arm64-release.zip"
sha256 "e9f3bc610cea89c972a85767d86ab424cce6cab46e3da4065cfdd1aadfc43bef"
else
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.15.0-65.0.dev/sdk/dartsdk-linux-arm-release.zip"
sha256 "71da750dd2e78970b69c3b2cb388527d7a1ccd70ea8e8cacb5bba191841effa0"
end
end
end
''';

const _devFormulaExpected = '''
head do
version "2.16.0-76.0.dev"
if OS.mac? && Hardware::CPU.intel?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-macos-x64-release.zip"
sha256 "bbb"
elsif OS.mac? && Hardware::CPU.arm?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-macos-arm64-release.zip"
sha256 "aaa"
elsif OS.linux? && Hardware::CPU.intel?
if Hardware::CPU.is_64_bit?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-linux-x64-release.zip"
sha256 "ccc"
else
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-linux-ia32-release.zip"
sha256 "eee"
end
elsif OS.linux? && Hardware::CPU.arm?
if Hardware::CPU.is_64_bit?
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-linux-arm64-release.zip"
sha256 "ddd"
else
url "https://storage.googleapis.com/dart-archive/channels/dev/release/2.16.0-76.0.dev/sdk/dartsdk-linux-arm-release.zip"
sha256 "fff"
end
end
end
''';

main() {
test('update dev formula', () {
var hashes = {
'dartsdk-macos-arm64-release.zip': 'aaa',
'dartsdk-macos-x64-release.zip': 'bbb',
'dartsdk-linux-x64-release.zip': 'ccc',
'dartsdk-linux-arm64-release.zip': 'ddd',
'dartsdk-linux-ia32-release.zip': 'eee',
'dartsdk-linux-arm-release.zip': 'fff',
};
var updated = updateFormula('dev', _devFormula, '2.16.0-76.0.dev', hashes);
expect(updated, _devFormulaExpected);
});
}
4 changes: 4 additions & 0 deletions test/update_homebrew_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2021, 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 'package:test/test.dart';

main() {
Expand Down