Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Npm installer #210

Merged
merged 7 commits into from
Apr 17, 2023
Merged

Npm installer #210

merged 7 commits into from
Apr 17, 2023

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented Apr 12, 2023

This introduces minimal experimental support for generating an NPM installer with installer = ["npm"]. Adding the installer to your config will result in a new global artifact, RELEASE_ID-npm-package.tar.gz containing a ready-to-publish npm package for your application. The installer works similar to the shell and powershell installers, but instead of installing to PATH via the cargo home, it installs the binaries into your node_modules so that things like npx will work.

npm publish actually supports taking a path or url to a gzipped package to publish, so all you need to do is copy the URL of npm-package.tar.gz from your Github Release and pass it to npm publish.

The npm package will inherit its properties from your Cargo.toml and cargo-dist config (package name, version, authors, license, description, repository, homepage...). Static assets for executable-zips (README, CHANGELOG, LICENSE...) will also be copied into the package (in the future we might need to introduce configs to tune this).

This also introduces a new optional config key, npm-scope, which can be used to prefix the package name with a scope. e.g. the package axolotlsay with npm-scope = "@axodotdev" will get the package name @axodotdev/axolotlsay.

The current largest caveat is that the npm installer uses the binary-install npm library to unpack the tarballs/zips containings your binaries, and that library only supports unpacking .tar.gz. So you must set windows-archive and unix-archive to ".tar.gz" in your config for this feature to work. In the future we will improve this.

Note that you can in principle retroactively generate the npm packages completely locally with cargo dist build --artifacts=global, but the above caveats about tarball/zip format means that no existing cargo-dist-based Github Releases will have usable contents. Still, you can at least inspect the package locally to see if you like what we generate.

@Gankra
Copy link
Contributor Author

Gankra commented Apr 12, 2023

You can test this out by running cargo run --release -- dist --artifacts=global

Which should spit out blahblah-npm-package.tar.gz

@Gankra
Copy link
Contributor Author

Gankra commented Apr 12, 2023

The two files of interest are package.json and binary.js, which come out as follows:

package.json
{
  "name": "axodotdev/cargo-dist",
  "version": "0.0.5",
  "description": "Shippable application packaging for Rust",
  "repository": "https://github.com/axodotdev/cargo-dist",
  
  
  "license": "MIT OR Apache-2.0",
  
  "main": "index.js",
  "bin": {
    "rover": "run.js"
  },
  "scripts": {
    "postinstall": "node ./install.js",
    "fmt": "prettier --write **/*.js",
    "fmt:check": "prettier --check **/*.js"
  },
  "engines": {
    "node": ">=14",
    "npm": ">=6"
  },
  "volta": {
    "node": "18.14.1",
    "npm": "9.5.0"
  },
  "dependencies": {
    "axios-proxy-builder": "^0.1.1",
    "binary-install": "^1.0.6",
    "console.table": "^0.10.0",
    "detect-libc": "^2.0.0"
  },
  "devDependencies": {
    "prettier": "2.8.4"
  }
}
binary.js
const { Binary } = require("binary-install");
const os = require("os");
const cTable = require("console.table");
const libc = require("detect-libc");
const { configureProxy } = require("axios-proxy-builder");

const error = (msg) => {
  console.error(msg);
  process.exit(1);
};

const { version } = require("./package.json");
// These are injected by cargo-dist
const name = "cargo-dist";
const artifact_download_url = "https://github.com/axodotdev/cargo-dist/releases/download/v0.0.5"

// These are injected by cargo-dist
const supportedPlatforms = {
  "aarch64-apple-darwin": {
    "artifact_name": "cargo-dist-v0.0.5-aarch64-apple-darwin.tar.xz",
    "bins": ["cargo-dist"],
    "zip_ext": ".tar.xz"
  },
  "x86_64-apple-darwin": {
    "artifact_name": "cargo-dist-v0.0.5-x86_64-apple-darwin.tar.xz",
    "bins": ["cargo-dist"],
    "zip_ext": ".tar.xz"
  },
  "x86_64-pc-windows-msvc": {
    "artifact_name": "cargo-dist-v0.0.5-x86_64-pc-windows-msvc.zip",
    "bins": ["cargo-dist.exe"],
    "zip_ext": ".zip"
  },
  "x86_64-unknown-linux-gnu": {
    "artifact_name": "cargo-dist-v0.0.5-x86_64-unknown-linux-gnu.tar.xz",
    "bins": ["cargo-dist"],
    "zip_ext": ".tar.xz"
  }};

const getPlatform = () => {
  const raw_os_type = os.type();
  const raw_architecture = os.arch();

  // We want to use rust-style target triples as the canonical key
  // for a platform, so translate the "os" library's concepts into rust ones
  let os_type = "";
  switch (raw_os_type) {
    case "Windows_NT": 
      os_type = "pc-windows-msvc";
      break;
    case "Darwin":
      os_type = "apple-darwin";
      break;
    case "Linux":
      os_type = "unknown-linux-gnu"
      break;
  }

  let arch = "";
  switch (raw_architecture) {
    case "x64":
      arch = "x86_64";
      break;
    case "arm64":
      arch = "aarch64";
      break;
  }

  // Assume the above succeeded and build a target triple to look things up with.
  // If any of it failed, this lookup will fail and we'll handle it like normal.
  let target_triple = `${arch}-${os_type}`;
  let platform = supportedPlatforms[target_triple];

  if (!!platform) {
    error(
      `Platform with type "${raw_os_type}" and architecture "${raw_architecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${supportedPlatforms}`
    );
  }

  // These are both situation where you might toggle to unknown-linux-musl but we don't support that yet
  if (supportedPlatform.TYPE === "Linux") {
    if (libc.isNonGlibcLinuxSync()) {
      error("This operating system does not support dynamic linking to glibc.");
    } else {
      let libc_version = libc.versionSync();
      let split_libc_version = libc_version.split(".");
      let libc_major_version = split_libc_version[0];
      let libc_minor_version = split_libc_version[1];
      let min_major_version = 2;
      let min_minor_version = 17;
      if (
        libc_major_version < min_major_version ||
        libc_minor_version < min_minor_version
      ) {
        error(
          `This operating system needs glibc >= ${min_major_version}.${min_minor_version}, but only has ${libc_version} installed.`
        );
      }
    }
  }
};

const getBinary = () => {
  const platform = getPlatform();
  const url = `${artifact_download_url}/${platform.artifact_name}`;

  if (platform.bins.length > 1) {
    // Not yet supported
    error("this app has multiple binaries, which isn't yet implemented");
  }
  let binary = new Binary(platform.bins[0], url);

  return binary;
};

const install = () => {
  const binary = getBinary();
  const proxy = configureProxy(binary.url);

  return binary.install(proxy, suppressLogs);
};

const run = () => {
  const binary = getBinary();
  binary.run();
};

module.exports = {
  install,
  run,
  getBinary,
};

@Gankra Gankra merged commit ef245ee into main Apr 17, 2023
@Gankra Gankra deleted the npm-installer branch April 17, 2023 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants