blob: c427e3f75aad7629222d92b2155c008080c23504 [file] [edit]
// Copyright (c) 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:io';
import 'syntax.g.dart';
/// An operating system which the Dart VM can run on.
final class OS {
/// The name of this operating system.
final String name;
const OS._(this.name);
/// The
/// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29)
/// operating system.
static const OS android = OS._('android');
/// The [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia) operating
/// system.
static const OS fuchsia = OS._('fuchsia');
/// The [iOS](https://en.wikipedia.org/wiki/IOS) operating system.
static const OS iOS = OS._('ios');
/// The [Linux](https://en.wikipedia.org/wiki/Linux) operating system.
static const OS linux = OS._('linux');
/// The [macOS](https://en.wikipedia.org/wiki/MacOS) operating system.
static const OS macOS = OS._('macos');
/// The
/// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows)
/// operating system.
static const OS windows = OS._('windows');
/// Known values for [OS].
static const List<OS> values = [android, fuchsia, iOS, linux, macOS, windows];
/// Typical cross compilation between OSes.
static final osCrossCompilationDefault = {
OS.macOS: [OS.macOS, OS.iOS, OS.android],
OS.linux: [OS.linux, OS.android],
OS.windows: [OS.windows, OS.android],
};
/// The name of this [OS].
///
/// This returns a stable string that can be used to construct a
/// [OS] via [OS.fromString].
@override
String toString() => name;
/// Creates a [OS] from the given [name].
///
/// The name can be obtained from [OS.name] or [OS.toString].
factory OS.fromString(String name) =>
values.firstWhere((e) => e.name == name, orElse: () => OS._(name));
/// The current [OS].
///
/// Consistent with the [Platform.version] string.
static final OS current = OS.fromString(Platform.operatingSystem);
@override
bool operator ==(Object other) => other is OS && other.name == name;
@override
int get hashCode => name.hashCode;
}
/// Extension methods for [OS] to convert to and from the syntax model.
extension OSSyntaxExtension on OS {
/// Converts this [OS] to its corresponding [OSSyntax].
OSSyntax toSyntax() => OSSyntax.fromJson(name);
/// Converts an [OSSyntax] to its corresponding [OS].
static OS fromSyntax(OSSyntax syntax) => OS.fromString(syntax.name);
}