OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library pub.utils; | 6 library pub.utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import "dart:convert"; | 9 import "dart:convert"; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 } | 778 } |
779 return map; | 779 return map; |
780 }); | 780 }); |
781 } | 781 } |
782 | 782 |
783 /// Whether "special" strings such as Unicode characters or color escapes are | 783 /// Whether "special" strings such as Unicode characters or color escapes are |
784 /// safe to use. | 784 /// safe to use. |
785 /// | 785 /// |
786 /// On Windows or when not printing to a terminal, only printable ASCII | 786 /// On Windows or when not printing to a terminal, only printable ASCII |
787 /// characters should be used. | 787 /// characters should be used. |
788 bool get canUseSpecialChars => !runningAsTest && | 788 bool get canUseSpecialChars => !runningFromTest && !runningAsTest && |
789 Platform.operatingSystem != 'windows' && | 789 Platform.operatingSystem != 'windows' && |
790 stdioType(stdout) == StdioType.TERMINAL; | 790 stdioType(stdout) == StdioType.TERMINAL; |
791 | 791 |
792 /// Gets a "special" string (ANSI escape or Unicode). | 792 /// Gets a "special" string (ANSI escape or Unicode). |
793 /// | 793 /// |
794 /// On Windows or when not printing to a terminal, returns something else since | 794 /// On Windows or when not printing to a terminal, returns something else since |
795 /// those aren't supported. | 795 /// those aren't supported. |
796 String getSpecial(String special, [String onWindows = '']) => | 796 String getSpecial(String special, [String onWindows = '']) => |
797 canUseSpecialChars ? special : onWindows; | 797 canUseSpecialChars ? special : onWindows; |
798 | 798 |
799 /// Prepends each line in [text] with [prefix]. | 799 /// Prepends each line in [text] with [prefix]. |
800 /// | 800 /// |
801 /// If [firstPrefix] is passed, the first line is prefixed with that instead. | 801 /// If [firstPrefix] is passed, the first line is prefixed with that instead. |
802 String prefixLines(String text, {String prefix: '| ', String firstPrefix}) { | 802 String prefixLines(String text, {String prefix: '| ', String firstPrefix}) { |
803 var lines = text.split('\n'); | 803 var lines = text.split('\n'); |
804 if (firstPrefix == null) { | 804 if (firstPrefix == null) { |
805 return lines.map((line) => '$prefix$line').join('\n'); | 805 return lines.map((line) => '$prefix$line').join('\n'); |
806 } | 806 } |
807 | 807 |
808 var firstLine = "$firstPrefix${lines.first}"; | 808 var firstLine = "$firstPrefix${lines.first}"; |
809 lines = lines.skip(1).map((line) => '$prefix$line').toList(); | 809 lines = lines.skip(1).map((line) => '$prefix$line').toList(); |
810 lines.insert(0, firstLine); | 810 lines.insert(0, firstLine); |
811 return lines.join('\n'); | 811 return lines.join('\n'); |
812 } | 812 } |
813 | 813 |
814 /// Whether pub is running as a subprocess in an integration test or in a unit | |
815 /// test that has explicitly set this. | |
816 bool runningAsTest = Platform.environment.containsKey('_PUB_TESTING'); | |
817 | |
818 /// Whether today is April Fools' day. | 814 /// Whether today is April Fools' day. |
819 bool get isAprilFools { | 815 bool get isAprilFools { |
820 // Tests should never see April Fools' output. | 816 // Tests should never see April Fools' output. |
821 if (runningAsTest) return false; | 817 if (runningFromTest) return false; |
822 | 818 |
823 var date = new DateTime.now(); | 819 var date = new DateTime.now(); |
824 return date.month == 4 && date.day == 1; | 820 return date.month == 4 && date.day == 1; |
825 } | 821 } |
826 | 822 |
827 /// Wraps [fn] to guard against several different kinds of stack overflow | 823 /// Wraps [fn] to guard against several different kinds of stack overflow |
828 /// exceptions: | 824 /// exceptions: |
829 /// | 825 /// |
830 /// * A sufficiently long [Future] chain can cause a stack overflow if there are | 826 /// * A sufficiently long [Future] chain can cause a stack overflow if there are |
831 /// no asynchronous operations in it (issue 9583). | 827 /// no asynchronous operations in it (issue 9583). |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 } else { | 915 } else { |
920 throw new ApplicationException(message); | 916 throw new ApplicationException(message); |
921 } | 917 } |
922 } | 918 } |
923 | 919 |
924 /// Throw a [DataException] with [message] to indicate that the command has | 920 /// Throw a [DataException] with [message] to indicate that the command has |
925 /// failed because of invalid input data. | 921 /// failed because of invalid input data. |
926 /// | 922 /// |
927 /// This will report the error and cause pub to exit with [exit_codes.DATA]. | 923 /// This will report the error and cause pub to exit with [exit_codes.DATA]. |
928 void dataError(String message) => throw new DataException(message); | 924 void dataError(String message) => throw new DataException(message); |
OLD | NEW |