1. # The Distro class and its methods, underlying $*DISTRO, are a work in progress.
  2. # It is very hard to capture data about a changing universe in a stable API.
  3. # If you find errors for your hardware or OS distribution, please report them
  4. # with the values that you expected and how to get them in your situation.
  5. class Distro does Systemic {
  6. has Str $.release;
  7. has Bool $.is-win;
  8. has Str $.path-sep;
  9. submethod BUILD (
  10. :$name,
  11. :$version,
  12. :$!release,
  13. :$!auth,
  14. :$!path-sep,
  15. :$!signature = Blob,
  16. :$!desc = Str
  17. --> Nil
  18. ) {
  19. $!name = $name.lc; # lowercase
  20. $!name ~~ s:g/" "//; # spaceless
  21. $!version = Version.new($version);
  22. $!is-win = so $!name eq any <mswin32 mingw msys cygwin>;
  23. }
  24. # This is a temporary migration method needed for installation
  25. method cur-sep() { "," }
  26. }
  27. sub INITIALIZE-A-DISTRO-NOW() {
  28. my $config := INITIALIZE-A-VM-NOW.config;
  29. my $name := $config<osname>;
  30. my $version := $config<osvers>;
  31. my $path-sep := $name eq 'MSWin32' ?? ';' !! ':';
  32. my Str $release := "unknown";
  33. my Str $auth := "unknown";
  34. # darwin specific info
  35. if $name eq 'darwin' {
  36. if qx/sw_vers/ ~~ m/^
  37. ProductName\: \s+ (<[\w\ ]>+) \s+
  38. ProductVersion\: \s+ (<[\d\.]>+) \s+
  39. BuildVersion\: \s+ (<[\w\d]>+)
  40. / {
  41. $name := ~$0;
  42. $version := ~$1;
  43. $release := ~$2;
  44. }
  45. else {
  46. $name := 'Mac OS X'; # we assume
  47. $version := "unknown";
  48. $release := qx/uname -r/.chomp;
  49. }
  50. $auth := 'Apple Computer, Inc.'; # presumably
  51. }
  52. elsif Rakudo::Internals.FILETEST-E('/etc/os-release') {
  53. $_ := '/etc/os-release'.IO.slurp.subst(:g, /'"'/,'');
  54. $auth := ~$0 if m/^^ HOME_URL \= (\N*) /;
  55. $name := ~$0 if m/^^ ID \= (\N*) /;
  56. $version := ~$0 if m/^^ VERSION \= (\N*) /;
  57. $release := ~$0 if m/^^ VERSION_ID \= (\N*) /;
  58. }
  59. elsif $name eq 'linux' {
  60. if try qx{lsb_release -a 2> /dev/null} ~~ m/
  61. Distributor \s+ ID\: \s+ (<[\w\ ]>+) \s+
  62. Description\: \s+ (<[\w\ ]>+) \s+ (<[\d\.]>+) \s+
  63. Release\: \s+ (<[\d\.]>+)
  64. / {
  65. $auth := ~$0;
  66. $name := ~$1;
  67. $version := ~$2;
  68. $release := ~$3;
  69. }
  70. }
  71. my $desc := DateTime.now.Str;
  72. Distro.new(:$name, :$version, :$release, :$auth, :$path-sep, :$desc);
  73. }
  74. # set up $*DISTRO
  75. Rakudo::Internals.REGISTER-DYNAMIC: '$*DISTRO', {
  76. PROCESS::<$DISTRO> := INITIALIZE-A-DISTRO-NOW();
  77. }