Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
DitherWither committed Aug 22, 2024
0 parents commit 3baa631
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.4.1"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# platform

[![Package Version](https://img.shields.io/hexpm/v/platform)](https://hex.pm/packages/platform)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/platform/)

```sh
gleam add platform@1
```
```gleam
import platform
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/platform>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
```
19 changes: 19 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "platform"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
description = "Zero dependency platform detection library for gleam"
licences = ["Apache-2.0"]
repository = { type = "github", user = "ditherwither", repo = "platform" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
103 changes: 103 additions & 0 deletions src/platform.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import gleam/io
import gleam/string

pub type Runtime {
Erlang
Node
Bun
Deno
Browser
OtherRuntime(String)
}

pub type Os {
Aix
Darwin
FreeBsd
Linux
OpenBsd
SunOs
Win32
OtherOs(String)
}

// Architecture list from node's docs

pub type Arch {
Arm
Arm64
X86
X64
Loong64
Mips
MipsLittleEndian
PPC
PPC64
RiscV64
S390
S390X
OtherArch(String)
}

@external(erlang, "platform_ffi", "runtime")
@external(javascript, "./platform_ffi.mjs", "runtime")
fn runtime_() -> String

@external(erlang, "platform_ffi", "os")
@external(javascript, "./platform_ffi.mjs", "os")
fn os_() -> String

@external(erlang, "platform_ffi", "arch")
@external(javascript, "./platform_ffi.mjs", "arch")
fn arch_() -> String

pub fn runtime() -> Runtime {
case runtime_() {
"erlang" -> Erlang
"node" -> Node
"bun" -> Bun
"deno" -> Deno
"browser" -> Browser
runtime -> OtherRuntime(runtime)
}
}

pub fn os() -> Os {
case os_() {
"aix" | "aix" <> _ -> Aix
"darwin" | "darwin" <> _ -> Darwin
"freebsd" | "freebsd" <> _ -> FreeBsd
"linux" | "linux" <> _ -> Linux
"openbsd" | "openbsd" <> _ -> OpenBsd
"sunos" | "sunos" <> _ -> SunOs
"win" | "win" <> _ -> Win32
os -> OtherOs(os)
}
}

pub fn arch() -> Arch {
case string.lowercase(arch_()) {
"arm" -> Arm
"arm64" | "aarch64" -> Arm64
"x86" | "ia32" -> X86
"x64" | "x86_64" | "amd64" -> X64
"loong64" -> Loong64
"mips" -> Mips
"mipsel" -> MipsLittleEndian
"ppc" -> PPC
"ppc64" -> PPC64
"riscv64" -> RiscV64
"s390" -> S390
"s390x" -> S390X
arch -> OtherArch(arch)
}
}

pub fn main() {
// io.debug("Runtime")
io.debug(runtime())
// io.debug("Os")
io.debug(os())
// io.debug("Arch")
io.debug(arch())
}
19 changes: 19 additions & 0 deletions src/platform_ffi.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(platform_ffi).
-export([runtime/0, os/0, arch/0]).

runtime() -> unicode:characters_to_binary("erlang").

os() ->
ArchitectureString = erlang:system_info(system_architecture),
Tokens = string:tokens(ArchitectureString, "-"),
unicode:characters_to_binary(
case length(Tokens) of
3 -> lists:nth(3, Tokens);
2 -> lists:nth(2, Tokens)
end
).

arch() ->
ArchitectureString = erlang:system_info(system_architecture),
[CpuArch | _] = string:tokens(ArchitectureString, "-"),
unicode:characters_to_binary(CpuArch).
55 changes: 55 additions & 0 deletions src/platform_ffi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

export function runtime() {
if (globalThis.Deno) {
return "deno";
}
if (globalThis.Bun) {
return "bun";
}
if (globalThis.process?.release?.name === "node") {
return "node";
}

if (window !== "undefined" && typeof window.document !== "undefined") {
return "browser";
}

return "unknown";

}

export function os() {
const currentRuntime = runtime();

if (currentRuntime === "node" || currentRuntime === "bun") {
return process.platform;
}
if (currentRuntime === "browser") {
// TODO get browser's host os using user agent data
return "unknown";
}
// if (currentRuntime === "deno") {
// const {platform} = await import("node:process")
// return platform;
// }
return "unknown";
}

export function arch() {
const currentRuntime = runtime();

if (currentRuntime === "node" || currentRuntime === "bun") {
return process.arch;
}
// if (currentRuntime === "deno") {
// const {arch} = await import("node:process")
// return arch;
// }
if (currentRuntime === "browser") {
// TODO get browser's architecture using user agent data
return "unknown";
}

return "unknown";
}
12 changes: 12 additions & 0 deletions test/platform_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gleeunit
import gleeunit/should

pub fn main() {
gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn hello_world_test() {
1
|> should.equal(1)
}

0 comments on commit 3baa631

Please sign in to comment.