OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 library mojom.utils; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert'; | |
9 import 'dart:io'; | |
10 | |
11 import 'package:crypto/crypto.dart' as crypto; | |
12 import 'package:logging/logging.dart' as logging; | |
13 import 'package:path/path.dart' as path; | |
14 | |
15 bool isMojomDart(String path) => path.endsWith('.mojom.dart'); | |
16 bool isMojom(String path) => path.endsWith('.mojom'); | |
17 bool isDotMojoms(String path) => path.endsWith(".mojoms"); | |
18 | |
19 String makeAbsolute(String p) => | |
20 path.isAbsolute(p) ? path.normalize(p) : path.normalize(path.absolute(p)); | |
21 | |
22 String makeRelative(String p) => path.isAbsolute(p) | |
23 ? path.normalize(path.relative(p, from: Directory.current.path)) | |
24 : path.normalize(p); | |
25 | |
26 logging.Logger log; | |
27 | |
28 /// An Error for problems on the command line. | |
29 class CommandLineError { | |
30 final _msg; | |
31 CommandLineError(this._msg); | |
32 toString() => "Command Line Error: $_msg"; | |
33 } | |
34 | |
35 /// An Error for failures of the bindings generation script. | |
36 class GenerationError { | |
37 final _msg; | |
38 GenerationError(this._msg); | |
39 toString() => "Generation Error: $_msg"; | |
40 } | |
41 | |
42 /// An Error for failing to fetch a .mojom file. | |
43 class FetchError { | |
44 final _msg; | |
45 FetchError(this._msg); | |
46 toString() => "Fetch Error: $_msg"; | |
47 } | |
48 | |
49 Future<String> _downloadUrl(HttpClient httpClient, Uri uri) async { | |
50 try { | |
51 var request = await httpClient.getUrl(uri); | |
52 var response = await request.close(); | |
53 if (response.statusCode >= 400) { | |
54 var msg = "Failed to download $uri\nCode ${response.statusCode}"; | |
55 if (response.reasonPhrase != null) { | |
56 msg = "$msg: ${response.reasonPhrase}"; | |
57 } | |
58 throw new FetchError(msg); | |
59 } | |
60 var fileString = new StringBuffer(); | |
61 await for (String contents in response.transform(UTF8.decoder)) { | |
62 fileString.write(contents); | |
63 } | |
64 return fileString.toString(); | |
65 } catch (e) { | |
66 throw new FetchError("$e"); | |
67 } | |
68 } | |
69 | |
70 /// Fetch file at [uri] using [httpClient] if needed. Throw a [FetchError] if | |
71 /// the file is not successfully fetched. | |
72 Future<String> fetchUri(HttpClient httpClient, Uri uri) async { | |
73 if (uri.scheme.startsWith('http')) { | |
74 return _downloadUrl(httpClient, uri); | |
75 } | |
76 try { | |
77 File f = new File.fromUri(uri); | |
78 return await f.readAsString(); | |
79 } catch (e) { | |
80 throw new FetchError("$e"); | |
81 } | |
82 } | |
83 | |
84 markFileReadOnly(String file) async { | |
85 if (!Platform.isLinux && !Platform.isMacOS) { | |
86 String os = Platform.operatingSystem; | |
87 throw 'Setting file $file read-only is not supported on $os.'; | |
88 } | |
89 var process = await Process.run('chmod', ['a-w', file]); | |
90 if (process.exitCode != 0) { | |
91 print('Setting file $file read-only failed: ${process.stderr}'); | |
92 } | |
93 } | |
94 | |
95 Future<DateTime> getModificationTime(File f) async { | |
96 var stat = await f.stat(); | |
97 return stat.modified; | |
98 } | |
99 | |
100 Future<List<int>> fileSHA1(File f) async { | |
101 var sha1 = new crypto.SHA1(); | |
102 await for (var bytes in f.openRead()) { | |
103 sha1.add(bytes); | |
104 } | |
105 return sha1.close(); | |
106 } | |
107 | |
108 Future<bool> equalSHA1(File file1, File file2) async { | |
109 List<int> file1sha1 = await fileSHA1(file1); | |
110 List<int> file2sha1 = await fileSHA1(file2); | |
111 if (file1sha1.length != file2sha1.length) return false; | |
112 for (int i = 0; i < file1sha1.length; i++) { | |
113 if (file1sha1[i] != file2sha1[i]) return false; | |
114 } | |
115 return true; | |
116 } | |
117 | |
118 /// If the files are the same, returns 0. | |
119 /// Otherwise, returns a negative number if f1 less recently modified than f2, | |
120 /// or a positive number if f1 is more recently modified than f2. | |
121 Future<int> compareFiles(File f1, File f2) async { | |
122 FileStat f1stat = await f1.stat(); | |
123 FileStat f2stat = await f2.stat(); | |
124 if ((f1stat.size != f2stat.size) || !await equalSHA1(f1, f2)) { | |
125 return (f1stat.modified.isBefore(f2stat.modified)) ? -1 : 1; | |
126 } | |
127 return 0; | |
128 } | |
129 | |
130 /// Is any element of [haystack] a prefix of [needle]? | |
131 bool containsPrefix(String needle, List<String> haystack) { | |
132 if (haystack == null) return false; | |
133 var match = | |
134 haystack.firstWhere((p) => needle.startsWith(p), orElse: () => null); | |
135 return match != null; | |
136 } | |
OLD | NEW |