Chromium Code Reviews| Index: tools/apps/update_homebrew/bin/update_homebrew.dart |
| diff --git a/tools/apps/update_homebrew/bin/update_homebrew.dart b/tools/apps/update_homebrew/bin/update_homebrew.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c433c2070f147b2bc11745ea7541ab1dea62dfa8 |
| --- /dev/null |
| +++ b/tools/apps/update_homebrew/bin/update_homebrew.dart |
| @@ -0,0 +1,136 @@ |
| +// Copyright (c) 2014, 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. |
| + |
| +library update_repo; |
| + |
| +import 'dart:io'; |
| +import 'dart:convert'; |
| +import 'dart:async'; |
| +import 'package:http/http.dart' as http; |
| +import 'package:args/args.dart'; |
| +import 'package:googleapis/storage/v1.dart' as storage; |
| +import 'package:googleapis/common/common.dart' show DownloadOptions, Media; |
| + |
| +String repository; // The path to the temporary git checkout of dart-homebrew. |
| +Map gitEnvironment; // Pass a wrapper script for SSH to git in the environment. |
| + |
| +void check(String message, bool condition) { |
| + if (!condition) { |
| + print(message); |
| + exit(1); |
| + } |
| +} |
| + |
| +int firstIndexWhere(List l, bool test(e)) { |
| + for (int i = 0; i < l.length; ++i) { |
| + if (test(l[i])) return i; |
| + } |
| + return -1; |
| +} |
| + |
| +Future<String> getHash256(String channel, int revision, String download) { |
| + var client = new http.Client(); |
| + var api = new storage.StorageApi(client); |
| + return |
| + api.objects.get('dart-archive', |
| + 'channels/$channel/release/$revision/$download.sha256sum', |
| + downloadOptions: DownloadOptions.FullMedia) |
| + .then((Media media) => ASCII.decodeStream(media.stream)) |
| + .whenComplete(client.close); |
| +} |
| + |
| +Future<String> getVersion(String channel, int revision) { |
| + var client = new http.Client(); |
| + var api = new storage.StorageApi(client); |
| + return api.objects.get('dart-archive', |
| + 'channels/$channel/release/$revision/VERSION', |
| + downloadOptions: DownloadOptions.FullMedia) |
| + .then((Media media) => JSON.fuse(ASCII).decoder.bind(media.stream).first) |
| + .then((versionObject) => versionObject['version']) |
| + .whenComplete(client.close); |
| +} |
| + |
| +Future modifyHomebrewFormulas(String channel, int revision) { |
|
Søren Gjesse
2014/09/26 08:55:00
Wouldn't it be easier to have the full Ruby source
|
| + // Read script to lines, from file |
| + Future<List<String>> getLines(String filename) { |
| + final file = new File('$repository/$filename'); |
| + final lines = ASCII.decoder.fuse(new LineSplitter()).bind(file.openRead()); |
| + return lines.toList(); |
| + } |
| + |
| + // Modify url, sha256, version lines |
| + Future<List<String>> modifyLines(List<String> lines, String download) { |
| + final int pos = firstIndexWhere(lines, (line) => line.contains( |
| + "url 'https://storage.googleapis.com/dart-archive/channels/$channel") |
| + && line.contains(download)); |
| + check("URL line not found", pos >= 0); |
| + check("Version line not found", lines[pos - 1].contains("version '")); |
| + check("sha256 line not found", lines[pos + 1].contains("sha256 '")); |
| + |
| + final indent = new RegExp('^\\s*').stringMatch(lines[pos]); |
| + lines[pos] = |
| + "${indent}url 'https://storage.googleapis.com/dart-archive/channels/" |
| + "$channel/release/$revision/$download'"; |
| + return getHash256(channel, revision, download) |
| + .then((hashLine) { |
| + final hash = new RegExp('[0-9a-fA-F]*').stringMatch(hashLine); |
| + lines[pos + 1] = "${indent}sha256 '$hash'"; |
| + }) |
| + .then((_) => getVersion(channel, revision)) |
| + .then((version) { |
| + lines[pos - 1] = "${indent}version '$version'"; |
| + return lines; |
| + }); |
| + } |
| + |
| + Future saveLines(List<String> lines, String filename) { |
| + final file = new File('$repository/$filename'); |
| + return (file.openWrite()..writeAll(lines, '\n')..write('\n')).close(); |
| + } |
| + |
| + // Write lines to file |
| + return getLines('dartium.rb') |
| + .then((lines) => |
| + modifyLines(lines, 'dartium/dartium-macos-ia32-release.zip')) |
| + .then((lines) => saveLines(lines, 'dartium.rb')) |
| + .then((_) => getLines('dart.rb')) |
| + .then((lines) => modifyLines(lines, 'sdk/dartsdk-macos-x64-release.zip')) |
| + .then((lines) => modifyLines(lines, 'sdk/dartsdk-macos-ia32-release.zip')) |
| + .then((lines) => saveLines(lines, 'dart.rb')); |
| +} |
| + |
| +Future runGit(List<String> args) { |
| + print("git ${args.join(' ')}"); |
| + return Process.run('git', args, workingDirectory: repository, |
| + environment: gitEnvironment) |
| + .then((result) { |
| + print(result.stdout); |
| + print(result.stderr); |
| + }); |
| +} |
| + |
| +main(args) { |
| + final parser = new ArgParser() |
| + ..addOption('revision', abbr: 'r') |
| + ..addOption('channel', abbr: 'c', allowed: ['dev', 'stable']) |
| + ..addOption('key', abbr: 'k'); |
| + final options = parser.parse(args); |
| + final revision = options['revision']; |
| + final channel = options['channel']; |
| + final sshWrapper = Platform.script.resolve('ssh_with_key').toFilePath(); |
| + gitEnvironment = {'GIT_SSH': sshWrapper, |
| + 'SSH_KEY_PATH': options['key']}; |
| + |
| + Directory.systemTemp.createTemp('update_homebrew') |
| + .then((tempDir) { |
| + repository = tempDir.path; |
| + }) |
| + .then((_) => runGit( |
| + ['clone', 'git@github.com:dart-lang/homebrew-dart.git', '.'])) |
| + .then((_) => modifyHomebrewFormulas(channel, revision)) |
| + .then((_) => runGit(['commit', '-a', '-m', |
| + 'Updated $channel branch to revision $revision'])) |
| + .then((_) => runGit(['push'])) |
| + .whenComplete(() => new Directory(repository).delete(recursive: true)); |
| +} |